~mterry/ubuntu-calculator-app/confined

« back to all changes in this revision

Viewing changes to app/ui/ScrollableView.qml

  • Committer: Tarmac
  • Author(s): Giulio Collura, Giulio Collura
  • Date: 2015-01-03 15:37:06 UTC
  • mfrom: (45.2.8 reboot-scrollable-view)
  • Revision ID: tarmac-20150103153706-c3kk14iqzo55hnnz
Fix CMakeLists when building click packages. ScrollableView to manage history, textentry and keyboard.

Approved by Ubuntu Phone Apps Jenkins Bot, Riccardo Padovani.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical Ltd
 
3
 *
 
4
 * This file is part of Ubuntu Calculator App
 
5
 *
 
6
 * Ubuntu Calculator App is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 3 as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * Ubuntu Calculator App is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
import QtQuick 2.3
 
19
 
 
20
Flickable {
 
21
    id: flickable
 
22
 
 
23
    default property alias data: column.children
 
24
    property double oldContentHeight: 0
 
25
    contentHeight: column.height
 
26
    boundsBehavior: Flickable.DragOverBounds
 
27
    property bool snap: true
 
28
 
 
29
    onMovementEnded: {
 
30
        if (contentY <= 0 || !snap) {
 
31
            return;
 
32
        }
 
33
        var posy = flickable.height + flickable.visibleArea.yPosition * flickable.contentHeight
 
34
        // FIXME see ubuntu-calculator-app:269:
 
35
        // It's column.width - units.gu(2) because of the weird alignment of TextField
 
36
        var obj = column.childAt(column.width - units.gu(2), posy)
 
37
        if (Math.abs(posy - obj.y) < obj.height / 2) {
 
38
            // scroll up
 
39
            var destY = obj.y - flickable.height;
 
40
            // don't go out of bound
 
41
            if (destY < 0) destY = 0;
 
42
            scrollingAnimation.to = destY;
 
43
        } else {
 
44
            // scroll down
 
45
            scrollingAnimation.to = obj.y + obj.height - flickable.height;
 
46
        }
 
47
        scrollingAnimation.start()
 
48
    }
 
49
 
 
50
    NumberAnimation on contentY {
 
51
        id: scrollingAnimation
 
52
        duration: 300
 
53
        easing.type: Easing.OutQuad
 
54
    }
 
55
 
 
56
    function scrollToBottom() {
 
57
        if (column.height > flickable.height) {
 
58
            flickable.contentY = flickable.contentHeight - flickable.height;
 
59
        }
 
60
    }
 
61
 
 
62
    Connections {
 
63
        target: column
 
64
        onHeightChanged: {
 
65
            // scroll to bottom only when something is inserted.
 
66
            if (oldContentHeight < contentHeight) {
 
67
                flickable.scrollToBottom();
 
68
            }
 
69
            oldContentHeight = contentHeight;
 
70
        }
 
71
    }
 
72
 
 
73
    Item {
 
74
        id: padding
 
75
        anchors {
 
76
            left: parent.left
 
77
            right: parent.right
 
78
        }
 
79
        height: flickable.height > column.height ? flickable.height - column.height : 0
 
80
        visible: height > 0
 
81
    }
 
82
 
 
83
    Column {
 
84
        id: column
 
85
        anchors {
 
86
            top: padding.bottom
 
87
            left: parent.left
 
88
            right: parent.right
 
89
        }
 
90
        spacing: 0
 
91
    }
 
92
}