2 * Copyright (C) 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 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
19 import Ubuntu.Gestures 0.1
20 import Unity.Application 0.1
23 import "../Components"
30 // Functions to be called from outside
31 function updateFocusedAppOrientation() {
32 var mainStageAppIndex = priv.indexOf(priv.mainStageAppId);
33 if (mainStageAppIndex >= 0 && mainStageAppIndex < spreadRepeater.count) {
34 spreadRepeater.itemAt(mainStageAppIndex).matchShellOrientation();
37 for (var i = 0; i < spreadRepeater.count; ++i) {
39 if (i === mainStageAppIndex) {
43 var spreadDelegate = spreadRepeater.itemAt(i);
45 var delta = spreadDelegate.appWindowOrientationAngle - root.shellOrientationAngle;
46 if (delta < 0) { delta += 360; }
49 var supportedOrientations = spreadDelegate.application.supportedOrientations;
50 if (supportedOrientations === Qt.PrimaryOrientation) {
51 supportedOrientations = spreadDelegate.orientations.primary;
54 if (delta === 180 && (supportedOrientations & spreadDelegate.shellOrientation)) {
55 spreadDelegate.matchShellOrientation();
59 function updateFocusedAppOrientationAnimated() {
60 var mainStageAppIndex = priv.indexOf(priv.mainStageAppId);
61 if (mainStageAppIndex >= 0 && mainStageAppIndex < spreadRepeater.count) {
62 spreadRepeater.itemAt(mainStageAppIndex).animateToShellOrientation();
65 if (priv.sideStageAppId) {
66 var sideStageAppIndex = priv.indexOf(priv.sideStageAppId);
67 if (sideStageAppIndex >= 0 && sideStageAppIndex < spreadRepeater.count) {
68 spreadRepeater.itemAt(sideStageAppIndex).matchShellOrientation();
73 orientationChangesEnabled: priv.mainAppOrientationChangesEnabled
76 spreadView.selectedIndex = -1;
78 spreadView.contentX = -spreadView.shift;
81 onShellOrientationChanged: {
82 if (shellOrientation == Qt.PortraitOrientation || shellOrientation == Qt.InvertedPortraitOrientation) {
83 ApplicationManager.focusApplication(priv.mainStageAppId);
84 priv.sideStageAppId = "";
88 onInverseProgressChanged: {
89 // This can't be a simple binding because that would be triggered after this handler
90 // while we need it active before doing the anition left/right
91 spreadView.animateX = (inverseProgress == 0)
92 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
93 // left edge drag released. Minimum distance is given by design.
94 if (priv.oldInverseProgress > units.gu(22)) {
95 ApplicationManager.requestFocusApplication("unity8-dash");
98 priv.oldInverseProgress = inverseProgress;
103 objectName: "stagesPriv"
105 property string focusedAppId: ApplicationManager.focusedApplicationId
106 readonly property var focusedAppDelegate: {
107 var index = indexOf(focusedAppId);
108 return index >= 0 && index < spreadRepeater.count ? spreadRepeater.itemAt(index) : null
111 property string oldFocusedAppId: ""
112 property bool mainAppOrientationChangesEnabled: false
114 property real landscapeHeight: root.orientations.native_ == Qt.LandscapeOrientation ?
115 root.nativeHeight : root.nativeWidth
117 property bool shellIsLandscape: root.shellOrientation === Qt.LandscapeOrientation
118 || root.shellOrientation === Qt.InvertedLandscapeOrientation
120 property string mainStageAppId
121 property string sideStageAppId
123 // For convenience, keep properties of the first two apps in the model
124 property string appId0
125 property string appId1
127 property int oldInverseProgress: 0
129 onFocusedAppIdChanged: {
130 if (priv.focusedAppId.length > 0) {
131 var focusedApp = ApplicationManager.findApplication(focusedAppId);
132 if (focusedApp.stage == ApplicationInfoInterface.SideStage) {
133 priv.sideStageAppId = focusedAppId;
135 priv.mainStageAppId = focusedAppId;
136 root.mainApp = focusedApp;
140 appId0 = ApplicationManager.count >= 1 ? ApplicationManager.get(0).appId : "";
141 appId1 = ApplicationManager.count > 1 ? ApplicationManager.get(1).appId : "";
144 onFocusedAppDelegateChanged: {
145 if (focusedAppDelegate) {
146 focusedAppDelegate.focus = true;
150 property bool focusedAppDelegateIsDislocated: focusedAppDelegate &&
151 (focusedAppDelegate.dragOffset !== 0 || focusedAppDelegate.xTranslateAnimating)
152 function indexOf(appId) {
153 for (var i = 0; i < ApplicationManager.count; i++) {
154 if (ApplicationManager.get(i).appId == appId) {
161 function evaluateOneWayFlick(gesturePoints) {
162 // Need to have at least 3 points to recognize it as a flick
163 if (gesturePoints.length < 3) {
166 // Need to have a movement of at least 2 grid units to recognize it as a flick
167 if (Math.abs(gesturePoints[gesturePoints.length - 1] - gesturePoints[0]) < units.gu(2)) {
171 var oneWayFlick = true;
172 var smallestX = gesturePoints[0];
173 var leftWards = gesturePoints[1] < gesturePoints[0];
174 for (var i = 1; i < gesturePoints.length; i++) {
175 if ((leftWards && gesturePoints[i] >= smallestX)
176 || (!leftWards && gesturePoints[i] <= smallestX)) {
180 smallestX = gesturePoints[i];
187 target: ApplicationManager
189 if (spreadView.interactive) {
190 spreadView.snapTo(priv.indexOf(appId));
192 ApplicationManager.focusApplication(appId);
196 onApplicationAdded: {
197 if (spreadView.phase == 2) {
198 spreadView.snapTo(ApplicationManager.count - 1);
200 spreadView.phase = 0;
201 spreadView.contentX = -spreadView.shift;
202 ApplicationManager.focusApplication(appId);
206 onApplicationRemoved: {
207 if (priv.mainStageAppId == appId) {
208 ApplicationManager.focusApplication("unity8-dash")
210 if (priv.sideStageAppId == appId) {
211 priv.sideStageAppId = "";
214 if (ApplicationManager.count == 0) {
215 spreadView.phase = 0;
216 spreadView.contentX = -spreadView.shift;
217 } else if (spreadView.closingIndex == -1) {
218 // Unless we're closing the app ourselves in the spread,
219 // lets make sure the spread doesn't mess up by the changing app list.
220 spreadView.phase = 0;
221 spreadView.contentX = -spreadView.shift;
222 ApplicationManager.focusApplication(ApplicationManager.get(0).appId);
229 objectName: "spreadView"
231 interactive: (spreadDragArea.dragging || phase > 1) && draggedDelegateCount === 0
232 contentWidth: spreadRow.width - shift
235 property int tileDistance: units.gu(20)
236 property int sideStageWidth: units.gu(40)
237 property bool sideStageVisible: priv.sideStageAppId
239 // This indicates when the spreadView is active. That means, all the animations
240 // are activated and tiles need to line up for the spread.
241 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
243 // The flickable needs to fill the screen in order to get touch events all over.
244 // However, we don't want to the user to be able to scroll back all the way. For
245 // that, the beginning of the gesture starts with a negative value for contentX
246 // so the flickable wants to pull it into the view already. "shift" tunes the
247 // distance where to "lock" the content.
248 readonly property real shift: width / 2
249 readonly property real shiftedContentX: contentX + shift
251 // Phase of the animation:
252 // 0: Starting from right edge, a new app (index 1) comes in from the right
253 // 1: The app has reached the first snap position.
254 // 2: The list is dragged further and snaps into the spread view when entering phase 2
257 readonly property int phase0Width: sideStageWidth
258 readonly property int phase1Width: sideStageWidth
260 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
261 // 0 - 1: following finger, snap back to the beginning on release
262 readonly property real positionMarker1: 0.2
263 // 1 - 2: curved snapping movement, snap to nextInStack on release
264 readonly property real positionMarker2: sideStageWidth / spreadView.width
265 // 2 - 3: movement follows finger, snaps to phase 2 (full spread) on release
266 readonly property real positionMarker3: 0.6
267 // passing 3, we detach movement from the finger and snap to phase 2 (full spread)
268 readonly property real positionMarker4: 0.8
270 readonly property int startSnapPosition: phase0Width * 0.5
271 readonly property int endSnapPosition: phase0Width * 0.75
272 readonly property real snapPosition: 0.75
274 property int selectedIndex: -1
275 property int draggedDelegateCount: 0
276 property int closingIndex: -1
278 // FIXME: Workaround Flickable's not keepping its contentX still when resized
279 onContentXChanged: { forceItToRemainStillIfBeingResized(); }
280 onShiftChanged: { forceItToRemainStillIfBeingResized(); }
281 function forceItToRemainStillIfBeingResized() {
282 if (root.beingResized && contentX != -shift) {
287 property bool animateX: true
288 property bool beingResized: root.beingResized
289 onBeingResizedChanged: {
291 // Brace yourselves for impact!
298 property bool sideStageDragging: sideStageDragHandle.dragging
299 property real sideStageDragProgress: sideStageDragHandle.progress
301 onSideStageDragProgressChanged: {
302 if (sideStageDragProgress == 1) {
303 ApplicationManager.focusApplication(priv.mainStageAppId);
304 priv.sideStageAppId = "";
308 // In case the ApplicationManager already holds an app when starting up we're missing animations
309 // Make sure we end up in the same state
310 Component.onCompleted: {
311 spreadView.contentX = -spreadView.shift
314 property int nextInStack: {
317 if (ApplicationManager.count > 1) {
321 case "mainAndOverlay":
322 if (ApplicationManager.count <= 2) {
325 if (priv.appId0 == priv.mainStageAppId || priv.appId0 == priv.sideStageAppId) {
326 if (priv.appId1 == priv.mainStageAppId || priv.appId1 == priv.sideStageAppId) {
337 property int nextZInStack: indexToZIndex(nextInStack)
346 State { // Side Stage only in overlay mode
349 State { // Main Stage and Side Stage in overlay mode
350 name: "mainAndOverlay"
352 State { // Main Stage and Side Stage in split mode
357 if (priv.mainStageAppId && !priv.sideStageAppId) {
360 if (!priv.mainStageAppId && priv.sideStageAppId) {
363 if (priv.mainStageAppId && priv.sideStageAppId) {
364 return "mainAndOverlay";
369 onShiftedContentXChanged: {
370 if (root.beingResized) {
371 // Flickabe.contentX wiggles during resizes. Don't react to it.
374 if (spreadView.phase == 0 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker2) {
375 spreadView.phase = 1;
376 } else if (spreadView.phase == 1 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker4) {
377 spreadView.phase = 2;
378 } else if (spreadView.phase == 1 && spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
379 spreadView.phase = 0;
384 if (shiftedContentX < phase0Width) {
385 snapAnimation.targetContentX = -shift;
386 snapAnimation.start();
387 } else if (shiftedContentX < phase1Width) {
390 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
391 snapAnimation.targetContentX = spreadView.width * spreadView.positionMarker4 + 1 - shift;
392 snapAnimation.start();
395 function snapTo(index) {
396 spreadView.selectedIndex = index;
397 snapAnimation.targetContentX = -shift;
398 snapAnimation.start();
401 // We need to shuffle z ordering a bit in order to keep side stage apps above main stage apps.
402 // We don't want to really reorder them in the model because that allows us to keep track
403 // of the last focused order.
404 function indexToZIndex(index) {
405 var app = ApplicationManager.get(index);
410 var active = app.appId == priv.mainStageAppId || app.appId == priv.sideStageAppId;
411 if (active && app.stage == ApplicationInfoInterface.MainStage) {
412 // if this app is active, and its the MainStage, always put it to index 0
415 if (active && app.stage == ApplicationInfoInterface.SideStage) {
416 if (!priv.mainStageAppId) {
417 // Only have SS apps running. Put the active one at 0
421 // Precondition now: There's an active MS app and this is SS app:
422 if (spreadView.nextInStack >= 0 && ApplicationManager.get(spreadView.nextInStack).stage == ApplicationInfoInterface.MainStage) {
423 // If the next app coming from the right is a MS app, we need to elevate this SS ap above it.
424 // Put it to at least level 2, or higher if there's more apps coming in before this one.
425 return Math.max(index, 2);
427 // if this is no next app to come in from the right, place this one at index 1, just on top the active MS app.
431 if (index <= 2 && app.stage == ApplicationInfoInterface.MainStage && priv.sideStageAppId) {
432 // Ok, this is an inactive MS app. If there's an active SS app around, we need to place this one
433 // in between the active MS app and the active SS app, so that it comes in from there when dragging from the right.
434 // If there's now active SS app, just leave it where it is.
435 return priv.indexOf(priv.sideStageAppId) < index ? index - 1 : index;
437 if (index == spreadView.nextInStack && app.stage == ApplicationInfoInterface.SideStage) {
438 // This is a SS app and the next one to come in from the right:
439 if (priv.sideStageAppId && priv.mainStageAppId) {
440 // If there's both, an active MS and an active SS app, put this one right on top of that
443 // Or if there's only one other active app, put it on top of that.
444 // The case that there isn't any other active app is already handled above.
447 if (index == 2 && spreadView.nextInStack == 1 && priv.sideStageAppId) {
448 // If its index 2 but not the next one to come in, it means
449 // we've pulled another one down to index 2. Move this one up to 2 instead.
452 // don't touch all others... (mostly index > 3 + simple cases where the above doesn't shuffle much)
456 SequentialAnimation {
458 property int targetContentX: -spreadView.shift
460 UbuntuNumberAnimation {
463 to: snapAnimation.targetContentX
464 duration: UbuntuAnimation.FastDuration
469 if (spreadView.selectedIndex >= 0) {
470 var newIndex = spreadView.selectedIndex;
471 spreadView.selectedIndex = -1;
472 ApplicationManager.focusApplication(ApplicationManager.get(newIndex).appId);
473 spreadView.phase = 0;
474 spreadView.contentX = -spreadView.shift;
482 x: spreadView.contentX
483 width: spreadView.width + Math.max(spreadView.width, ApplicationManager.count * spreadView.tileDistance)
487 spreadView.snapTo(0);
491 id: sideStageBackground
493 width: spreadView.sideStageWidth * (1 - sideStageDragHandle.progress)
494 height: priv.landscapeHeight
495 x: spreadView.width - width
496 z: spreadView.indexToZIndex(priv.indexOf(priv.sideStageAppId))
497 opacity: spreadView.phase == 0 ? 1 : 0
498 Behavior on opacity { UbuntuNumberAnimation {} }
502 id: sideStageDragHandle
503 anchors.right: sideStageBackground.left
504 anchors.top: sideStageBackground.top
506 height: priv.landscapeHeight
507 z: sideStageBackground.z
508 opacity: spreadView.phase <= 0 && spreadView.sideStageVisible ? 1 : 0
509 property real progress: 0
510 property bool dragging: false
512 Behavior on opacity { UbuntuNumberAnimation {} }
516 onSideStageVisibleChanged: {
517 if (spreadView.sideStageVisible) {
518 sideStageDragHandle.progress = 0;
524 anchors.centerIn: parent
525 width: sideStageDragHandleMouseArea.pressed ? parent.width * 2 : parent.width
526 height: parent.height
527 source: "graphics/sidestage_handle@20.png"
528 Behavior on width { UbuntuNumberAnimation {} }
532 id: sideStageDragHandleMouseArea
534 enabled: spreadView.shiftedContentX == 0
536 property var gesturePoints: new Array()
537 property real totalDiff
543 sideStageDragHandle.progress = 0;
544 sideStageDragHandle.dragging = true;
547 totalDiff += mouseX - startX;
548 if (priv.mainStageAppId) {
549 sideStageDragHandle.progress = Math.max(0, totalDiff / spreadView.sideStageWidth);
551 gesturePoints.push(mouseX);
554 if (priv.mainStageAppId) {
555 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
556 sideStageDragSnapAnimation.to = sideStageDragHandle.progress > 0.5 || oneWayFlick ? 1 : 0;
557 sideStageDragSnapAnimation.start();
559 sideStageDragHandle.dragging = false;
563 UbuntuNumberAnimation {
564 id: sideStageDragSnapAnimation
565 target: sideStageDragHandle
570 sideStageDragHandle.dragging = false;
578 objectName: "spreadRepeater"
579 model: ApplicationManager
581 delegate: TransformedTabletSpreadDelegate {
583 objectName: model.appId ? "tabletSpreadDelegate_" + model.appId
584 : "tabletSpreadDelegate_null";
586 if (wantsMainStage) {
587 return spreadView.width;
589 return spreadView.sideStageWidth;
593 if (wantsMainStage) {
594 return spreadView.height;
596 return priv.landscapeHeight;
599 active: model.appId == priv.mainStageAppId || model.appId == priv.sideStageAppId
600 zIndex: spreadView.indexToZIndex(index)
601 selected: spreadView.selectedIndex == index
602 otherSelected: spreadView.selectedIndex >= 0 && !selected
603 isInSideStage: priv.sideStageAppId == model.appId
604 interactive: !spreadView.interactive && spreadView.phase === 0 && root.interactive
605 swipeToCloseEnabled: spreadView.interactive && !snapAnimation.running
606 maximizedAppTopMargin: root.maximizedAppTopMargin
607 dragOffset: !isDash && model.appId == priv.mainStageAppId && root.inverseProgress > 0 && spreadView.phase === 0 ? root.inverseProgress : 0
608 application: ApplicationManager.get(index)
611 readonly property bool wantsMainStage: model.stage == ApplicationInfoInterface.MainStage
613 readonly property bool isDash: model.appId == "unity8-dash"
615 readonly property bool canSuspend: model.isTouchApp
616 && !isExemptFromLifecycle(model.appId)
619 target: spreadTile.application
620 property: "requestedState"
622 || (isDash && root.keepDashRunning)
623 || (!root.suspended && (model.appId == priv.mainStageAppId
624 || model.appId == priv.sideStageAppId))
625 ? ApplicationInfoInterface.RequestedRunning
626 : ApplicationInfoInterface.RequestedSuspended
629 // FIXME: A regular binding doesn't update any more after closing an app.
630 // Using a Binding for now.
634 value: (!spreadView.active && isDash && !active) ? -1 : spreadTile.zIndex
638 property real behavioredZIndex: zIndex
639 Behavior on behavioredZIndex {
640 enabled: spreadView.closingIndex >= 0
641 UbuntuNumberAnimation {}
644 // This is required because none of the bindings are triggered in some cases:
645 // When an app is closed, it might happen that ApplicationManager.get(nextInStack)
646 // returns a different app even though the nextInStackIndex and all the related
647 // bindings (index, mainStageApp, sideStageApp, etc) don't change. Let's force a
648 // binding update in that case.
650 target: ApplicationManager
651 onApplicationRemoved: spreadTile.z = Qt.binding(function() {
652 return spreadView.indexToZIndex(index);
657 var tileProgress = (spreadView.shiftedContentX - behavioredZIndex * spreadView.tileDistance) / spreadView.width;
658 // Some tiles (nextInStack, active) need to move directly from the beginning, normalize progress to immediately start at 0
659 if ((index == spreadView.nextInStack && spreadView.phase < 2) || (active && spreadView.phase < 1)) {
660 tileProgress += behavioredZIndex * spreadView.tileDistance / spreadView.width;
665 // TODO: Hiding tile when progress is such that it will be off screen.
666 property bool occluded: {
667 if (spreadView.active) return false;
668 else if (spreadTile.active) return false;
669 else if (xTranslateAnimating) return false;
670 else if (z <= 1 && priv.focusedAppDelegateIsDislocated) return false;
674 visible: Powerd.status == Powerd.On &&
675 !greeter.fullyShown &&
679 if (spreadView.phase == 0 && (spreadTile.active || spreadView.nextInStack == index)) {
680 if (progress < spreadView.positionMarker1) {
682 } else if (progress < spreadView.positionMarker1 + snappingCurve.period) {
683 return spreadView.positionMarker1 + snappingCurve.value * 3;
685 return spreadView.positionMarker2;
691 shellOrientationAngle: wantsMainStage ? root.shellOrientationAngle : 0
692 shellOrientation: wantsMainStage ? root.shellOrientation : Qt.PortraitOrientation
693 orientations: Orientations {
694 primary: spreadTile.wantsMainStage ? root.orientations.primary : Qt.PortraitOrientation
695 native_: spreadTile.wantsMainStage ? root.orientations.native_ : Qt.PortraitOrientation
696 portrait: root.orientations.portrait
697 invertedPortrait: root.orientations.invertedPortrait
698 landscape: root.orientations.landscape
699 invertedLandscape: root.orientations.invertedLandscape
703 if (spreadView.phase == 2) {
704 spreadView.snapTo(index);
710 spreadView.draggedDelegateCount++;
712 spreadView.draggedDelegateCount--;
717 spreadView.closingIndex = index;
718 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
723 when: model.appId == priv.mainStageAppId
724 property: "mainAppWindowOrientationAngle"
725 value: appWindowOrientationAngle
729 when: model.appId == priv.mainStageAppId
730 property: "mainAppOrientationChangesEnabled"
731 value: orientationChangesEnabled
736 type: EasingCurve.Linear
737 period: (spreadView.positionMarker2 - spreadView.positionMarker1) / 3
738 progress: spreadTile.progress - spreadView.positionMarker1
745 //eat touch events during the right edge gesture
748 enabled: spreadDragArea.dragging
751 DirectionalDragArea {
753 objectName: "spreadDragArea"
754 anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
755 width: root.dragAreaWidth
756 direction: Direction.Leftwards
757 enabled: (spreadView.phase != 2 && root.spreadEnabled) || dragging
759 property var gesturePoints: new Array()
763 spreadView.phase = 0;
764 spreadView.contentX = -spreadView.shift;
768 var dragX = -touchX + spreadDragArea.width - spreadView.shift;
769 var maxDrag = spreadView.width * spreadView.positionMarker4 - spreadView.shift;
770 spreadView.contentX = Math.min(dragX, maxDrag);
772 gesturePoints.push(touchX);
777 // Gesture recognized. Start recording this gesture
780 // Ok. The user released. Find out if it was a one-way movement.
781 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
784 if (oneWayFlick && spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
785 // If it was a short one-way movement, do the Alt+Tab switch
786 // no matter if we didn't cross positionMarker1 yet.
787 spreadView.snapTo(spreadView.nextInStack);
789 if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker1) {
791 } else if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
792 spreadView.snapTo(spreadView.nextInStack);
794 // otherwise snap to the closest snap position we can find
795 // (might be back to start, to app 1 or to spread)