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