Unity 8
PhoneStage.qml
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 import QtQuick 2.2
18 import Ubuntu.Components 0.1
19 import Ubuntu.Gestures 0.1
20 import Unity.Application 0.1
21 import Unity.Session 0.1
22 import Utils 0.1
23 import "../Components"
24 
25 Rectangle {
26  id: root
27 
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
40 
41  // How far left the stage has been dragged
42  readonly property real dragProgress: spreadRepeater.count > 0 ? -spreadRepeater.itemAt(0).xTranslate : 0
43 
44  readonly property alias dragging: spreadDragArea.dragging
45 
46  // Only used by the tutorial right now, when it is teasing the right edge
47  property real dragAreaOverlap
48 
49  signal opened()
50 
51  color: "#111111"
52 
53  function select(appId) {
54  spreadView.snapTo(priv.indexOf(appId));
55  }
56 
57  onWidthChanged: {
58  spreadView.selectedIndex = -1;
59  spreadView.phase = 0;
60  spreadView.contentX = -spreadView.shift;
61  }
62 
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");
71  }
72  }
73  priv.oldInverseProgress = inverseProgress;
74  }
75 
76  Connections {
77  target: applicationManager
78 
79  onFocusRequested: {
80  if (spreadView.phase > 0) {
81  spreadView.snapTo(priv.indexOf(appId));
82  } else {
83  applicationManager.focusApplication(appId);
84  }
85  }
86 
87  onApplicationAdded: {
88  if (spreadView.phase == 2) {
89  spreadView.snapTo(applicationManager.count - 1);
90  } else {
91  spreadView.phase = 0;
92  spreadView.contentX = -spreadView.shift;
93  applicationManager.focusApplication(appId);
94  }
95  }
96 
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;
103  focusTopMostApp();
104  }
105  }
106 
107  function focusTopMostApp() {
108  if (applicationManager.count > 0) {
109  var topmostApp = applicationManager.get(0);
110  applicationManager.focusApplication(topmostApp.appId);
111  }
112  }
113  }
114 
115  QtObject {
116  id: priv
117 
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
122 
123  property real oldInverseProgress: 0
124  property bool animateX: false
125 
126  onFocusedAppIdChanged: focusedAppDelegate = spreadRepeater.itemAt(0);
127 
128  onFocusedAppDelegateChanged: focusedAppDelegate.focus = true;
129 
130  function indexOf(appId) {
131  for (var i = 0; i < applicationManager.count; i++) {
132  if (applicationManager.get(i).appId == appId) {
133  return i;
134  }
135  }
136  return -1;
137  }
138 
139  }
140 
141  Flickable {
142  id: spreadView
143  objectName: "spreadView"
144  anchors.fill: parent
145  interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
146  && draggedDelegateCount === 0
147  contentWidth: spreadRow.width - shift
148  contentX: -shift
149 
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
153 
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
161 
162  property int tileDistance: width / 4
163 
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
173 
174  // This is where the first app snaps to when bringing it in from the right edge.
175  property real snapPosition: 0.7
176 
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
182 
183  property int selectedIndex: -1
184  property int draggedDelegateCount: 0
185  property int closingIndex: -1
186 
187  property bool focusChanging: false
188 
189  onShiftedContentXChanged: {
190  switch (phase) {
191  case 0:
192  if (shiftedContentX > width * positionMarker2) {
193  phase = 1;
194  }
195  break;
196  case 1:
197  if (shiftedContentX < width * positionMarker2) {
198  phase = 0;
199  } else if (shiftedContentX >= width * positionMarker4 && !spreadDragArea.dragging) {
200  phase = 2;
201  }
202  break;
203  }
204  }
205 
206  function snap() {
207  if (shiftedContentX < positionMarker1 * width) {
208  snapAnimation.targetContentX = -shift;
209  snapAnimation.start();
210  } else if (shiftedContentX < positionMarker2 * width) {
211  snapTo(1);
212  } else if (shiftedContentX < positionMarker3 * width) {
213  snapTo(1);
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();
218  root.opened();
219  }
220  }
221  function snapTo(index) {
222  if (!root.altTabEnabled) {
223  // Reset to start instead
224  snapAnimation.targetContentX = -shift;
225  snapAnimation.start();
226  return;
227  }
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
230  index = 0;
231  }
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;
237  } else {
238  snapAnimation.targetContentX = -shift + index * spreadView.tileDistance;
239  }
240  snapAnimation.start();
241  }
242 
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();
249  }
250 
251  SequentialAnimation {
252  id: snapAnimation
253  property int targetContentX: -spreadView.shift
254 
255  UbuntuNumberAnimation {
256  target: spreadView
257  property: "contentX"
258  to: snapAnimation.targetContentX
259  duration: UbuntuAnimation.FastDuration
260  }
261 
262  ScriptAction {
263  script: {
264  if (spreadView.selectedIndex >= 0) {
265  applicationManager.focusApplication(applicationManager.get(spreadView.selectedIndex).appId);
266 
267  spreadView.selectedIndex = -1;
268  spreadView.phase = 0;
269  spreadView.contentX = -spreadView.shift;
270  }
271  }
272  }
273  }
274 
275  MouseArea {
276  id: spreadRow
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
282  Behavior on width {
283  enabled: spreadView.closingIndex >= 0
284  UbuntuNumberAnimation {}
285  }
286  onWidthChanged: {
287  if (spreadView.closingIndex >= 0) {
288  spreadView.contentX = Math.min(spreadView.contentX, width - spreadView.width - spreadView.shift);
289  }
290  }
291 
292  x: spreadView.contentX
293 
294  onClicked: {
295  if (root.altTabEnabled) {
296  spreadView.snapTo(0);
297  }
298  }
299 
300  Repeater {
301  id: spreadRepeater
302  objectName: "spreadRepeater"
303  model: applicationManager
304  delegate: TransformedSpreadDelegate {
305  id: appDelegate
306  objectName: "appDelegate" + index
307  startAngle: 45
308  endAngle: 5
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
324 
325  readonly property bool isDash: model.appId == "unity8-dash"
326 
327  z: isDash && !spreadView.active ? -1 : behavioredIndex
328 
329  x: {
330  // focused app is always positioned at 0 except when following left edge drag
331  if (isFocused) {
332  if (!isDash && root.inverseProgress > 0 && spreadView.phase === 0) {
333  return root.inverseProgress;
334  }
335  return 0;
336  }
337  if (isDash && !spreadView.active && !spreadDragArea.dragging) {
338  return 0;
339  }
340 
341  // Otherwise line up for the spread
342  return spreadView.width + spreadIndex * spreadView.tileDistance;
343  }
344 
345  application: applicationManager.get(index)
346  closeable: !isDash
347 
348  property real behavioredIndex: index
349  Behavior on behavioredIndex {
350  enabled: spreadView.closingIndex >= 0
351  UbuntuNumberAnimation {
352  id: appXAnimation
353  onRunningChanged: {
354  if (!running) {
355  spreadView.closingIndex = -1;
356  }
357  }
358  }
359  }
360 
361  Behavior on x {
362  enabled: root.spreadEnabled &&
363  !spreadView.active &&
364  !snapAnimation.running &&
365  priv.animateX
366  UbuntuNumberAnimation {
367  duration: UbuntuAnimation.BriskDuration
368  onRunningChanged: {
369  if (!running && root.inverseProgress == 0) {
370  spreadView.focusChanging = false;
371  }
372  }
373  }
374  }
375 
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
380  progress: {
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;
385  }
386  // Limiting progress to ~0 and 1.7 to avoid binding calculations when tiles are not
387  // visible.
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));
393  return tileProgress;
394  }
395 
396  // This mostly is the same as progress, just adds the snapping to phase 1 for tiles 0 and 1
397  animatedProgress: {
398  if (spreadView.phase == 0 && index <= priv.firstSpreadIndex) {
399  if (progress < spreadView.positionMarker1) {
400  return progress;
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
404  } else {
405  return spreadView.positionMarker2;
406  }
407  }
408  return progress;
409  }
410 
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)
414 
415  Binding {
416  target: appDelegate
417  property: "orientation"
418  when: appDelegate.interactive
419  value: root.orientation
420  }
421 
422  onClicked: {
423  if (root.altTabEnabled && spreadView.phase == 2) {
424  if (applicationManager.focusedApplicationId == applicationManager.get(index).appId) {
425  spreadView.snapTo(index);
426  } else {
427  applicationManager.requestFocusApplication(applicationManager.get(index).appId);
428  }
429  }
430  }
431 
432  onDraggedChanged: {
433  if (dragged) {
434  spreadView.draggedDelegateCount++;
435  } else {
436  spreadView.draggedDelegateCount--;
437  }
438  }
439 
440  onClosed: {
441  spreadView.closingIndex = index;
442  applicationManager.stopApplication(applicationManager.get(index).appId);
443  }
444  }
445  }
446  }
447  }
448 
449  EdgeDragArea {
450  id: spreadDragArea
451  objectName: "spreadDragArea"
452  direction: Direction.Leftwards
453  enabled: (spreadView.phase != 2 && root.spreadEnabled) || dragging
454 
455  anchors { top: parent.top; right: parent.right; bottom: parent.bottom; rightMargin: -root.dragAreaOverlap }
456  width: root.dragAreaWidth
457 
458  property var gesturePoints: new Array()
459 
460  onTouchXChanged: {
461  if (!dragging) {
462  // Initial touch. Let's reset the spreadView to the starting position.
463  spreadView.phase = 0;
464  spreadView.contentX = -spreadView.shift;
465  }
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);
473  }
474  gesturePoints.push(touchX);
475  }
476 
477  property int previousStatus: -1
478  property int currentStatus: DirectionalDragArea.WaitingForTouch
479 
480  onStatusChanged: {
481  previousStatus = currentStatus;
482  currentStatus = status;
483  }
484 
485  onDraggingChanged: {
486  if (dragging) {
487  // A potential edge-drag gesture has started. Start recording it
488  gesturePoints = [];
489  return;
490  }
491 
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) {
497  oneWayFlick = false;
498  break;
499  }
500  smallestX = gesturePoints[i];
501  }
502  gesturePoints = [];
503 
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)
513  spreadView.snap();
514  }
515  }
516  }
517 }