Debian Codegraph @

src/app/qml/components/FastScroll.qml

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** Copyright (C) 2014, 2015 Canonical Ltd.
5 ** All rights reserved.
6 ** Contact: Nokia Corporation ([email protected])
7 **
8 ** This file is part of the Qt Components project.
9 **
10 ** $QT_BEGIN_LICENSE:BSD$
11 ** You may use this file under the terms of the BSD license as follows:
12 **
13 ** "Redistribution and use in source and binary forms, with or without
14 ** modification, are permitted provided that the following conditions are
15 ** met:
16 ** * Redistributions of source code must retain the above copyright
17 ** notice, this list of conditions and the following disclaimer.
18 ** * Redistributions in binary form must reproduce the above copyright
19 ** notice, this list of conditions and the following disclaimer in
20 ** the documentation and/or other materials provided with the
21 ** distribution.
22 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
23 ** the names of its contributors may be used to endorse or promote
24 ** products derived from this software without specific prior written
25 ** permission.
26 **
27 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 // FastScroll.qml
43 import QtQuick 2.12
44 import Lomiri.Components 1.3
45 import "FastScroll.js" as Sections
46
47 Item {
48 id: root
49
50 property ListView listView
51 property int pinSize: units.gu(2)
52
53 readonly property var letters: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
54 readonly property alias fastScrolling: internal.fastScrolling
55 readonly property bool showing: (rail.opacity !== 0.0)
56 readonly property double minimumHeight: rail.height
57
58 width: units.gu(7)
59 height: rail.height
60
61 onListViewChanged: {
62 if (listView && listView.model) {
63 internal.initDirtyObserver();
64 } else if (listView) {
65 listView.modelChanged.connect(function() {
66 if (listView.model) {
67 internal.initDirtyObserver();
68 }
69 });
70 }
71 }
72
73 Connections {
74 target: listView
75 onCurrentIndexChanged: {
76 if (currentIndex != -1) {
77 rail.opacity = 0.0
78 }
79 }
80 }
81
82 Rectangle {
83 id: magnified
84
85 color: Theme.palette.normal.overlay
86 radius: height * 0.3
87 height: pinSize * 2
88 width: height
89 opacity: internal.fastScrolling && root.enabled ? 1.0 : 0.0
90 x: -cursor.width - units.gu(3)
91 y: {
92 if (internal.currentItem) {
93 var itemCenterY = rail.y + internal.currentItem.y + (internal.currentItem.height / 2)
94 return (itemCenterY - (magnified.height / 2))
95 } else {
96 return 0
97 }
98 }
99
100 Label {
101 anchors.fill: parent
102 horizontalAlignment: Text.AlignHCenter
103 verticalAlignment: Text.AlignVCenter
104 text: internal.desireSection
105 fontSize: "small"
106 }
107
108 Behavior on opacity {
109 LomiriNumberAnimation {}
110 }
111 }
112
113 Rectangle {
114 id: cursor
115
116 property bool showLabel: false
117 property string currentSectionName: ""
118
119 radius: pinSize * 0.3
120 height: pinSize
121 width: height
122 color: LomiriColors.orange
123 opacity: rail.opacity
124 x: rail.x
125 y: {
126 if (internal.currentItem) {
127 var itemCenterY = rail.y + internal.currentItem.y + (internal.currentItem.height / 2)
128 return (itemCenterY - (cursor.height / 2))
129 } else {
130 return 0
131 }
132 }
133 Behavior on y {
134 enabled: !internal.fastScrolling
135 LomiriNumberAnimation { }
136 }
137 }
138
139 Column {
140 id: rail
141
142 property bool isVisible: root.enabled &&
143 (listView.flicking || dragArea.pressed) &&
144 (listView.currentIndex == -1)
145 anchors {
146 right: parent.right
147 rightMargin: units.gu(2)
148 left: parent.left
149 leftMargin: units.gu(2)
150 top: parent.top
151 }
152 height: childrenRect.height
153 opacity: 0.0
154 onIsVisibleChanged: {
155 if (isVisible) {
156 rail.opacity = 1.0
157 hideTimer.stop()
158 } else if (!root.enabled) {
159 rail.opacity = 0.0
160 } else {
161 hideTimer.restart()
162 }
163 }
164
165 Behavior on opacity {
166 LomiriNumberAnimation { }
167 }
168
169 Repeater {
170 id: sectionsRepeater
171
172 model: root.letters
173 Label {
174 id: lbl
175
176 anchors.left: parent.left
177 height: pinSize
178 width: pinSize
179 verticalAlignment: Text.AlignVCenter
180 horizontalAlignment: Text.AlignHCenter
181 text: modelData
182 fontSize: "x-small"
183 color: cursor.y === y ? theme.palette.normal.foreground : theme.palette.normal.foregroundText
184 opacity: !internal.modelDirty && Sections.contains(text) ? 1.0 : 0.5
185 }
186 }
187
188 Timer {
189 id: hideTimer
190
191 running: false
192 interval: 2000
193 onTriggered: rail.opacity = 0.0
194 }
195 }
196
197 MouseArea {
198 id: dragArea
199
200 anchors {
201 left: parent.left
202 right: parent.right
203 }
204 y: rail.y
205 height: rail.height
206 visible: rail.opacity == 1.0
207
208 preventStealing: true
209 onPressed: {
210 internal.adjustContentPosition(mouseY)
211 dragginTimer.start()
212 }
213
214 onReleased: {
215 dragginTimer.stop()
216 internal.desireSection = ""
217 internal.fastScrolling = false
218 }
219
220 onPositionChanged: internal.adjustContentPosition(mouseY)
221
222 Timer {
223 id: dragginTimer
224
225 running: false
226 interval: 150
227 onTriggered: {
228 internal.fastScrolling = true
229 }
230 }
231 }
232
233 Timer {
234 id: dirtyTimer
235 interval: 500
236 running: false
237 onTriggered: {
238 Sections.initSectionData(listView);
239 internal.modelDirty = false;
240 }
241 }
242
243 Timer {
244 id: timerScroll
245
246 running: false
247 interval: 10
248 onTriggered: {
249 if (internal.desireSection != internal.currentSection) {
250 var idx = Sections.getIndexFor(internal.desireSection)
251 if (idx !== -1) {
252 listView.cancelFlick()
253 listView.positionViewAtIndex(idx, ListView.Beginning)
254 }
255 }
256 }
257 }
258
259 QtObject {
260 id: internal
261
262 property string currentSection: listView.currentSection
263 property string desireSection: ""
264 property string targetSection: fastScrolling ? desireSection : currentSection
265 property int oldY: 0
266 property bool modelDirty: false
267 property bool down: true
268 property bool fastScrolling: false
269 property var currentItem: null
270
271 onTargetSectionChanged: moveIndicator(targetSection)
272
273 function initDirtyObserver() {
274 Sections.initialize(listView);
275 function dirtyObserver() {
276 if (!internal.modelDirty) {
277 internal.modelDirty = true;
278 dirtyTimer.running = true;
279 }
280 }
281
282 if (listView.model.countChanged)
283 listView.model.countChanged.connect(dirtyObserver);
284
285 if (listView.model.itemsChanged)
286 listView.model.itemsChanged.connect(dirtyObserver);
287
288 if (listView.model.itemsInserted)
289 listView.model.itemsInserted.connect(dirtyObserver);
290
291 if (listView.model.itemsMoved)
292 listView.model.itemsMoved.connect(dirtyObserver);
293
294 if (listView.model.itemsRemoved)
295 listView.model.itemsRemoved.connect(dirtyObserver);
296 }
297
298 function adjustContentPosition(mouseY) {
299 var child = rail.childAt(rail.width / 2, mouseY)
300 if (!child || child.text === "") {
301 return
302 }
303 var section = child.text
304 if (internal.desireSection !== section) {
305 internal.desireSection = section
306 moveIndicator(section)
307 if (dragArea.pressed) {
308 timerScroll.restart()
309 }
310 }
311 }
312
313 function moveIndicator(section)
314 {
315 var index = root.letters.indexOf(section)
316 if (index != -1) {
317 currentItem = sectionsRepeater.itemAt(index)
318 }
319 }
320 }
321 }