on a142f21 help sign in
unity8/qml/Components/ScrollCalculator.qml
81 lines 2.7 kB history raw
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 */
17import QtQuick 2.2
18import Ubuntu.Components 1.1
20Item {
21 id: scrollArea
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
31 signal scroll(real scrollAmount)
33 width: units.gu(5)
34 rotation: direction === Qt.LeftToRight ? 0 : 180
36 onAreaActiveChanged: areaActive ? handleEnter() : handleExit()
38 function handleEnter() {
39 d.thresholdAreaX = -scrollArea.stopScrollThreshold;
40 scrollTimer.restart();
41 }
43 function handleExit() {
44 d.thresholdAreaX = -scrollArea.stopScrollThreshold;
45 scrollTimer.stop();
46 }
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 }
61 d.progression = lateralPosition / width;
62 }
63 }
65 Timer {
66 id: scrollTimer
67 interval: 16
68 repeat: true
70 onTriggered: {
71 var scrollAmount = scrollArea.baseScrollAmount + scrollArea.maximumScrollAmount * d.progression;
72 scrollArea.scroll(scrollAmount);
73 }
74 }
76 QtObject {
77 id: d
78 property real progression: 0
79 property real thresholdAreaX: -scrollArea.stopScrollThreshold
80 }