Unity 8
 All Classes Functions
TabletStage.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.0
18 import Ubuntu.Components 0.1
19 import Ubuntu.Gestures 0.1
20 import Unity.Application 0.1
21 import Utils 0.1
22 import "../Components"
23 
24 Rectangle {
25  id: root
26  objectName: "stages"
27  anchors.fill: parent
28  color: "black"
29 
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.
37  property int orientation: Qt.PortraitOrientation
38 
39  onInverseProgressChanged: {
40  // This can't be a simple binding because that would be triggered after this handler
41  // while we need it active before doing the anition left/right
42  spreadView.animateX = (inverseProgress == 0)
43  if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
44  // left edge drag released. Minimum distance is given by design.
45  if (priv.oldInverseProgress > units.gu(22)) {
46  ApplicationManager.focusApplication("unity8-dash");
47  }
48  }
49  priv.oldInverseProgress = inverseProgress;
50  }
51 
52  QtObject {
53  id: priv
54 
55  property string focusedAppId: ApplicationManager.focusedApplicationId
56  property string oldFocusedAppId: ""
57 
58  property string mainStageAppId
59  property string sideStageAppId
60 
61  // For convenience, keep properties of the first two apps in the model
62  property string appId0
63  property string appId1
64 
65  property int oldInverseProgress: 0
66 
67  onFocusedAppIdChanged: {
68  if (priv.focusedAppId.length > 0) {
69  var focusedApp = ApplicationManager.findApplication(focusedAppId);
70  if (focusedApp.stage == ApplicationInfoInterface.SideStage) {
71  priv.sideStageAppId = focusedAppId;
72  } else {
73  priv.mainStageAppId = focusedAppId;
74  }
75  }
76 
77  appId0 = ApplicationManager.count >= 1 ? ApplicationManager.get(0).appId : "";
78  appId1 = ApplicationManager.count > 1 ? ApplicationManager.get(1).appId : "";
79  }
80 
81  function indexOf(appId) {
82  for (var i = 0; i < ApplicationManager.count; i++) {
83  if (ApplicationManager.get(i).appId == appId) {
84  return i;
85  }
86  }
87  return -1;
88  }
89 
90  function evaluateOneWayFlick(gesturePoints) {
91  // Need to have at least 3 points to recognize it as a flick
92  if (gesturePoints.length < 3) {
93  return false;
94  }
95  // Need to have a movement of at least 2 grid units to recognize it as a flick
96  if (Math.abs(gesturePoints[gesturePoints.length - 1] - gesturePoints[0]) < units.gu(2)) {
97  return false;
98  }
99 
100  var oneWayFlick = true;
101  var smallestX = gesturePoints[0];
102  var leftWards = gesturePoints[1] < gesturePoints[0];
103  for (var i = 1; i < gesturePoints.length; i++) {
104  if ((leftWards && gesturePoints[i] >= smallestX)
105  || (!leftWards && gesturePoints[i] <= smallestX)) {
106  oneWayFlick = false;
107  break;
108  }
109  smallestX = gesturePoints[i];
110  }
111  return oneWayFlick;
112  }
113  }
114 
115  Connections {
116  target: ApplicationManager
117  onFocusRequested: {
118  if (spreadView.interactive) {
119  spreadView.snapTo(priv.indexOf(appId));
120  } else {
121  ApplicationManager.focusApplication(appId);
122  }
123  }
124 
125  onApplicationAdded: {
126  if (spreadView.phase == 2) {
127  spreadView.snapTo(ApplicationManager.count - 1);
128  } else {
129  spreadView.phase = 0;
130  spreadView.contentX = -spreadView.shift;
131  ApplicationManager.focusApplication(appId);
132  }
133  }
134 
135  onApplicationRemoved: {
136  if (priv.mainStageAppId == appId) {
137  ApplicationManager.focusApplication("unity8-dash")
138  }
139  if (priv.sideStageAppId == appId) {
140  priv.sideStageAppId = "";
141  }
142  if (ApplicationManager.count == 0) {
143  spreadView.phase = 0;
144  spreadView.contentX = -spreadView.shift;
145  }
146  }
147  }
148 
149  Flickable {
150  id: spreadView
151  anchors.fill: parent
152  interactive: (spreadDragArea.status == DirectionalDragArea.Recognized || phase > 1)
153  && draggedDelegateCount === 0
154  contentWidth: spreadRow.width - shift
155  contentX: -shift
156 
157  property int tileDistance: units.gu(20)
158  property int sideStageWidth: units.gu(40)
159  property bool sideStageVisible: priv.sideStageAppId
160 
161  // This indicates when the spreadView is active. That means, all the animations
162  // are activated and tiles need to line up for the spread.
163  readonly property bool active: shiftedContentX > 0 || spreadDragArea.dragging
164 
165  // The flickable needs to fill the screen in order to get touch events all over.
166  // However, we don't want to the user to be able to scroll back all the way. For
167  // that, the beginning of the gesture starts with a negative value for contentX
168  // so the flickable wants to pull it into the view already. "shift" tunes the
169  // distance where to "lock" the content.
170  readonly property real shift: width / 2
171  readonly property real shiftedContentX: contentX + shift
172 
173  // Phase of the animation:
174  // 0: Starting from right edge, a new app (index 1) comes in from the right
175  // 1: The app has reached the first snap position.
176  // 2: The list is dragged further and snaps into the spread view when entering phase 2
177  property int phase
178 
179  readonly property int phase0Width: sideStageWidth
180  readonly property int phase1Width: sideStageWidth
181 
182  // Those markers mark the various positions in the spread (ratio to screen width from right to left):
183  // 0 - 1: following finger, snap back to the beginning on release
184  readonly property real positionMarker1: 0.2
185  // 1 - 2: curved snapping movement, snap to nextInStack on release
186  readonly property real positionMarker2: sideStageWidth / spreadView.width
187  // 2 - 3: movement follows finger, snaps to phase 2 (full spread) on release
188  readonly property real positionMarker3: 0.6
189  // passing 3, we detach movement from the finger and snap to phase 2 (full spread)
190  readonly property real positionMarker4: 0.8
191 
192  readonly property int startSnapPosition: phase0Width * 0.5
193  readonly property int endSnapPosition: phase0Width * 0.75
194  readonly property real snapPosition: 0.75
195 
196  property int selectedIndex: -1
197  property int draggedDelegateCount: 0
198  property int closingIndex: -1
199 
200  property bool animateX: true
201 
202  property bool sideStageDragging: sideStageDragHandle.dragging
203  property real sideStageDragProgress: sideStageDragHandle.progress
204 
205  onSideStageDragProgressChanged: {
206  if (sideStageDragProgress == 1) {
207  ApplicationManager.focusApplication(priv.mainStageAppId);
208  priv.sideStageAppId = "";
209  }
210  }
211 
212  // In case the ApplicationManager already holds an app when starting up we're missing animations
213  // Make sure we end up in the same state
214  Component.onCompleted: {
215  spreadView.contentX = -spreadView.shift
216  }
217 
218  property int nextInStack: {
219  switch (state) {
220  case "main":
221  if (ApplicationManager.count > 1) {
222  return 1;
223  }
224  return -1;
225  case "mainAndOverlay":
226  if (ApplicationManager.count <= 2) {
227  return -1;
228  }
229  if (priv.appId0 == priv.mainStageAppId || priv.appId0 == priv.sideStageAppId) {
230  if (priv.appId1 == priv.mainStageAppId || priv.appId1 == priv.sideStageAppId) {
231  return 2;
232  }
233  return 1;
234  }
235  return 0;
236  case "overlay":
237  return 1;
238  }
239  print("Unhandled nextInStack case! This shouldn't happen any more when the Dash is an app!");
240  return -1;
241  }
242  property int nextZInStack: indexToZIndex(nextInStack)
243 
244  states: [
245  State {
246  name: "empty"
247  },
248  State {
249  name: "main"
250  },
251  State { // Side Stage only in overlay mode
252  name: "overlay"
253  },
254  State { // Main Stage and Side Stage in overlay mode
255  name: "mainAndOverlay"
256  },
257  State { // Main Stage and Side Stage in split mode
258  name: "mainAndSplit"
259  }
260  ]
261  state: {
262  if (priv.mainStageAppId && !priv.sideStageAppId) {
263  return "main";
264  }
265  if (!priv.mainStageAppId && priv.sideStageAppId) {
266  return "overlay";
267  }
268  if (priv.mainStageAppId && priv.sideStageAppId) {
269  return "mainAndOverlay";
270  }
271  return "empty";
272  }
273 
274  onShiftedContentXChanged: {
275  if (spreadView.phase == 0 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker2) {
276  spreadView.phase = 1;
277  } else if (spreadView.phase == 1 && spreadView.shiftedContentX > spreadView.width * spreadView.positionMarker4) {
278  spreadView.phase = 2;
279  } else if (spreadView.phase == 1 && spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
280  spreadView.phase = 0;
281  }
282  }
283 
284  function snap() {
285  if (shiftedContentX < phase0Width) {
286  snapAnimation.targetContentX = -shift;
287  snapAnimation.start();
288  } else if (shiftedContentX < phase1Width) {
289  snapTo(1);
290  } else {
291  // Add 1 pixel to make sure we definitely hit positionMarker4 even with rounding errors of the animation.
292  snapAnimation.targetContentX = spreadView.width * spreadView.positionMarker4 + 1 - shift;
293  snapAnimation.start();
294  }
295  }
296  function snapTo(index) {
297  spreadView.selectedIndex = index;
298  snapAnimation.targetContentX = -shift;
299  snapAnimation.start();
300  }
301 
302  // We need to shuffle z ordering a bit in order to keep side stage apps above main stage apps.
303  // We don't want to really reorder them in the model because that allows us to keep track
304  // of the last focused order.
305  function indexToZIndex(index) {
306  var app = ApplicationManager.get(index);
307  if (!app) {
308  return index;
309  }
310 
311  var active = app.appId == priv.mainStageAppId || app.appId == priv.sideStageAppId;
312  if (active && app.stage == ApplicationInfoInterface.MainStage) {
313  // if this app is active, and its the MainStage, always put it to index 0
314  return 0;
315  }
316  if (active && app.stage == ApplicationInfoInterface.SideStage) {
317  if (!priv.mainStageAppId) {
318  // Only have SS apps running. Put the active one at 0
319  return 0;
320  }
321 
322  // Precondition now: There's an active MS app and this is SS app:
323  if (spreadView.nextInStack >= 0 && ApplicationManager.get(spreadView.nextInStack).stage == ApplicationInfoInterface.MainStage) {
324  // If the next app coming from the right is a MS app, we need to elevate this SS ap above it.
325  // Put it to at least level 2, or higher if there's more apps coming in before this one.
326  return Math.max(index, 2);
327  } else {
328  // 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.
329  return 1;
330  }
331  }
332  if (index <= 2 && app.stage == ApplicationInfoInterface.MainStage && priv.sideStageAppId) {
333  // Ok, this is an inactive MS app. If there's an active SS app around, we need to place this one
334  // in between the active MS app and the active SS app, so that it comes in from there when dragging from the right.
335  // If there's now active SS app, just leave it where it is.
336  return priv.indexOf(priv.sideStageAppId) < index ? index - 1 : index;
337  }
338  if (index == spreadView.nextInStack && app.stage == ApplicationInfoInterface.SideStage) {
339  // This is a SS app and the next one to come in from the right:
340  if (priv.sideStageAppId && priv.mainStageAppId) {
341  // If there's both, an active MS and an active SS app, put this one right on top of that
342  return 2;
343  }
344  // Or if there's only one other active app, put it on top of that.
345  // The case that there isn't any other active app is already handled above.
346  return 1;
347  }
348  if (index == 2 && spreadView.nextInStack == 1 && priv.sideStageAppId) {
349  // If its index 2 but not the next one to come in, it means
350  // we've pulled another one down to index 2. Move this one up to 2 instead.
351  return 3;
352  }
353  // don't touch all others... (mostly index > 3 + simple cases where the above doesn't shuffle much)
354  return index;
355  }
356 
357  SequentialAnimation {
358  id: snapAnimation
359  property int targetContentX: -spreadView.shift
360 
361  UbuntuNumberAnimation {
362  target: spreadView
363  property: "contentX"
364  to: snapAnimation.targetContentX
365  duration: UbuntuAnimation.FastDuration
366  }
367 
368  ScriptAction {
369  script: {
370  if (spreadView.selectedIndex >= 0) {
371  var newIndex = spreadView.selectedIndex;
372  spreadView.selectedIndex = -1;
373  ApplicationManager.focusApplication(ApplicationManager.get(newIndex).appId);
374  spreadView.phase = 0;
375  spreadView.contentX = -spreadView.shift;
376  }
377  }
378  }
379  }
380 
381  Item {
382  id: spreadRow
383  x: spreadView.contentX
384  height: root.height
385  width: spreadView.width + Math.max(spreadView.width, ApplicationManager.count * spreadView.tileDistance)
386 
387  Rectangle {
388  id: sideStageBackground
389  color: "black"
390  anchors.fill: parent
391  anchors.leftMargin: spreadView.width - (1 - sideStageDragHandle.progress) * spreadView.sideStageWidth
392  z: spreadView.indexToZIndex(priv.indexOf(priv.sideStageAppId))
393  opacity: spreadView.phase == 0 ? 1 : 0
394  Behavior on opacity { UbuntuNumberAnimation {} }
395  }
396 
397  Item {
398  id: sideStageDragHandle
399  anchors { top: parent.top; bottom: parent.bottom; left: parent.left; leftMargin: spreadView.width - spreadView.sideStageWidth - width }
400  width: units.gu(2)
401  z: sideStageBackground.z
402  opacity: spreadView.phase <= 0 && spreadView.sideStageVisible ? 1 : 0
403  property real progress: 0
404  property bool dragging: false
405 
406  Behavior on opacity { UbuntuNumberAnimation {} }
407 
408  Connections {
409  target: spreadView
410  onSideStageVisibleChanged: {
411  if (spreadView.sideStageVisible) {
412  sideStageDragHandle.progress = 0;
413  }
414  }
415  }
416 
417  Image {
418  anchors.centerIn: parent
419  anchors.horizontalCenterOffset: parent.progress * spreadView.sideStageWidth - (width - parent.width) / 2
420  width: sideStageDragHandleMouseArea.pressed ? parent.width * 2 : parent.width
421  height: parent.height
422  source: "graphics/sidestage_handle@20.png"
423  Behavior on width { UbuntuNumberAnimation {} }
424  }
425 
426  MouseArea {
427  id: sideStageDragHandleMouseArea
428  anchors.fill: parent
429  enabled: spreadView.shiftedContentX == 0
430  property int startX
431  property var gesturePoints: new Array()
432 
433  onPressed: {
434  gesturePoints = [];
435  startX = mouseX;
436  sideStageDragHandle.progress = 0;
437  sideStageDragHandle.dragging = true;
438  }
439  onMouseXChanged: {
440  if (priv.mainStageAppId) {
441  sideStageDragHandle.progress = Math.max(0, (-startX + mouseX) / spreadView.sideStageWidth);
442  }
443  gesturePoints.push(mouseX);
444  }
445  onReleased: {
446  if (priv.mainStageAppId) {
447  var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
448  sideStageDragSnapAnimation.to = sideStageDragHandle.progress > 0.5 || oneWayFlick ? 1 : 0;
449  sideStageDragSnapAnimation.start();
450  } else {
451  sideStageDragHandle.dragging = false;
452  }
453  }
454  }
455  UbuntuNumberAnimation {
456  id: sideStageDragSnapAnimation
457  target: sideStageDragHandle
458  property: "progress"
459 
460  onRunningChanged: {
461  if (!running) {
462  sideStageDragHandle.dragging = false;
463  }
464  }
465  }
466  }
467 
468  Repeater {
469  id: spreadRepeater
470  model: ApplicationManager
471 
472  delegate: TransformedTabletSpreadDelegate {
473  id: spreadTile
474  height: spreadView.height
475  width: model.stage == ApplicationInfoInterface.MainStage ? spreadView.width : spreadView.sideStageWidth
476  active: model.appId == priv.mainStageAppId || model.appId == priv.sideStageAppId
477  zIndex: spreadView.indexToZIndex(index)
478  selected: spreadView.selectedIndex == index
479  otherSelected: spreadView.selectedIndex >= 0 && !selected
480  isInSideStage: priv.sideStageAppId == model.appId
481  interactive: !spreadView.interactive && spreadView.phase === 0 && root.interactive
482  swipeToCloseEnabled: spreadView.interactive
483  maximizedAppTopMargin: root.maximizedAppTopMargin
484  dragOffset: !isDash && model.appId == priv.mainStageAppId && root.inverseProgress > 0 ? root.inverseProgress : 0
485  application: ApplicationManager.get(index)
486  closeable: !isDash
487 
488  readonly property bool isDash: model.appId == "unity8-dash"
489 
490  // FIXME: A regular binding doesn't update any more after closing an app.
491  // Using a Binding for now.
492  Binding {
493  target: spreadTile
494  property: "z"
495  value: (!spreadView.active && isDash && !active) ? -1 : spreadTile.zIndex
496  }
497  x: spreadView.width
498 
499  property real behavioredZIndex: zIndex
500  Behavior on behavioredZIndex {
501  enabled: spreadView.closingIndex >= 0
502  UbuntuNumberAnimation {}
503  }
504 
505  // This is required because none of the bindings are triggered in some cases:
506  // When an app is closed, it might happen that ApplicationManager.get(nextInStack)
507  // returns a different app even though the nextInStackIndex and all the related
508  // bindings (index, mainStageApp, sideStageApp, etc) don't change. Let's force a
509  // binding update in that case.
510  Connections {
511  target: ApplicationManager
512  onApplicationRemoved: spreadTile.z = Qt.binding(function() {
513  return spreadView.indexToZIndex(index);
514  })
515  }
516 
517  progress: {
518  var tileProgress = (spreadView.shiftedContentX - behavioredZIndex * spreadView.tileDistance) / spreadView.width;
519  // Some tiles (nextInStack, active) need to move directly from the beginning, normalize progress to immediately start at 0
520  if ((index == spreadView.nextInStack && spreadView.phase < 2) || (active && spreadView.phase < 1)) {
521  tileProgress += behavioredZIndex * spreadView.tileDistance / spreadView.width;
522  }
523  return tileProgress;
524  }
525 
526  animatedProgress: {
527  if (spreadView.phase == 0 && (spreadTile.active || spreadView.nextInStack == index)) {
528  if (progress < spreadView.positionMarker1) {
529  return progress;
530  } else if (progress < spreadView.positionMarker1 + snappingCurve.period) {
531  return spreadView.positionMarker1 + snappingCurve.value * 3;
532  } else {
533  return spreadView.positionMarker2;
534  }
535  }
536  return progress;
537  }
538 
539  Binding {
540  target: spreadTile
541  property: "orientation"
542  when: spreadTile.interactive
543  value: root.orientation
544  }
545 
546  onClicked: {
547  if (spreadView.phase == 2) {
548  spreadView.snapTo(index);
549  }
550  }
551 
552  onDraggedChanged: {
553  if (dragged) {
554  spreadView.draggedDelegateCount++;
555  } else {
556  spreadView.draggedDelegateCount--;
557  }
558  }
559 
560  onClosed: {
561  spreadView.closingIndex = index;
562  ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
563  }
564 
565  EasingCurve {
566  id: snappingCurve
567  type: EasingCurve.Linear
568  period: (spreadView.positionMarker2 - spreadView.positionMarker1) / 3
569  progress: spreadTile.progress - spreadView.positionMarker1
570  }
571  }
572  }
573  }
574  }
575 
576  EdgeDragArea {
577  id: spreadDragArea
578  anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
579  width: root.dragAreaWidth
580  direction: Direction.Leftwards
581 
582  property bool attachedToView: false
583  property var gesturePoints: new Array()
584 
585  onTouchXChanged: {
586  if (!dragging) {
587  spreadView.phase = 0;
588  spreadView.contentX = -spreadView.shift;
589  }
590 
591  if (dragging && attachedToView) {
592  spreadView.contentX = -touchX + spreadDragArea.width - spreadView.shift;
593  if (spreadView.shiftedContentX > spreadView.phase0Width + spreadView.phase1Width / 2) {
594  attachedToView = false;
595  spreadView.snap();
596  }
597  }
598  gesturePoints.push(touchX);
599  }
600 
601  onStatusChanged: {
602  if (status == DirectionalDragArea.Recognized) {
603  attachedToView = true;
604  }
605  }
606 
607  onDraggingChanged: {
608  if (dragging) {
609  // Gesture recognized. Start recording this gesture
610  gesturePoints = [];
611  return;
612  }
613 
614  // Ok. The user released. Find out if it was a one-way movement.
615  var oneWayFlick = priv.evaluateOneWayFlick(gesturePoints);
616  gesturePoints = [];
617 
618  if (oneWayFlick && spreadView.shiftedContentX < spreadView.positionMarker1 * spreadView.width) {
619  // If it was a short one-way movement, do the Alt+Tab switch
620  // no matter if we didn't cross positionMarker1 yet.
621  spreadView.snapTo(spreadView.nextInStack);
622  } else if (!dragging && attachedToView) {
623  if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker1) {
624  spreadView.snap();
625  } else if (spreadView.shiftedContentX < spreadView.width * spreadView.positionMarker2) {
626  spreadView.snapTo(spreadView.nextInStack);
627  } else {
628  // otherwise snap to the closest snap position we can find
629  // (might be back to start, to app 1 or to spread)
630  spreadView.snap();
631  }
632  }
633  }
634  }
635 }