Debian Codegraph @

src/imports/Components/2.0/ScrollView.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components
19
20 /*!
21 \qmltype ScrollView
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \inherits StyledItem
25 \brief ScrollView is a scrollable view that features scrollbars and scrolling via keyboard keys.
26
27 ScrollView is the recommended component to use in the implementation of scrollable content. It
28 features scrollbars and handles keyboard input.
29 Old applications that implemented scrollable views directly using the \l Scrollbar component should
30 migrate to ScrollView, to ensure the UX is ready for convergent devices and is consistent with the
31 rest of the platform.
32
33 ScrollView wraps \l {Scrollbar}(s) in a view, and provides additional features such as:
34 \list
35 \li - Keyboard navigation and focus handling, for a complete convergent experience.
36 \li - Automatic position handling of vertical and horizontal scrollbars, preventing them from
37 overlapping one another when both on screen.
38 \endlist
39
40 Adding scrollbars and keyboard input handling to a QML item is as simple as wrapping that item in a
41 ScrollView, as shown in the following example:
42
43 \qml
44 import QtQuick 2.4
45 import Lomiri.Components
46
47 ScrollView {
48 width: units.gu(40)
49 height: units.gu(30)
50
51 Rectangle {
52 width: units.gu(140)
53 height: units.gu(40)
54 gradient: Gradient {
55 GradientStop { position: 0.0; color: "lightsteelblue" }
56 GradientStop { position: 1.0; color: "blue" }
57 }
58 }
59 }
60 \endqml
61
62 NOTE: the items that are wrapped in the ScrollView are reparanted to \l viewport.
63 */
64 StyledItem {
65 id: root
66
67 implicitWidth: 240
68 implicitHeight: 150
69 activeFocusOnPress: true
70
71 //TODO: add horizontalScrollbarPolicy
72 //TODO: add verticalScrollbarPolicy
73
74 //Currently we want to push the platform towards overlay scrollbars
75 //readonly property bool alwaysOnScrollbars: false
76
77 /*!
78 \qmlproperty Item ScrollView::viewport
79 This property holds the viewport Item. The children of the ScrollView element are
80 reparented to this item to make sure the scrollbars are correctly positioned and
81 the items are clipped at their boundaries.
82 */
83 readonly property alias viewport: viewportItem
84
85 /*!
86 \qmlproperty Item ScrollView::flickableItem
87 The flickableItem of the ScrollView. If the contentItem provided
88 to the ScrollView is a Flickable, that will be the \l flickableItem.
89 Otherwise ScrollView will create a Flickable which will hold the
90 items provided as children.
91 */
92 readonly property alias flickableItem: internal.flickableItem
93
94 /*!
95 The contentItem of the ScrollView. This is set by the user.
96 Note that the definition of contentItem is somewhat different to that
97 of a Flickable, where the contentItem is implicitly created.
98 */
99 default property Item contentItem
100
101 Keys.enabled: true
102 Keys.onLeftPressed: {
103 if (horizontalScrollbar.__styleInstance !== null) {
104 horizontalScrollbar.__styleInstance.scrollBy(-flickableItem.width*internal.shortScrollingRatio, true)
105 }
106 }
107 Keys.onRightPressed: {
108 if (horizontalScrollbar.__styleInstance !== null) {
109 horizontalScrollbar.__styleInstance.scrollBy(flickableItem.width*internal.shortScrollingRatio, true)
110 }
111 }
112 Keys.onDownPressed: {
113 if (verticalScrollbar.__styleInstance !== null) {
114 verticalScrollbar.__styleInstance.scrollBy(flickableItem.height*internal.shortScrollingRatio, true)
115 }
116 }
117 Keys.onUpPressed: {
118 if (verticalScrollbar.__styleInstance !== null) {
119 verticalScrollbar.__styleInstance.scrollBy(-flickableItem.height*internal.shortScrollingRatio, true)
120 }
121 }
122 Keys.onPressed: function(event) {
123 if (event.key === Qt.Key_Escape) {
124 var scrollbarWithActiveDrag = (horizontalScrollbar.__styleInstance && horizontalScrollbar.__styleInstance.draggingThumb && horizontalScrollbar)
125 || (verticalScrollbar.__styleInstance && verticalScrollbar.__styleInstance.draggingThumb && verticalScrollbar)
126 || null
127 if (scrollbarWithActiveDrag !== null) {
128 scrollbarWithActiveDrag.__styleInstance.resetScrollingToPreDrag()
129 event.accepted = true
130 }
131 } else if (verticalScrollbar.__styleInstance !== null) {
132 if (event.key === Qt.Key_PageDown) {
133 verticalScrollbar.__styleInstance.scrollBy(flickableItem.height*internal.longScrollingRatio, true)
134 event.accepted = true
135 } else if (event.key === Qt.Key_PageUp) {
136 verticalScrollbar.__styleInstance.scrollBy(-flickableItem.height*internal.longScrollingRatio, true)
137 event.accepted = true
138 } else if (event.key === Qt.Key_Home) {
139 verticalScrollbar.__styleInstance.scrollToBeginning(true)
140 event.accepted = true
141 } else if (event.key === Qt.Key_End) {
142 verticalScrollbar.__styleInstance.scrollToEnd(true)
143 event.accepted = true
144 }
145 }
146 }
147
148 onContentItemChanged: {
149 // Check if the item provided is a Flickable
150 if (QuickUtils.inherits(contentItem, "QQuickFlickable")) {
151 internal.flickableItem = contentItem
152 internal.flickableItem.parent = viewportItem
153 } else {
154 //Create a dummy Flickable if the app dev didn't provide any
155 internal.flickableItem = flickableComponent.createObject(viewportItem)
156 contentItem.parent = internal.flickableItem.contentItem
157 }
158 internal.flickableItem.anchors.fill = viewportItem
159 }
160
161 /*!
162 \qmlproperty Scrollbar ScrollView::horizontalScrollbar
163
164 This property holds the horizontal scrollbar used in the ScrollView.
165 It provides access to the scrollbar thus making it possible to change its properties or
166 completely replace it with a custom \l Scrollbar object.
167
168 This property defaults to an instance of \l Scrollbar.
169
170 \b NOTE: some \l Scrollbar properties are already handled by ScrollView, and their value
171 should hence not be overridden. These properties are:
172 \list
173 \li anchors margins, such as anchors.topMargin etc.
174 \li the parent Item
175 \li \l Scrollbar::flickableItem
176 \endlist
177 */
178 property Scrollbar horizontalScrollbar: Scrollbar {
179 objectName: "horizontalScrollbar"
180 align: Qt.AlignBottom
181 focus: false
182 }
183
184 /*!
185 \qmlproperty Scrollbar ScrollView::verticalScrollbar
186
187 This property holds the vertical scrollbar used in the ScrollView.
188 It provides access to the scrollbar thus making it possible to change its properties or
189 completely replace it with a custom \l Scrollbar object.
190
191 This property defaults to an instance of \l Scrollbar.
192
193 \b NOTE: some \l Scrollbar properties are already handled by ScrollView, and their value
194 should hence not be overridden. These properties are:
195 \list
196 \li anchors margins, such as anchors.topMargin etc.
197 \li the parent Item
198 \li \l Scrollbar::flickableItem
199 \endlist
200 */
201 property Scrollbar verticalScrollbar: Scrollbar {
202 objectName: "verticalScrollbar"
203 align: Qt.AlignTrailing
204 focus: false
205 }
206
207 children: [
208 Item {
209 id: viewportItem
210 anchors.fill: parent
211 anchors.topMargin: (horizontalScrollbar.align === Qt.AlignTop && horizontalScrollbar.__alwaysOnScrollbars)
212 ? internal.nonOverlayScrollbarMargin : 0
213 anchors.leftMargin: (verticalScrollbar.align === Qt.AlignLeading && horizontalScrollbar.__alwaysOnScrollbars)
214 ? internal.nonOverlayScrollbarMargin : 0
215 anchors.rightMargin: (verticalScrollbar.align === Qt.AlignTrailing && horizontalScrollbar.__alwaysOnScrollbars)
216 ? internal.nonOverlayScrollbarMargin : 0
217 anchors.bottomMargin: (horizontalScrollbar.align === Qt.AlignBottom && horizontalScrollbar.__alwaysOnScrollbars)
218 ? internal.nonOverlayScrollbarMargin : 0
219
220 clip: true
221 focus: true
222 },
223
224 Item {
225 id: internal
226 anchors.fill: parent
227
228 property Flickable flickableItem
229 property real nonOverlayScrollbarMargin: verticalScrollbar.__styleInstance ? verticalScrollbar.__styleInstance.nonOverlayScrollbarMargin : 0
230 //shortScrollingRation is used for arrow keys, longScrollingRatio is used for pgUp/pgDown
231 //0.1 means we will scroll 10% of the *visible* flickable area
232 property real shortScrollingRatio: __styleInstance ? __styleInstance.shortScrollingRatio : 0.1
233 property real longScrollingRatio: __styleInstance ? __styleInstance.longScrollingRatio : 0.9
234
235 Component {
236 id: flickableComponent
237 Flickable {}
238 }
239
240 //if the flickable is not coming from the user but from our internal Component...
241 Binding {
242 target: flickableItem
243 when: contentItem !== flickableItem
244 property: "contentHeight"
245 value: contentItem ? contentItem.height : 0
246 }
247 Binding {
248 target: flickableItem
249 when: contentItem !== flickableItem
250 property: "contentWidth"
251 value: contentItem ? contentItem.width : 0
252 }
253
254 Binding {
255 when: root.verticalScrollbar
256 target: root.verticalScrollbar
257 property: "parent"
258 value: root
259 }
260 Binding {
261 when: root.horizontalScrollbar
262 target: root.horizontalScrollbar
263 property: "parent"
264 value: root
265 }
266
267 //The viewport MUST be set, when Scrollbar is used inside a ScrollView, because that is the
268 //item it will anchor to.
269 Binding {
270 //NOTE: we have to wait for the parent to be set before setting the viewport item or that
271 //will trigger anchoring errors
272 when: root.horizontalScrollbar && root.horizontalScrollbar.parent
273 target: root.horizontalScrollbar
274 property: "__viewport"
275 value: viewportItem
276 }
277 Binding {
278 when: root.verticalScrollbar && root.verticalScrollbar.parent
279 target: root.verticalScrollbar
280 property: "__viewport"
281 value: viewportItem
282 }
283
284 Binding {
285 //wait for viewport to be set, otherwise Scrollbar will try anchoring to flickableItem, which
286 //is not a parent or sibling (see definition of Scrollbar anchors) when Scrollbar is inside
287 //a ScrollView (it is a sibling usually when Scrollbar is used directly, without ScrollView)
288 when: root.horizontalScrollbar && root.horizontalScrollbar.__viewport
289 target: root.horizontalScrollbar
290 property: "flickableItem"
291 value: internal.flickableItem
292 }
293 Binding {
294 when: root.verticalScrollbar && root.verticalScrollbar.__viewport
295 target: root.verticalScrollbar
296 property: "flickableItem"
297 value: internal.flickableItem
298 }
299
300 Binding {
301 when: root.verticalScrollbar
302 target: root.verticalScrollbar
303 property: "__buddyScrollbar"
304 value: root.horizontalScrollbar
305 }
306 Binding {
307 when: root.horizontalScrollbar
308 target: root.horizontalScrollbar
309 property: "__buddyScrollbar"
310 value: root.verticalScrollbar
311 }
312
313 //When you click outside of a child of the scrollview, this restores the focus to the ScrollView
314 //Why is this needed?
315 // Suppose the viewport has a child ScrollView (or another Item which handles some or all of the hw keys we handle).
316 // Now, if that child is coded to set its focus to true when you tap on it (which is, for instance, what
317 // StyledItem's activeFocusOnPress does), tapping on that child will set viewportItem.focus = false, because
318 // that child is just another Item inside "root" and "root" is a FocusScope, and that's how FocusScope works.
319 // At this point, we need something to restore the focus to the viewportItem once you tap on viewportItem, outside
320 // any other child. And that's what the MouseArea below does.
321 //
322 //The alternative could be making the viewport a StyledItem and using activeFocusOnPress:true, but that wouldn't work
323 //because it would make viewportItem a focus scope, and we don't want that, because at that point,
324 //being in a similar situation as described above, we wouldn't be able to transfer the focus to viewportItem, because
325 //being a focus scope means viewportItem would forward the focus to the one between its children who asked it most
326 //recently
327 MouseArea {
328 anchors.fill: parent
329 onPressed: function(mouse) {
330 //FIXME: we can't force focus on the viewport, this breaks text selection inside scrollview!
331 //+ it still doesn't work in Sliders page, for yet unknown reasons
332 //Without this we can't shift focus from a child ScrollView to a parent ScrollView
333 //viewportItem.focus = true
334
335 //activeFocusOnPress only works if *all* the parents have activeFocusOnPress enabled
336 //Some applications (like messaging-app at the moment) have activeFocusOnPress:false on
337 //the MainView, and that would make focusing the scrollview impossible. Hence we also
338 //force it from QML side
339 root.forceActiveFocus()
340
341 mouse.accepted = false
342 }
343 }
344 }
345 ]
346 }