on be81ccf help sign in
unity8/Launcher/Launcher.qml
254 lines 7.1 kB history raw
1/*
2 * Copyright (C) 2013 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 */
17import QtQuick 2.0
18import "../Components"
19import Ubuntu.Components 0.1
20import Ubuntu.Gestures 0.1
21import Unity 0.1
23Item {
24 id: root
26 property bool available: true // can be used to disable all interactions
28 property bool teasing: false
29 property int panelWidth: units.gu(8.5)
30 property int dragAreaWidth: units.gu(1)
31 property real progress: dragArea.dragging && dragArea.touchX > panel.width ? dragArea.touchX : 0
33 readonly property bool shown: panel.x > -panel.width
35 // emitted when an application is selected
36 signal launcherApplicationSelected(string desktopFile)
38 // emitted when the apps dash should be shown because of a swipe gesture
39 signal dash()
41 // emitted when the dash icon in the launcher has been tapped
42 signal dashItemSelected()
44 onStateChanged: {
45 if (state == "") {
46 dismissTimer.stop()
47 } else {
48 dismissTimer.restart()
49 }
50 }
52 function hide() {
53 state = ""
54 }
56 function switchToNextState(state) {
57 animateTimer.nextState = state
58 animateTimer.start();
59 }
61 onTeasingChanged: {
62 if (teasing) {
63 teaseTimer.start();
64 }
65 }
67 Timer {
68 id: teaseTimer
69 interval: 200
70 }
72 Timer {
73 id: dismissTimer
74 interval: 5000
75 onTriggered: {
76 if (!panel.moving) {
77 root.state = ""
78 } else {
79 dismissTimer.restart()
80 }
81 }
82 }
84 // Because the animation on x is disabled while dragging
85 // switching state directly in the drag handlers would not animate
86 // the completion of the hide/reveal gesture. Lets update the state
87 // machine and switch to the final state in the next event loop run
88 Timer {
89 id: animateTimer
90 interval: 1
91 property string nextState: ""
92 onTriggered: {
93 // switching to an intermediate state here to make sure all the
94 // values are restored, even if we were already in the target state
95 root.state = "tmp"
96 root.state = nextState
97 }
98 }
100 LauncherModel {
101 id: launcherModel
102 }
104 MouseArea {
105 id: launcherDragArea
106 enabled: root.state == "visible"
107 anchors.fill: panel
108 anchors.rightMargin: -units.gu(2)
109 drag {
110 axis: Drag.XAxis
111 maximumX: 0
112 target: panel
113 }
115 onReleased: {
116 if (panel.x < -panel.width/3) {
117 root.switchToNextState("")
118 } else {
119 root.switchToNextState("visible")
120 }
121 }
123 }
124 MouseArea {
125 id: closeMouseArea
126 anchors {
127 left: launcherDragArea.right
128 top: parent.top
129 right: parent.right
130 bottom: parent.bottom
131 }
132 enabled: root.state == "visible"
133 onPressed: {
134 root.state = ""
135 }
136 }
138 Rectangle {
139 id: backgroundShade
140 anchors.fill: parent
141 color: "black"
142 opacity: root.state == "visible" ? 0.4 : 0
144 Behavior on opacity { NumberAnimation { duration: 200; easing.type: Easing.OutQuad} }
145 }
147 LauncherPanel {
148 id: panel
149 objectName: "launcherPanel"
150 width: root.panelWidth
151 anchors {
152 top: parent.top
153 bottom: parent.bottom
154 }
155 x: -width
156 model: launcherModel
158 property bool animate: true
160 onApplicationSelected: {
161 root.state = ""
162 launcherApplicationSelected(desktopFile)
163 }
164 onDashItemSelected: {
165 root.state = ""
166 root.dashItemSelected(index)
167 }
169 onMovingChanged: {
170 if (dismissTimer.running) {
171 dismissTimer.restart();
172 }
173 }
175 Behavior on x {
176 NumberAnimation {
177 // Disabling animation when dragging
178 duration: dragArea.dragging || launcherDragArea.drag.active ? 0 : 300;
179 easing.type: Easing.OutCubic
180 }
181 }
182 }
184 DirectionalDragArea {
185 id: dragArea
187 direction: Direction.Rightwards
189 // values to be tweaked and later removed from here and set in stone as defaults
190 // once we are confident it's all good.
191 maxDeviation: units.gu(1)
192 wideningAngle: 30
193 distanceThreshold: units.gu(3)
194 minSpeed: units.gu(5)
196 enabled: root.available && root.state !== "visible"
197 width: root.dragAreaWidth
198 height: root.height
200 onTouchXChanged: {
201 if (status !== DirectionalDragArea.Recognized)
202 return;
204 // When the gesture finally gets recognized, the finger will likely be
205 // reasonably far from the edge. If we made the panel immediately
206 // follow the finger position it would be visually unpleasant as it
207 // would appear right next to the user's finger out of nowhere.
208 // Instead, we make the panel go towards the user's finger in several
209 // steps. ie., in an animated way.
210 var targetPanelX = Math.min(0, touchX - panel.width)
211 var delta = targetPanelX - panel.x
212 // the trick is not to go all the way (1.0) as it would cause a sudden jump
213 panel.x += 0.4 * delta
214 }
216 onDraggingChanged: {
217 if (!dragging) {
218 if (distance > root.width / 2) {
219 root.dash()
220 root.switchToNextState("")
221 } else if (distance > panel.width/2) {
222 root.switchToNextState("visible")
223 } else {
224 root.switchToNextState("")
225 }
226 }
227 }
228 }
230 states: [
231 State {
232 name: "" // hidden state. Must be the default state ("") because "when:" falls back to this.
233 PropertyChanges {
234 target: panel
235 x: -root.panelWidth
236 }
237 },
238 State {
239 name: "visible"
240 PropertyChanges {
241 target: panel
242 x: 0
243 }
244 },
245 State {
246 name: "teasing"
247 when: root.teasing || teaseTimer.running
248 PropertyChanges {
249 target: panel
250 x: -root.panelWidth + units.gu(2)
251 }
252 }
253 ]