on e586ed5 help sign in
unity8/qml/Panel/IndicatorsMenu.qml
369 lines 12 kB history raw
1/*
2 * Copyright (C) 2014 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.4
18import Ubuntu.Components 1.3
19import Ubuntu.Gestures 0.1
20import "../Components"
21import "Indicators"
23Showable {
24 id: root
25 property alias indicatorsModel: bar.indicatorsModel
26 property alias showDragHandle: __showDragHandle
27 property alias hideDragHandle: __hideDragHandle
28 property alias overFlowWidth: bar.overFlowWidth
29 property alias verticalVelocityThreshold: yVelocityCalculator.velocityThreshold
30 property alias currentIndicator: bar.currentIndicator
31 property int minimizedPanelHeight: units.gu(3)
32 property int expandedPanelHeight: units.gu(7)
33 property real openedHeight: units.gu(71)
34 readonly property real unitProgress: Math.max(0, (height - minimizedPanelHeight) / (openedHeight - minimizedPanelHeight))
35 readonly property bool fullyOpened: unitProgress >= 1
36 readonly property bool partiallyOpened: unitProgress > 0 && unitProgress < 1.0
37 readonly property bool fullyClosed: unitProgress == 0
38 property bool enableHint: true
39 property bool showOnClick: true
40 property color panelColor: theme.palette.normal.background
42 signal showTapped()
44 // TODO: Perhaps we need a animation standard for showing/hiding? Each showable seems to
45 // use its own values. Need to ask design about this.
46 showAnimation: SequentialAnimation {
47 StandardAnimation {
48 target: root
49 property: "height"
50 to: openedHeight
51 duration: UbuntuAnimation.BriskDuration
52 easing.type: Easing.OutCubic
53 }
54 // set binding in case units.gu changes while menu open, so height correctly adjusted to fit
55 ScriptAction { script: root.height = Qt.binding( function(){ return root.openedHeight; } ) }
56 }
58 hideAnimation: SequentialAnimation {
59 StandardAnimation {
60 target: root
61 property: "height"
62 to: minimizedPanelHeight
63 duration: UbuntuAnimation.BriskDuration
64 easing.type: Easing.OutCubic
65 }
66 // set binding in case units.gu changes while menu closed, so menu adjusts to fit
67 ScriptAction { script: root.height = Qt.binding( function(){ return root.minimizedPanelHeight; } ) }
68 }
70 height: minimizedPanelHeight
72 onUnitProgressChanged: d.updateState()
73 clip: root.partiallyOpened
75 IndicatorsLight {
76 id: indicatorLights
77 }
79 // eater
80 MouseArea {
81 anchors.fill: parent
82 hoverEnabled: true
83 onWheel: wheel.accepted = true;
84 }
86 MenuContent {
87 id: content
88 objectName: "menuContent"
90 anchors {
91 left: parent.left
92 right: parent.right
93 top: bar.bottom
94 }
95 height: openedHeight - bar.height - handle.height
96 indicatorsModel: root.indicatorsModel
97 visible: root.unitProgress > 0
98 currentMenuIndex: bar.currentItemIndex
99 }
101 Handle {
102 id: handle
103 objectName: "handle"
104 anchors {
105 left: parent.left
106 right: parent.right
107 bottom: parent.bottom
108 }
109 height: units.gu(2)
110 active: d.activeDragHandle ? true : false
112 //small shadow gradient at bottom of menu
113 Rectangle {
114 anchors {
115 left: parent.left
116 right: parent.right
117 bottom: parent.top
118 }
119 height: units.gu(0.5)
120 gradient: Gradient {
121 GradientStop { position: 0.0; color: "transparent" }
122 GradientStop { position: 1.0; color: theme.palette.normal.background }
123 }
124 opacity: 0.3
125 }
126 }
128 Rectangle {
129 anchors.fill: bar
130 color: panelColor
131 }
133 IndicatorsBar {
134 id: bar
135 objectName: "indicatorsBar"
137 anchors {
138 left: parent.left
139 right: parent.right
140 }
141 expanded: false
142 enableLateralChanges: false
143 lateralPosition: -1
144 unitProgress: root.unitProgress
146 height: expanded ? expandedPanelHeight : minimizedPanelHeight
147 Behavior on height { NumberAnimation { duration: UbuntuAnimation.SnapDuration; easing: UbuntuAnimation.StandardEasing } }
148 }
150 ScrollCalculator {
151 id: leftScroller
152 width: units.gu(5)
153 anchors.left: bar.left
154 height: bar.height
156 forceScrollingPercentage: 0.33
157 stopScrollThreshold: units.gu(0.75)
158 direction: Qt.RightToLeft
159 lateralPosition: -1
161 onScroll: bar.addScrollOffset(-scrollAmount);
162 }
164 ScrollCalculator {
165 id: rightScroller
166 width: units.gu(5)
167 anchors.right: bar.right
168 height: bar.height
170 forceScrollingPercentage: 0.33
171 stopScrollThreshold: units.gu(0.75)
172 direction: Qt.LeftToRight
173 lateralPosition: -1
175 onScroll: bar.addScrollOffset(scrollAmount);
176 }
178 MouseArea {
179 anchors.bottom: parent.bottom
180 anchors.left: parent.left
181 anchors.right: parent.right
182 height: minimizedPanelHeight
183 enabled: __showDragHandle.enabled && showOnClick
184 onClicked: {
185 bar.selectItemAt(mouseX)
186 root.show()
187 }
188 }
190 DragHandle {
191 id: __showDragHandle
192 objectName: "showDragHandle"
193 anchors.bottom: parent.bottom
194 anchors.left: parent.left
195 anchors.right: parent.right
196 height: minimizedPanelHeight
197 direction: Direction.Downwards
198 enabled: !root.shown && root.available
199 autoCompleteDragThreshold: maxTotalDragDistance / 2
200 stretch: true
202 onPressedChanged: {
203 if (pressed) {
204 touchPressTime = new Date().getTime();
205 } else {
206 var touchReleaseTime = new Date().getTime();
207 if (touchReleaseTime - touchPressTime <= 300) {
208 root.showTapped();
209 }
210 }
211 }
212 property var touchPressTime
214 // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
215 overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
216 maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
217 hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
218 }
220 MouseArea {
221 anchors.fill: __hideDragHandle
222 enabled: __hideDragHandle.enabled
223 onClicked: root.hide()
224 }
226 DragHandle {
227 id: __hideDragHandle
228 objectName: "hideDragHandle"
229 anchors.fill: handle
230 direction: Direction.Upwards
231 enabled: root.shown && root.available
232 hintDisplacement: units.gu(3)
233 autoCompleteDragThreshold: maxTotalDragDistance / 6
234 stretch: true
235 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
237 onTouchPositionChanged: {
238 if (root.state === "locked") {
239 d.xDisplacementSinceLock += (touchPosition.x - d.lastHideTouchX)
240 d.lastHideTouchX = touchPosition.x;
241 }
242 }
243 }
245 PanelVelocityCalculator {
246 id: yVelocityCalculator
247 velocityThreshold: d.hasCommitted ? 0.1 : 0.3
248 trackedValue: d.activeDragHandle ?
249 (Direction.isPositive(d.activeDragHandle.direction) ?
250 d.activeDragHandle.distance :
251 -d.activeDragHandle.distance)
252 : 0
254 onVelocityAboveThresholdChanged: d.updateState()
255 }
257 Connections {
258 target: showAnimation
259 onRunningChanged: {
260 if (showAnimation.running) {
261 root.state = "commit";
262 }
263 }
264 }
266 Connections {
267 target: hideAnimation
268 onRunningChanged: {
269 if (hideAnimation.running) {
270 root.state = "initial";
271 }
272 }
273 }
275 QtObject {
276 id: d
277 property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
278 property bool hasCommitted: false
279 property real lastHideTouchX: 0
280 property real xDisplacementSinceLock: 0
281 onXDisplacementSinceLockChanged: d.updateState()
283 property real rowMappedLateralPosition: {
284 if (!d.activeDragHandle) return -1;
285 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchPosition.x, 0).x;
286 }
288 function updateState() {
289 if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
290 if (unitProgress <= 0) {
291 root.state = "initial";
292 // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
293 } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
294 root.state = "locked";
295 } else {
296 root.state = "reveal";
297 }
298 }
299 }
300 }
302 states: [
303 State {
304 name: "initial"
305 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
306 },
307 State {
308 name: "reveal"
309 StateChangeScript {
310 script: {
311 yVelocityCalculator.reset();
312 // initial item selection
313 if (!d.hasCommitted) bar.selectItemAt(d.activeDragHandle ? d.activeDragHandle.touchPosition.x : -1);
314 d.hasCommitted = false;
315 }
316 }
317 PropertyChanges {
318 target: bar
319 expanded: true
320 // changes to lateral touch position effect which indicator is selected
321 lateralPosition: d.rowMappedLateralPosition
322 // vertical velocity determines if changes in lateral position has an effect
323 enableLateralChanges: d.activeDragHandle &&
324 !yVelocityCalculator.velocityAboveThreshold
325 }
326 // left scroll bar handling
327 PropertyChanges {
328 target: leftScroller
329 lateralPosition: {
330 if (!d.activeDragHandle) return -1;
331 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchPosition.x, 0);
332 return mapped.x;
333 }
334 }
335 // right scroll bar handling
336 PropertyChanges {
337 target: rightScroller
338 lateralPosition: {
339 if (!d.activeDragHandle) return -1;
340 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchPosition.x, 0);
341 return mapped.x;
342 }
343 }
344 },
345 State {
346 name: "locked"
347 StateChangeScript {
348 script: {
349 d.xDisplacementSinceLock = 0;
350 d.lastHideTouchX = hideDragHandle.touchPosition.x;
351 }
352 }
353 PropertyChanges { target: bar; expanded: true }
354 },
355 State {
356 name: "commit"
357 extend: "locked"
358 PropertyChanges { target: bar; interactive: true }
359 PropertyChanges {
360 target: d;
361 hasCommitted: true
362 lastHideTouchX: 0
363 xDisplacementSinceLock: 0
364 restoreEntryValues: false
365 }
366 }
367 ]
368 state: "initial"