2 * Copyright (C) 2013-2015 Canonical Ltd.
3 * Copyright (C) 2021 UBports Foundation
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.
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.
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/>.
20import Lomiri.Components 1.3
21import Lomiri.Gestures 0.1
22import Lomiri.Launcher 0.1
23import Utils 0.1 as Utils
28 readonly property int ignoreHideIfMouseOverLauncher: 1
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
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
45 property bool superPressed: false
46 property bool superTabPressed: false
47 property bool takesFocus: false;
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
54 readonly property bool shown: panel.x > -panel.width
55 readonly property bool drawerShown: drawer.x == 0
57 // emitted when an application is selected
58 signal launcherApplicationSelected(string appId)
60 // emitted when the dash icon in the launcher has been tapped
65 panel.dismissTimer.stop()
67 panel.dismissTimer.restart()
71 onFocusChanged: {if (!focus) { root.takesFocus = false; }}
73 onSuperPressedChanged: {
74 if (state == "drawer")
78 superPressTimer.start();
79 superLongPressTimer.start();
81 superPressTimer.stop();
82 superLongPressTimer.stop();
83 switchToNextState(root.lockedVisible ? "visible" : "");
84 panel.shortcutHintsShown = false;
88 onSuperTabPressedChanged: {
89 if (superTabPressed) {
90 switchToNextState("visible")
91 panel.highlightIndex = -1;
92 root.takesFocus = true;
94 superPressTimer.stop();
95 superLongPressTimer.stop();
97 switchToNextState(root.lockedVisible ? "visible" : "");
99 if (panel.highlightIndex == -1) {
101 } else if (panel.highlightIndex >= 0){
102 launcherApplicationSelected(LauncherModel.get(panel.highlightIndex).appId);
104 panel.highlightIndex = -2;
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
113 if (animateTimer.nextState == "drawer")
116 if (lockedVisible && state == "") {
117 panel.dismissTimer.stop();
118 fadeOutAnimation.stop();
119 switchToNextState("visible")
120 } else if (!lockedVisible && (state == "visible" || state == "drawer")) {
125 onPanelWidthChanged: {
129 // Switches the Launcher to the visible state, but only if it's not already
131 // Prevents closing the Drawer when trying to show the Launcher.
133 if (state === "" || state === "visibleTemporary") {
134 switchToNextState("visible");
138 function hide(flags) {
139 if ((flags & ignoreHideIfMouseOverLauncher) && Utils.Functions.itemUnderMouse(panel)) {
140 if (state == "drawer") {
141 switchToNextState("visibleTemporary");
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")
151 switchToNextState("")
157 if (!root.lockedVisible) {
158 fadeOutAnimation.start();
162 function switchToNextState(state) {
163 animateTimer.nextState = state
164 animateTimer.start();
168 if (available && !dragArea.dragging) {
169 teaseTimer.mode = "teasing"
175 if (available && root.state == "") {
176 teaseTimer.mode = "hinting"
181 function pushEdge(amount) {
182 if (root.state === "" || root.state == "visible" || root.state == "visibleTemporary") {
183 edgeBarrier.push(amount);
187 function openForKeyboardNavigation() {
188 panel.highlightIndex = -1; // The BFB
189 drawer.focus = false;
190 root.takesFocus = true;
192 switchToNextState("visible")
195 function toggleDrawer(focusInputField, onlyOpen, alsoToggleLauncher) {
196 if (!drawerEnabled) {
200 panel.shortcutHintsShown = false;
201 superPressTimer.stop();
202 superLongPressTimer.stop();
203 root.takesFocus = true;
205 if (focusInputField) {
208 if (state === "drawer" && !onlyOpen)
209 if (alsoToggleLauncher && !root.lockedVisible)
210 switchToNextState("");
212 switchToNextState("visible");
214 switchToNextState("drawer");
220 panel.highlightPrevious();
221 event.accepted = true;
225 panel.highlightNext()
227 panel.highlightPrevious();
229 event.accepted = true;
232 panel.highlightNext();
233 event.accepted = true;
237 panel.highlightPrevious();
239 panel.highlightNext();
241 event.accepted = true;
245 panel.openQuicklist(panel.highlightIndex)
246 event.accepted = true;
249 panel.highlightIndex = -2;
250 // Falling through intentionally
254 if (panel.highlightIndex == -1) {
256 } else if (panel.highlightIndex >= 0) {
257 launcherApplicationSelected(LauncherModel.get(panel.highlightIndex).appId);
260 panel.highlightIndex = -2
261 event.accepted = true;
269 switchToNextState("visible")
274 id: superLongPressTimer
277 switchToNextState("visible")
278 panel.shortcutHintsShown = true;
284 interval: mode == "teasing" ? 200 : 300
285 property string mode: "teasing"
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
294 objectName: "animateTimer"
296 property string nextState: ""
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
301 root.state = nextState
306 target: LauncherModel
312 onLanguageChanged: LauncherModel.refresh()
315 SequentialAnimation {
319 animateTimer.stop(); // Don't change the state behind our back
320 panel.layer.enabled = true
323 LomiriNumberAnimation {
326 easing.type: Easing.InQuad
331 panel.layer.enabled = false
332 panel.animate = false;
334 panel.x = -panel.width
336 panel.animate = true;
344 enabled: (root.state == "visible" && !root.lockedVisible) || root.state == "drawer" || hoverEnabled
345 hoverEnabled: panel.quickListOpen
348 mouse.accepted = false;
349 panel.highlightIndex = -2;
356 enabled: root.available && (root.state == "visible" || root.state == "visibleTemporary") && !root.lockedVisible
358 anchors.rightMargin: -units.gu(2)
366 if (panel.x < -panel.width/3) {
367 root.switchToNextState("")
369 root.switchToNextState("visible")
378 width: drawer.width + drawer.x
379 height: drawer.height
385 height: drawer.height
386 visible: drawer.x > -drawer.width
387 sourceItem: root.blurSource
392 occluding: (drawer.width == root.width) && drawer.fullyOpen
401 topMargin: root.inverted ? root.topPanelHeight : 0
402 bottom: parent.bottom
405 background: root.background
406 width: Math.min(root.width, units.gu(81))
407 panelWidth: panel.width
408 allowSlidingAnimation: !dragArea.dragging && !launcherDragArea.drag.active && panel.animate
410 onApplicationSelected: {
411 root.launcherApplicationSelected(appId)
421 root.toggleDrawer(false, true);
424 onFullyClosedChanged: {
428 drawer.unFocusInput()
435 objectName: "launcherPanel"
436 enabled: root.available && (root.state == "visible" || root.state == "visibleTemporary" || root.state == "drawer")
437 width: root.panelWidth
440 bottom: parent.bottom
443 visible: root.x > 0 || x > -width || dragArea.pressed
446 property var dismissTimer: Timer { interval: 500 }
448 target: panel.dismissTimer
450 if (root.state !== "drawer" && root.autohideEnabled && !root.lockedVisible) {
451 if (!edgeBarrier.containsMouse && !panel.preventHiding) {
454 panel.dismissTimer.restart()
460 property bool animate: true
462 onApplicationSelected: {
463 launcherApplicationSelected(appId);
464 root.hide(ignoreHideIfMouseOverLauncher);
467 root.hide(ignoreHideIfMouseOverLauncher);
471 onPreventHidingChanged: {
472 if (panel.dismissTimer.running) {
473 panel.dismissTimer.restart();
477 onKbdNavigationCancelled: {
478 panel.highlightIndex = -2;
484 drawer.unFocusInput()
488 enabled: !dragArea.dragging && !launcherDragArea.drag.active && panel.animate;
491 easing.type: Easing.OutCubic
495 Behavior on opacity {
497 duration: LomiriAnimation.FastDuration; easing.type: Easing.OutCubic
506 enabled: root.available
508 if (progress > .5 && root.state != "visibleTemporary" && root.state != "drawer" && root.state != "visible") {
509 root.switchToNextState("visibleTemporary");
513 if (root.drawerEnabled) {
518 material: Component {
524 anchors.centerIn: parent
526 GradientStop { position: 0.0; color: Qt.rgba(panel.color.r, panel.color.g, panel.color.b, .5)}
527 GradientStop { position: 1.0; color: Qt.rgba(panel.color.r,panel.color.g,panel.color.b,0)}
536 objectName: "launcherDragArea"
538 direction: Direction.Rightwards
540 enabled: root.available
541 x: -root.x // so if launcher is adjusted relative to screen, we stay put (like tutorial does when teasing)
542 width: root.dragAreaWidth
545 function easeInOutCubic(t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
547 property var lastDragPoints: []
549 function dragDirection() {
550 if (lastDragPoints.length < 5) {
556 for (var i = lastDragPoints.length - 5; i < lastDragPoints.length; i++) {
557 if (toRight && lastDragPoints[i] < lastDragPoints[i-1]) {
560 if (toLeft && lastDragPoints[i] > lastDragPoints[i-1]) {
564 return toRight ? "right" : toLeft ? "left" : "unknown";
568 if (dragging && launcher.state != "visible" && launcher.state != "drawer") {
569 panel.x = -panel.width + Math.min(Math.max(0, distance), panel.width);
572 if (root.drawerEnabled && dragging && launcher.state != "drawer") {
573 lastDragPoints.push(distance)
574 var drawerHintDistance = panel.width + units.gu(1)
575 if (distance < drawerHintDistance) {
576 drawer.anchors.rightMargin = -Math.min(Math.max(0, distance), drawer.width);
578 var linearDrawerX = Math.min(Math.max(0, distance - drawerHintDistance), drawer.width);
579 var linearDrawerProgress = linearDrawerX / (drawer.width)
580 var easedDrawerProgress = easeInOutCubic(linearDrawerProgress);
581 drawer.anchors.rightMargin = -(drawerHintDistance + easedDrawerProgress * (drawer.width - drawerHintDistance));
588 if (distance > panel.width / 2) {
589 if (root.drawerEnabled && distance > panel.width * 3 && dragDirection() !== "left") {
590 root.toggleDrawer(false)
592 root.switchToNextState("visible");
594 } else if (root.state === "") {
595 // didn't drag far enough. rollback
596 root.switchToNextState("");
605 name: "" // hidden state. Must be the default state ("") because "when:" falls back to this.
608 restoreEntryValues: false
613 restoreEntryValues: false
614 anchors.rightMargin: 0
622 restoreEntryValues: false
623 x: -root.x // so we never go past panelWidth, even when teased by tutorial
628 restoreEntryValues: false
629 anchors.rightMargin: 0
634 restoreEntryValues: false
635 autohideEnabled: false
642 restoreEntryValues: false
643 x: -root.x // so we never go past panelWidth, even when teased by tutorial
648 restoreEntryValues: false
649 anchors.rightMargin: -drawer.width + root.x // so we never go past panelWidth, even when teased by tutorial
654 name: "visibleTemporary"
658 restoreEntryValues: false
659 autohideEnabled: true
664 when: teaseTimer.running && teaseTimer.mode == "teasing"
667 restoreEntryValues: false
668 x: -root.panelWidth + units.gu(2)
673 when: teaseTimer.running && teaseTimer.mode == "hinting"
676 restoreEntryValues: false