Unity 8
IndicatorsMenu.qml
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import Ubuntu.Gestures 0.1
20 import "../Components"
21 import "Indicators"
22 
23 Showable {
24  id: root
25  property alias indicatorsModel: bar.indicatorsModel
26  property alias showDragHandle: __showDragHandle
27  property alias hideDragHandle: __hideDragHandle
28  property alias overFlowWidth: bar.overFlowWidth
29  property alias verticalVelocityThreshold: yVelocityCalculator.velocityThreshold
30  property alias currentIndicator: bar.currentIndicator
31  property int minimizedPanelHeight: units.gu(3)
32  property int expandedPanelHeight: units.gu(7)
33  property real openedHeight: units.gu(71)
34  readonly property real unitProgress: Math.max(0, (height - minimizedPanelHeight) / (openedHeight - minimizedPanelHeight))
35  readonly property bool fullyOpened: unitProgress >= 1
36  readonly property bool partiallyOpened: unitProgress > 0 && unitProgress < 1.0
37  readonly property bool fullyClosed: unitProgress == 0
38  property bool enableHint: true
39  property bool showOnClick: true
40  property color panelColor: theme.palette.normal.background
41 
42  signal showTapped(point position)
43 
44  // TODO: Perhaps we need a animation standard for showing/hiding? Each showable seems to
45  // use its own values. Need to ask design about this.
46  showAnimation: StandardAnimation {
47  property: "height"
48  to: openedHeight
49  duration: UbuntuAnimation.BriskDuration
50  easing.type: Easing.OutCubic
51  }
52 
53  hideAnimation: StandardAnimation {
54  property: "height"
55  to: minimizedPanelHeight
56  duration: UbuntuAnimation.BriskDuration
57  easing.type: Easing.OutCubic
58  }
59 
60  height: minimizedPanelHeight
61 
62  onUnitProgressChanged: d.updateState()
63  clip: root.partiallyOpened
64 
65  IndicatorsLight {
66  id: indicatorLights
67  }
68 
69  // eater
70  MouseArea {
71  anchors.fill: parent
72  hoverEnabled: true
73  }
74 
75  MenuContent {
76  id: content
77  objectName: "menuContent"
78 
79  anchors {
80  left: parent.left
81  right: parent.right
82  top: bar.bottom
83  }
84  height: openedHeight - bar.height - handle.height
85  indicatorsModel: root.indicatorsModel
86  visible: root.unitProgress > 0
87  currentMenuIndex: bar.currentItemIndex
88  }
89 
90  Handle {
91  id: handle
92  objectName: "handle"
93  anchors {
94  left: parent.left
95  right: parent.right
96  bottom: parent.bottom
97  }
98  height: units.gu(2)
99  active: d.activeDragHandle ? true : false
100 
101  //small shadow gradient at bottom of menu
102  Rectangle {
103  anchors {
104  left: parent.left
105  right: parent.right
106  bottom: parent.top
107  }
108  height: units.gu(0.5)
109  gradient: Gradient {
110  GradientStop { position: 0.0; color: "transparent" }
111  GradientStop { position: 1.0; color: theme.palette.normal.background }
112  }
113  opacity: 0.3
114  }
115  }
116 
117  Rectangle {
118  anchors.fill: bar
119  color: panelColor
120  }
121 
122  IndicatorsBar {
123  id: bar
124  objectName: "indicatorsBar"
125 
126  anchors {
127  left: parent.left
128  right: parent.right
129  }
130  expanded: false
131  enableLateralChanges: false
132  lateralPosition: -1
133  unitProgress: root.unitProgress
134 
135  height: expanded ? expandedPanelHeight : minimizedPanelHeight
136  Behavior on height { NumberAnimation { duration: UbuntuAnimation.SnapDuration; easing: UbuntuAnimation.StandardEasing } }
137  }
138 
139  ScrollCalculator {
140  id: leftScroller
141  width: units.gu(5)
142  anchors.left: bar.left
143  height: bar.height
144 
145  forceScrollingPercentage: 0.33
146  stopScrollThreshold: units.gu(0.75)
147  direction: Qt.RightToLeft
148  lateralPosition: -1
149 
150  onScroll: bar.addScrollOffset(-scrollAmount);
151  }
152 
153  ScrollCalculator {
154  id: rightScroller
155  width: units.gu(5)
156  anchors.right: bar.right
157  height: bar.height
158 
159  forceScrollingPercentage: 0.33
160  stopScrollThreshold: units.gu(0.75)
161  direction: Qt.LeftToRight
162  lateralPosition: -1
163 
164  onScroll: bar.addScrollOffset(scrollAmount);
165  }
166 
167  MouseArea {
168  anchors.bottom: parent.bottom
169  anchors.left: parent.left
170  anchors.right: parent.right
171  height: minimizedPanelHeight
172  enabled: __showDragHandle.enabled && showOnClick
173  onClicked: {
174  bar.selectItemAt(mouseX)
175  root.show()
176  }
177  }
178 
179  DragHandle {
180  id: __showDragHandle
181  objectName: "showDragHandle"
182  anchors.bottom: parent.bottom
183  anchors.left: parent.left
184  anchors.right: parent.right
185  height: minimizedPanelHeight
186  direction: Direction.Downwards
187  enabled: !root.shown && root.available
188  autoCompleteDragThreshold: maxTotalDragDistance / 2
189  stretch: true
190 
191  onPressedChanged: {
192  if (pressed) {
193  touchPressTime = new Date().getTime();
194  } else {
195  var touchReleaseTime = new Date().getTime();
196  if (touchReleaseTime - touchPressTime <= 300) {
197  root.showTapped(Qt.point(touchSceneX, touchSceneY));
198  }
199  }
200  }
201  property var touchPressTime
202 
203  // using hint regulates minimum to hint displacement, but in fullscreen mode, we need to do it manually.
204  overrideStartValue: enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height
205  maxTotalDragDistance: openedHeight - (enableHint ? minimizedPanelHeight : expandedPanelHeight + handle.height)
206  hintDisplacement: enableHint ? expandedPanelHeight - minimizedPanelHeight + handle.height : 0
207  }
208 
209  MouseArea {
210  anchors.fill: __hideDragHandle
211  enabled: __hideDragHandle.enabled
212  onClicked: root.hide()
213  }
214 
215  DragHandle {
216  id: __hideDragHandle
217  objectName: "hideDragHandle"
218  anchors.fill: handle
219  direction: Direction.Upwards
220  enabled: root.shown && root.available
221  hintDisplacement: units.gu(3)
222  autoCompleteDragThreshold: maxTotalDragDistance / 6
223  stretch: true
224  maxTotalDragDistance: openedHeight - expandedPanelHeight - handle.height
225 
226  onTouchSceneXChanged: {
227  if (root.state === "locked") {
228  d.xDisplacementSinceLock += (touchSceneX - d.lastHideTouchSceneX)
229  d.lastHideTouchSceneX = touchSceneX;
230  }
231  }
232  }
233 
234  PanelVelocityCalculator {
235  id: yVelocityCalculator
236  velocityThreshold: d.hasCommitted ? 0.1 : 0.3
237  trackedValue: d.activeDragHandle ? d.activeDragHandle.touchSceneY : 0
238 
239  onVelocityAboveThresholdChanged: d.updateState()
240  }
241 
242  Connections {
243  target: showAnimation
244  onRunningChanged: {
245  if (showAnimation.running) {
246  root.state = "commit";
247  }
248  }
249  }
250 
251  Connections {
252  target: hideAnimation
253  onRunningChanged: {
254  if (hideAnimation.running) {
255  root.state = "initial";
256  }
257  }
258  }
259 
260  QtObject {
261  id: d
262  property var activeDragHandle: showDragHandle.dragging ? showDragHandle : hideDragHandle.dragging ? hideDragHandle : null
263  property bool hasCommitted: false
264  property real lastHideTouchSceneX: 0
265  property real xDisplacementSinceLock: 0
266  onXDisplacementSinceLockChanged: d.updateState()
267 
268  property real rowMappedLateralPosition: {
269  if (!d.activeDragHandle) return -1;
270  return d.activeDragHandle.mapToItem(bar, d.activeDragHandle.touchX, 0).x;
271  }
272 
273  function updateState() {
274  if (!showAnimation.running && !hideAnimation.running && d.activeDragHandle) {
275  if (unitProgress <= 0) {
276  root.state = "initial";
277  // lock indicator if we've been committed and aren't moving too much laterally or too fast up.
278  } else if (d.hasCommitted && (Math.abs(d.xDisplacementSinceLock) < units.gu(2) || yVelocityCalculator.velocityAboveThreshold)) {
279  root.state = "locked";
280  } else {
281  root.state = "reveal";
282  }
283  }
284  }
285  }
286 
287  states: [
288  State {
289  name: "initial"
290  PropertyChanges { target: d; hasCommitted: false; restoreEntryValues: false }
291  },
292  State {
293  name: "reveal"
294  StateChangeScript {
295  script: {
296  yVelocityCalculator.reset();
297  // initial item selection
298  if (!d.hasCommitted) bar.selectItemAt(d.activeDragHandle ? d.activeDragHandle.touchX : -1);
299  d.hasCommitted = false;
300  }
301  }
302  PropertyChanges {
303  target: bar
304  expanded: true
305  // changes to lateral touch position effect which indicator is selected
306  lateralPosition: d.rowMappedLateralPosition
307  // vertical velocity determines if changes in lateral position has an effect
308  enableLateralChanges: d.activeDragHandle &&
309  !yVelocityCalculator.velocityAboveThreshold
310  }
311  // left scroll bar handling
312  PropertyChanges {
313  target: leftScroller
314  lateralPosition: {
315  if (!d.activeDragHandle) return -1;
316  var mapped = d.activeDragHandle.mapToItem(leftScroller, d.activeDragHandle.touchX, 0);
317  return mapped.x;
318  }
319  }
320  // right scroll bar handling
321  PropertyChanges {
322  target: rightScroller
323  lateralPosition: {
324  if (!d.activeDragHandle) return -1;
325  var mapped = d.activeDragHandle.mapToItem(rightScroller, d.activeDragHandle.touchX, 0);
326  return mapped.x;
327  }
328  }
329  },
330  State {
331  name: "locked"
332  StateChangeScript {
333  script: {
334  d.xDisplacementSinceLock = 0;
335  d.lastHideTouchSceneX = hideDragHandle.touchSceneX;
336  }
337  }
338  PropertyChanges { target: bar; expanded: true }
339  },
340  State {
341  name: "commit"
342  extend: "locked"
343  PropertyChanges { target: bar; interactive: true }
344  PropertyChanges {
345  target: d;
346  hasCommitted: true
347  lastHideTouchSceneX: 0
348  xDisplacementSinceLock: 0
349  restoreEntryValues: false
350  }
351  }
352  ]
353  state: "initial"
354 }