~mterry/+junk/u8.2

« back to all changes in this revision

Viewing changes to qml/Components/ScrollCalculator.qml

  • Committer: Michael Terry
  • Date: 2014-11-17 14:56:04 UTC
  • mfrom: (1317.1.118 unity8)
  • Revision ID: michael.terry@canonical.com-20141117145604-96dn9p5nwkifq2f4
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.2
 
18
import Ubuntu.Components 1.1
 
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
}