2 * Copyright (C) 2014-2015 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 1.3
19 import Ubuntu.Gestures 0.1
20 import Unity.Application 0.1
21 import Unity.Session 0.1
24 import "../Components"
29 property QtObject applicationManager: ApplicationManager
30 property bool focusFirstApp: true // If false, focused app will appear on right edge like other apps
31 property bool altTabEnabled: true
32 property real startScale: 1.1
33 property real endScale: 0.7
35 onBeingResizedChanged: {
37 // Brace yourselves for impact!
41 onSpreadEnabledChanged: {
47 // Functions to be called from outside
48 function updateFocusedAppOrientation() {
49 if (spreadRepeater.count > 0) {
50 spreadRepeater.itemAt(0).matchShellOrientation();
53 for (var i = 1; i < spreadRepeater.count; ++i) {
55 var spreadDelegate = spreadRepeater.itemAt(i);
57 var delta = spreadDelegate.appWindowOrientationAngle - root.shellOrientationAngle;
58 if (delta < 0) { delta += 360; }
61 var supportedOrientations = spreadDelegate.application.supportedOrientations;
62 if (supportedOrientations === Qt.PrimaryOrientation) {
63 supportedOrientations = root.orientations.primary;
66 if (delta === 180 && (supportedOrientations & spreadDelegate.shellOrientation)) {
67 spreadDelegate.matchShellOrientation();
71 function updateFocusedAppOrientationAnimated() {
72 if (spreadRepeater.count > 0) {
73 spreadRepeater.itemAt(0).animateToShellOrientation();
77 mainApp: applicationManager.focusedApplicationId
78 ? applicationManager.findApplication(applicationManager.focusedApplicationId)
81 orientationChangesEnabled: priv.focusedAppOrientationChangesEnabled
82 && !priv.focusedAppDelegateIsDislocated
83 && !(priv.focusedAppDelegate && priv.focusedAppDelegate.xBehavior.running)
84 && spreadView.phase === 0
86 // How far left the stage has been dragged
87 readonly property real dragProgress: spreadRepeater.count > 0 ? -spreadRepeater.itemAt(0).xTranslate : 0
89 readonly property alias dragging: spreadDragArea.dragging
91 // Only used by the tutorial right now, when it is teasing the right edge
92 property real dragAreaOverlap
96 function select(appId) {
97 spreadView.snapTo(priv.indexOf(appId));
100 onInverseProgressChanged: {
101 // This can't be a simple binding because that would be triggered after this handler
102 // while we need it active before doing the anition left/right
103 priv.animateX = (inverseProgress == 0)
104 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
105 // left edge drag released. Minimum distance is given by design.
106 if (priv.oldInverseProgress > units.gu(22)) {
107 applicationManager.requestFocusApplication("unity8-dash");
110 priv.oldInverseProgress = inverseProgress;
113 // <FIXME-contentX> See rationale in the next comment with this tag
115 if (!root.beingResized) {
116 // we're being resized without a warning (ie, the corresponding property wasn't set
117 root.beingResized = true;
118 beingResizedTimer.start();
122 id: beingResizedTimer
124 onTriggered: { root.beingResized = false; }
128 target: applicationManager
131 if (spreadView.phase > 0) {
132 spreadView.snapTo(priv.indexOf(appId));
134 applicationManager.focusApplication(appId);
138 onApplicationAdded: {
139 if (spreadView.phase == 2) {
140 spreadView.snapTo(applicationManager.count - 1);
142 spreadView.phase = 0;
143 spreadView.contentX = -spreadView.shift;
144 applicationManager.focusApplication(appId);
148 onApplicationRemoved: {
149 // Unless we're closing the app ourselves in the spread,
150 // lets make sure the spread doesn't mess up by the changing app list.
151 if (spreadView.closingIndex == -1) {
152 spreadView.phase = 0;
153 spreadView.contentX = -spreadView.shift;
158 function focusTopMostApp() {
159 if (applicationManager.count > 0) {
160 var topmostApp = applicationManager.get(0);
161 applicationManager.focusApplication(topmostApp.appId);
169 property string focusedAppId: root.applicationManager.focusedApplicationId
170 property bool focusedAppOrientationChangesEnabled: false
171 readonly property int firstSpreadIndex: root.focusFirstApp ? 1 : 0
172 readonly property var focusedAppDelegate: {
173 var index = indexOf(focusedAppId);
174 return index >= 0 && index < spreadRepeater.count ? spreadRepeater.itemAt(index) : null
177 property real oldInverseProgress: 0
178 property bool animateX: false
180 onFocusedAppDelegateChanged: {
181 if (focusedAppDelegate) {
182 focusedAppDelegate.focus = true;
186 property bool focusedAppDelegateIsDislocated: focusedAppDelegate &&
187 (focusedAppDelegate.x !== 0 || focusedAppDelegate.xBehavior.running)
189 function indexOf(appId) {
190 for (var i = 0; i < root.applicationManager.count; i++) {
191 if (root.applicationManager.get(i).appId == appId) {
198 // Is more stable than "spreadView.shiftedContentX === 0" as it filters out noise caused by
199 // Flickable.contentX changing due to resizes.
200 property bool fullyShowingFocusedApp: true
203 // The app that's about to go to foreground has to be focused, otherwise
204 // it would leave us in an inconsistent state.
205 if (!root.applicationManager.focusedApplicationId && root.applicationManager.count > 0) {
206 root.applicationManager.focusApplication(root.applicationManager.get(0).appId);
209 spreadView.selectedIndex = -1;
210 spreadView.phase = 0;
211 spreadView.contentX = -spreadView.shift;
215 id: fullyShowingFocusedAppUpdateTimer
218 priv.fullyShowingFocusedApp = spreadView.shiftedContentX === 0;
224 objectName: "spreadView"
226 interactive: (spreadDragArea.dragging || phase > 1) && draggedDelegateCount === 0
227 contentWidth: spreadRow.width - shift
230 // This indicates when the spreadView is active. That means, all the animations
231 // are activated and tiles need to line up for the spread.
232 readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging || !root.focusFirstApp
234 // The flickable needs to fill the screen in order to get touch events all over.
235 // However, we don't want to the user to be able to scroll back all the way. For
236 // that, the beginning of the gesture starts with a negative value for contentX
237 // so the flickable wants to pull it into the view already. "shift" tunes the
238 // distance where to "lock" the content.
239 readonly property real shift: width / 2
240 readonly property real shiftedContentX: contentX + shift
242 property int tileDistance: width / 4
244 // Those markers mark the various positions in the spread (ratio to screen width from right to left):
245 // 0 - 1: following finger, snap back to the beginning on release
246 property real positionMarker1: 0.2
247 // 1 - 2: curved snapping movement, snap to app 1 on release
248 property real positionMarker2: 0.3
249 // 2 - 3: movement follows finger, snaps back to app 1 on release
250 property real positionMarker3: 0.35
251 // passing 3, we detach movement from the finger and snap to 4
252 property real positionMarker4: 0.9
254 // This is where the first app snaps to when bringing it in from the right edge.
255 property real snapPosition: 0.7
257 // Phase of the animation:
258 // 0: Starting from right edge, a new app (index 1) comes in from the right
259 // 1: The app has reached the first snap position.
260 // 2: The list is dragged further and snaps into the spread view when entering phase 2
261 property int phase: 0
263 property int selectedIndex: -1
264 property int draggedDelegateCount: 0
265 property int closingIndex: -1
267 // <FIXME-contentX> Workaround Flickable's behavior of bringing contentX back between valid boundaries
268 // when resized. The proper way to fix this is refactoring PhoneStage so that it doesn't
269 // rely on having Flickable.contentX keeping an out-of-bounds value when it's set programatically
270 // (as opposed to having contentX reaching an out-of-bounds value through dragging, which will trigger
271 // the Flickable.boundsBehavior upon release).
272 onContentXChanged: { forceItToRemainStillIfBeingResized(); }
273 onShiftChanged: { forceItToRemainStillIfBeingResized(); }
274 function forceItToRemainStillIfBeingResized() {
275 if (root.beingResized && contentX != -spreadView.shift) {
276 contentX = -spreadView.shift;
280 onShiftedContentXChanged: {
281 if (root.beingResized) {
282 // Flickabe.contentX wiggles during resizes. Don't react to it.
288 // the "spreadEnabled" part is because when code does "phase = 0; contentX = -shift" to
289 // dismiss the spread because spreadEnabled went to false, for some reason, during tests,
290 // Flickable might jump in and change contentX value back, causing the code below to do
291 // "phase = 1" which will make the spread stay.
292 // It sucks that we have no control whatsoever over whether or when Flickable animates its
294 if (root.spreadEnabled && shiftedContentX > width * positionMarker2) {
299 if (shiftedContentX < width * positionMarker2) {
301 } else if (shiftedContentX >= width * positionMarker4 && !spreadDragArea.dragging) {
306 fullyShowingFocusedAppUpdateTimer.restart();
310 if (shiftedContentX < positionMarker1 * width) {
311 snapAnimation.targetContentX = -shift;
312 snapAnimation.start();
313 } else if (shiftedContentX < positionMarker2 * width) {
315 } else if (shiftedContentX < positionMarker3 * width) {
317 } else if (phase < 2){
318 // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
319 snapAnimation.targetContentX = width * positionMarker4 + 1 - shift;
320 snapAnimation.start();
324 function snapTo(index) {
325 if (!root.altTabEnabled) {
326 // Reset to start instead
327 snapAnimation.targetContentX = -shift;
328 snapAnimation.start();
331 if (root.applicationManager.count <= index) {
332 // In case we're trying to snap to some non existing app, lets snap back to the first one
335 spreadView.selectedIndex = index;
336 // If we're not in full spread mode yet, always unwind to start pos
337 // otherwise unwind up to progress 0 of the selected index
338 if (spreadView.phase < 2) {
339 snapAnimation.targetContentX = -shift;
341 snapAnimation.targetContentX = -shift + index * spreadView.tileDistance;
343 snapAnimation.start();
346 // In case the applicationManager already holds an app when starting up we're missing animations
347 // Make sure we end up in the same state
348 Component.onCompleted: {
349 spreadView.contentX = -spreadView.shift
350 priv.animateX = true;
351 snapAnimation.complete();
354 SequentialAnimation {
356 property int targetContentX: -spreadView.shift
358 UbuntuNumberAnimation {
361 to: snapAnimation.targetContentX
362 duration: UbuntuAnimation.FastDuration
367 if (spreadView.selectedIndex >= 0) {
368 root.applicationManager.focusApplication(root.applicationManager.get(spreadView.selectedIndex).appId);
370 spreadView.selectedIndex = -1;
371 spreadView.phase = 0;
372 spreadView.contentX = -spreadView.shift;
380 // This width controls how much the spread can be flicked left/right. It's composed of:
381 // tileDistance * app count (with a minimum of 3 apps, in order to also allow moving 1 and 2 apps a bit)
382 // + some constant value (still scales with the screen width) which looks good and somewhat fills the screen
383 width: Math.max(3, root.applicationManager.count) * spreadView.tileDistance + (spreadView.width - spreadView.tileDistance) * 1.5
384 height: parent.height
386 enabled: spreadView.closingIndex >= 0
387 UbuntuNumberAnimation {}
390 if (spreadView.closingIndex >= 0) {
391 spreadView.contentX = Math.min(spreadView.contentX, width - spreadView.width - spreadView.shift);
395 x: spreadView.contentX
398 if (root.altTabEnabled) {
399 spreadView.snapTo(0);
405 objectName: "spreadRepeater"
406 model: root.applicationManager
407 delegate: TransformedSpreadDelegate {
409 objectName: "appDelegate" + index
412 startScale: root.startScale
413 endScale: root.endScale
414 startDistance: spreadView.tileDistance
415 endDistance: units.gu(.5)
416 width: spreadView.width
417 height: spreadView.height
418 selected: spreadView.selectedIndex == index
419 otherSelected: spreadView.selectedIndex >= 0 && !selected
420 interactive: !spreadView.interactive && spreadView.phase === 0
421 && priv.fullyShowingFocusedApp && root.interactive && isFocused
422 swipeToCloseEnabled: spreadView.interactive && root.interactive && !snapAnimation.running
423 maximizedAppTopMargin: root.maximizedAppTopMargin
424 dropShadow: spreadView.active || priv.focusedAppDelegateIsDislocated
425 focusFirstApp: root.focusFirstApp
427 readonly property bool isDash: model.appId == "unity8-dash"
429 readonly property bool canSuspend: model.isTouchApp
430 && !isExemptFromLifecycle(model.appId)
433 target: appDelegate.application
434 property: "requestedState"
436 || (isDash && root.keepDashRunning)
437 || (!root.suspended && appDelegate.focus)
438 ? ApplicationInfoInterface.RequestedRunning
439 : ApplicationInfoInterface.RequestedSuspended
442 z: isDash && !spreadView.active ? -1 : behavioredIndex
445 // focused app is always positioned at 0 except when following left edge drag
447 if (!isDash && root.inverseProgress > 0 && spreadView.phase === 0) {
448 return root.inverseProgress;
452 if (isDash && !spreadView.active && !spreadDragArea.dragging) {
456 // Otherwise line up for the spread
457 return spreadView.width + spreadIndex * spreadView.tileDistance;
460 application: root.applicationManager.get(index)
463 property real behavioredIndex: index
464 Behavior on behavioredIndex {
465 enabled: spreadView.closingIndex >= 0
466 UbuntuNumberAnimation {
470 spreadView.closingIndex = -1;
476 property var xBehavior: xBehavior
478 enabled: root.spreadEnabled &&
479 !spreadView.active &&
480 !snapAnimation.running &&
481 !spreadDragArea.pressed &&
484 UbuntuNumberAnimation {
486 duration: UbuntuAnimation.BriskDuration
490 // Each tile has a different progress value running from 0 to 1.
491 // 0: means the tile is at the right edge.
492 // 1: means the tile has finished the main animation towards the left edge.
493 // >1: after the main animation has finished, tiles will continue to move very slowly to the left
495 var tileProgress = (spreadView.shiftedContentX - behavioredIndex * spreadView.tileDistance) / spreadView.width;
496 // Tile 1 needs to move directly from the beginning...
497 if (root.focusFirstApp && behavioredIndex == 1 && spreadView.phase < 2) {
498 tileProgress += spreadView.tileDistance / spreadView.width;
500 // Limiting progress to ~0 and 1.7 to avoid binding calculations when tiles are not
502 // < 0 : The tile is outside the screen on the right
503 // > 1.7: The tile is *very* close to the left edge and covered by other tiles now.
504 // Using 0.0001 to differentiate when a tile should still be visible (==0)
505 // or we can hide it (< 0)
506 tileProgress = Math.max(-0.0001, Math.min(1.7, tileProgress));
510 // This mostly is the same as progress, just adds the snapping to phase 1 for tiles 0 and 1
512 if (spreadView.phase == 0 && index <= priv.firstSpreadIndex) {
513 if (progress < spreadView.positionMarker1) {
515 } else if (progress < spreadView.positionMarker1 + 0.05){
516 // p : 0.05 = x : pm2
517 return spreadView.positionMarker1 + (progress - spreadView.positionMarker1) * (spreadView.positionMarker2 - spreadView.positionMarker1) / 0.05
519 return spreadView.positionMarker2;
525 // Hide tile when progress is such that it will be off screen.
526 property bool occluded: {
527 if (spreadView.active && (progress >= 0 && progress < 1.7)) return false;
528 else if (!spreadView.active && isFocused) return false;
529 else if (xBehavior.running) return false;
530 else if (z <= 1 && priv.focusedAppDelegateIsDislocated) return false;
534 visible: Powerd.status == Powerd.On &&
535 !greeter.fullyShown &&
538 shellOrientationAngle: root.shellOrientationAngle
539 shellOrientation: root.shellOrientation
540 orientations: root.orientations
543 if (root.altTabEnabled && spreadView.phase == 2) {
544 if (root.applicationManager.focusedApplicationId == root.applicationManager.get(index).appId) {
545 spreadView.snapTo(index);
547 root.applicationManager.requestFocusApplication(root.applicationManager.get(index).appId);
554 spreadView.draggedDelegateCount++;
556 spreadView.draggedDelegateCount--;
561 spreadView.closingIndex = index;
562 root.applicationManager.stopApplication(root.applicationManager.get(index).appId);
568 property: "mainAppWindowOrientationAngle"
569 value: appWindowOrientationAngle
574 property: "focusedAppOrientationChangesEnabled"
575 value: orientationChangesEnabled
582 //eat touch events during the right edge gesture
584 objectName: "eventEaterArea"
586 enabled: spreadDragArea.dragging
589 DirectionalDragArea {
591 objectName: "spreadDragArea"
592 direction: Direction.Leftwards
593 enabled: (spreadView.phase != 2 && root.spreadEnabled) || dragging
595 anchors { top: parent.top; right: parent.right; bottom: parent.bottom; rightMargin: -root.dragAreaOverlap }
596 width: root.dragAreaWidth
598 property var gesturePoints: new Array()
602 // Gesture recognized. Let's move the spreadView with the finger
603 var dragX = Math.min(touchX + width, width); // Prevent dragging rightwards
604 dragX = -dragX + spreadDragArea.width - spreadView.shift;
605 // Don't allow dragging further than the animation crossing with phase2's animation
606 var maxMovement = spreadView.width * spreadView.positionMarker4 - spreadView.shift;
608 spreadView.contentX = Math.min(dragX, maxMovement);
610 // Initial touch. Let's reset the spreadView to the starting position.
611 spreadView.phase = 0;
612 spreadView.contentX = -spreadView.shift;
615 gesturePoints.push(touchX);
620 // A potential edge-drag gesture has started. Start recording it
623 // Ok. The user released. Find out if it was a one-way movement.
624 var oneWayFlick = true;
625 var smallestX = spreadDragArea.width;
626 for (var i = 0; i < gesturePoints.length; i++) {
627 if (gesturePoints[i] >= smallestX) {
631 smallestX = gesturePoints[i];
635 if (oneWayFlick && spreadView.shiftedContentX > units.gu(2) &&
636 spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
637 // If it was a short one-way movement, do the Alt+Tab switch
638 // no matter if we didn't cross positionMarker1 yet.
639 spreadView.snapTo(1);
640 } else if (!dragging) {
641 // otherwise snap to the closest snap position we can find
642 // (might be back to start, to app 1 or to spread)