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 property var blurSource : null
45 property rect blurRect : Qt.rect(0, 0, 0, 0)
47 readonly property real unitProgress: Math.max(0, (height - minimizedPanelHeight) / (openedHeight - minimizedPanelHeight))
48 readonly property bool fullyOpened: unitProgress >= 1
49 readonly property bool partiallyOpened: unitProgress > 0 && unitProgress < 1.0
50 readonly property bool fullyClosed: unitProgress == 0
51 readonly property alias expanded: bar.expanded
52 readonly property int barWidth: bar.width
53 readonly property alias currentMenuIndex: bar.currentItemIndex
55 // Exposes the current contentX of the PanelBar's internal ListView. This
56 // must be used to offset absolute x values against the ListView, since
57 // we commonly add or remove elements and cause the contentX to change.
58 readonly property int rowContentX: bar.rowContentX
60 // The user tapped the panel and did not move.
61 // Note that this does not fire on mouse events, only touch events.
64 // TODO: Perhaps we need a animation standard for showing/hiding? Each showable seems to
65 // use its own values. Need to ask design about this.
66 showAnimation: SequentialAnimation {
71 duration: LomiriAnimation.BriskDuration
72 easing.type: Easing.OutCubic
74 // set binding in case units.gu changes while menu open, so height correctly adjusted to fit
75 ScriptAction { script: root.height = Qt.binding( function(){ return root.openedHeight; } ) }
78 hideAnimation: SequentialAnimation {
82 to: minimizedPanelHeight
83 duration: LomiriAnimation.BriskDuration
84 easing.type: Easing.OutCubic
86 // set binding in case units.gu changes while menu closed, so menu adjusts to fit
87 ScriptAction { script: root.height = Qt.binding( function(){ return root.minimizedPanelHeight; } ) }
91 height: minimizedPanelHeight
93 onUnitProgressChanged: d.updateState()
98 width: root.blurRect.width
99 height: root.blurRect.height
100 visible: root.height > root.minimizedPanelHeight
101 sourceItem: root.blurSource
102 blurRect: root.blurRect
111 bottom: parent.bottom
113 clip: root.partiallyOpened
122 anchors.fill: content
124 acceptedButtons: Qt.AllButtons
125 onWheel: wheel.accepted = true;
126 enabled: root.state != "initial"
127 visible: content.visible
132 objectName: "menuContent"
139 height: openedHeight - bar.height - handle.height
141 visible: root.unitProgress > 0
142 currentMenuIndex: bar.currentItemIndex
152 bottom: parent.bottom
155 active: d.activeDragHandle ? true : false
156 visible: !root.fullyClosed
162 visible: !root.fullyClosed
166 if (event.key === Qt.Key_Left) {
167 bar.selectPreviousItem();
168 event.accepted = true;
169 } else if (event.key === Qt.Key_Right) {
170 bar.selectNextItem();
171 event.accepted = true;
172 } else if (event.key === Qt.Key_Escape) {
174 event.accepted = true;
180 objectName: "indicatorsBar"
187 enableLateralChanges: false
189 unitProgress: root.unitProgress
191 height: expanded ? expandedPanelHeight : minimizedPanelHeight
192 Behavior on height { NumberAnimation { duration: LomiriAnimation.SnapDuration; easing: LomiriAnimation.StandardEasing } }
198 anchors.left: bar.left
201 forceScrollingPercentage: 0.33
202 stopScrollThreshold: units.gu(0.75)
203 direction: Qt.RightToLeft
206 onScroll: bar.addScrollOffset(-scrollAmount);
212 anchors.right: bar.right
215 forceScrollingPercentage: 0.33
216 stopScrollThreshold: units.gu(0.75)
217 direction: Qt.LeftToRight
220 onScroll: bar.addScrollOffset(scrollAmount);
224 anchors.bottom: parent.bottom
225 anchors.left: alignment == Qt.AlignLeft ? parent.left : __showDragHandle.left
226 anchors.right: alignment == Qt.AlignRight ? parent.right : __showDragHandle.right
227 height: minimizedPanelHeight
228 enabled: __showDragHandle.enabled && showOnClick
230 var barPosition = mapToItem(bar, mouseX, mouseY);
231 bar.selectItemAt(barPosition.x)
238 objectName: "showDragHandle"
239 anchors.bottom: parent.bottom
240 anchors.left: alignment == Qt.AlignLeft ? parent.left : undefined
241 anchors.leftMargin: -root.menuContentX
242 anchors.right: alignment == Qt.AlignRight ? parent.right : undefined
243 width: root.overFlowWidth + root.menuContentX
244 height: minimizedPanelHeight
245 direction: Direction.Downwards
246 enabled: !root.shown && root.available && !hideAnimation.running && !showAnimation.running
247 autoCompleteDragThreshold: maxTotalDragDistance / 2
252 touchPressTime = new Date().getTime();
254 var touchReleaseTime = new Date().getTime();
255 if (touchReleaseTime - touchPressTime <= 300 && distance < units.gu(1)) {
260 property var touchPressTime
262 // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
263 overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
264 maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
265 hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
269 anchors.fill: __hideDragHandle
270 enabled: __hideDragHandle.enabled
271 onClicked: root.hide()
276 objectName: "hideDragHandle"
278 direction: Direction.Upwards
279 enabled: root.shown && root.available && !hideAnimation.running && !showAnimation.running
280 hintDisplacement: units.gu(3)
281 autoCompleteDragThreshold: maxTotalDragDistance / 6
283 maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
285 onTouchPositionChanged: {
286 if (root.state === "locked") {
287 d.xDisplacementSinceLock += (touchPosition.x - d.lastHideTouchX)
288 d.lastHideTouchX = touchPosition.x;
293 PanelVelocityCalculator {
294 id: yVelocityCalculator
295 velocityThreshold: d.hasCommitted ? 0.1 : 0.3
296 trackedValue: d.activeDragHandle ?
297 (Direction.isPositive(d.activeDragHandle.direction) ?
298 d.activeDragHandle.distance :
299 -d.activeDragHandle.distance)
302 onVelocityAboveThresholdChanged: d.updateState()
306 target: showAnimation
308 if (showAnimation.running) {
309 root.state = "commit";
315 target: hideAnimation
317 if (hideAnimation.running) {
318 root.state = "initial";
325 property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
326 property bool hasCommitted: false
327 property real lastHideTouchX: 0
328 property real xDisplacementSinceLock: 0
329 onXDisplacementSinceLockChanged: d.updateState()
331 property real rowMappedLateralPosition: {
332 if (!d.activeDragHandle) return -1;
333 return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchPosition.x, 0).x;
336 function updateState() {
337 if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
338 if (unitProgress <= 0) {
339 root.state = "initial";
340 // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
341 } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
342 root.state = "locked";
344 root.state = "reveal";
353 PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
359 yVelocityCalculator.reset();
360 // initial item selection
361 if (!d.hasCommitted) bar.selectItemAt(d.rowMappedLateralPosition);
362 d.hasCommitted = false;
368 // changes to lateral touch position effect which indicator is selected
369 lateralPosition: d.rowMappedLateralPosition
370 // vertical velocity determines if changes in lateral position has an effect
371 enableLateralChanges: d.activeDragHandle &&
372 !yVelocityCalculator.velocityAboveThreshold
374 // left scroll bar handling
378 if (!d.activeDragHandle) return -1;
379 var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchPosition.x, 0);
383 // right scroll bar handling
385 target: rightScroller
387 if (!d.activeDragHandle) return -1;
388 var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchPosition.x, 0);
397 d.xDisplacementSinceLock = 0;
398 d.lastHideTouchX = hideDragHandle.touchPosition.x;
401 PropertyChanges { target: bar; expanded: true }
406 PropertyChanges { target: root; focus: true }
407 PropertyChanges { target: bar; interactive: true }
412 xDisplacementSinceLock: 0
413 restoreEntryValues: false