2 * Copyright (C) 2014-2016 Canonical Ltd.
3 * Copyright (C) 2020 UBports Foundation
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.
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.
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/>.
19import Lomiri.Components 1.3
20import Lomiri.Gestures 0.1
21import "../../../Components"
22import "../../Indicators"
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
40 property alias alignment: bar.alignment
41 property alias hideRow: bar.hideRow
42 property alias rowItemDelegate: bar.rowItemDelegate
43 property alias pageDelegate: content.pageDelegate
45 property var blurSource : null
46 property rect blurRect : Qt.rect(0, 0, 0, 0)
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
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
61 // The user tapped the panel and did not move.
62 // Note that this does not fire on mouse events, only touch events.
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 {
72 duration: LomiriAnimation.BriskDuration
73 easing.type: Easing.OutCubic
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; } ) }
79 hideAnimation: SequentialAnimation {
83 to: minimizedPanelHeight
84 duration: LomiriAnimation.BriskDuration
85 easing.type: Easing.OutCubic
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; } ) }
92 height: minimizedPanelHeight
94 onUnitProgressChanged: d.updateState()
99 width: root.blurRect.width
100 height: root.blurRect.height
101 visible: root.height > root.minimizedPanelHeight
102 sourceItem: root.blurSource
103 blurRect: root.blurRect
112 bottom: parent.bottom
114 clip: root.partiallyOpened
117 color: Qt.rgba(root.panelColor.r,
127 anchors.fill: content
129 acceptedButtons: Qt.AllButtons
130 onWheel: wheel.accepted = true;
131 enabled: root.state != "initial"
132 visible: content.visible
137 objectName: "menuContent"
144 height: openedHeight - bar.height - handle.height
146 visible: root.unitProgress > 0
147 currentMenuIndex: bar.currentItemIndex
157 bottom: parent.bottom
160 active: d.activeDragHandle ? true : false
161 visible: !root.fullyClosed
167 visible: !root.fullyClosed
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) {
179 event.accepted = true;
185 objectName: "indicatorsBar"
192 enableLateralChanges: false
194 unitProgress: root.unitProgress
196 height: expanded ? expandedPanelHeight : minimizedPanelHeight
197 Behavior on height { NumberAnimation { duration: LomiriAnimation.SnapDuration; easing: LomiriAnimation.StandardEasing } }
203 anchors.left: bar.left
206 forceScrollingPercentage: 0.33
207 stopScrollThreshold: units.gu(0.75)
208 direction: Qt.RightToLeft
211 onScroll: bar.addScrollOffset(-scrollAmount);
217 anchors.right: bar.right
220 forceScrollingPercentage: 0.33
221 stopScrollThreshold: units.gu(0.75)
222 direction: Qt.LeftToRight
225 onScroll: bar.addScrollOffset(scrollAmount);
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
235 var barPosition = mapToItem(bar, mouseX, mouseY);
236 bar.selectItemAt(barPosition.x)
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
257 touchPressTime = new Date().getTime();
259 var touchReleaseTime = new Date().getTime();
260 if (touchReleaseTime - touchPressTime <= 300 && distance < units.gu(1)) {
265 property var touchPressTime
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
274 anchors.fill: __hideDragHandle
275 enabled: __hideDragHandle.enabled
276 onClicked: root.hide()
281 objectName: "hideDragHandle"
283 direction: Direction.Upwards
284 enabled: root.shown && root.available && !hideAnimation.running && !showAnimation.running
285 hintDisplacement: units.gu(3)
286 autoCompleteDragThreshold: maxTotalDragDistance / 6
288 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
290 onTouchPositionChanged: {
291 if (root.state === "locked") {
292 d.xDisplacementSinceLock += (touchPosition.x - d.lastHideTouchX)
293 d.lastHideTouchX = touchPosition.x;
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)
307 onVelocityAboveThresholdChanged: d.updateState()
311 target: showAnimation
312 function onRunningChanged() {
313 if (showAnimation.running) {
314 root.state = "commit";
320 target: hideAnimation
321 function onRunningChanged() {
322 if (hideAnimation.running) {
323 root.state = "initial";
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()
336 property real rowMappedLateralPosition: {
337 if (!d.activeDragHandle) return -1;
338 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchPosition.x, 0).x;
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";
349 root.state = "reveal";
358 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
364 yVelocityCalculator.reset();
365 // initial item selection
366 if (!d.hasCommitted) bar.selectItemAt(d.rowMappedLateralPosition);
367 d.hasCommitted = false;
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
379 // left scroll bar handling
383 if (!d.activeDragHandle) return -1;
384 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchPosition.x, 0);
388 // right scroll bar handling
390 target: rightScroller
392 if (!d.activeDragHandle) return -1;
393 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchPosition.x, 0);
402 d.xDisplacementSinceLock = 0;
403 d.lastHideTouchX = hideDragHandle.touchPosition.x;
406 PropertyChanges { target: bar; expanded: true }
411 PropertyChanges { target: root; focus: true }
412 PropertyChanges { target: bar; interactive: true }
417 xDisplacementSinceLock: 0
418 restoreEntryValues: false