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
26 property alias model: bar.model
27 property alias showDragHandle: __showDragHandle
28 property alias hideDragHandle: __hideDragHandle
29 property alias overFlowWidth: bar.overFlowWidth
30 property alias verticalVelocityThreshold: yVelocityCalculator.velocityThreshold
31 property int minimizedPanelHeight: units.gu(3)
32 property int expandedPanelHeight: units.gu(7)
33 property real openedHeight: units.gu(71)
34 property bool enableHint: true
35 property bool showOnClick: true
36 property color panelColor: theme.palette.normal.background
37 property real menuContentX: 0
39 property alias alignment: bar.alignment
40 property alias hideRow: bar.hideRow
41 property alias rowItemDelegate: bar.rowItemDelegate
42 property alias pageDelegate: content.pageDelegate
44 readonly property real unitProgress: Math.max(0, (height - minimizedPanelHeight) / (openedHeight - minimizedPanelHeight))
45 readonly property bool fullyOpened: unitProgress >= 1
46 readonly property bool partiallyOpened: unitProgress > 0 && unitProgress < 1.0
47 readonly property bool fullyClosed: unitProgress == 0
48 readonly property alias expanded: bar.expanded
49 readonly property int barWidth: bar.width
50 readonly property alias currentMenuIndex: bar.currentItemIndex
52 // Exposes the current contentX of the PanelBar's internal ListView. This
53 // must be used to offset absolute x values against the ListView, since
54 // we commonly add or remove elements and cause the contentX to change.
55 readonly property int rowContentX: bar.rowContentX
57 // The user tapped the panel and did not move.
58 // Note that this does not fire on mouse events, only touch events.
61 // TODO: Perhaps we need a animation standard for showing/hiding? Each showable seems to
62 // use its own values. Need to ask design about this.
63 showAnimation: SequentialAnimation {
68 duration: LomiriAnimation.BriskDuration
69 easing.type: Easing.OutCubic
71 // set binding in case units.gu changes while menu open, so height correctly adjusted to fit
72 ScriptAction { script: root.height = Qt.binding( function(){ return root.openedHeight; } ) }
75 hideAnimation: SequentialAnimation {
79 to: minimizedPanelHeight
80 duration: LomiriAnimation.BriskDuration
81 easing.type: Easing.OutCubic
83 // set binding in case units.gu changes while menu closed, so menu adjusts to fit
84 ScriptAction { script: root.height = Qt.binding( function(){ return root.minimizedPanelHeight; } ) }
88 height: minimizedPanelHeight
90 onUnitProgressChanged: d.updateState()
99 clip: root.partiallyOpened
103 anchors.fill: content
105 acceptedButtons: Qt.AllButtons
106 onWheel: wheel.accepted = true;
107 enabled: root.state != "initial"
108 visible: content.visible
113 objectName: "menuContent"
120 height: openedHeight - bar.height - handle.height
122 visible: root.unitProgress > 0
123 currentMenuIndex: bar.currentItemIndex
133 bottom: parent.bottom
136 active: d.activeDragHandle ? true : false
137 visible: !root.fullyClosed
139 //small shadow gradient at bottom of menu
146 height: units.gu(0.5)
148 GradientStop { position: 0.0; color: "transparent" }
149 GradientStop { position: 1.0; color: theme.palette.normal.background }
158 visible: !root.fullyClosed
162 if (event.key === Qt.Key_Left) {
163 bar.selectPreviousItem();
164 event.accepted = true;
165 } else if (event.key === Qt.Key_Right) {
166 bar.selectNextItem();
167 event.accepted = true;
168 } else if (event.key === Qt.Key_Escape) {
170 event.accepted = true;
176 objectName: "indicatorsBar"
183 enableLateralChanges: false
185 unitProgress: root.unitProgress
187 height: expanded ? expandedPanelHeight : minimizedPanelHeight
188 Behavior on height { NumberAnimation { duration: LomiriAnimation.SnapDuration; easing: LomiriAnimation.StandardEasing } }
194 anchors.left: bar.left
197 forceScrollingPercentage: 0.33
198 stopScrollThreshold: units.gu(0.75)
199 direction: Qt.RightToLeft
202 onScroll: bar.addScrollOffset(-scrollAmount);
208 anchors.right: bar.right
211 forceScrollingPercentage: 0.33
212 stopScrollThreshold: units.gu(0.75)
213 direction: Qt.LeftToRight
216 onScroll: bar.addScrollOffset(scrollAmount);
220 anchors.bottom: parent.bottom
221 anchors.left: alignment == Qt.AlignLeft ? parent.left : __showDragHandle.left
222 anchors.right: alignment == Qt.AlignRight ? parent.right : __showDragHandle.right
223 height: minimizedPanelHeight
224 enabled: __showDragHandle.enabled && showOnClick
226 var barPosition = mapToItem(bar, mouseX, mouseY);
227 bar.selectItemAt(barPosition.x)
234 objectName: "showDragHandle"
235 anchors.bottom: parent.bottom
236 anchors.left: alignment == Qt.AlignLeft ? parent.left : undefined
237 anchors.leftMargin: -root.menuContentX
238 anchors.right: alignment == Qt.AlignRight ? parent.right : undefined
239 width: root.overFlowWidth + root.menuContentX
240 height: minimizedPanelHeight
241 direction: Direction.Downwards
242 enabled: !root.shown && root.available && !hideAnimation.running && !showAnimation.running
243 autoCompleteDragThreshold: maxTotalDragDistance / 2
248 touchPressTime = new Date().getTime();
250 var touchReleaseTime = new Date().getTime();
251 if (touchReleaseTime - touchPressTime <= 300 && distance < units.gu(1)) {
256 property var touchPressTime
258 // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
259 overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
260 maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
261 hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
265 anchors.fill: __hideDragHandle
266 enabled: __hideDragHandle.enabled
267 onClicked: root.hide()
272 objectName: "hideDragHandle"
274 direction: Direction.Upwards
275 enabled: root.shown && root.available && !hideAnimation.running && !showAnimation.running
276 hintDisplacement: units.gu(3)
277 autoCompleteDragThreshold: maxTotalDragDistance / 6
279 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
281 onTouchPositionChanged: {
282 if (root.state === "locked") {
283 d.xDisplacementSinceLock += (touchPosition.x - d.lastHideTouchX)
284 d.lastHideTouchX = touchPosition.x;
289 PanelVelocityCalculator {
290 id: yVelocityCalculator
291 velocityThreshold: d.hasCommitted ? 0.1 : 0.3
292 trackedValue: d.activeDragHandle ?
293 (Direction.isPositive(d.activeDragHandle.direction) ?
294 d.activeDragHandle.distance :
295 -d.activeDragHandle.distance)
298 onVelocityAboveThresholdChanged: d.updateState()
302 target: showAnimation
304 if (showAnimation.running) {
305 root.state = "commit";
311 target: hideAnimation
313 if (hideAnimation.running) {
314 root.state = "initial";
321 property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
322 property bool hasCommitted: false
323 property real lastHideTouchX: 0
324 property real xDisplacementSinceLock: 0
325 onXDisplacementSinceLockChanged: d.updateState()
327 property real rowMappedLateralPosition: {
328 if (!d.activeDragHandle) return -1;
329 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchPosition.x, 0).x;
332 function updateState() {
333 if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
334 if (unitProgress <= 0) {
335 root.state = "initial";
336 // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
337 } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
338 root.state = "locked";
340 root.state = "reveal";
349 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
355 yVelocityCalculator.reset();
356 // initial item selection
357 if (!d.hasCommitted) bar.selectItemAt(d.rowMappedLateralPosition);
358 d.hasCommitted = false;
364 // changes to lateral touch position effect which indicator is selected
365 lateralPosition: d.rowMappedLateralPosition
366 // vertical velocity determines if changes in lateral position has an effect
367 enableLateralChanges: d.activeDragHandle &&
368 !yVelocityCalculator.velocityAboveThreshold
370 // left scroll bar handling
374 if (!d.activeDragHandle) return -1;
375 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchPosition.x, 0);
379 // right scroll bar handling
381 target: rightScroller
383 if (!d.activeDragHandle) return -1;
384 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchPosition.x, 0);
393 d.xDisplacementSinceLock = 0;
394 d.lastHideTouchX = hideDragHandle.touchPosition.x;
397 PropertyChanges { target: bar; expanded: true }
402 PropertyChanges { target: root; focus: true }
403 PropertyChanges { target: bar; interactive: true }
408 xDisplacementSinceLock: 0
409 restoreEntryValues: false