on 496bb7a help sign in
unity8/Launcher/Launcher.qml
243 lines 6.4 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: DirectionalDragArea.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.5)
192 wideningAngle: 30
193 distanceThreshold: units.gu(4)
195 enabled: root.available && root.state !== "visible"
196 width: root.dragAreaWidth
197 height: root.height
199 onTouchXChanged: {
200 if (dragging) {
201 panel.x = Math.min(0, touchX - panel.width);
202 }
203 }
205 onDraggingChanged: {
206 if (!dragging) {
207 if (distance > root.width / 2) {
208 root.dash()
209 root.switchToNextState("")
210 } else if (distance > panel.width/2) {
211 root.switchToNextState("visible")
212 } else {
213 root.switchToNextState("")
214 }
215 }
216 }
217 }
219 states: [
220 State {
221 name: "" // hidden state. Must be the default state ("") because "when:" falls back to this.
222 PropertyChanges {
223 target: panel
224 x: -root.panelWidth
225 }
226 },
227 State {
228 name: "visible"
229 PropertyChanges {
230 target: panel
231 x: 0
232 }
233 },
234 State {
235 name: "teasing"
236 when: root.teasing || teaseTimer.running
237 PropertyChanges {
238 target: panel
239 x: -root.panelWidth + units.gu(2)
240 }
241 }
242 ]