on ef246e1 help sign in
unity8/Launcher/Launcher.qml
246 lines 6.8 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 int panelWidth: units.gu(8.5)
29 property int dragAreaWidth: units.gu(1)
30 property real progress: dragArea.dragging && dragArea.touchX > panel.width ? dragArea.touchX : 0
32 readonly property bool shown: panel.x > -panel.width
34 // emitted when an application is selected
35 signal launcherApplicationSelected(string desktopFile)
37 // emitted when the apps dash should be shown because of a swipe gesture
38 signal dash()
40 // emitted when the dash icon in the launcher has been tapped
41 signal dashItemSelected()
43 onStateChanged: {
44 if (state == "") {
45 dismissTimer.stop()
46 } else {
47 dismissTimer.restart()
48 }
49 }
51 function hide() {
52 switchToNextState("")
53 }
55 function switchToNextState(state) {
56 animateTimer.nextState = state
57 animateTimer.start();
58 }
60 function tease() {
61 if (available) {
62 teaseTimer.start();
63 }
64 }
66 Timer {
67 id: teaseTimer
68 interval: 200
69 }
71 Timer {
72 id: dismissTimer
73 interval: 5000
74 onTriggered: {
75 if (!panel.moving) {
76 root.state = ""
77 } else {
78 dismissTimer.restart()
79 }
80 }
81 }
83 // Because the animation on x is disabled while dragging
84 // switching state directly in the drag handlers would not animate
85 // the completion of the hide/reveal gesture. Lets update the state
86 // machine and switch to the final state in the next event loop run
87 Timer {
88 id: animateTimer
89 interval: 1
90 property string nextState: ""
91 onTriggered: {
92 // switching to an intermediate state here to make sure all the
93 // values are restored, even if we were already in the target state
94 root.state = "tmp"
95 root.state = nextState
96 }
97 }
99 LauncherModel {
100 id: launcherModel
101 }
103 MouseArea {
104 id: launcherDragArea
105 enabled: root.state == "visible"
106 anchors.fill: panel
107 anchors.rightMargin: -units.gu(2)
108 drag {
109 axis: Drag.XAxis
110 maximumX: 0
111 target: panel
112 }
114 onReleased: {
115 if (panel.x < -panel.width/3) {
116 root.switchToNextState("")
117 } else {
118 root.switchToNextState("visible")
119 }
120 }
122 }
123 MouseArea {
124 id: closeMouseArea
125 anchors {
126 left: launcherDragArea.right
127 top: parent.top
128 right: parent.right
129 bottom: parent.bottom
130 }
131 enabled: root.state == "visible"
132 onPressed: {
133 root.state = ""
134 }
135 }
137 Rectangle {
138 id: backgroundShade
139 anchors.fill: parent
140 color: "black"
141 opacity: root.state == "visible" ? 0.4 : 0
143 Behavior on opacity { NumberAnimation { duration: 200; easing.type: Easing.OutQuad} }
144 }
146 LauncherPanel {
147 id: panel
148 objectName: "launcherPanel"
149 width: root.panelWidth
150 anchors {
151 top: parent.top
152 bottom: parent.bottom
153 }
154 x: -width
155 model: launcherModel
157 property bool animate: true
159 onApplicationSelected: {
160 root.state = ""
161 launcherApplicationSelected(desktopFile)
162 }
163 onDashItemSelected: {
164 root.state = ""
165 root.dashItemSelected(index)
166 }
168 onMovingChanged: {
169 if (dismissTimer.running) {
170 dismissTimer.restart();
171 }
172 }
174 Behavior on x {
175 NumberAnimation {
176 // Disabling animation when dragging
177 duration: dragArea.dragging || launcherDragArea.drag.active ? 0 : 300;
178 easing.type: Easing.OutCubic
179 }
180 }
181 }
183 EdgeDragArea {
184 id: dragArea
186 direction: Direction.Rightwards
188 enabled: root.available && root.state !== "visible"
189 width: root.dragAreaWidth
190 height: root.height
192 onTouchXChanged: {
193 if (status !== DirectionalDragArea.Recognized)
194 return;
196 // When the gesture finally gets recognized, the finger will likely be
197 // reasonably far from the edge. If we made the panel immediately
198 // follow the finger position it would be visually unpleasant as it
199 // would appear right next to the user's finger out of nowhere.
200 // Instead, we make the panel go towards the user's finger in several
201 // steps. ie., in an animated way.
202 var targetPanelX = Math.min(0, touchX - panel.width)
203 var delta = targetPanelX - panel.x
204 // the trick is not to go all the way (1.0) as it would cause a sudden jump
205 panel.x += 0.4 * delta
206 }
208 onDraggingChanged: {
209 if (!dragging) {
210 if (distance > panel.width / 2) {
211 root.switchToNextState("visible")
212 if (distance > panel.width * 2) {
213 root.dash()
214 }
215 } else {
216 root.switchToNextState("")
217 }
218 }
219 }
220 }
222 states: [
223 State {
224 name: "" // hidden state. Must be the default state ("") because "when:" falls back to this.
225 PropertyChanges {
226 target: panel
227 x: -root.panelWidth
228 }
229 },
230 State {
231 name: "visible"
232 PropertyChanges {
233 target: panel
234 x: 0
235 }
236 },
237 State {
238 name: "teasing"
239 when: teaseTimer.running
240 PropertyChanges {
241 target: panel
242 x: -root.panelWidth + units.gu(2)
243 }
244 }
245 ]