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"
30 // Controls to be set from outside
31 property bool shown: false
32 property bool moving: false
33 property int dragAreaWidth
34 property real maximizedAppTopMargin
35 property bool interactive
36 property real inverseProgress: 0 // This is the progress for left edge drags, in pixels.
37 property int orientation: Qt.PortraitOrientation
39 onInverseProgressChanged: {
40 // This can't be a simple binding because that would be triggered after this handler
41 // while we need it active before doing the anition left/right
42 spreadView.animateX = (inverseProgress == 0)
43 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
44 // left edge drag released. Minimum distance is given by design.
45 if (priv.oldInverseProgress > units.gu(22)) {
46 ApplicationManager.focusApplication("unity8-dash");
49 priv.oldInverseProgress = inverseProgress;
55 property string focusedAppId: ApplicationManager.focusedApplicationId
56 property string oldFocusedAppId: ""
58 property string mainStageAppId
59 property string sideStageAppId
61 // For convenience, keep properties of the first two apps in the model
62 property string appId0
63 property string appId1
65 property int oldInverseProgress: 0
67 onFocusedAppIdChanged: {
68 if (priv.focusedAppId.length > 0) {
69 var focusedApp = ApplicationManager.findApplication(focusedAppId);
70 if (focusedApp.stage == ApplicationInfoInterface.SideStage) {
71 priv.sideStageAppId = focusedAppId;
73 priv.mainStageAppId = focusedAppId;
77 appId0 = ApplicationManager.count >= 1 ? ApplicationManager.get(0).appId : "";
78 appId1 = ApplicationManager.count > 1 ? ApplicationManager.get(1).appId : "";
81 function indexOf(appId) {
82 for (var i = 0; i < ApplicationManager.count; i++) {
83 if (ApplicationManager.get(i).appId == appId) {
90 function evaluateOneWayFlick(gesturePoints) {
91 // Need to have at least 3 points to recognize it as a flick
92 if (gesturePoints.length < 3) {
95 // Need to have a movement of at least 2 grid units to recognize it as a flick
96 if (Math.abs(gesturePoints[gesturePoints.length - 1] - gesturePoints[0]) < units.gu(2)) {
100 var oneWayFlick = true;
101 var smallestX = gesturePoints[0];
102 var leftWards = gesturePoints[1] < gesturePoints[0];
103 for (var i = 1; i < gesturePoints.length; i++) {
104 if ((leftWards && gesturePoints[i] >= smallestX)
105 || (!leftWards && gesturePoints[i] <= smallestX)) {
109 smallestX = gesturePoints[i];
116 target: ApplicationManager
118 if (spreadView.interactive) {
119 spreadView.snapTo(priv.indexOf(appId));
121 ApplicationManager.focusApplication(appId);
125 onApplicationAdded: {
126 if (spreadView.phase == 2) {
127 spreadView.snapTo(ApplicationManager.count - 1);
129 spreadView.phase = 0;
130 spreadView.contentX = -spreadView.shift;
131 ApplicationManager.focusApplication(appId);
135 onApplicationRemoved: {
136 if (priv.mainStageAppId == appId) {
137 ApplicationManager.focusApplication("unity8-dash")
139 if (priv.sideStageAppId == appId) {
140 priv.sideStageAppId = "";
142 if (ApplicationManager.count == 0) {
143 spreadView.phase = 0;
144 spreadView.contentX = -spreadView.shift;
152 interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
153 && draggedDelegateCount === 0
154 contentWidth: spreadRow.width - shift
157 property int tileDistance: units.gu(20)
158 property int sideStageWidth: units.gu(40)
159 property bool sideStageVisible: priv.sideStageAppId
161 // This indicates when the spreadView is active. That means, all the animations
162 // are activated and tiles need to line up for the spread.
163 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
165 // The flickable needs to fill the screen in order to get touch events all over.
166 // However, we don't want to the user to be able to scroll back all the way. For
167 // that, the beginning of the gesture starts with a negative value for contentX
168 // so the flickable wants to pull it into the view already. "shift" tunes the
169 // distance where to "lock" the content.
170 readonly property real shift: width / 2
171 readonly property real shiftedContentX: contentX + shift
173 // Phase of the animation:
174 // 0: Starting from right edge, a new app (index 1) comes in from the right
175 // 1: The app has reached the first snap position.
176 // 2: The list is dragged further and snaps into the spread view when entering phase 2
179 readonly property int phase0Width: sideStageWidth
180 readonly property int phase1Width: sideStageWidth
182 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
183 // 0 - 1: following finger, snap back to the beginning on release
184 readonly property real positionMarker1: 0.2
185 // 1 - 2: curved snapping movement, snap to nextInStack on release
186 readonly property real positionMarker2: sideStageWidth / spreadView.width
187 // 2 - 3: movement follows finger, snaps to phase 2 (full spread) on release
188 readonly property real positionMarker3: 0.6
189 // passing 3, we detach movement from the finger and snap to phase 2 (full spread)
190 readonly property real positionMarker4: 0.8
192 readonly property int startSnapPosition: phase0Width * 0.5
193 readonly property int endSnapPosition: phase0Width * 0.75
194 readonly property real snapPosition: 0.75
196 property int selectedIndex: -1
197 property int draggedDelegateCount: 0
198 property int closingIndex: -1
200 property bool animateX: true
202 property bool sideStageDragging: sideStageDragHandle.dragging
203 property real sideStageDragProgress: sideStageDragHandle.progress
205 onSideStageDragProgressChanged: {
206 if (sideStageDragProgress == 1) {
207 ApplicationManager.focusApplication(priv.mainStageAppId);
208 priv.sideStageAppId = "";
212 // In case the ApplicationManager already holds an app when starting up we're missing animations
213 // Make sure we end up in the same state
214 Component.onCompleted: {
215 spreadView.contentX = -spreadView.shift
218 property int nextInStack: {
221 if (ApplicationManager.count > 1) {
225 case "mainAndOverlay":
226 if (ApplicationManager.count <= 2) {
229 if (priv.appId0 == priv.mainStageAppId || priv.appId0 == priv.sideStageAppId) {
230 if (priv.appId1 == priv.mainStageAppId || priv.appId1 == priv.sideStageAppId) {
239 print("Unhandled nextInStack case! This shouldn't happen any more when the Dash is an app!");
242 property int nextZInStack: indexToZIndex(nextInStack)
251 State { // Side Stage only in overlay mode
254 State { // Main Stage and Side Stage in overlay mode
255 name: "mainAndOverlay"
257 State { // Main Stage and Side Stage in split mode
262 if (priv.mainStageAppId && !priv.sideStageAppId) {
265 if (!priv.mainStageAppId && priv.sideStageAppId) {
268 if (priv.mainStageAppId && priv.sideStageAppId) {
269 return "mainAndOverlay";
274 onShiftedContentXChanged: {
275 if (spreadView.phase == 0 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker2) {
276 spreadView.phase = 1;
277 } else if (spreadView.phase == 1 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker4) {
278 spreadView.phase = 2;
279 } else if (spreadView.phase == 1 && spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
280 spreadView.phase = 0;
285 if (shiftedContentX < phase0Width) {
286 snapAnimation.targetContentX = -shift;
287 snapAnimation.start();
288 } else if (shiftedContentX < phase1Width) {
291 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
292 snapAnimation.targetContentX = spreadView.width * spreadView.positionMarker4 + 1 - shift;
293 snapAnimation.start();
296 function snapTo(index) {
297 spreadView.selectedIndex = index;
298 snapAnimation.targetContentX = -shift;
299 snapAnimation.start();
302 // We need to shuffle z ordering a bit in order to keep side stage apps above main stage apps.
303 // We don't want to really reorder them in the model because that allows us to keep track
304 // of the last focused order.
305 function indexToZIndex(index) {
306 var app = ApplicationManager.get(index);
311 var active = app.appId == priv.mainStageAppId || app.appId == priv.sideStageAppId;
312 if (active && app.stage == ApplicationInfoInterface.MainStage) {
313 // if this app is active, and its the MainStage, always put it to index 0
316 if (active && app.stage == ApplicationInfoInterface.SideStage) {
317 if (!priv.mainStageAppId) {
318 // Only have SS apps running. Put the active one at 0
322 // Precondition now: There's an active MS app and this is SS app:
323 if (spreadView.nextInStack >= 0 && ApplicationManager.get(spreadView.nextInStack).stage == ApplicationInfoInterface.MainStage) {
324 // If the next app coming from the right is a MS app, we need to elevate this SS ap above it.
325 // Put it to at least level 2, or higher if there's more apps coming in before this one.
326 return Math.max(index, 2);
328 // 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.
332 if (index <= 2 && app.stage == ApplicationInfoInterface.MainStage && priv.sideStageAppId) {
333 // Ok, this is an inactive MS app. If there's an active SS app around, we need to place this one
334 // in between the active MS app and the active SS app, so that it comes in from there when dragging from the right.
335 // If there's now active SS app, just leave it where it is.
336 return priv.indexOf(priv.sideStageAppId) < index ? index - 1 : index;
338 if (index == spreadView.nextInStack && app.stage == ApplicationInfoInterface.SideStage) {
339 // This is a SS app and the next one to come in from the right:
340 if (priv.sideStageAppId && priv.mainStageAppId) {
341 // If there's both, an active MS and an active SS app, put this one right on top of that
344 // Or if there's only one other active app, put it on top of that.
345 // The case that there isn't any other active app is already handled above.
348 if (index == 2 && spreadView.nextInStack == 1 && priv.sideStageAppId) {
349 // If its index 2 but not the next one to come in, it means
350 // we've pulled another one down to index 2. Move this one up to 2 instead.
353 // don't touch all others... (mostly index > 3 + simple cases where the above doesn't shuffle much)
357 SequentialAnimation {
359 property int targetContentX: -spreadView.shift
361 UbuntuNumberAnimation {
364 to: snapAnimation.targetContentX
365 duration: UbuntuAnimation.FastDuration
370 if (spreadView.selectedIndex >= 0) {
371 var newIndex = spreadView.selectedIndex;
372 spreadView.selectedIndex = -1;
373 ApplicationManager.focusApplication(ApplicationManager.get(newIndex).appId);
374 spreadView.phase = 0;
375 spreadView.contentX = -spreadView.shift;
383 x: spreadView.contentX
385 width: spreadView.width + Math.max(spreadView.width, ApplicationManager.count * spreadView.tileDistance)
388 id: sideStageBackground
391 anchors.leftMargin: spreadView.width - (1 - sideStageDragHandle.progress) * spreadView.sideStageWidth
392 z: spreadView.indexToZIndex(priv.indexOf(priv.sideStageAppId))
393 opacity: spreadView.phase == 0 ? 1 : 0
394 Behavior on opacity { UbuntuNumberAnimation {} }
398 id: sideStageDragHandle
399 anchors { top: parent.top; bottom: parent.bottom; left: parent.left; leftMargin: spreadView.width - spreadView.sideStageWidth - width }
401 z: sideStageBackground.z
402 opacity: spreadView.phase <= 0 && spreadView.sideStageVisible ? 1 : 0
403 property real progress: 0
404 property bool dragging: false
406 Behavior on opacity { UbuntuNumberAnimation {} }
410 onSideStageVisibleChanged: {
411 if (spreadView.sideStageVisible) {
412 sideStageDragHandle.progress = 0;
418 anchors.centerIn: parent
419 anchors.horizontalCenterOffset: parent.progress * spreadView.sideStageWidth - (width - parent.width) / 2
420 width: sideStageDragHandleMouseArea.pressed ? parent.width * 2 : parent.width
421 height: parent.height
422 source: "graphics/sidestage_handle@20.png"
423 Behavior on width { UbuntuNumberAnimation {} }
427 id: sideStageDragHandleMouseArea
429 enabled: spreadView.shiftedContentX == 0
431 property var gesturePoints: new Array()
436 sideStageDragHandle.progress = 0;
437 sideStageDragHandle.dragging = true;
440 if (priv.mainStageAppId) {
441 sideStageDragHandle.progress = Math.max(0, (-startX + mouseX) / spreadView.sideStageWidth);
443 gesturePoints.push(mouseX);
446 if (priv.mainStageAppId) {
447 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
448 sideStageDragSnapAnimation.to = sideStageDragHandle.progress > 0.5 || oneWayFlick ? 1 : 0;
449 sideStageDragSnapAnimation.start();
451 sideStageDragHandle.dragging = false;
455 UbuntuNumberAnimation {
456 id: sideStageDragSnapAnimation
457 target: sideStageDragHandle
462 sideStageDragHandle.dragging = false;
470 model: ApplicationManager
472 delegate: TransformedTabletSpreadDelegate {
474 height: spreadView.height
475 width: model.stage == ApplicationInfoInterface.MainStage ? spreadView.width : spreadView.sideStageWidth
476 active: model.appId == priv.mainStageAppId || model.appId == priv.sideStageAppId
477 zIndex: spreadView.indexToZIndex(index)
478 selected: spreadView.selectedIndex == index
479 otherSelected: spreadView.selectedIndex >= 0 && !selected
480 isInSideStage: priv.sideStageAppId == model.appId
481 interactive: !spreadView.interactive && spreadView.phase === 0 && root.interactive
482 swipeToCloseEnabled: spreadView.interactive
483 maximizedAppTopMargin: root.maximizedAppTopMargin
484 dragOffset: !isDash && model.appId == priv.mainStageAppId && root.inverseProgress > 0 ? root.inverseProgress : 0
485 application: ApplicationManager.get(index)
488 readonly property bool isDash: model.appId == "unity8-dash"
490 // FIXME: A regular binding doesn't update any more after closing an app.
491 // Using a Binding for now.
495 value: (!spreadView.active && isDash && !active) ? -1 : spreadTile.zIndex
499 property real behavioredZIndex: zIndex
500 Behavior on behavioredZIndex {
501 enabled: spreadView.closingIndex >= 0
502 UbuntuNumberAnimation {}
505 // This is required because none of the bindings are triggered in some cases:
506 // When an app is closed, it might happen that ApplicationManager.get(nextInStack)
507 // returns a different app even though the nextInStackIndex and all the related
508 // bindings (index, mainStageApp, sideStageApp, etc) don't change. Let's force a
509 // binding update in that case.
511 target: ApplicationManager
512 onApplicationRemoved: spreadTile.z = Qt.binding(function() {
513 return spreadView.indexToZIndex(index);
518 var tileProgress = (spreadView.shiftedContentX - behavioredZIndex * spreadView.tileDistance) / spreadView.width;
519 // Some tiles (nextInStack, active) need to move directly from the beginning, normalize progress to immediately start at 0
520 if ((index == spreadView.nextInStack && spreadView.phase < 2) || (active && spreadView.phase < 1)) {
521 tileProgress += behavioredZIndex * spreadView.tileDistance / spreadView.width;
527 if (spreadView.phase == 0 && (spreadTile.active || spreadView.nextInStack == index)) {
528 if (progress < spreadView.positionMarker1) {
530 } else if (progress < spreadView.positionMarker1 + snappingCurve.period) {
531 return spreadView.positionMarker1 + snappingCurve.value * 3;
533 return spreadView.positionMarker2;
541 property: "orientation"
542 when: spreadTile.interactive
543 value: root.orientation
547 if (spreadView.phase == 2) {
548 spreadView.snapTo(index);
554 spreadView.draggedDelegateCount++;
556 spreadView.draggedDelegateCount--;
561 spreadView.closingIndex = index;
562 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
567 type: EasingCurve.Linear
568 period: (spreadView.positionMarker2 - spreadView.positionMarker1) / 3
569 progress: spreadTile.progress - spreadView.positionMarker1
578 anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
579 width: root.dragAreaWidth
580 direction: Direction.Leftwards
582 property bool attachedToView: false
583 property var gesturePoints: new Array()
587 spreadView.phase = 0;
588 spreadView.contentX = -spreadView.shift;
591 if (dragging && attachedToView) {
592 spreadView.contentX = -touchX + spreadDragArea.width - spreadView.shift;
593 if (spreadView.shiftedContentX > spreadView.phase0Width + spreadView.phase1Width / 2) {
594 attachedToView = false;
598 gesturePoints.push(touchX);
602 if (status == DirectionalDragArea.Recognized) {
603 attachedToView = true;
609 // Gesture recognized. Start recording this gesture
614 // Ok. The user released. Find out if it was a one-way movement.
615 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
618 if (oneWayFlick && spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
619 // If it was a short one-way movement, do the Alt+Tab switch
620 // no matter if we didn't cross positionMarker1 yet.
621 spreadView.snapTo(spreadView.nextInStack);
622 } else if (!dragging && attachedToView) {
623 if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker1) {
625 } else if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
626 spreadView.snapTo(spreadView.nextInStack);
628 // otherwise snap to the closest snap position we can find
629 // (might be back to start, to app 1 or to spread)