Lomiri
Launcher.qml
1 /*
2  * Copyright (C) 2013-2015 Canonical Ltd.
3  * Copyright (C) 2021 UBports Foundation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 import QtQuick 2.4
19 import "../Components"
20 import Lomiri.Components 1.3
21 import Lomiri.Gestures 0.1
22 import Lomiri.Launcher 0.1
23 import Utils 0.1 as Utils
24 
25 FocusScope {
26  id: root
27 
28  readonly property int ignoreHideIfMouseOverLauncher: 1
29 
30  property bool autohideEnabled: false
31  property bool lockedVisible: false
32  property bool available: true // can be used to disable all interactions
33  property alias inverted: panel.inverted
34  property int topPanelHeight: 0
35  property bool drawerEnabled: true
36  property alias privateMode: panel.privateMode
37  property url background
38  property alias backgroundSourceSize: drawer.backgroundSourceSize
39 
40  property int panelWidth: units.gu(10)
41  property int dragAreaWidth: units.gu(1)
42  property real progress: dragArea.dragging && dragArea.touchPosition.x > panelWidth ?
43  (width * (dragArea.touchPosition.x-panelWidth) / (width - panelWidth)) : 0
44 
45  property bool superPressed: false
46  property bool superTabPressed: false
47  property bool takesFocus: false;
48 
49  readonly property bool dragging: dragArea.dragging
50  readonly property real dragDistance: dragArea.dragging ? dragArea.touchPosition.x : 0
51  readonly property real visibleWidth: panel.width + panel.x
52  readonly property alias shortcutHintsShown: panel.shortcutHintsShown
53 
54  readonly property bool shown: panel.x > -panel.width
55  readonly property bool drawerShown: drawer.x == 0
56 
57  // emitted when an application is selected
58  signal launcherApplicationSelected(string appId)
59 
60  // emitted when the dash icon in the launcher has been tapped
61  signal showDashHome()
62 
63  onStateChanged: {
64  if (state == "") {
65  panel.dismissTimer.stop()
66  } else {
67  panel.dismissTimer.restart()
68  }
69  }
70 
71  onFocusChanged: {if (!focus) { root.takesFocus = false; }}
72 
73  onSuperPressedChanged: {
74  if (state == "drawer")
75  return;
76 
77  if (superPressed) {
78  superPressTimer.start();
79  superLongPressTimer.start();
80  } else {
81  superPressTimer.stop();
82  superLongPressTimer.stop();
83  switchToNextState(root.lockedVisible ? "visible" : "");
84  panel.shortcutHintsShown = false;
85  }
86  }
87 
88  onSuperTabPressedChanged: {
89  if (superTabPressed) {
90  switchToNextState("visible")
91  panel.highlightIndex = -1;
92  root.takesFocus = true;
93  root.focus = true;
94  superPressTimer.stop();
95  superLongPressTimer.stop();
96  } else {
97  switchToNextState(root.lockedVisible ? "visible" : "");
98  root.focus = false;
99  if (panel.highlightIndex == -1) {
100  root.showDashHome();
101  } else if (panel.highlightIndex >= 0){
102  launcherApplicationSelected(LauncherModel.get(panel.highlightIndex).appId);
103  }
104  panel.highlightIndex = -2;
105  }
106  }
107 
108  onLockedVisibleChanged: {
109  // We are in the progress of moving to the drawer
110  // this is caused by the user pressing the bfb on unlock
111  // in this case we want to show the drawer and not
112  // just visible
113  if (animateTimer.nextState == "drawer")
114  return;
115 
116  if (lockedVisible && state == "") {
117  panel.dismissTimer.stop();
118  fadeOutAnimation.stop();
119  switchToNextState("visible")
120  } else if (!lockedVisible && (state == "visible" || state == "drawer")) {
121  hide();
122  }
123  }
124 
125  onPanelWidthChanged: {
126  hint();
127  }
128 
129  // Switches the Launcher to the visible state, but only if it's not already
130  // opened.
131  // Prevents closing the Drawer when trying to show the Launcher.
132  function show() {
133  if (state === "" || state === "visibleTemporary") {
134  switchToNextState("visible");
135  }
136  }
137 
138  function hide(flags) {
139  if ((flags & ignoreHideIfMouseOverLauncher) && Utils.Functions.itemUnderMouse(panel)) {
140  if (state == "drawer") {
141  switchToNextState("visibleTemporary");
142  }
143  return;
144  }
145  if (root.lockedVisible) {
146  // Due to binding updates when switching between modes
147  // it could happen that our request to show will be overwritten
148  // with a hide request. Rewrite it when we know hiding is not allowed.
149  switchToNextState("visible")
150  } else {
151  switchToNextState("")
152  }
153  root.focus = false;
154  }
155 
156  function fadeOut() {
157  if (!root.lockedVisible) {
158  fadeOutAnimation.start();
159  }
160  }
161 
162  function switchToNextState(state) {
163  animateTimer.nextState = state
164  animateTimer.start();
165  }
166 
167  function tease() {
168  if (available && !dragArea.dragging) {
169  teaseTimer.mode = "teasing"
170  teaseTimer.start();
171  }
172  }
173 
174  function hint() {
175  if (available && root.state == "") {
176  teaseTimer.mode = "hinting"
177  teaseTimer.start();
178  }
179  }
180 
181  function pushEdge(amount) {
182  if (root.state === "" || root.state == "visible" || root.state == "visibleTemporary") {
183  edgeBarrier.push(amount);
184  }
185  }
186 
187  function openForKeyboardNavigation() {
188  panel.highlightIndex = -1; // The BFB
189  drawer.focus = false;
190  root.takesFocus = true;
191  root.focus = true;
192  switchToNextState("visible")
193  }
194 
195  function toggleDrawer(focusInputField, onlyOpen, alsoToggleLauncher) {
196  if (!drawerEnabled) {
197  return;
198  }
199 
200  panel.shortcutHintsShown = false;
201  superPressTimer.stop();
202  superLongPressTimer.stop();
203  root.takesFocus = true;
204  root.focus = true;
205  if (focusInputField) {
206  drawer.focusInput();
207  }
208  if (state === "drawer" && !onlyOpen)
209  if (alsoToggleLauncher && !root.lockedVisible)
210  switchToNextState("");
211  else
212  switchToNextState("visible");
213  else
214  switchToNextState("drawer");
215  }
216 
217  Keys.onPressed: {
218  switch (event.key) {
219  case Qt.Key_Backtab:
220  panel.highlightPrevious();
221  event.accepted = true;
222  break;
223  case Qt.Key_Up:
224  if (root.inverted) {
225  panel.highlightNext()
226  } else {
227  panel.highlightPrevious();
228  }
229  event.accepted = true;
230  break;
231  case Qt.Key_Tab:
232  panel.highlightNext();
233  event.accepted = true;
234  break;
235  case Qt.Key_Down:
236  if (root.inverted) {
237  panel.highlightPrevious();
238  } else {
239  panel.highlightNext();
240  }
241  event.accepted = true;
242  break;
243  case Qt.Key_Right:
244  case Qt.Key_Menu:
245  panel.openQuicklist(panel.highlightIndex)
246  event.accepted = true;
247  break;
248  case Qt.Key_Escape:
249  panel.highlightIndex = -2;
250  // Falling through intentionally
251  case Qt.Key_Enter:
252  case Qt.Key_Return:
253  case Qt.Key_Space:
254  if (panel.highlightIndex == -1) {
255  root.showDashHome();
256  } else if (panel.highlightIndex >= 0) {
257  launcherApplicationSelected(LauncherModel.get(panel.highlightIndex).appId);
258  }
259  root.hide();
260  panel.highlightIndex = -2
261  event.accepted = true;
262  }
263  }
264 
265  Timer {
266  id: superPressTimer
267  interval: 200
268  onTriggered: {
269  switchToNextState("visible")
270  }
271  }
272 
273  Timer {
274  id: superLongPressTimer
275  interval: 1000
276  onTriggered: {
277  switchToNextState("visible")
278  panel.shortcutHintsShown = true;
279  }
280  }
281 
282  Timer {
283  id: teaseTimer
284  interval: mode == "teasing" ? 200 : 300
285  property string mode: "teasing"
286  }
287 
288  // Because the animation on x is disabled while dragging
289  // switching state directly in the drag handlers would not animate
290  // the completion of the hide/reveal gesture. Lets update the state
291  // machine and switch to the final state in the next event loop run
292  Timer {
293  id: animateTimer
294  objectName: "animateTimer"
295  interval: 1
296  property string nextState: ""
297  onTriggered: {
298  // switching to an intermediate state here to make sure all the
299  // values are restored, even if we were already in the target state
300  root.state = "tmp"
301  root.state = nextState
302  }
303  }
304 
305  Connections {
306  target: LauncherModel
307  onHint: hint();
308  }
309 
310  Connections {
311  target: i18n
312  onLanguageChanged: LauncherModel.refresh()
313  }
314 
315  SequentialAnimation {
316  id: fadeOutAnimation
317  ScriptAction {
318  script: {
319  animateTimer.stop(); // Don't change the state behind our back
320  panel.layer.enabled = true
321  }
322  }
323  LomiriNumberAnimation {
324  target: panel
325  property: "opacity"
326  easing.type: Easing.InQuad
327  to: 0
328  }
329  ScriptAction {
330  script: {
331  panel.layer.enabled = false
332  panel.animate = false;
333  root.state = "";
334  panel.x = -panel.width
335  panel.opacity = 1;
336  panel.animate = true;
337  }
338  }
339  }
340 
341  InverseMouseArea {
342  id: closeMouseArea
343  anchors.fill: panel
344  enabled: (root.state == "visible" && !root.lockedVisible) || root.state == "drawer" || hoverEnabled
345  hoverEnabled: panel.quickListOpen
346  visible: enabled
347  onPressed: {
348  mouse.accepted = false;
349  panel.highlightIndex = -2;
350  root.hide();
351  }
352  }
353 
354  MouseArea {
355  id: launcherDragArea
356  enabled: root.available && (root.state == "visible" || root.state == "visibleTemporary") && !root.lockedVisible
357  anchors.fill: panel
358  anchors.rightMargin: -units.gu(2)
359  drag {
360  axis: Drag.XAxis
361  maximumX: 0
362  target: panel
363  }
364 
365  onReleased: {
366  if (panel.x < -panel.width/3) {
367  root.switchToNextState("")
368  } else {
369  root.switchToNextState("visible")
370  }
371  }
372  }
373 
374  Drawer {
375  id: drawer
376  objectName: "drawer"
377  anchors {
378  top: parent.top
379  topMargin: root.inverted ? root.topPanelHeight : 0
380  bottom: parent.bottom
381  right: parent.left
382  }
383  background: root.background
384  width: Math.min(root.width, units.gu(81))
385  panelWidth: panel.width
386  allowSlidingAnimation: !dragArea.dragging && !launcherDragArea.drag.active && panel.animate
387 
388  onApplicationSelected: {
389  root.launcherApplicationSelected(appId)
390  root.hide();
391  root.focus = false;
392  }
393 
394  onHideRequested: {
395  root.hide();
396  }
397 
398  onOpenRequested: {
399  root.toggleDrawer(false, true);
400  }
401  }
402 
403  LauncherPanel {
404  id: panel
405  objectName: "launcherPanel"
406  enabled: root.available && (root.state == "visible" || root.state == "visibleTemporary" || root.state == "drawer")
407  width: root.panelWidth
408  anchors {
409  top: parent.top
410  bottom: parent.bottom
411  }
412  x: -width
413  visible: root.x > 0 || x > -width || dragArea.pressed
414  model: LauncherModel
415 
416  property var dismissTimer: Timer { interval: 500 }
417  Connections {
418  target: panel.dismissTimer
419  onTriggered: {
420  if (root.autohideEnabled && !root.lockedVisible) {
421  if (!edgeBarrier.containsMouse && !panel.preventHiding) {
422  root.state = ""
423  } else {
424  panel.dismissTimer.restart()
425  }
426  }
427  }
428  }
429 
430  property bool animate: true
431 
432  onApplicationSelected: {
433  launcherApplicationSelected(appId);
434  root.hide(ignoreHideIfMouseOverLauncher);
435  }
436  onShowDashHome: {
437  root.hide(ignoreHideIfMouseOverLauncher);
438  root.showDashHome();
439  }
440 
441  onPreventHidingChanged: {
442  if (panel.dismissTimer.running) {
443  panel.dismissTimer.restart();
444  }
445  }
446 
447  onKbdNavigationCancelled: {
448  panel.highlightIndex = -2;
449  root.hide();
450  root.focus = false;
451  }
452 
453  onDraggingChanged: {
454  drawer.unFocusInput()
455  }
456 
457  Behavior on x {
458  enabled: !dragArea.dragging && !launcherDragArea.drag.active && panel.animate;
459  NumberAnimation {
460  duration: 300
461  easing.type: Easing.OutCubic
462  }
463  }
464 
465  Behavior on opacity {
466  NumberAnimation {
467  duration: LomiriAnimation.FastDuration; easing.type: Easing.OutCubic
468  }
469  }
470  }
471 
472  EdgeBarrier {
473  id: edgeBarrier
474  edge: Qt.LeftEdge
475  target: parent
476  enabled: root.available
477  onProgressChanged: {
478  if (progress > .5 && root.state != "visibleTemporary" && root.state != "drawer" && root.state != "visible") {
479  root.switchToNextState("visibleTemporary");
480  }
481  }
482  onPassed: {
483  if (root.drawerEnabled) {
484  root.toggleDrawer()
485  }
486  }
487 
488  material: Component {
489  Item {
490  Rectangle {
491  width: parent.height
492  height: parent.width
493  rotation: -90
494  anchors.centerIn: parent
495  gradient: Gradient {
496  GradientStop { position: 0.0; color: Qt.rgba(panel.color.r, panel.color.g, panel.color.b, .5)}
497  GradientStop { position: 1.0; color: Qt.rgba(panel.color.r,panel.color.g,panel.color.b,0)}
498  }
499  }
500  }
501  }
502  }
503 
504  SwipeArea {
505  id: dragArea
506  objectName: "launcherDragArea"
507 
508  direction: Direction.Rightwards
509 
510  enabled: root.available
511  x: -root.x // so if launcher is adjusted relative to screen, we stay put (like tutorial does when teasing)
512  width: root.dragAreaWidth
513  height: root.height
514 
515  function easeInOutCubic(t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
516 
517  property var lastDragPoints: []
518 
519  function dragDirection() {
520  if (lastDragPoints.length < 5) {
521  return "unknown";
522  }
523 
524  var toRight = true;
525  var toLeft = true;
526  for (var i = lastDragPoints.length - 5; i < lastDragPoints.length; i++) {
527  if (toRight && lastDragPoints[i] < lastDragPoints[i-1]) {
528  toRight = false;
529  }
530  if (toLeft && lastDragPoints[i] > lastDragPoints[i-1]) {
531  toLeft = false;
532  }
533  }
534  return toRight ? "right" : toLeft ? "left" : "unknown";
535  }
536 
537  onDistanceChanged: {
538  if (dragging && launcher.state != "visible" && launcher.state != "drawer") {
539  panel.x = -panel.width + Math.min(Math.max(0, distance), panel.width);
540  }
541 
542  if (root.drawerEnabled && dragging && launcher.state != "drawer") {
543  lastDragPoints.push(distance)
544  var drawerHintDistance = panel.width + units.gu(1)
545  if (distance < drawerHintDistance) {
546  drawer.anchors.rightMargin = -Math.min(Math.max(0, distance), drawer.width);
547  } else {
548  var linearDrawerX = Math.min(Math.max(0, distance - drawerHintDistance), drawer.width);
549  var linearDrawerProgress = linearDrawerX / (drawer.width)
550  var easedDrawerProgress = easeInOutCubic(linearDrawerProgress);
551  drawer.anchors.rightMargin = -(drawerHintDistance + easedDrawerProgress * (drawer.width - drawerHintDistance));
552  }
553  }
554  }
555 
556  onDraggingChanged: {
557  if (!dragging) {
558  if (distance > panel.width / 2) {
559  if (root.drawerEnabled && distance > panel.width * 3 && dragDirection() !== "left") {
560  root.toggleDrawer(false)
561  } else {
562  root.switchToNextState("visible");
563  }
564  } else if (root.state === "") {
565  // didn't drag far enough. rollback
566  root.switchToNextState("");
567  }
568  }
569  lastDragPoints = [];
570  }
571  }
572 
573  states: [
574  State {
575  name: "" // hidden state. Must be the default state ("") because "when:" falls back to this.
576  PropertyChanges {
577  target: panel
578  restoreEntryValues: false
579  x: -root.panelWidth
580  }
581  PropertyChanges {
582  target: drawer
583  restoreEntryValues: false
584  anchors.rightMargin: 0
585  focus: false
586  }
587  },
588  State {
589  name: "visible"
590  PropertyChanges {
591  target: panel
592  restoreEntryValues: false
593  x: -root.x // so we never go past panelWidth, even when teased by tutorial
594  focus: true
595  }
596  PropertyChanges {
597  target: drawer
598  restoreEntryValues: false
599  anchors.rightMargin: 0
600  focus: false
601  }
602  },
603  State {
604  name: "drawer"
605  PropertyChanges {
606  target: panel
607  restoreEntryValues: false
608  x: -root.x // so we never go past panelWidth, even when teased by tutorial
609  focus: false
610  }
611  PropertyChanges {
612  target: drawer
613  restoreEntryValues: false
614  anchors.rightMargin: -drawer.width + root.x // so we never go past panelWidth, even when teased by tutorial
615  focus: true
616  }
617  },
618  State {
619  name: "visibleTemporary"
620  extend: "visible"
621  PropertyChanges {
622  target: root
623  restoreEntryValues: false
624  autohideEnabled: true
625  }
626  },
627  State {
628  name: "teasing"
629  when: teaseTimer.running && teaseTimer.mode == "teasing"
630  PropertyChanges {
631  target: panel
632  restoreEntryValues: false
633  x: -root.panelWidth + units.gu(2)
634  }
635  },
636  State {
637  name: "hinting"
638  when: teaseTimer.running && teaseTimer.mode == "hinting"
639  PropertyChanges {
640  target: panel
641  restoreEntryValues: false
642  x: 0
643  }
644  }
645  ]
646 }