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