on 7a1bfb2 help sign in
unity8/qml/Panel/IndicatorsMenu.qml
364 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(point position)
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 }
85 MenuContent {
86 id: content
87 objectName: "menuContent"
89 anchors {
90 left: parent.left
91 right: parent.right
92 top: bar.bottom
93 }
94 height: openedHeight - bar.height - handle.height
95 indicatorsModel: root.indicatorsModel
96 visible: root.unitProgress > 0
97 currentMenuIndex: bar.currentItemIndex
98 }
100 Handle {
101 id: handle
102 objectName: "handle"
103 anchors {
104 left: parent.left
105 right: parent.right
106 bottom: parent.bottom
107 }
108 height: units.gu(2)
109 active: d.activeDragHandle ? true : false
111 //small shadow gradient at bottom of menu
112 Rectangle {
113 anchors {
114 left: parent.left
115 right: parent.right
116 bottom: parent.top
117 }
118 height: units.gu(0.5)
119 gradient: Gradient {
120 GradientStop { position: 0.0; color: "transparent" }
121 GradientStop { position: 1.0; color: theme.palette.normal.background }
122 }
123 opacity: 0.3
124 }
125 }
127 Rectangle {
128 anchors.fill: bar
129 color: panelColor
130 }
132 IndicatorsBar {
133 id: bar
134 objectName: "indicatorsBar"
136 anchors {
137 left: parent.left
138 right: parent.right
139 }
140 expanded: false
141 enableLateralChanges: false
142 lateralPosition: -1
143 unitProgress: root.unitProgress
145 height: expanded ? expandedPanelHeight : minimizedPanelHeight
146 Behavior on height { NumberAnimation { duration: UbuntuAnimation.SnapDuration; easing: UbuntuAnimation.StandardEasing } }
147 }
149 ScrollCalculator {
150 id: leftScroller
151 width: units.gu(5)
152 anchors.left: bar.left
153 height: bar.height
155 forceScrollingPercentage: 0.33
156 stopScrollThreshold: units.gu(0.75)
157 direction: Qt.RightToLeft
158 lateralPosition: -1
160 onScroll: bar.addScrollOffset(-scrollAmount);
161 }
163 ScrollCalculator {
164 id: rightScroller
165 width: units.gu(5)
166 anchors.right: bar.right
167 height: bar.height
169 forceScrollingPercentage: 0.33
170 stopScrollThreshold: units.gu(0.75)
171 direction: Qt.LeftToRight
172 lateralPosition: -1
174 onScroll: bar.addScrollOffset(scrollAmount);
175 }
177 MouseArea {
178 anchors.bottom: parent.bottom
179 anchors.left: parent.left
180 anchors.right: parent.right
181 height: minimizedPanelHeight
182 enabled: __showDragHandle.enabled && showOnClick
183 onClicked: {
184 bar.selectItemAt(mouseX)
185 root.show()
186 }
187 }
189 DragHandle {
190 id: __showDragHandle
191 objectName: "showDragHandle"
192 anchors.bottom: parent.bottom
193 anchors.left: parent.left
194 anchors.right: parent.right
195 height: minimizedPanelHeight
196 direction: Direction.Downwards
197 enabled: !root.shown && root.available
198 autoCompleteDragThreshold: maxTotalDragDistance / 2
199 stretch: true
201 onPressedChanged: {
202 if (pressed) {
203 touchPressTime = new Date().getTime();
204 } else {
205 var touchReleaseTime = new Date().getTime();
206 if (touchReleaseTime - touchPressTime <= 300) {
207 root.showTapped(Qt.point(touchSceneX, touchSceneY));
208 }
209 }
210 }
211 property var touchPressTime
213 // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
214 overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
215 maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
216 hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
217 }
219 MouseArea {
220 anchors.fill: __hideDragHandle
221 enabled: __hideDragHandle.enabled
222 onClicked: root.hide()
223 }
225 DragHandle {
226 id: __hideDragHandle
227 objectName: "hideDragHandle"
228 anchors.fill: handle
229 direction: Direction.Upwards
230 enabled: root.shown && root.available
231 hintDisplacement: units.gu(3)
232 autoCompleteDragThreshold: maxTotalDragDistance / 6
233 stretch: true
234 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
236 onTouchSceneXChanged: {
237 if (root.state === "locked") {
238 d.xDisplacementSinceLock += (touchSceneX - d.lastHideTouchSceneX)
239 d.lastHideTouchSceneX = touchSceneX;
240 }
241 }
242 }
244 PanelVelocityCalculator {
245 id: yVelocityCalculator
246 velocityThreshold: d.hasCommitted ? 0.1 : 0.3
247 trackedValue: d.activeDragHandle ? d.activeDragHandle.touchSceneY : 0
249 onVelocityAboveThresholdChanged: d.updateState()
250 }
252 Connections {
253 target: showAnimation
254 onRunningChanged: {
255 if (showAnimation.running) {
256 root.state = "commit";
257 }
258 }
259 }
261 Connections {
262 target: hideAnimation
263 onRunningChanged: {
264 if (hideAnimation.running) {
265 root.state = "initial";
266 }
267 }
268 }
270 QtObject {
271 id: d
272 property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
273 property bool hasCommitted: false
274 property real lastHideTouchSceneX: 0
275 property real xDisplacementSinceLock: 0
276 onXDisplacementSinceLockChanged: d.updateState()
278 property real rowMappedLateralPosition: {
279 if (!d.activeDragHandle) return -1;
280 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchX, 0).x;
281 }
283 function updateState() {
284 if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
285 if (unitProgress <= 0) {
286 root.state = "initial";
287 // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
288 } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
289 root.state = "locked";
290 } else {
291 root.state = "reveal";
292 }
293 }
294 }
295 }
297 states: [
298 State {
299 name: "initial"
300 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
301 },
302 State {
303 name: "reveal"
304 StateChangeScript {
305 script: {
306 yVelocityCalculator.reset();
307 // initial item selection
308 if (!d.hasCommitted) bar.selectItemAt(d.activeDragHandle ? d.activeDragHandle.touchX : -1);
309 d.hasCommitted = false;
310 }
311 }
312 PropertyChanges {
313 target: bar
314 expanded: true
315 // changes to lateral touch position effect which indicator is selected
316 lateralPosition: d.rowMappedLateralPosition
317 // vertical velocity determines if changes in lateral position has an effect
318 enableLateralChanges: d.activeDragHandle &&
319 !yVelocityCalculator.velocityAboveThreshold
320 }
321 // left scroll bar handling
322 PropertyChanges {
323 target: leftScroller
324 lateralPosition: {
325 if (!d.activeDragHandle) return -1;
326 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchX, 0);
327 return mapped.x;
328 }
329 }
330 // right scroll bar handling
331 PropertyChanges {
332 target: rightScroller
333 lateralPosition: {
334 if (!d.activeDragHandle) return -1;
335 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchX, 0);
336 return mapped.x;
337 }
338 }
339 },
340 State {
341 name: "locked"
342 StateChangeScript {
343 script: {
344 d.xDisplacementSinceLock = 0;
345 d.lastHideTouchSceneX = hideDragHandle.touchSceneX;
346 }
347 }
348 PropertyChanges { target: bar; expanded: true }
349 },
350 State {
351 name: "commit"
352 extend: "locked"
353 PropertyChanges { target: bar; interactive: true }
354 PropertyChanges {
355 target: d;
356 hasCommitted: true
357 lastHideTouchSceneX: 0
358 xDisplacementSinceLock: 0
359 restoreEntryValues: false
360 }
361 }
362 ]
363 state: "initial"