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