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.
38 onInverseProgressChanged: {
39 // This can't be a simple binding because that would be triggered after this handler
40 // while we need it active before doing the anition left/right
41 spreadView.animateX = (inverseProgress == 0)
42 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
43 // left edge drag released. Minimum distance is given by design.
44 if (priv.oldInverseProgress > units.gu(22)) {
45 ApplicationManager.focusApplication("unity8-dash");
48 priv.oldInverseProgress = inverseProgress;
54 property string focusedAppId: ApplicationManager.focusedApplicationId
55 property string oldFocusedAppId: ""
57 property string mainStageAppId
58 property string sideStageAppId
60 // For convenience, keep properties of the first two apps in the model
61 property string appId0
62 property string appId1
64 property int oldInverseProgress: 0
66 onFocusedAppIdChanged: {
67 if (priv.focusedAppId.length > 0) {
68 var focusedApp = ApplicationManager.findApplication(focusedAppId);
69 if (focusedApp.stage == ApplicationInfoInterface.SideStage) {
70 priv.sideStageAppId = focusedAppId;
72 priv.mainStageAppId = focusedAppId;
76 appId0 = ApplicationManager.count >= 1 ? ApplicationManager.get(0).appId : "";
77 appId1 = ApplicationManager.count > 1 ? ApplicationManager.get(1).appId : "";
80 function indexOf(appId) {
81 for (var i = 0; i < ApplicationManager.count; i++) {
82 if (ApplicationManager.get(i).appId == appId) {
89 function evaluateOneWayFlick(gesturePoints) {
90 // Need to have at least 3 points to recognize it as a flick
91 if (gesturePoints.length < 3) {
94 // Need to have a movement of at least 2 grid units to recognize it as a flick
95 if (Math.abs(gesturePoints[gesturePoints.length - 1] - gesturePoints[0]) < units.gu(2)) {
99 var oneWayFlick = true;
100 var smallestX = gesturePoints[0];
101 var leftWards = gesturePoints[1] < gesturePoints[0];
102 for (var i = 1; i < gesturePoints.length; i++) {
103 if ((leftWards && gesturePoints[i] >= smallestX)
104 || (!leftWards && gesturePoints[i] <= smallestX)) {
108 smallestX = gesturePoints[i];
115 target: ApplicationManager
117 if (spreadView.interactive) {
118 spreadView.snapTo(priv.indexOf(appId));
120 ApplicationManager.focusApplication(appId);
124 onApplicationAdded: {
125 if (spreadView.phase == 2) {
126 spreadView.snapTo(ApplicationManager.count - 1);
128 spreadView.phase = 0;
129 spreadView.contentX = -spreadView.shift;
130 ApplicationManager.focusApplication(appId);
134 onApplicationRemoved: {
135 if (priv.mainStageAppId == appId) {
136 ApplicationManager.focusApplication("unity8-dash")
138 if (priv.sideStageAppId == appId) {
139 priv.sideStageAppId = "";
141 if (ApplicationManager.count == 0) {
142 spreadView.phase = 0;
143 spreadView.contentX = -spreadView.shift;
151 interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
152 && draggedDelegateCount === 0
153 contentWidth: spreadRow.width - shift
156 property int tileDistance: units.gu(20)
157 property int sideStageWidth: units.gu(40)
158 property bool sideStageVisible: priv.sideStageAppId
160 // This indicates when the spreadView is active. That means, all the animations
161 // are activated and tiles need to line up for the spread.
162 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
164 // The flickable needs to fill the screen in order to get touch events all over.
165 // However, we don't want to the user to be able to scroll back all the way. For
166 // that, the beginning of the gesture starts with a negative value for contentX
167 // so the flickable wants to pull it into the view already. "shift" tunes the
168 // distance where to "lock" the content.
169 readonly property real shift: width / 2
170 readonly property real shiftedContentX: contentX + shift
172 // Phase of the animation:
173 // 0: Starting from right edge, a new app (index 1) comes in from the right
174 // 1: The app has reached the first snap position.
175 // 2: The list is dragged further and snaps into the spread view when entering phase 2
178 readonly property int phase0Width: sideStageWidth
179 readonly property int phase1Width: sideStageWidth
181 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
182 // 0 - 1: following finger, snap back to the beginning on release
183 readonly property real positionMarker1: 0.2
184 // 1 - 2: curved snapping movement, snap to nextInStack on release
185 readonly property real positionMarker2: sideStageWidth / spreadView.width
186 // 2 - 3: movement follows finger, snaps to phase 2 (full spread) on release
187 readonly property real positionMarker3: 0.6
188 // passing 3, we detach movement from the finger and snap to phase 2 (full spread)
189 readonly property real positionMarker4: 0.8
191 readonly property int startSnapPosition: phase0Width * 0.5
192 readonly property int endSnapPosition: phase0Width * 0.75
193 readonly property real snapPosition: 0.75
195 property int selectedIndex: -1
196 property int draggedDelegateCount: 0
197 property int closingIndex: -1
199 property bool animateX: true
201 property bool sideStageDragging: sideStageDragHandle.dragging
202 property real sideStageDragProgress: sideStageDragHandle.progress
204 onSideStageDragProgressChanged: {
205 if (sideStageDragProgress == 1) {
206 ApplicationManager.focusApplication(priv.mainStageAppId);
207 priv.sideStageAppId = "";
211 // In case the ApplicationManager already holds an app when starting up we're missing animations
212 // Make sure we end up in the same state
213 Component.onCompleted: {
214 spreadView.contentX = -spreadView.shift
217 property int nextInStack: {
220 if (ApplicationManager.count > 1) {
224 case "mainAndOverlay":
225 if (ApplicationManager.count <= 2) {
228 if (priv.appId0 == priv.mainStageAppId || priv.appId0 == priv.sideStageAppId) {
229 if (priv.appId1 == priv.mainStageAppId || priv.appId1 == priv.sideStageAppId) {
238 print("Unhandled nextInStack case! This shouldn't happen any more when the Dash is an app!");
241 property int nextZInStack: indexToZIndex(nextInStack)
250 State { // Side Stage only in overlay mode
253 State { // Main Stage and Side Stage in overlay mode
254 name: "mainAndOverlay"
256 State { // Main Stage and Side Stage in split mode
261 if (priv.mainStageAppId && !priv.sideStageAppId) {
264 if (!priv.mainStageAppId && priv.sideStageAppId) {
267 if (priv.mainStageAppId && priv.sideStageAppId) {
268 return "mainAndOverlay";
273 onShiftedContentXChanged: {
274 if (spreadView.phase == 0 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker2) {
275 spreadView.phase = 1;
276 } else if (spreadView.phase == 1 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker4) {
277 spreadView.phase = 2;
278 } else if (spreadView.phase == 1 && spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
279 spreadView.phase = 0;
284 if (shiftedContentX < phase0Width) {
285 snapAnimation.targetContentX = -shift;
286 snapAnimation.start();
287 } else if (shiftedContentX < phase1Width) {
290 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
291 snapAnimation.targetContentX = spreadView.width * spreadView.positionMarker4 + 1 - shift;
292 snapAnimation.start();
295 function snapTo(index) {
296 spreadView.selectedIndex = index;
297 snapAnimation.targetContentX = -shift;
298 snapAnimation.start();
301 // We need to shuffle z ordering a bit in order to keep side stage apps above main stage apps.
302 // We don't want to really reorder them in the model because that allows us to keep track
303 // of the last focused order.
304 function indexToZIndex(index) {
305 var app = ApplicationManager.get(index);
310 var active = app.appId == priv.mainStageAppId || app.appId == priv.sideStageAppId;
311 if (active && app.stage == ApplicationInfoInterface.MainStage) {
312 // if this app is active, and its the MainStage, always put it to index 0
315 if (active && app.stage == ApplicationInfoInterface.SideStage) {
316 if (!priv.mainStageAppId) {
317 // Only have SS apps running. Put the active one at 0
321 // Precondition now: There's an active MS app and this is SS app:
322 if (spreadView.nextInStack >= 0 && ApplicationManager.get(spreadView.nextInStack).stage == ApplicationInfoInterface.MainStage) {
323 // If the next app coming from the right is a MS app, we need to elevate this SS ap above it.
324 // Put it to at least level 2, or higher if there's more apps coming in before this one.
325 return Math.max(index, 2);
327 // 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.
331 if (index <= 2 && app.stage == ApplicationInfoInterface.MainStage && priv.sideStageAppId) {
332 // Ok, this is an inactive MS app. If there's an active SS app around, we need to place this one
333 // in between the active MS app and the active SS app, so that it comes in from there when dragging from the right.
334 // If there's now active SS app, just leave it where it is.
335 return priv.indexOf(priv.sideStageAppId) < index ? index - 1 : index;
337 if (index == spreadView.nextInStack && app.stage == ApplicationInfoInterface.SideStage) {
338 // This is a SS app and the next one to come in from the right:
339 if (priv.sideStageAppId && priv.mainStageAppId) {
340 // If there's both, an active MS and an active SS app, put this one right on top of that
343 // Or if there's only one other active app, put it on top of that.
344 // The case that there isn't any other active app is already handled above.
347 if (index == 2 && spreadView.nextInStack == 1 && priv.sideStageAppId) {
348 // If its index 2 but not the next one to come in, it means
349 // we've pulled another one down to index 2. Move this one up to 2 instead.
352 // don't touch all others... (mostly index > 3 + simple cases where the above doesn't shuffle much)
356 SequentialAnimation {
358 property int targetContentX: -spreadView.shift
360 UbuntuNumberAnimation {
363 to: snapAnimation.targetContentX
364 duration: UbuntuAnimation.FastDuration
369 if (spreadView.selectedIndex >= 0) {
370 var newIndex = spreadView.selectedIndex;
371 spreadView.selectedIndex = -1;
372 ApplicationManager.focusApplication(ApplicationManager.get(newIndex).appId);
373 spreadView.phase = 0;
374 spreadView.contentX = -spreadView.shift;
382 x: spreadView.contentX
384 width: spreadView.width + Math.max(spreadView.width, ApplicationManager.count * spreadView.tileDistance)
387 id: sideStageBackground
390 anchors.leftMargin: spreadView.width - (1 - sideStageDragHandle.progress) * spreadView.sideStageWidth
391 z: spreadView.indexToZIndex(priv.indexOf(priv.sideStageAppId))
392 opacity: spreadView.phase == 0 ? 1 : 0
393 Behavior on opacity { UbuntuNumberAnimation {} }
397 id: sideStageDragHandle
398 anchors { top: parent.top; bottom: parent.bottom; left: parent.left; leftMargin: spreadView.width - spreadView.sideStageWidth - width }
400 z: sideStageBackground.z
401 opacity: spreadView.phase <= 0 && spreadView.sideStageVisible ? 1 : 0
402 property real progress: 0
403 property bool dragging: false
405 Behavior on opacity { UbuntuNumberAnimation {} }
409 onSideStageVisibleChanged: {
410 if (spreadView.sideStageVisible) {
411 sideStageDragHandle.progress = 0;
417 anchors.centerIn: parent
418 anchors.horizontalCenterOffset: parent.progress * spreadView.sideStageWidth - (width - parent.width) / 2
419 width: sideStageDragHandleMouseArea.pressed ? parent.width * 2 : parent.width
420 height: parent.height
421 source: "graphics/sidestage_handle@20.png"
422 Behavior on width { UbuntuNumberAnimation {} }
426 id: sideStageDragHandleMouseArea
428 enabled: spreadView.shiftedContentX == 0
430 property var gesturePoints: new Array()
435 sideStageDragHandle.progress = 0;
436 sideStageDragHandle.dragging = true;
439 if (priv.mainStageAppId) {
440 sideStageDragHandle.progress = Math.max(0, (-startX + mouseX) / spreadView.sideStageWidth);
442 gesturePoints.push(mouseX);
445 if (priv.mainStageAppId) {
446 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
447 sideStageDragSnapAnimation.to = sideStageDragHandle.progress > 0.5 || oneWayFlick ? 1 : 0;
448 sideStageDragSnapAnimation.start();
450 sideStageDragHandle.dragging = false;
454 UbuntuNumberAnimation {
455 id: sideStageDragSnapAnimation
456 target: sideStageDragHandle
461 sideStageDragHandle.dragging = false;
469 model: ApplicationManager
471 delegate: TransformedTabletSpreadDelegate {
473 height: spreadView.height
474 width: model.stage == ApplicationInfoInterface.MainStage ? spreadView.width : spreadView.sideStageWidth
475 active: model.appId == priv.mainStageAppId || model.appId == priv.sideStageAppId
476 zIndex: spreadView.indexToZIndex(index)
477 selected: spreadView.selectedIndex == index
478 otherSelected: spreadView.selectedIndex >= 0 && !selected
479 isInSideStage: priv.sideStageAppId == model.appId
480 interactive: !spreadView.interactive && spreadView.phase === 0 && root.interactive
481 swipeToCloseEnabled: spreadView.interactive
482 maximizedAppTopMargin: root.maximizedAppTopMargin
483 dragOffset: !isDash && model.appId == priv.mainStageAppId && root.inverseProgress > 0 ? root.inverseProgress : 0
484 application: ApplicationManager.get(index)
487 readonly property bool isDash: model.appId == "unity8-dash"
489 // FIXME: A regular binding doesn't update any more after closing an app.
490 // Using a Binding for now.
494 value: (!spreadView.active && isDash && !active) ? -1 : spreadTile.zIndex
498 property real behavioredZIndex: zIndex
499 Behavior on behavioredZIndex {
500 enabled: spreadView.closingIndex >= 0
501 UbuntuNumberAnimation {}
504 // This is required because none of the bindings are triggered in some cases:
505 // When an app is closed, it might happen that ApplicationManager.get(nextInStack)
506 // returns a different app even though the nextInStackIndex and all the related
507 // bindings (index, mainStageApp, sideStageApp, etc) don't change. Let's force a
508 // binding update in that case.
510 target: ApplicationManager
511 onApplicationRemoved: spreadTile.z = Qt.binding(function() {
512 return spreadView.indexToZIndex(index);
517 var tileProgress = (spreadView.shiftedContentX - behavioredZIndex * spreadView.tileDistance) / spreadView.width;
518 // Some tiles (nextInStack, active) need to move directly from the beginning, normalize progress to immediately start at 0
519 if ((index == spreadView.nextInStack && spreadView.phase < 2) || (active && spreadView.phase < 1)) {
520 tileProgress += behavioredZIndex * spreadView.tileDistance / spreadView.width;
526 if (spreadView.phase == 0 && (spreadTile.active || spreadView.nextInStack == index)) {
527 if (progress < spreadView.positionMarker1) {
529 } else if (progress < spreadView.positionMarker1 + snappingCurve.period) {
530 return spreadView.positionMarker1 + snappingCurve.value * 3;
532 return spreadView.positionMarker2;
539 if (spreadView.phase == 2) {
540 spreadView.snapTo(index);
546 spreadView.draggedDelegateCount++;
548 spreadView.draggedDelegateCount--;
553 spreadView.closingIndex = index;
554 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
559 type: EasingCurve.Linear
560 period: (spreadView.positionMarker2 - spreadView.positionMarker1) / 3
561 progress: spreadTile.progress - spreadView.positionMarker1
570 anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
571 width: root.dragAreaWidth
572 direction: Direction.Leftwards
574 property bool attachedToView: false
575 property var gesturePoints: new Array()
579 spreadView.phase = 0;
580 spreadView.contentX = -spreadView.shift;
583 if (dragging && attachedToView) {
584 spreadView.contentX = -touchX + spreadDragArea.width - spreadView.shift;
585 if (spreadView.shiftedContentX > spreadView.phase0Width + spreadView.phase1Width / 2) {
586 attachedToView = false;
590 gesturePoints.push(touchX);
594 if (status == DirectionalDragArea.Recognized) {
595 attachedToView = true;
601 // Gesture recognized. Start recording this gesture
606 // Ok. The user released. Find out if it was a one-way movement.
607 var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
610 if (oneWayFlick && spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
611 // If it was a short one-way movement, do the Alt+Tab switch
612 // no matter if we didn't cross positionMarker1 yet.
613 spreadView.snapTo(spreadView.nextInStack);
614 } else if (!dragging && attachedToView) {
615 if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker1) {
617 } else if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
618 spreadView.snapTo(spreadView.nextInStack);
620 // otherwise snap to the closest snap position we can find
621 // (might be back to start, to app 1 or to spread)