Debian Codegraph @

app/ui/ScrollableView.qml

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