2 * Copyright (C) 2014 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 0.1
19 import Ubuntu.Gestures 0.1
20 import Unity.Application 0.1
22 import "../Components"
23 import "../Components/Flickables" as Flickables
31 // Controls to be set from outside
32 property bool shown: false
33 property bool moving: false
34 property int dragAreaWidth
35 property real maximizedAppTopMargin
36 property bool interactive
37 property real inverseProgress: 0 // This is the progress for left edge drags, in pixels.
38 property int orientation: Qt.PortraitOrientation
40 onInverseProgressChanged: {
41 // This can't be a simple binding because that would be triggered after this handler
42 // while we need it active before doing the anition left/right
43 spreadView.animateX = (inverseProgress == 0)
44 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
45 // left edge drag released. Minimum distance is given by design.
46 if (priv.oldInverseProgress > units.gu(22)) {
47 ApplicationManager.focusApplication("unity8-dash");
50 priv.oldInverseProgress = inverseProgress;
56 property string focusedAppId: ApplicationManager.focusedApplicationId
57 property string oldFocusedAppId: ""
59 property string mainStageAppId
60 property string sideStageAppId
62 // For convenience, keep properties of the first two apps in the model
63 property string appId0
64 property string appId1
66 property int oldInverseProgress: 0
68 onFocusedAppIdChanged: {
69 if (priv.focusedAppId.length > 0) {
70 var focusedApp = ApplicationManager.findApplication(focusedAppId);
71 if (focusedApp.stage == ApplicationInfoInterface.SideStage) {
72 priv.sideStageAppId = focusedAppId;
74 priv.mainStageAppId = focusedAppId;
78 appId0 = ApplicationManager.count >= 1 ? ApplicationManager.get(0).appId : "";
79 appId1 = ApplicationManager.count > 1 ? ApplicationManager.get(1).appId : "";
82 function indexOf(appId) {
83 for (var i = 0; i < ApplicationManager.count; i++) {
84 if (ApplicationManager.get(i).appId == appId) {
91 function evaluateOneWayFlick(gesturePoints) {
92 // Need to have at least 3 points to recognize it as a flick
93 if (gesturePoints.length < 3) {
96 // Need to have a movement of at least 2 grid units to recognize it as a flick
97 if (Math.abs(gesturePoints[gesturePoints.length - 1] - gesturePoints[0]) < units.gu(2)) {
101 var oneWayFlick = true;
102 var smallestX = gesturePoints[0];
103 var leftWards = gesturePoints[1] < gesturePoints[0];
104 for (var i = 1; i < gesturePoints.length; i++) {
105 if ((leftWards && gesturePoints[i] >= smallestX)
106 || (!leftWards && gesturePoints[i] <= smallestX)) {
110 smallestX = gesturePoints[i];
117 target: ApplicationManager
119 if (spreadView.interactive) {
120 spreadView.snapTo(priv.indexOf(appId));
122 ApplicationManager.focusApplication(appId);
126 onApplicationAdded: {
127 if (spreadView.phase == 2) {
128 spreadView.snapTo(ApplicationManager.count - 1);
130 spreadView.phase = 0;
131 spreadView.contentX = -spreadView.shift;
132 ApplicationManager.focusApplication(appId);
136 onApplicationRemoved: {
137 if (priv.mainStageAppId == appId) {
138 ApplicationManager.focusApplication("unity8-dash")
140 if (priv.sideStageAppId == appId) {
141 priv.sideStageAppId = "";
144 if (ApplicationManager.count == 0) {
145 spreadView.phase = 0;
146 spreadView.contentX = -spreadView.shift;
147 } else if (spreadView.closingIndex == -1) {
148 // Unless we're closing the app ourselves in the spread,
149 // lets make sure the spread doesn't mess up by the changing app list.
150 spreadView.phase = 0;
151 spreadView.contentX = -spreadView.shift;
152 ApplicationManager.focusApplication(ApplicationManager.get(0).appId);
157 Flickables.Flickable {
160 interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
161 && draggedDelegateCount === 0
162 contentWidth: spreadRow.width - shift
165 property int tileDistance: units.gu(20)
166 property int sideStageWidth: units.gu(40)
167 property bool sideStageVisible: priv.sideStageAppId
169 // This indicates when the spreadView is active. That means, all the animations
170 // are activated and tiles need to line up for the spread.
171 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
173 // The flickable needs to fill the screen in order to get touch events all over.
174 // However, we don't want to the user to be able to scroll back all the way. For
175 // that, the beginning of the gesture starts with a negative value for contentX
176 // so the flickable wants to pull it into the view already. "shift" tunes the
177 // distance where to "lock" the content.
178 readonly property real shift: width / 2
179 readonly property real shiftedContentX: contentX + shift
181 // Phase of the animation:
182 // 0: Starting from right edge, a new app (index 1) comes in from the right
183 // 1: The app has reached the first snap position.
184 // 2: The list is dragged further and snaps into the spread view when entering phase 2
187 readonly property int phase0Width: sideStageWidth
188 readonly property int phase1Width: sideStageWidth
190 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
191 // 0 - 1: following finger, snap back to the beginning on release
192 readonly property real positionMarker1: 0.2
193 // 1 - 2: curved snapping movement, snap to nextInStack on release
194 readonly property real positionMarker2: sideStageWidth / spreadView.width
195 // 2 - 3: movement follows finger, snaps to phase 2 (full spread) on release
196 readonly property real positionMarker3: 0.6
197 // passing 3, we detach movement from the finger and snap to phase 2 (full spread)
198 readonly property real positionMarker4: 0.8
200 readonly property int startSnapPosition: phase0Width * 0.5
201 readonly property int endSnapPosition: phase0Width * 0.75
202 readonly property real snapPosition: 0.75
204 property int selectedIndex: -1
205 property int draggedDelegateCount: 0
206 property int closingIndex: -1
208 property bool animateX: true
210 property bool sideStageDragging: sideStageDragHandle.dragging
211 property real sideStageDragProgress: sideStageDragHandle.progress
213 onSideStageDragProgressChanged: {
214 if (sideStageDragProgress == 1) {
215 ApplicationManager.focusApplication(priv.mainStageAppId);
216 priv.sideStageAppId = "";
220 // In case the ApplicationManager already holds an app when starting up we're missing animations
221 // Make sure we end up in the same state
222 Component.onCompleted: {
223 spreadView.contentX = -spreadView.shift
226 property int nextInStack: {
229 if (ApplicationManager.count > 1) {
233 case "mainAndOverlay":
234 if (ApplicationManager.count <= 2) {
237 if (priv.appId0 == priv.mainStageAppId || priv.appId0 == priv.sideStageAppId) {
238 if (priv.appId1 == priv.mainStageAppId || priv.appId1 == priv.sideStageAppId) {
247 print("Unhandled nextInStack case! This shouldn't happen any more when the Dash is an app!");
250 property int nextZInStack: indexToZIndex(nextInStack)
259 State { // Side Stage only in overlay mode
262 State { // Main Stage and Side Stage in overlay mode
263 name: "mainAndOverlay"
265 State { // Main Stage and Side Stage in split mode
270 if (priv.mainStageAppId && !priv.sideStageAppId) {
273 if (!priv.mainStageAppId && priv.sideStageAppId) {
276 if (priv.mainStageAppId && priv.sideStageAppId) {
277 return "mainAndOverlay";
282 onShiftedContentXChanged: {
283 if (spreadView.phase == 0 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker2) {
284 spreadView.phase = 1;
285 } else if (spreadView.phase == 1 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker4) {
286 spreadView.phase = 2;
287 } else if (spreadView.phase == 1 && spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
288 spreadView.phase = 0;
293 if (shiftedContentX < phase0Width) {
294 snapAnimation.targetContentX = -shift;
295 snapAnimation.start();
296 } else if (shiftedContentX < phase1Width) {
299 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
300 snapAnimation.targetContentX = spreadView.width * spreadView.positionMarker4 + 1 - shift;
301 snapAnimation.start();
304 function snapTo(index) {
305 spreadView.selectedIndex = index;
306 snapAnimation.targetContentX = -shift;
307 snapAnimation.start();
310 // We need to shuffle z ordering a bit in order to keep side stage apps above main stage apps.
311 // We don't want to really reorder them in the model because that allows us to keep track
312 // of the last focused order.
313 function indexToZIndex(index) {
314 var app = ApplicationManager.get(index);
319 var active = app.appId == priv.mainStageAppId || app.appId == priv.sideStageAppId;
320 if (active && app.stage == ApplicationInfoInterface.MainStage) {
321 // if this app is active, and its the MainStage, always put it to index 0
324 if (active && app.stage == ApplicationInfoInterface.SideStage) {
325 if (!priv.mainStageAppId) {
326 // Only have SS apps running. Put the active one at 0
330 // Precondition now: There's an active MS app and this is SS app:
331 if (spreadView.nextInStack >= 0 && ApplicationManager.get(spreadView.nextInStack).stage == ApplicationInfoInterface.MainStage) {
332 // If the next app coming from the right is a MS app, we need to elevate this SS ap above it.
333 // Put it to at least level 2, or higher if there's more apps coming in before this one.
334 return Math.max(index, 2);
336 // 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.
340 if (index <= 2 && app.stage == ApplicationInfoInterface.MainStage && priv.sideStageAppId) {
341 // Ok, this is an inactive MS app. If there's an active SS app around, we need to place this one
342 // in between the active MS app and the active SS app, so that it comes in from there when dragging from the right.
343 // If there's now active SS app, just leave it where it is.
344 return priv.indexOf(priv.sideStageAppId) < index ? index - 1 : index;
346 if (index == spreadView.nextInStack && app.stage == ApplicationInfoInterface.SideStage) {
347 // This is a SS app and the next one to come in from the right:
348 if (priv.sideStageAppId && priv.mainStageAppId) {
349 // If there's both, an active MS and an active SS app, put this one right on top of that
352 // Or if there's only one other active app, put it on top of that.
353 // The case that there isn't any other active app is already handled above.
356 if (index == 2 && spreadView.nextInStack == 1 && priv.sideStageAppId) {
357 // If its index 2 but not the next one to come in, it means
358 // we've pulled another one down to index 2. Move this one up to 2 instead.
361 // don't touch all others... (mostly index > 3 + simple cases where the above doesn't shuffle much)
365 SequentialAnimation {
367 property int targetContentX: -spreadView.shift
369 UbuntuNumberAnimation {
372 to: snapAnimation.targetContentX
373 duration: UbuntuAnimation.FastDuration
378 if (spreadView.selectedIndex >= 0) {
379 var newIndex = spreadView.selectedIndex;
380 spreadView.selectedIndex = -1;
381 ApplicationManager.focusApplication(ApplicationManager.get(newIndex).appId);
382 spreadView.phase = 0;
383 spreadView.contentX = -spreadView.shift;
391 x: spreadView.contentX
393 width: spreadView.width + Math.max(spreadView.width, ApplicationManager.count * spreadView.tileDistance)
396 id: sideStageBackground
399 anchors.leftMargin: spreadView.width - (1 - sideStageDragHandle.progress) * spreadView.sideStageWidth
400 z: spreadView.indexToZIndex(priv.indexOf(priv.sideStageAppId))
401 opacity: spreadView.phase == 0 ? 1 : 0
402 Behavior on opacity { UbuntuNumberAnimation {} }
406 id: sideStageDragHandle
407 anchors { top: parent.top; bottom: parent.bottom; left: parent.left; leftMargin: spreadView.width - spreadView.sideStageWidth - width }
409 z: sideStageBackground.z
410 opacity: spreadView.phase <= 0 && spreadView.sideStageVisible ? 1 : 0
411 property real progress: 0
412 property bool dragging: false
414 Behavior on opacity { UbuntuNumberAnimation {} }
418 onSideStageVisibleChanged: {
419 if (spreadView.sideStageVisible) {
420 sideStageDragHandle.progress = 0;
426 anchors.centerIn: parent
427 anchors.horizontalCenterOffset: parent.progress * spreadView.sideStageWidth - (width - parent.width) / 2
428 width: sideStageDragHandleMouseArea.pressed ? parent.width * 2 : parent.width
429 height: parent.height
430 source: "graphics/sidestage_handle@20.png"
431 Behavior on width { UbuntuNumberAnimation {} }
435 id: sideStageDragHandleMouseArea
437 enabled: spreadView.shiftedContentX == 0
439 property var gesturePoints: new Array()
444 sideStageDragHandle.progress = 0;
445 sideStageDragHandle.dragging = true;
448 if (priv.mainStageAppId) {
449 sideStageDragHandle.progress = Math.max(0, (-startX + mouseX) / spreadView.sideStageWidth);
451 gesturePoints.push(mouseX);
454 if (priv.mainStageAppId) {
455 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
456 sideStageDragSnapAnimation.to = sideStageDragHandle.progress > 0.5 || oneWayFlick ? 1 : 0;
457 sideStageDragSnapAnimation.start();
459 sideStageDragHandle.dragging = false;
463 UbuntuNumberAnimation {
464 id: sideStageDragSnapAnimation
465 target: sideStageDragHandle
470 sideStageDragHandle.dragging = false;
478 model: ApplicationManager
480 delegate: TransformedTabletSpreadDelegate {
482 height: spreadView.height
483 width: model.stage == ApplicationInfoInterface.MainStage ? spreadView.width : spreadView.sideStageWidth
484 active: model.appId == priv.mainStageAppId || model.appId == priv.sideStageAppId
485 zIndex: spreadView.indexToZIndex(index)
486 selected: spreadView.selectedIndex == index
487 otherSelected: spreadView.selectedIndex >= 0 && !selected
488 isInSideStage: priv.sideStageAppId == model.appId
489 interactive: !spreadView.interactive && spreadView.phase === 0 && root.interactive
490 swipeToCloseEnabled: spreadView.interactive
491 maximizedAppTopMargin: root.maximizedAppTopMargin
492 dragOffset: !isDash && model.appId == priv.mainStageAppId && root.inverseProgress > 0 ? root.inverseProgress : 0
493 application: ApplicationManager.get(index)
496 readonly property bool isDash: model.appId == "unity8-dash"
498 // FIXME: A regular binding doesn't update any more after closing an app.
499 // Using a Binding for now.
503 value: (!spreadView.active && isDash && !active) ? -1 : spreadTile.zIndex
507 property real behavioredZIndex: zIndex
508 Behavior on behavioredZIndex {
509 enabled: spreadView.closingIndex >= 0
510 UbuntuNumberAnimation {}
513 // This is required because none of the bindings are triggered in some cases:
514 // When an app is closed, it might happen that ApplicationManager.get(nextInStack)
515 // returns a different app even though the nextInStackIndex and all the related
516 // bindings (index, mainStageApp, sideStageApp, etc) don't change. Let's force a
517 // binding update in that case.
519 target: ApplicationManager
520 onApplicationRemoved: spreadTile.z = Qt.binding(function() {
521 return spreadView.indexToZIndex(index);
526 var tileProgress = (spreadView.shiftedContentX - behavioredZIndex * spreadView.tileDistance) / spreadView.width;
527 // Some tiles (nextInStack, active) need to move directly from the beginning, normalize progress to immediately start at 0
528 if ((index == spreadView.nextInStack && spreadView.phase < 2) || (active && spreadView.phase < 1)) {
529 tileProgress += behavioredZIndex * spreadView.tileDistance / spreadView.width;
535 if (spreadView.phase == 0 && (spreadTile.active || spreadView.nextInStack == index)) {
536 if (progress < spreadView.positionMarker1) {
538 } else if (progress < spreadView.positionMarker1 + snappingCurve.period) {
539 return spreadView.positionMarker1 + snappingCurve.value * 3;
541 return spreadView.positionMarker2;
549 property: "orientation"
550 when: spreadTile.interactive
551 value: root.orientation
555 if (spreadView.phase == 2) {
556 spreadView.snapTo(index);
562 spreadView.draggedDelegateCount++;
564 spreadView.draggedDelegateCount--;
569 spreadView.closingIndex = index;
570 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
575 type: EasingCurve.Linear
576 period: (spreadView.positionMarker2 - spreadView.positionMarker1) / 3
577 progress: spreadTile.progress - spreadView.positionMarker1
586 anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
587 width: root.dragAreaWidth
588 direction: Direction.Leftwards
590 property bool attachedToView: false
591 property var gesturePoints: new Array()
595 spreadView.phase = 0;
596 spreadView.contentX = -spreadView.shift;
599 if (dragging && attachedToView) {
600 spreadView.contentX = -touchX + spreadDragArea.width - spreadView.shift;
601 if (spreadView.shiftedContentX > spreadView.phase0Width + spreadView.phase1Width / 2) {
602 attachedToView = false;
606 gesturePoints.push(touchX);
610 if (status == DirectionalDragArea.Recognized) {
611 attachedToView = true;
617 // Gesture recognized. Start recording this gesture
622 // Ok. The user released. Find out if it was a one-way movement.
623 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
626 if (oneWayFlick && spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
627 // If it was a short one-way movement, do the Alt+Tab switch
628 // no matter if we didn't cross positionMarker1 yet.
629 spreadView.snapTo(spreadView.nextInStack);
630 } else if (!dragging && attachedToView) {
631 if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker1) {
633 } else if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
634 spreadView.snapTo(spreadView.nextInStack);
636 // otherwise snap to the closest snap position we can find
637 // (might be back to start, to app 1 or to spread)