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
21 import Unity.Session 0.1
23 import "../Components"
28 // Controls to be set from outside
29 property int dragAreaWidth
30 property real maximizedAppTopMargin
31 property bool interactive
32 property bool spreadEnabled: true // If false, animations and right edge will be disabled
33 property real inverseProgress: 0 // This is the progress for left edge drags, in pixels.
34 property int orientation: Qt.PortraitOrientation
35 property QtObject applicationManager: ApplicationManager
36 property bool focusFirstApp: true // If false, focused app will appear on right edge like other apps
37 property bool altTabEnabled: true
38 property real startScale: 1.1
39 property real endScale: 0.7
41 // How far left the stage has been dragged
42 readonly property real dragProgress: spreadRepeater.count > 0 ? -spreadRepeater.itemAt(0).xTranslate : 0
44 readonly property alias dragging: spreadDragArea.dragging
46 // Only used by the tutorial right now, when it is teasing the right edge
47 property real dragAreaOverlap
53 function select(appId) {
54 spreadView.snapTo(priv.indexOf(appId));
58 spreadView.selectedIndex = -1;
60 spreadView.contentX = -spreadView.shift;
63 onInverseProgressChanged: {
64 // This can't be a simple binding because that would be triggered after this handler
65 // while we need it active before doing the anition left/right
66 priv.animateX = (inverseProgress == 0)
67 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
68 // left edge drag released. Minimum distance is given by design.
69 if (priv.oldInverseProgress > units.gu(22)) {
70 applicationManager.requestFocusApplication("unity8-dash");
73 priv.oldInverseProgress = inverseProgress;
77 target: applicationManager
80 if (spreadView.phase > 0) {
81 spreadView.snapTo(priv.indexOf(appId));
83 applicationManager.focusApplication(appId);
88 if (spreadView.phase == 2) {
89 spreadView.snapTo(applicationManager.count - 1);
92 spreadView.contentX = -spreadView.shift;
93 applicationManager.focusApplication(appId);
97 onApplicationRemoved: {
98 // Unless we're closing the app ourselves in the spread,
99 // lets make sure the spread doesn't mess up by the changing app list.
100 if (spreadView.closingIndex == -1) {
101 spreadView.phase = 0;
102 spreadView.contentX = -spreadView.shift;
107 function focusTopMostApp() {
108 if (applicationManager.count > 0) {
109 var topmostApp = applicationManager.get(0);
110 applicationManager.focusApplication(topmostApp.appId);
118 readonly property int firstSpreadIndex: root.focusFirstApp ? 1 : 0
119 property string focusedAppId: applicationManager.focusedApplicationId
120 property var focusedApplication: applicationManager.findApplication(focusedAppId)
121 property var focusedAppDelegate: null
123 property real oldInverseProgress: 0
124 property bool animateX: false
126 onFocusedAppIdChanged: focusedAppDelegate = spreadRepeater.itemAt(0);
128 onFocusedAppDelegateChanged: focusedAppDelegate.focus = true;
130 function indexOf(appId) {
131 for (var i = 0; i < applicationManager.count; i++) {
132 if (applicationManager.get(i).appId == appId) {
143 objectName: "spreadView"
145 interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
146 && draggedDelegateCount === 0
147 contentWidth: spreadRow.width - shift
150 // This indicates when the spreadView is active. That means, all the animations
151 // are activated and tiles need to line up for the spread.
152 readonly property bool active: shiftedContentX > 0 || spreadDragArea.status === DirectionalDragArea.Recognized || !root.focusFirstApp
154 // The flickable needs to fill the screen in order to get touch events all over.
155 // However, we don't want to the user to be able to scroll back all the way. For
156 // that, the beginning of the gesture starts with a negative value for contentX
157 // so the flickable wants to pull it into the view already. "shift" tunes the
158 // distance where to "lock" the content.
159 readonly property real shift: width / 2
160 readonly property real shiftedContentX: contentX + shift
162 property int tileDistance: width / 4
164 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
165 // 0 - 1: following finger, snap back to the beginning on release
166 property real positionMarker1: 0.2
167 // 1 - 2: curved snapping movement, snap to app 1 on release
168 property real positionMarker2: 0.3
169 // 2 - 3: movement follows finger, snaps back to app 1 on release
170 property real positionMarker3: 0.35
171 // passing 3, we detach movement from the finger and snap to 4
172 property real positionMarker4: 0.9
174 // This is where the first app snaps to when bringing it in from the right edge.
175 property real snapPosition: 0.7
177 // Phase of the animation:
178 // 0: Starting from right edge, a new app (index 1) comes in from the right
179 // 1: The app has reached the first snap position.
180 // 2: The list is dragged further and snaps into the spread view when entering phase 2
181 property int phase: 0
183 property int selectedIndex: -1
184 property int draggedDelegateCount: 0
185 property int closingIndex: -1
187 property bool focusChanging: false
189 onShiftedContentXChanged: {
192 if (shiftedContentX > width * positionMarker2) {
197 if (shiftedContentX < width * positionMarker2) {
199 } else if (shiftedContentX >= width * positionMarker4 && !spreadDragArea.dragging) {
207 if (shiftedContentX < positionMarker1 * width) {
208 snapAnimation.targetContentX = -shift;
209 snapAnimation.start();
210 } else if (shiftedContentX < positionMarker2 * width) {
212 } else if (shiftedContentX < positionMarker3 * width) {
214 } else if (phase < 2){
215 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
216 snapAnimation.targetContentX = width * positionMarker4 + 1 - shift;
217 snapAnimation.start();
221 function snapTo(index) {
222 if (!root.altTabEnabled) {
223 // Reset to start instead
224 snapAnimation.targetContentX = -shift;
225 snapAnimation.start();
228 if (applicationManager.count <= index) {
229 // In case we're trying to snap to some non existing app, lets snap back to the first one
232 spreadView.selectedIndex = index;
233 // If we're not in full spread mode yet, always unwind to start pos
234 // otherwise unwind up to progress 0 of the selected index
235 if (spreadView.phase < 2) {
236 snapAnimation.targetContentX = -shift;
238 snapAnimation.targetContentX = -shift + index * spreadView.tileDistance;
240 snapAnimation.start();
243 // In case the applicationManager already holds an app when starting up we're missing animations
244 // Make sure we end up in the same state
245 Component.onCompleted: {
246 spreadView.contentX = -spreadView.shift
247 priv.animateX = true;
248 snapAnimation.complete();
251 SequentialAnimation {
253 property int targetContentX: -spreadView.shift
255 UbuntuNumberAnimation {
258 to: snapAnimation.targetContentX
259 duration: UbuntuAnimation.FastDuration
264 if (spreadView.selectedIndex >= 0) {
265 applicationManager.focusApplication(applicationManager.get(spreadView.selectedIndex).appId);
267 spreadView.selectedIndex = -1;
268 spreadView.phase = 0;
269 spreadView.contentX = -spreadView.shift;
277 // This width controls how much the spread can be flicked left/right. It's composed of:
278 // tileDistance * app count (with a minimum of 3 apps, in order to also allow moving 1 and 2 apps a bit)
279 // + some constant value (still scales with the screen width) which looks good and somewhat fills the screen
280 width: Math.max(3, applicationManager.count) * spreadView.tileDistance + (spreadView.width - spreadView.tileDistance) * 1.5
281 height: parent.height
283 enabled: spreadView.closingIndex >= 0
284 UbuntuNumberAnimation {}
287 if (spreadView.closingIndex >= 0) {
288 spreadView.contentX = Math.min(spreadView.contentX, width - spreadView.width - spreadView.shift);
292 x: spreadView.contentX
295 if (root.altTabEnabled) {
296 spreadView.snapTo(0);
302 objectName: "spreadRepeater"
303 model: applicationManager
304 delegate: TransformedSpreadDelegate {
306 objectName: "appDelegate" + index
309 startScale: root.startScale
310 endScale: root.endScale
311 startDistance: spreadView.tileDistance
312 endDistance: units.gu(.5)
313 width: spreadView.width
314 height: spreadView.height
315 selected: spreadView.selectedIndex == index
316 otherSelected: spreadView.selectedIndex >= 0 && !selected
317 interactive: !spreadView.interactive && spreadView.phase === 0
318 && spreadView.shiftedContentX === 0 && root.interactive && isFocused
319 swipeToCloseEnabled: spreadView.interactive && root.interactive && !snapAnimation.running
320 maximizedAppTopMargin: root.maximizedAppTopMargin
321 dropShadow: spreadView.active ||
322 (priv.focusedAppDelegate && priv.focusedAppDelegate.x !== 0)
323 focusFirstApp: root.focusFirstApp
325 readonly property bool isDash: model.appId == "unity8-dash"
327 z: isDash && !spreadView.active ? -1 : behavioredIndex
330 // focused app is always positioned at 0 except when following left edge drag
332 if (!isDash && root.inverseProgress > 0 && spreadView.phase === 0) {
333 return root.inverseProgress;
337 if (isDash && !spreadView.active && !spreadDragArea.dragging) {
341 // Otherwise line up for the spread
342 return spreadView.width + spreadIndex * spreadView.tileDistance;
345 application: applicationManager.get(index)
348 property real behavioredIndex: index
349 Behavior on behavioredIndex {
350 enabled: spreadView.closingIndex >= 0
351 UbuntuNumberAnimation {
355 spreadView.closingIndex = -1;
362 enabled: root.spreadEnabled &&
363 !spreadView.active &&
364 !snapAnimation.running &&
366 UbuntuNumberAnimation {
367 duration: UbuntuAnimation.BriskDuration
369 if (!running && root.inverseProgress == 0) {
370 spreadView.focusChanging = false;
376 // Each tile has a different progress value running from 0 to 1.
377 // 0: means the tile is at the right edge.
378 // 1: means the tile has finished the main animation towards the left edge.
379 // >1: after the main animation has finished, tiles will continue to move very slowly to the left
381 var tileProgress = (spreadView.shiftedContentX - behavioredIndex * spreadView.tileDistance) / spreadView.width;
382 // Tile 1 needs to move directly from the beginning...
383 if (root.focusFirstApp && behavioredIndex == 1 && spreadView.phase < 2) {
384 tileProgress += spreadView.tileDistance / spreadView.width;
386 // Limiting progress to ~0 and 1.7 to avoid binding calculations when tiles are not
388 // < 0 : The tile is outside the screen on the right
389 // > 1.7: The tile is *very* close to the left edge and covered by other tiles now.
390 // Using 0.0001 to differentiate when a tile should still be visible (==0)
391 // or we can hide it (< 0)
392 tileProgress = Math.max(-0.0001, Math.min(1.7, tileProgress));
396 // This mostly is the same as progress, just adds the snapping to phase 1 for tiles 0 and 1
398 if (spreadView.phase == 0 && index <= priv.firstSpreadIndex) {
399 if (progress < spreadView.positionMarker1) {
401 } else if (progress < spreadView.positionMarker1 + 0.05){
402 // p : 0.05 = x : pm2
403 return spreadView.positionMarker1 + (progress - spreadView.positionMarker1) * (spreadView.positionMarker2 - spreadView.positionMarker1) / 0.05
405 return spreadView.positionMarker2;
411 // Hiding tiles when their progress is negative or reached the maximum
412 visible: (progress >= 0 && progress < 1.7) ||
413 (isDash && priv.focusedAppDelegate.x !== 0)
417 property: "orientation"
418 when: appDelegate.interactive
419 value: root.orientation
423 if (root.altTabEnabled && spreadView.phase == 2) {
424 if (applicationManager.focusedApplicationId == applicationManager.get(index).appId) {
425 spreadView.snapTo(index);
427 applicationManager.requestFocusApplication(applicationManager.get(index).appId);
434 spreadView.draggedDelegateCount++;
436 spreadView.draggedDelegateCount--;
441 spreadView.closingIndex = index;
442 applicationManager.stopApplication(applicationManager.get(index).appId);
451 objectName: "spreadDragArea"
452 direction: Direction.Leftwards
453 enabled: (spreadView.phase != 2 && root.spreadEnabled) || dragging
455 anchors { top: parent.top; right: parent.right; bottom: parent.bottom; rightMargin: -root.dragAreaOverlap }
456 width: root.dragAreaWidth
458 property var gesturePoints: new Array()
462 // Initial touch. Let's reset the spreadView to the starting position.
463 spreadView.phase = 0;
464 spreadView.contentX = -spreadView.shift;
466 if (dragging && status == DirectionalDragArea.Recognized) {
467 // Gesture recognized. Let's move the spreadView with the finger
468 var dragX = Math.min(touchX + width, width); // Prevent dragging rightwards
469 dragX = -dragX + spreadDragArea.width - spreadView.shift;
470 // Don't allow dragging further than the animation crossing with phase2's animation
471 var maxMovement = spreadView.width * spreadView.positionMarker4 - spreadView.shift;
472 spreadView.contentX = Math.min(dragX, maxMovement);
474 gesturePoints.push(touchX);
477 property int previousStatus: -1
478 property int currentStatus: DirectionalDragArea.WaitingForTouch
481 previousStatus = currentStatus;
482 currentStatus = status;
487 // A potential edge-drag gesture has started. Start recording it
492 // Ok. The user released. Find out if it was a one-way movement.
493 var oneWayFlick = true;
494 var smallestX = spreadDragArea.width;
495 for (var i = 0; i < gesturePoints.length; i++) {
496 if (gesturePoints[i] >= smallestX) {
500 smallestX = gesturePoints[i];
504 if (previousStatus == DirectionalDragArea.Recognized &&
505 oneWayFlick && spreadView.shiftedContentX > units.gu(2) &&
506 spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
507 // If it was a short one-way movement, do the Alt+Tab switch
508 // no matter if we didn't cross positionMarker1 yet.
509 spreadView.snapTo(1);
510 } else if (!dragging) {
511 // otherwise snap to the closest snap position we can find
512 // (might be back to start, to app 1 or to spread)