2 * Copyright (C) 2015,2016 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 1.3
21 // Why the state machine is done that way:
22 // We cannot use regular PropertyChanges{} inside the State elements as steps in the
23 // transition animations must take place in a well defined order.
24 // Which means that we also cannot jump to a new state in the middle of a transition
25 // as that would make hell brake loose.
29 // to be set from the outside
30 property Item orientedShell
32 property Item shellCover
33 property Item shellSnapshot
35 property int rotationDuration: 450
36 property int rotationEasing: Easing.InOutCubic
37 // Those values are good for debugging/development
38 //property int rotationDuration: 3000
39 //property int rotationEasing: Easing.Linear
45 State { name: "180" },
49 property QtObject d: QtObject {
52 property bool startingUp: true
53 property var finishStartUpTimer: Timer {
55 onTriggered: d.startingUp = false
57 Component.onCompleted: {
58 finishStartUpTimer.start();
61 property bool transitioning: false
62 onTransitioningChanged: {
66 readonly property int requestedOrientationAngle: root.orientedShell.acceptedOrientationAngle
68 // Avoiding a direct call to tryUpdateState() as the state change might trigger an immediate
69 // change to Shell.orientationAngle which, in its turn, causes a reevaluation of
70 // requestedOrientationAngle (ie., OrientedShell.acceptedOrientationAngle). A reentrant evaluation
71 // of a binding is detected by QML as a binding loop and QML will deny the reevalutation, which
72 // will leave us in a bogus state.
74 // To avoid this mess we update the state in the next event loop iteration, ensuring a clean
76 onRequestedOrientationAngleChanged: {
77 stateUpdateTimer.start();
79 property Timer stateUpdateTimer: Timer {
82 onTriggered: { d.tryUpdateState(); }
85 function tryUpdateState() {
86 if (d.transitioning || (!d.startingUp && !root.orientedShell.orientationChangesEnabled)) {
90 var requestedState = d.requestedOrientationAngle.toString();
91 if (requestedState === root.state) {
95 d.resolveAnimationType();
97 var angleDiff = Math.abs(root.shell.orientationAngle - d.requestedOrientationAngle);
98 var isNinetyRotationAnimation = angleDiff == 90 || angleDiff == 270;
99 var needsShellSnapshot = d.animationType == d.fullAnimation && isNinetyRotationAnimation;
101 if (needsShellSnapshot && !shellSnapshotReady) {
102 root.shellSnapshot.take();
103 // try again once we have a shell snapshot ready for use. Snapshot taking is async.
107 if (!needsShellSnapshot && shellSnapshotReady) {
108 root.shellSnapshot.discard();
111 root.state = requestedState;
114 property bool shellSnapshotReady: root.shellSnapshot && root.shellSnapshot.ready
115 onShellSnapshotReadyChanged: tryUpdateState();
117 property Connections shellConnections: Connections {
118 target: root.orientedShell
119 onOrientationChangesEnabledChanged: {
124 readonly property int fullAnimation: 0
125 readonly property int indicatorsBarAnimation: 1
126 readonly property int noAnimation: 2
128 property int animationType
130 // animationType update *must* take place *before* the state update.
131 // If animationType and state were updated through bindings, as with normal qml code,
132 // there would be no guarantee in the order of the binding updates, which could then
133 // cause the wrong transitions to be chosen for the state changes.
134 function resolveAnimationType() {
136 // During start up, inital property values are still settling while we're still
137 // to render the very first frame
138 d.animationType = d.noAnimation;
139 } else if (Powerd.status === Powerd.Off) {
140 // There's no point in animating if the user can't see it (display is off).
141 d.animationType = d.noAnimation;
142 } else if (root.shell.showingGreeter) {
143 // A rotating greeter looks weird.
144 d.animationType = d.noAnimation;
146 if (!root.shell.mainApp) {
147 // shouldn't happen but, anyway
148 d.animationType = d.fullAnimation;
152 if (root.shell.mainApp.rotatesWindowContents) {
153 // The application will animate its own GUI, so we don't have to do anything ourselves.
154 d.animationType = d.noAnimation;
155 } else if (root.shell.mainAppWindowOrientationAngle == d.requestedOrientationAngle) {
156 // The app window is already on its final orientation angle.
157 // So we just animate the indicators bar
158 // TODO: what if the app is fullscreen?
159 d.animationType = d.indicatorsBarAnimation;
161 d.animationType = d.fullAnimation;
166 // When an application switch takes place, d.requestedOrientationAngle and
167 // root.shell.mainAppWindowOrientationAngle get updated separately, at different moments.
168 // So, when one of those properties change, we shouldn't make a decision straight away
169 // as the other might be stale and about to be changed. So let's give it a bit of time for
170 // them to get properly updated.
171 // This approach is indeed a bit hacky.
172 property bool appWindowOrientationAngleNeedsUpdateUnstable:
173 root.shell.orientationAngle === d.requestedOrientationAngle
174 && root.shell.mainApp
175 && root.shell.mainAppWindowOrientationAngle !== root.shell.orientationAngle
177 onAppWindowOrientationAngleNeedsUpdateUnstableChanged: {
178 stableTimer.restart();
180 property Timer stableTimer: Timer {
183 if (d.appWindowOrientationAngleNeedsUpdateUnstable) {
184 shell.updateFocusedAppOrientationAnimated();
193 enabled: d.animationType == d.fullAnimation
194 NinetyRotationAnimation { fromAngle: 90; toAngle: 0
195 info: d; shell: root.shell }
199 enabled: d.animationType == d.fullAnimation
200 NinetyRotationAnimation { fromAngle: 0; toAngle: 90
201 info: d; shell: root.shell }
205 enabled: d.animationType == d.fullAnimation
206 NinetyRotationAnimation { fromAngle: 0; toAngle: 270
207 info: d; shell: root.shell }
211 enabled: d.animationType == d.fullAnimation
212 NinetyRotationAnimation { fromAngle: 270; toAngle: 0
213 info: d; shell: root.shell }
216 from: "90"; to: "180"
217 enabled: d.animationType == d.fullAnimation
218 NinetyRotationAnimation { fromAngle: 90; toAngle: 180
219 info: d; shell: root.shell }
222 from: "180"; to: "90"
223 enabled: d.animationType == d.fullAnimation
224 NinetyRotationAnimation { fromAngle: 180; toAngle: 90
225 info: d; shell: root.shell }
228 from: "180"; to: "270"
229 enabled: d.animationType == d.fullAnimation
230 NinetyRotationAnimation { fromAngle: 180; toAngle: 270
231 info: d; shell: root.shell }
234 from: "270"; to: "180"
235 enabled: d.animationType == d.fullAnimation
236 NinetyRotationAnimation { fromAngle: 270; toAngle: 180
237 info: d; shell: root.shell }
241 enabled: d.animationType == d.fullAnimation
242 HalfLoopRotationAnimation { fromAngle: 0; toAngle: 180
243 info: d; shell: root.shell }
247 enabled: d.animationType == d.fullAnimation
248 HalfLoopRotationAnimation { fromAngle: 180; toAngle: 0
249 info: d; shell: root.shell }
252 from: "90"; to: "270"
253 enabled: d.animationType == d.fullAnimation
254 HalfLoopRotationAnimation { fromAngle: 90; toAngle: 270
255 info: d; shell: root.shell }
258 from: "270"; to: "90"
259 enabled: d.animationType == d.fullAnimation
260 HalfLoopRotationAnimation { fromAngle: 270; toAngle: 90
261 info: d; shell: root.shell }
264 objectName: "immediateTransition"
265 enabled: d.animationType == d.noAnimation
266 ImmediateRotationAction { info: d; shell: root.shell }
269 enabled: d.animationType == d.indicatorsBarAnimation
270 SequentialAnimation {
271 ScriptAction { script: {
272 d.transitioning = true;
275 duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing
276 target: root.shell; property: "panelAreaShowProgress"
279 ImmediateRotationAction { info: d; shell: root.shell }
281 duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing
282 target: root.shell; property: "panelAreaShowProgress"
285 ScriptAction { script: {
286 d.transitioning = false;