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
38 function select(appId) {
39 spreadView.snapTo(priv.indexOf(appId));
43 spreadView.selectedIndex = -1;
45 spreadView.contentX = -spreadView.shift;
48 onInverseProgressChanged: {
49 // This can't be a simple binding because that would be triggered after this handler
50 // while we need it active before doing the anition left/right
51 priv.animateX = (inverseProgress == 0)
52 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
53 // left edge drag released. Minimum distance is given by design.
54 if (priv.oldInverseProgress > units.gu(22)) {
55 ApplicationManager.focusApplication("unity8-dash");
58 priv.oldInverseProgress = inverseProgress;
62 target: ApplicationManager
65 if (spreadView.phase > 0) {
66 spreadView.snapTo(priv.indexOf(appId));
68 ApplicationManager.focusApplication(appId);
73 if (spreadView.phase == 2) {
74 spreadView.snapTo(ApplicationManager.count - 1);
77 spreadView.contentX = -spreadView.shift;
78 ApplicationManager.focusApplication(appId);
82 onApplicationRemoved: {
83 // Unless we're closing the app ourselves in the spread,
84 // lets make sure the spread doesn't mess up by the changing app list.
85 if (spreadView.closingIndex == -1) {
87 spreadView.contentX = -spreadView.shift;
95 property string focusedAppId: ApplicationManager.focusedApplicationId
96 property var focusedApplication: ApplicationManager.findApplication(focusedAppId)
97 property var focusedAppDelegate: null
99 property real oldInverseProgress: 0
100 property bool animateX: true
102 onFocusedAppIdChanged: focusedAppDelegate = spreadRepeater.itemAt(0);
104 function indexOf(appId) {
105 for (var i = 0; i < ApplicationManager.count; i++) {
106 if (ApplicationManager.get(i).appId == appId) {
117 objectName: "spreadView"
119 interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
120 && draggedDelegateCount === 0
121 contentWidth: spreadRow.width - shift
124 // This indicates when the spreadView is active. That means, all the animations
125 // are activated and tiles need to line up for the spread.
126 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
128 // The flickable needs to fill the screen in order to get touch events all over.
129 // However, we don't want to the user to be able to scroll back all the way. For
130 // that, the beginning of the gesture starts with a negative value for contentX
131 // so the flickable wants to pull it into the view already. "shift" tunes the
132 // distance where to "lock" the content.
133 readonly property real shift: width / 2
134 readonly property real shiftedContentX: contentX + shift
136 property int tileDistance: width / 4
138 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
139 // 0 - 1: following finger, snap back to the beginning on release
140 property real positionMarker1: 0.3
141 // 1 - 2: curved snapping movement, snap to app 1 on release
142 property real positionMarker2: 0.45
143 // 2 - 3: movement follows finger, snaps back to app 1 on release
144 property real positionMarker3: 0.6
145 // passing 3, we detach movement from the finger and snap to 4
146 property real positionMarker4: 0.9
148 // This is where the first app snaps to when bringing it in from the right edge.
149 property real snapPosition: 0.75
151 // Phase of the animation:
152 // 0: Starting from right edge, a new app (index 1) comes in from the right
153 // 1: The app has reached the first snap position.
154 // 2: The list is dragged further and snaps into the spread view when entering phase 2
155 property int phase: 0
157 property int selectedIndex: -1
158 property int draggedDelegateCount: 0
159 property int closingIndex: -1
161 property bool focusChanging: false
163 onShiftedContentXChanged: {
166 if (shiftedContentX > width * positionMarker2) {
171 if (shiftedContentX < width * positionMarker2) {
173 } else if (shiftedContentX >= width * positionMarker4) {
181 if (shiftedContentX < positionMarker1 * width) {
182 snapAnimation.targetContentX = -shift;
183 snapAnimation.start();
184 } else if (shiftedContentX < positionMarker2 * width) {
186 } else if (shiftedContentX < positionMarker3 * width) {
188 } else if (phase < 2){
189 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
190 snapAnimation.targetContentX = width * positionMarker4 + 1 - shift;
191 snapAnimation.start();
194 function snapTo(index) {
195 if (ApplicationManager.count <= index) {
196 // In case we're trying to snap to some non existing app, lets snap back to the first one
199 spreadView.selectedIndex = index;
200 // If we're not in full spread mode yet, always unwind to start pos
201 // otherwise unwind up to progress 0 of the selected index
202 if (spreadView.phase < 2) {
203 snapAnimation.targetContentX = -shift;
205 snapAnimation.targetContentX = -shift + index * spreadView.tileDistance;
207 snapAnimation.start();
210 // In case the ApplicationManager already holds an app when starting up we're missing animations
211 // Make sure we end up in the same state
212 Component.onCompleted: {
213 spreadView.contentX = -spreadView.shift
216 SequentialAnimation {
218 property int targetContentX: -spreadView.shift
220 UbuntuNumberAnimation {
223 to: snapAnimation.targetContentX
224 duration: UbuntuAnimation.FastDuration
229 if (spreadView.selectedIndex >= 0) {
230 ApplicationManager.focusApplication(ApplicationManager.get(spreadView.selectedIndex).appId);
232 spreadView.selectedIndex = -1;
233 spreadView.phase = 0;
234 spreadView.contentX = -spreadView.shift;
242 // This width controls how much the spread can be flicked left/right. It's composed of:
243 // tileDistance * app count (with a minimum of 3 apps, in order to also allow moving 1 and 2 apps a bit)
244 // + some constant value (still scales with the screen width) which looks good and somewhat fills the screen
245 width: Math.max(3, ApplicationManager.count) * spreadView.tileDistance + (spreadView.width - spreadView.tileDistance) * 1.5
247 enabled: spreadView.closingIndex >= 0
248 UbuntuNumberAnimation {}
251 if (spreadView.closingIndex >= 0) {
252 spreadView.contentX = Math.min(spreadView.contentX, width - spreadView.width - spreadView.shift);
256 x: spreadView.contentX
260 model: ApplicationManager
261 delegate: TransformedSpreadDelegate {
263 objectName: "appDelegate" + index
268 startDistance: spreadView.tileDistance
269 endDistance: units.gu(.5)
270 width: spreadView.width
271 height: spreadView.height
272 selected: spreadView.selectedIndex == index
273 otherSelected: spreadView.selectedIndex >= 0 && !selected
274 interactive: !spreadView.interactive && spreadView.phase === 0
275 && spreadView.shiftedContentX === 0 && root.interactive && index === 0
276 swipeToCloseEnabled: spreadView.interactive
277 maximizedAppTopMargin: root.maximizedAppTopMargin
278 dropShadow: spreadView.active ||
279 (priv.focusedAppDelegate && priv.focusedAppDelegate.x !== 0)
281 readonly property bool isDash: model.appId == "unity8-dash"
283 z: isDash && !spreadView.active ? -1 : behavioredIndex
286 // focused app is always positioned at 0 except when following left edge drag
288 if (!isDash && root.inverseProgress > 0) {
289 return root.inverseProgress;
293 if (isDash && !spreadView.active && !spreadDragArea.dragging) {
297 // Otherwise line up for the spread
298 return spreadView.width + (index - 1) * spreadView.tileDistance;
301 application: ApplicationManager.get(index)
304 property real behavioredIndex: index
305 Behavior on behavioredIndex {
306 enabled: spreadView.closingIndex >= 0
307 UbuntuNumberAnimation {
311 spreadView.closingIndex = -1;
318 enabled: root.spreadEnabled &&
319 !spreadView.active &&
320 !snapAnimation.running &&
322 UbuntuNumberAnimation {
323 duration: UbuntuAnimation.FastDuration
325 if (!running && root.inverseProgress == 0) {
326 spreadView.focusChanging = false;
332 // Each tile has a different progress value running from 0 to 1.
333 // 0: means the tile is at the right edge.
334 // 1: means the tile has finished the main animation towards the left edge.
335 // >1: after the main animation has finished, tiles will continue to move very slowly to the left
337 var tileProgress = (spreadView.shiftedContentX - behavioredIndex * spreadView.tileDistance) / spreadView.width;
338 // Tile 1 needs to move directly from the beginning...
339 if (behavioredIndex == 1 && spreadView.phase < 2) {
340 tileProgress += spreadView.tileDistance / spreadView.width;
342 // Limiting progress to ~0 and 1.7 to avoid binding calculations when tiles are not
344 // < 0 : The tile is outside the screen on the right
345 // > 1.7: The tile is *very* close to the left edge and covered by other tiles now.
346 // Using 0.0001 to differentiate when a tile should still be visible (==0)
347 // or we can hide it (< 0)
348 tileProgress = Math.max(-0.0001, Math.min(1.7, tileProgress));
352 // This mostly is the same as progress, just adds the snapping to phase 1 for tiles 0 and 1
354 if (spreadView.phase == 0 && index < 2) {
355 if (progress < spreadView.positionMarker1) {
357 } else if (progress < spreadView.positionMarker1 + snappingCurve.period){
358 return spreadView.positionMarker1 + snappingCurve.value * 3;
360 return spreadView.positionMarker2;
366 // Hiding tiles when their progress is negative or reached the maximum
367 visible: (progress >= 0 && progress < 1.7) ||
368 (isDash && priv.focusedAppDelegate.x !== 0)
372 type: EasingCurve.Linear
374 progress: appDelegate.progress - spreadView.positionMarker1
379 property: "orientation"
380 when: appDelegate.interactive
381 value: root.orientation
385 if (spreadView.phase == 2) {
386 if (ApplicationManager.focusedApplicationId == ApplicationManager.get(index).appId) {
387 spreadView.snapTo(index);
389 ApplicationManager.requestFocusApplication(ApplicationManager.get(index).appId);
396 spreadView.draggedDelegateCount++;
398 spreadView.draggedDelegateCount--;
403 spreadView.closingIndex = index;
404 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
413 direction: Direction.Leftwards
414 enabled: spreadView.phase != 2 && root.spreadEnabled
416 anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
417 width: root.dragAreaWidth
419 // Sitting at the right edge of the screen, this EdgeDragArea directly controls the spreadView when
420 // attachedToView is true. When the finger movement passes positionMarker3 we detach it from the
421 // spreadView and make the spreadView snap to positionMarker4.
422 property bool attachedToView: true
424 property var gesturePoints: new Array()
428 // Initial touch. Let's reset the spreadView to the starting position.
429 spreadView.phase = 0;
430 spreadView.contentX = -spreadView.shift;
432 if (dragging && attachedToView) {
433 // Gesture recognized. Let's move the spreadView with the finger
434 spreadView.contentX = -touchX + spreadDragArea.width - spreadView.shift;
436 if (attachedToView && spreadView.shiftedContentX >= spreadView.width * spreadView.positionMarker3) {
437 // We passed positionMarker3. Detach from spreadView and snap it.
438 attachedToView = false;
441 gesturePoints.push(touchX);
445 if (status == DirectionalDragArea.Recognized) {
446 attachedToView = true;
452 // Gesture recognized. Start recording this gesture
457 // Ok. The user released. Find out if it was a one-way movement.
458 var oneWayFlick = true;
459 var smallestX = spreadDragArea.width;
460 for (var i = 0; i < gesturePoints.length; i++) {
461 if (gesturePoints[i] >= smallestX) {
465 smallestX = gesturePoints[i];
469 if (oneWayFlick && spreadView.shiftedContentX > units.gu(2) &&
470 spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
471 // If it was a short one-way movement, do the Alt+Tab switch
472 // no matter if we didn't cross positionMarker1 yet.
473 spreadView.snapTo(1);
474 } else if (!dragging && attachedToView) {
475 // otherwise snap to the closest snap position we can find
476 // (might be back to start, to app 1 or to spread)