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