on 309ebd3 help sign in
unity8/Launcher/Launcher.qml
243 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
22import Unity.Launcher 0.1
24Item {
25 id: root
27 property bool available: true // can be used to disable all interactions
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 switchToNextState("")
54 }
56 function switchToNextState(state) {
57 animateTimer.nextState = state
58 animateTimer.start();
59 }
61 function tease() {
62 if (available) {
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 MouseArea {
101 id: launcherDragArea
102 enabled: root.state == "visible"
103 anchors.fill: panel
104 anchors.rightMargin: -units.gu(2)
105 drag {
106 axis: Drag.XAxis
107 maximumX: 0
108 target: panel
109 }
111 onReleased: {
112 if (panel.x < -panel.width/3) {
113 root.switchToNextState("")
114 } else {
115 root.switchToNextState("visible")
116 }
117 }
119 }
120 MouseArea {
121 id: closeMouseArea
122 anchors {
123 left: launcherDragArea.right
124 top: parent.top
125 right: parent.right
126 bottom: parent.bottom
127 }
128 enabled: root.state == "visible"
129 onPressed: {
130 root.state = ""
131 }
132 }
134 Rectangle {
135 id: backgroundShade
136 anchors.fill: parent
137 color: "black"
138 opacity: root.state == "visible" ? 0.4 : 0
140 Behavior on opacity { NumberAnimation { duration: 200; easing.type: Easing.OutQuad} }
141 }
143 LauncherPanel {
144 id: panel
145 objectName: "launcherPanel"
146 width: root.panelWidth
147 anchors {
148 top: parent.top
149 bottom: parent.bottom
150 }
151 x: -width
152 model: LauncherModel
154 property bool animate: true
156 onApplicationSelected: {
157 root.state = ""
158 launcherApplicationSelected(desktopFile)
159 }
160 onDashItemSelected: {
161 root.state = ""
162 root.dashItemSelected(index)
163 }
165 onMovingChanged: {
166 if (dismissTimer.running) {
167 dismissTimer.restart();
168 }
169 }
171 Behavior on x {
172 NumberAnimation {
173 // Disabling animation when dragging
174 duration: dragArea.dragging || launcherDragArea.drag.active ? 0 : 300;
175 easing.type: Easing.OutCubic
176 }
177 }
178 }
180 EdgeDragArea {
181 id: dragArea
183 direction: Direction.Rightwards
185 enabled: root.available && root.state !== "visible"
186 width: root.dragAreaWidth
187 height: root.height
189 onTouchXChanged: {
190 if (status !== DirectionalDragArea.Recognized)
191 return;
193 // When the gesture finally gets recognized, the finger will likely be
194 // reasonably far from the edge. If we made the panel immediately
195 // follow the finger position it would be visually unpleasant as it
196 // would appear right next to the user's finger out of nowhere.
197 // Instead, we make the panel go towards the user's finger in several
198 // steps. ie., in an animated way.
199 var targetPanelX = Math.min(0, touchX - panel.width)
200 var delta = targetPanelX - panel.x
201 // the trick is not to go all the way (1.0) as it would cause a sudden jump
202 panel.x += 0.4 * delta
203 }
205 onDraggingChanged: {
206 if (!dragging) {
207 if (distance > panel.width / 2) {
208 root.switchToNextState("visible")
209 if (distance > panel.width * 2) {
210 root.dash()
211 }
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: teaseTimer.running
237 PropertyChanges {
238 target: panel
239 x: -root.panelWidth + units.gu(2)
240 }
241 }
242 ]