Lomiri
Loading...
Searching...
No Matches
PanelMenu.qml
1/*
2 * Copyright (C) 2014-2016 Canonical Ltd.
3 * Copyright (C) 2020 UBports Foundation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18import QtQuick 2.15
19import Lomiri.Components 1.3
20import Lomiri.Gestures 0.1
21import "../../../Components"
22import "../../Indicators"
23import "../.."
24
25Showable {
26 id: root
27 property alias model: bar.model
28 property alias showDragHandle: __showDragHandle
29 property alias hideDragHandle: __hideDragHandle
30 property alias overFlowWidth: bar.overFlowWidth
31 property alias verticalVelocityThreshold: yVelocityCalculator.velocityThreshold
32 property int minimizedPanelHeight: units.gu(3)
33 property int expandedPanelHeight: units.gu(7)
34 property real openedHeight: units.gu(71)
35 property bool enableHint: true
36 property bool showOnClick: true
37 property color panelColor: Qt.hsla(0, 0, Math.round(theme.palette.normal.background.hslLightness), 1)
38 property real menuContentX: 0
39
40 property alias alignment: bar.alignment
41 property alias hideRow: bar.hideRow
42 property alias rowItemDelegate: bar.rowItemDelegate
43 property alias pageDelegate: content.pageDelegate
44
45 property var blurSource : null
46 property rect blurRect : Qt.rect(0, 0, 0, 0)
47
48 readonly property real unitProgress: Math.max(0, (height - minimizedPanelHeight) / (openedHeight - minimizedPanelHeight))
49 readonly property bool fullyOpened: unitProgress >= 1
50 readonly property bool partiallyOpened: unitProgress > 0 && unitProgress < 1.0
51 readonly property bool fullyClosed: unitProgress == 0
52 readonly property alias expanded: bar.expanded
53 readonly property int barWidth: bar.width
54 readonly property alias currentMenuIndex: bar.currentItemIndex
55
56 // Exposes the current contentX of the PanelBar's internal ListView. This
57 // must be used to offset absolute x values against the ListView, since
58 // we commonly add or remove elements and cause the contentX to change.
59 readonly property int rowContentX: bar.rowContentX
60
61 // The user tapped the panel and did not move.
62 // Note that this does not fire on mouse events, only touch events.
63 signal showTapped()
64
65 // TODO: Perhaps we need a animation standard for showing/hiding? Each showable seems to
66 // use its own values. Need to ask design about this.
67 showAnimation: SequentialAnimation {
68 StandardAnimation {
69 target: root
70 property: "height"
71 to: openedHeight
72 duration: LomiriAnimation.BriskDuration
73 easing.type: Easing.OutCubic
74 }
75 // set binding in case units.gu changes while menu open, so height correctly adjusted to fit
76 ScriptAction { script: root.height = Qt.binding( function(){ return root.openedHeight; } ) }
77 }
78
79 hideAnimation: SequentialAnimation {
80 StandardAnimation {
81 target: root
82 property: "height"
83 to: minimizedPanelHeight
84 duration: LomiriAnimation.BriskDuration
85 easing.type: Easing.OutCubic
86 }
87 // set binding in case units.gu changes while menu closed, so menu adjusts to fit
88 ScriptAction { script: root.height = Qt.binding( function(){ return root.minimizedPanelHeight; } ) }
89 }
90
91 shown: false
92 height: minimizedPanelHeight
93
94 onUnitProgressChanged: d.updateState()
95
96 BackgroundBlur {
97 x: 0
98 y: 0
99 width: root.blurRect.width
100 height: root.blurRect.height
101 visible: root.height > root.minimizedPanelHeight
102 sourceItem: root.blurSource
103 blurRect: root.blurRect
104 occluding: false
105 }
106
107 Item {
108 anchors {
109 left: parent.left
110 right: parent.right
111 top: bar.bottom
112 bottom: parent.bottom
113 }
114 clip: root.partiallyOpened
115
116 Rectangle {
117 color: Qt.rgba(root.panelColor.r,
118 root.panelColor.g,
119 root.panelColor.b,
120 1.0)
121 opacity: 0.85
122 anchors.fill: parent
123 }
124
125 // eater
126 MouseArea {
127 anchors.fill: content
128 hoverEnabled: true
129 acceptedButtons: Qt.AllButtons
130 onWheel: wheel.accepted = true;
131 enabled: root.state != "initial"
132 visible: content.visible
133 }
134
135 MenuContent {
136 id: content
137 objectName: "menuContent"
138
139 anchors {
140 left: parent.left
141 right: parent.right
142 top: parent.top
143 }
144 height: openedHeight - bar.height - handle.height
145 model: root.model
146 visible: root.unitProgress > 0
147 currentMenuIndex: bar.currentItemIndex
148 }
149 }
150
151 Handle {
152 id: handle
153 objectName: "handle"
154 anchors {
155 left: parent.left
156 right: parent.right
157 bottom: parent.bottom
158 }
159 height: units.gu(2)
160 active: d.activeDragHandle ? true : false
161 visible: !root.fullyClosed
162 }
163
164 Rectangle {
165 anchors.fill: bar
166 color: panelColor
167 visible: !root.fullyClosed
168 }
169
170 Keys.onPressed: {
171 if (event.key === Qt.Key_Left) {
172 bar.selectPreviousItem();
173 event.accepted = true;
174 } else if (event.key === Qt.Key_Right) {
175 bar.selectNextItem();
176 event.accepted = true;
177 } else if (event.key === Qt.Key_Escape) {
178 root.hide();
179 event.accepted = true;
180 }
181 }
182
183 PanelBar {
184 id: bar
185 objectName: "indicatorsBar"
186
187 anchors {
188 left: parent.left
189 right: parent.right
190 }
191 expanded: false
192 enableLateralChanges: false
193 lateralPosition: -1
194 unitProgress: root.unitProgress
195
196 height: expanded ? expandedPanelHeight : minimizedPanelHeight
197 Behavior on height { NumberAnimation { duration: LomiriAnimation.SnapDuration; easing: LomiriAnimation.StandardEasing } }
198 }
199
200 ScrollCalculator {
201 id: leftScroller
202 width: units.gu(5)
203 anchors.left: bar.left
204 height: bar.height
205
206 forceScrollingPercentage: 0.33
207 stopScrollThreshold: units.gu(0.75)
208 direction: Qt.RightToLeft
209 lateralPosition: -1
210
211 onScroll: bar.addScrollOffset(-scrollAmount);
212 }
213
214 ScrollCalculator {
215 id: rightScroller
216 width: units.gu(5)
217 anchors.right: bar.right
218 height: bar.height
219
220 forceScrollingPercentage: 0.33
221 stopScrollThreshold: units.gu(0.75)
222 direction: Qt.LeftToRight
223 lateralPosition: -1
224
225 onScroll: bar.addScrollOffset(scrollAmount);
226 }
227
228 MouseArea {
229 anchors.bottom: parent.bottom
230 anchors.left: alignment == Qt.AlignLeft ? parent.left : __showDragHandle.left
231 anchors.right: alignment == Qt.AlignRight ? parent.right : __showDragHandle.right
232 height: minimizedPanelHeight
233 enabled: __showDragHandle.enabled && showOnClick
234 onClicked: {
235 var barPosition = mapToItem(bar, mouseX, mouseY);
236 bar.selectItemAt(barPosition.x)
237 root.show()
238 }
239 }
240
241 DragHandle {
242 id: __showDragHandle
243 objectName: "showDragHandle"
244 anchors.bottom: parent.bottom
245 anchors.left: alignment == Qt.AlignLeft ? parent.left : undefined
246 anchors.leftMargin: -root.menuContentX
247 anchors.right: alignment == Qt.AlignRight ? parent.right : undefined
248 width: root.overFlowWidth + root.menuContentX
249 height: minimizedPanelHeight
250 direction: Direction.Downwards
251 enabled: !root.shown && root.available && !hideAnimation.running && !showAnimation.running
252 autoCompleteDragThreshold: maxTotalDragDistance / 2
253 stretch: true
254
255 onPressedChanged: {
256 if (pressed) {
257 touchPressTime = new Date().getTime();
258 } else {
259 var touchReleaseTime = new Date().getTime();
260 if (touchReleaseTime - touchPressTime <= 300 && distance < units.gu(1)) {
261 root.showTapped();
262 }
263 }
264 }
265 property var touchPressTime
266
267 // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
268 overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
269 maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
270 hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
271 }
272
273 MouseArea {
274 anchors.fill: __hideDragHandle
275 enabled: __hideDragHandle.enabled
276 onClicked: root.hide()
277 }
278
279 DragHandle {
280 id: __hideDragHandle
281 objectName: "hideDragHandle"
282 anchors.fill: handle
283 direction: Direction.Upwards
284 enabled: root.shown && root.available && !hideAnimation.running && !showAnimation.running
285 hintDisplacement: units.gu(3)
286 autoCompleteDragThreshold: maxTotalDragDistance / 6
287 stretch: true
288 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
289
290 onTouchPositionChanged: {
291 if (root.state === "locked") {
292 d.xDisplacementSinceLock += (touchPosition.x - d.lastHideTouchX)
293 d.lastHideTouchX = touchPosition.x;
294 }
295 }
296 }
297
298 PanelVelocityCalculator {
299 id: yVelocityCalculator
300 velocityThreshold: d.hasCommitted ? 0.1 : 0.3
301 trackedValue: d.activeDragHandle ?
302 (Direction.isPositive(d.activeDragHandle.direction) ?
303 d.activeDragHandle.distance :
304 -d.activeDragHandle.distance)
305 : 0
306
307 onVelocityAboveThresholdChanged: d.updateState()
308 }
309
310 Connections {
311 target: showAnimation
312 function onRunningChanged() {
313 if (showAnimation.running) {
314 root.state = "commit";
315 }
316 }
317 }
318
319 Connections {
320 target: hideAnimation
321 function onRunningChanged() {
322 if (hideAnimation.running) {
323 root.state = "initial";
324 }
325 }
326 }
327
328 QtObject {
329 id: d
330 property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
331 property bool hasCommitted: false
332 property real lastHideTouchX: 0
333 property real xDisplacementSinceLock: 0
334 onXDisplacementSinceLockChanged: d.updateState()
335
336 property real rowMappedLateralPosition: {
337 if (!d.activeDragHandle) return -1;
338 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchPosition.x, 0).x;
339 }
340
341 function updateState() {
342 if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
343 if (unitProgress <= 0) {
344 root.state = "initial";
345 // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
346 } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
347 root.state = "locked";
348 } else {
349 root.state = "reveal";
350 }
351 }
352 }
353 }
354
355 states: [
356 State {
357 name: "initial"
358 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
359 },
360 State {
361 name: "reveal"
362 StateChangeScript {
363 script: {
364 yVelocityCalculator.reset();
365 // initial item selection
366 if (!d.hasCommitted) bar.selectItemAt(d.rowMappedLateralPosition);
367 d.hasCommitted = false;
368 }
369 }
370 PropertyChanges {
371 target: bar
372 expanded: true
373 // changes to lateral touch position effect which indicator is selected
374 lateralPosition: d.rowMappedLateralPosition
375 // vertical velocity determines if changes in lateral position has an effect
376 enableLateralChanges: d.activeDragHandle &&
377 !yVelocityCalculator.velocityAboveThreshold
378 }
379 // left scroll bar handling
380 PropertyChanges {
381 target: leftScroller
382 lateralPosition: {
383 if (!d.activeDragHandle) return -1;
384 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchPosition.x, 0);
385 return mapped.x;
386 }
387 }
388 // right scroll bar handling
389 PropertyChanges {
390 target: rightScroller
391 lateralPosition: {
392 if (!d.activeDragHandle) return -1;
393 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchPosition.x, 0);
394 return mapped.x;
395 }
396 }
397 },
398 State {
399 name: "locked"
400 StateChangeScript {
401 script: {
402 d.xDisplacementSinceLock = 0;
403 d.lastHideTouchX = hideDragHandle.touchPosition.x;
404 }
405 }
406 PropertyChanges { target: bar; expanded: true }
407 },
408 State {
409 name: "commit"
410 extend: "locked"
411 PropertyChanges { target: root; focus: true }
412 PropertyChanges { target: bar; interactive: true }
413 PropertyChanges {
414 target: d;
415 hasCommitted: true
416 lastHideTouchX: 0
417 xDisplacementSinceLock: 0
418 restoreEntryValues: false
419 }
420 }
421 ]
422 state: "initial"
423}