2 * Copyright 2014-2015 Canonical Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
17 * Daniel d'Andrada <daniel.dandrada@canonical.com>
21 import QtQuick.Window 2.2
22 import Ubuntu.Components 1.3
23 import "../Components"
28 // to be read from outside
29 readonly property bool dragged: dragArea.moving
32 readonly property alias appWindowOrientationAngle: appWindowWithShadow.orientationAngle
33 readonly property alias appWindowRotation: appWindowWithShadow.rotation
34 readonly property alias orientationChangesEnabled: appWindow.orientationChangesEnabled
36 // to be set from outside
37 property bool interactive: true
38 property bool dropShadow: true
39 property real maximizedAppTopMargin
40 property alias swipeToCloseEnabled: dragArea.enabled
41 property bool closeable
42 property alias application: appWindow.application
43 property int shellOrientationAngle
44 property int shellOrientation
45 property QtObject orientations
47 function matchShellOrientation() {
48 if (!root.application)
50 appWindowWithShadow.orientationAngle = root.shellOrientationAngle;
53 function animateToShellOrientation() {
54 if (!root.application)
57 if (root.application.rotatesWindowContents) {
58 appWindowWithShadow.orientationAngle = root.shellOrientationAngle;
60 orientationChangeAnimation.start();
64 OrientationChangeAnimation {
65 id: orientationChangeAnimation
66 objectName: "orientationChangeAnimation"
68 background: background
69 window: appWindowWithShadow
70 screenshot: appWindowScreenshotWithShadow
75 property bool startingUp: true
78 Component.onCompleted: { finishStartUpTimer.start(); }
79 Timer { id: finishStartUpTimer; interval: 400; onTriggered: priv.startingUp = false }
89 objectName: "displacedAppWindowWithShadow"
91 readonly property real limit: root.height / 4
93 y: root.closeable ? dragArea.distance : elastic(dragArea.distance)
97 function elastic(distance) {
98 var k = distance < 0 ? -limit : limit
99 return k * (1 - Math.pow((k - 1) / k, distance))
103 id: appWindowWithShadow
104 objectName: "appWindowWithShadow"
106 property int orientationAngle
108 property real transformRotationAngle: 0
109 property real transformOriginX
110 property real transformOriginY
112 property var window: appWindow
114 transform: Rotation {
115 origin.x: appWindowWithShadow.transformOriginX
116 origin.y: appWindowWithShadow.transformOriginY
117 axis { x: 0; y: 0; z: 1 }
118 angle: appWindowWithShadow.transformRotationAngle
122 if (priv.startingUp) {
124 } else if (root.application && root.application.rotatesWindowContents) {
125 return "counterRotate";
126 } else if (orientationChangeAnimation.running) {
127 return "animatingRotation";
129 return "keepSceneRotation";
133 // Ensures the given angle is in the form (0,90,180,270)
134 function normalizeAngle(angle) {
142 // Sets the initial orientationAngle of the window, when it first slides into view
143 // (with the splash screen likely being displayed). At that point we just try to
144 // match shell's current orientation. We need a bit of time in this state as the
145 // information we need to decide orientationAngle may take a few cycles to
150 target: appWindowWithShadow
151 restoreEntryValues: false
153 if (!root.application || root.application.rotatesWindowContents) {
156 var supportedOrientations = root.application.supportedOrientations;
158 if (supportedOrientations === Qt.PrimaryOrientation) {
159 supportedOrientations = root.orientations.primary;
162 // If it doesn't support shell's current orientation
163 // then simply pick some arbitraty one that it does support
164 var chosenOrientation = 0;
165 if (supportedOrientations & root.shellOrientation) {
166 chosenOrientation = root.shellOrientation;
167 } else if (supportedOrientations & Qt.PortraitOrientation) {
168 chosenOrientation = root.orientations.portrait;
169 } else if (supportedOrientations & Qt.LandscapeOrientation) {
170 chosenOrientation = root.orientations.landscape;
171 } else if (supportedOrientations & Qt.InvertedPortraitOrientation) {
172 chosenOrientation = root.orientations.invertedPortrait;
173 } else if (supportedOrientations & Qt.InvertedLandscapeOrientation) {
174 chosenOrientation = root.orientations.invertedLandscape;
176 chosenOrientation = root.orientations.primary;
179 return Screen.angleBetween(root.orientations.native_, chosenOrientation);
182 rotation: normalizeAngle(appWindowWithShadow.orientationAngle - root.shellOrientationAngle)
184 if (rotation == 0 || rotation == 180) {
191 if (rotation == 0 || rotation == 180)
198 // In this state we stick to our currently set orientationAngle, which may change only due
199 // to calls made to matchShellOrientation() or animateToShellOrientation()
201 id: keepSceneRotationState
202 name: "keepSceneRotation"
204 StateChangeScript { script: {
206 appWindowWithShadow.orientationAngle = appWindowWithShadow.orientationAngle;
209 target: appWindowWithShadow
210 restoreEntryValues: false
211 rotation: normalizeAngle(appWindowWithShadow.orientationAngle - root.shellOrientationAngle)
213 if (rotation == 0 || rotation == 180) {
220 if (rotation == 0 || rotation == 180)
227 // In this state we counteract any shell rotation so that the window, in scene coordinates,
228 // remains unrotated.
230 name: "counterRotate"
231 StateChangeScript { script: {
233 appWindowWithShadow.orientationAngle = appWindowWithShadow.orientationAngle;
236 target: appWindowWithShadow
237 width: root.shellOrientationAngle == 0 || root.shellOrientationAngle == 180 ? root.width : root.height
238 height: root.shellOrientationAngle == 0 || root.shellOrientationAngle == 180 ? root.height : root.width
239 rotation: normalizeAngle(-root.shellOrientationAngle)
243 surfaceOrientationAngle: orientationAngle
247 name: "animatingRotation"
251 x: (parent.width - width) / 2
252 y: (parent.height - height) / 2
257 margins: -units.gu(2)
259 source: "graphics/dropshadow2gu.sci"
260 opacity: root.dropShadow ? .3 : 0
261 Behavior on opacity { UbuntuNumberAnimation {} }
266 objectName: application ? "appWindow_" + application.appId : "appWindow_null"
270 topMargin: appWindow.fullscreen || (application && application.rotatesWindowContents)
271 ? 0 : maximizedAppTopMargin
274 interactive: root.interactive
280 // mimics appWindowWithShadow. Do the positioning of screenshots of non-fullscreen
282 id: appWindowScreenshotWithShadow
285 property real transformRotationAngle: 0
286 property real transformOriginX
287 property real transformOriginY
289 transform: Rotation {
290 origin.x: appWindowScreenshotWithShadow.transformOriginX
291 origin.y: appWindowScreenshotWithShadow.transformOriginY
292 axis { x: 0; y: 0; z: 1 }
293 angle: appWindowScreenshotWithShadow.transformRotationAngle
296 property var window: appWindowScreenshot
299 // Format: "image://application/$APP_ID/$CURRENT_TIME_MS"
300 // eg: "image://application/calculator-app/123456"
301 var timeMs = new Date().getTime();
302 appWindowScreenshot.source = "image://application/" + root.application.appId + "/" + timeMs;
305 appWindowScreenshot.source = "";
309 id: appWindowScreenshot
314 sourceSize.width: width
315 sourceSize.height: height
321 objectName: "dragArea"
324 property bool moving: false
325 property real distance: 0
326 readonly property int threshold: units.gu(2)
327 property int offset: 0
329 readonly property real minSpeedToClose: units.gu(40)
331 onDragValueChanged: {
335 moving = moving || Math.abs(dragValue) > threshold;
337 distance = dragValue + offset;
343 offset = (dragValue > 0 ? -threshold: threshold)
356 if (!root.closeable) {
357 animation.animate("center")
361 // velocity and distance values specified by design prototype
362 if ((dragVelocity < -minSpeedToClose && distance < -units.gu(8)) || distance < -root.height / 2) {
363 animation.animate("up")
364 } else if ((dragVelocity > minSpeedToClose && distance > units.gu(8)) || distance > root.height / 2) {
365 animation.animate("down")
367 animation.animate("center")
371 UbuntuNumberAnimation {
373 objectName: "closeAnimation"
376 property bool requestClose: false
378 function animate(direction) {
379 animation.from = dragArea.distance;
382 animation.to = -root.height * 1.5;
386 animation.to = root.height * 1.5;
397 dragArea.moving = false;
401 dragArea.distance = 0;