Unity 8
ScrollCalculator.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 
20 Item {
21  id: scrollArea
22 
23  readonly property bool areaActive: lateralPosition >= 0
24  property real stopScrollThreshold: units.gu(2)
25  property int direction: Qt.LeftToRight
26  property real baseScrollAmount: units.dp(3)
27  property real maximumScrollAmount: units.dp(8)
28  property real lateralPosition: -1
29  property real forceScrollingPercentage: 0.4
30 
31  signal scroll(real scrollAmount)
32 
33  width: units.gu(5)
34  rotation: direction === Qt.LeftToRight ? 0 : 180
35 
36  onAreaActiveChanged: areaActive ? handleEnter() : handleExit()
37 
38  function handleEnter() {
39  d.thresholdAreaX = -scrollArea.stopScrollThreshold;
40  scrollTimer.restart();
41  }
42 
43  function handleExit() {
44  d.thresholdAreaX = -scrollArea.stopScrollThreshold;
45  scrollTimer.stop();
46  }
47 
48  onLateralPositionChanged: {
49  if (scrollArea.areaActive) {
50  if (lateralPosition > width * (1 - forceScrollingPercentage)) {
51  d.thresholdAreaX = width * (1 - forceScrollingPercentage);
52  if (!scrollTimer.running) scrollTimer.restart();
53  } else if (lateralPosition > d.thresholdAreaX + scrollArea.stopScrollThreshold) {
54  d.thresholdAreaX = lateralPosition - scrollArea.stopScrollThreshold;
55  if (!scrollTimer.running) scrollTimer.restart();
56  } else if (lateralPosition < d.thresholdAreaX) {
57  d.thresholdAreaX = lateralPosition;
58  scrollTimer.stop();
59  }
60 
61  d.progression = lateralPosition / width;
62  }
63  }
64 
65  Timer {
66  id: scrollTimer
67  interval: 16
68  repeat: true
69 
70  onTriggered: {
71  var scrollAmount = scrollArea.baseScrollAmount + scrollArea.maximumScrollAmount * d.progression;
72  scrollArea.scroll(scrollAmount);
73  }
74  }
75 
76  QtObject {
77  id: d
78  property real progression: 0
79  property real thresholdAreaX: -scrollArea.stopScrollThreshold
80  }
81 }