Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.2/ScrollbarStyle.qml

raw
1 /*
2 * Copyright 2012 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 1.2
19
20 /*
21 The visuals handle both active and passive modes. This behavior is driven yet by
22 the styledItem's __inactive property, however should be detected upon runtime based on
23 the device type.
24 On active scrollbars, positioning is handled so that the logic updates the flickable's
25 X/Y content positions, which is then synched with the contentPosition by the main
26 element.
27
28 Style properties used:
29 - interactive: bool - drives the interactive behavior of the scrollbar
30 - minimumSliderSize: real - specifies the minimum size of the slider
31 * overlay
32 - overlay: bool - true if the scrollbar is overlay type
33 - overlayOpacityWhenHidden: opacity when hidden
34 - overlayOpacityWhenShown: opacity when shown
35 * animations - where duration and easing properties are used only
36 - scrollbarFadeInAnimation: PropertyAnimation - animation used when fade in
37 - scrollbarFadeOutAnimation: PropertyAnimation - animation used when fade out
38 - scrollbarFadeOutPause: int - milliseconds to pause before fade out
39 * behaviors - animations are used as declared
40 - sliderAnimation: PropertyAnimation - animation for the slider size
41 - thumbConnectorFading: PropertyAnimation - animation for the thumb connector
42 - thumbFading: PropertyAnimation - animation for the thumb fading
43 * other styling properties
44 - color sliderColor: color for the slider
45 - color thumbConnectorColor: thumb connector color
46 - url forwardThumbReleased: forward thumb image when released
47 - url forwardThumbPressed: forward thumb image when pressed
48 - url backwardThumbReleased: backward thumb image when released
49 - url backwardThumbPressed: backward thumb image when pressed
50 - real scrollAreaThickness: scrollbar area thickness, the area where the
51 slider, thumb and thumb-connector appear
52 - real thumbConnectorMargin: margin of the thumb connector aligned to the
53 thumb visuals
54 */
55
56 Item {
57 id: visuals
58 // styling properties
59 property bool interactive: false
60 property real minimumSliderSize: units.gu(2)
61
62 property bool overlay: !interactive
63 property real overlayOpacityWhenShown: 0.6
64 property real overlayOpacityWhenHidden: 0.0
65
66 property PropertyAnimation scrollbarFadeInAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
67 property PropertyAnimation scrollbarFadeOutAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SlowDuration }
68 property int scrollbarFadeOutPause: 300
69 property PropertyAnimation sliderAnimation: LomiriNumberAnimation {}
70 property PropertyAnimation thumbConnectorFading: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
71 property PropertyAnimation thumbFading: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
72
73 property color sliderColor: Theme.palette.normal.base
74 property real sliderRadius: units.gu(0.5)
75 property color thumbConnectorColor: "white"
76 property url forwardThumbReleased: (styledItem.align === Qt.AlignLeading || styledItem.align === Qt.AlignTrailing) ? Qt.resolvedUrl("../artwork/ScrollbarBottomIdle.png") : Qt.resolvedUrl("../artwork/ScrollbarRightIdle.png")
77 property url forwardThumbPressed: (styledItem.align === Qt.AlignLeading || styledItem.align === Qt.AlignTrailing) ? Qt.resolvedUrl("../artwork/ScrollbarBottomPressed.png") : Qt.resolvedUrl("../artwork/ScrollbarRightPressed.png")
78 property url backwardThumbReleased: (styledItem.align === Qt.AlignLeading || styledItem.align === Qt.AlignTrailing) ? Qt.resolvedUrl("../artwork/ScrollbarTopIdle.png") : Qt.resolvedUrl("../artwork/ScrollbarLeftIdle.png")
79 property url backwardThumbPressed: (styledItem.align === Qt.AlignLeading || styledItem.align === Qt.AlignTrailing) ? Qt.resolvedUrl("../artwork/ScrollbarTopPressed.png") : Qt.resolvedUrl("../artwork/ScrollbarLeftPressed.png")
80
81 property real scrollAreaThickness: units.gu(0.5)
82 property real thumbConnectorMargin: units.dp(3)
83
84 // helper properties to ease code readability
85 property Flickable flickableItem: styledItem.flickableItem
86 property bool isScrollable: styledItem.__private.scrollable && pageSize > 0.0
87 && contentSize > 0.0 && contentSize > pageSize
88 property bool isVertical: (styledItem.align === Qt.AlignLeading) || (styledItem.align === Qt.AlignTrailing)
89 property bool frontAligned: (styledItem.align === Qt.AlignLeading)
90 property bool rearAligned: (styledItem.align === Qt.AlignTrailing)
91 property bool topAligned: (styledItem.align === Qt.AlignTop)
92 property bool bottomAligned: (styledItem.align === Qt.AlignBottom)
93
94 property real pageSize: (isVertical) ? styledItem.height : styledItem.width
95 property real contentSize: (isVertical) ? styledItem.flickableItem.contentHeight : styledItem.flickableItem.contentWidth
96
97 /*!
98 \internal
99 Object storing property names used in calculations.
100 */
101 QtObject {
102 id: scrollbarUtils
103
104 property string propOrigin: (isVertical) ? "originY" : "originX"
105 property string propContent: (isVertical) ? "contentY" : "contentX"
106 property string propPosRatio: (isVertical) ? "yPosition" : "xPosition"
107 property string propSizeRatio: (isVertical) ? "heightRatio" : "widthRatio"
108 property string propCoordinate: (isVertical) ? "y" : "x"
109 property string propSize: (isVertical) ? "height" : "width"
110
111 /*!
112 \internal
113 Calculates the slider position based on the visible area's ratios.
114 */
115 function sliderPos(min, max) {
116 return MathUtils.clamp(styledItem.flickableItem.visibleArea[propPosRatio] * styledItem.flickableItem[propSize], min, max);
117 }
118
119 /*!
120 \internal
121 Calculates the slider size for ListViews based on the visible area's position
122 and size ratios, clamping it between min and max.
123
124 The function can be used in Scrollbar styles to calculate the size of the slider.
125 */
126 function sliderSize(min, max) {
127 var sizeRatio = styledItem.flickableItem.visibleArea[propSizeRatio];
128 var posRatio = styledItem.flickableItem.visibleArea[propPosRatio];
129 var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
130 var startPos = posRatio * (max - sizeUnderflow)
131 var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
132 var overshootStart = startPos < 0 ? -startPos : 0
133 var overshootEnd = endPos > max ? endPos - max : 0
134
135 // overshoot adjusted start and end
136 var adjustedStartPos = startPos + overshootStart
137 var adjustedEndPos = endPos - overshootStart - overshootEnd
138
139 // final position and size of thumb
140 var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
141 var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
142
143 return result;
144 }
145
146 /*!
147 \internal
148 The function calculates and clamps the position to be scrolled to the minimum
149 and maximum values.
150
151 The scroll and drag functions require a slider that does not have any minimum
152 size set (meaning the minimum is set to 0.0). Implementations should consider
153 using an invisible cursor to drag the slider and the ListView position.
154 */
155 function scrollAndClamp(amount, min, max) {
156 return styledItem.flickableItem[propOrigin] +
157 MathUtils.clamp(styledItem.flickableItem[propContent] - styledItem.flickableItem[propOrigin] + amount,
158 min, max);
159 }
160
161 /*!
162 \internal
163 The function calculates the new position of the dragged slider. The amount is
164 relative to the contentSize, which is either the flickable's contentHeight or
165 contentWidth or other calculated value, depending on its orientation. The pageSize
166 specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
167 */
168 function dragAndClamp(cursor, contentSize, pageSize) {
169 styledItem.flickableItem[propContent] =
170 styledItem.flickableItem[propOrigin] + cursor[propCoordinate] * contentSize / pageSize;
171 }
172 }
173
174 /*****************************************
175 Visuals
176 *****************************************/
177 anchors.fill: parent
178
179 opacity: overlayOpacityWhenHidden
180 state: {
181 if (!isScrollable)
182 return '';
183 else if (overlay) {
184 if (flickableItem.moving)
185 return 'overlay';
186 else
187 return 'stopped';
188 } else
189 return 'shown';
190 }
191
192 states: [
193 State {
194 name: 'stopped'
195 extend: ''
196 PropertyChanges {
197 target: visuals
198 opacity: overlayOpacityWhenHidden
199 }
200 },
201 State {
202 name: "shown"
203 PropertyChanges {
204 target: visuals
205 opacity: overlayOpacityWhenShown
206 }
207 },
208 State {
209 name: 'overlay'
210 PropertyChanges {
211 target: visuals
212 opacity: overlayOpacityWhenShown
213 }
214 }
215 ]
216 transitions: [
217 Transition {
218 from: ''
219 to: 'shown'
220 NumberAnimation {
221 target: visuals
222 property: "opacity"
223 duration: scrollbarFadeInAnimation.duration
224 easing: scrollbarFadeInAnimation.easing
225 }
226 },
227 Transition {
228 from: '*'
229 to: 'overlay'
230 NumberAnimation {
231 target: visuals
232 property: "opacity"
233 duration: scrollbarFadeInAnimation.duration
234 easing: scrollbarFadeInAnimation.easing
235 }
236 },
237 Transition {
238 from: "overlay"
239 to: "stopped"
240 SequentialAnimation {
241 PauseAnimation { duration: scrollbarFadeOutPause }
242 NumberAnimation {
243 target: visuals
244 property: "opacity"
245 duration: scrollbarFadeOutAnimation.duration
246 easing: scrollbarFadeOutAnimation.easing
247 }
248 }
249 }
250 ]
251
252 function mapToPoint(map)
253 {
254 return Qt.point(map.x, map.y)
255 }
256
257 SmoothedAnimation {
258 id: scrollAnimation
259
260 duration: 200
261 easing.type: Easing.InOutQuad
262 target: styledItem.flickableItem
263 property: (isVertical) ? "contentY" : "contentX"
264 }
265
266 // represents the visible area of the scrollbar where slider and thumb connector are placed
267 Item {
268 id: scrollbarArea
269
270 property real thickness: scrollAreaThickness
271 property real proximityThickness: (isVertical) ? styledItem.width - thickness : styledItem.height - thickness
272 anchors {
273 fill: parent
274 leftMargin: (!isVertical || frontAligned) ? 0 : proximityThickness
275 rightMargin: (!isVertical || rearAligned) ? 0 : proximityThickness
276 topMargin: (isVertical || topAligned) ? 0 : proximityThickness
277 bottomMargin: (isVertical || bottomAligned) ? 0 : proximityThickness
278 }
279 }
280 // The thumb appears whenever the mouse gets close enough to the scrollbar
281 // and disappears after being for a long enough time far enough of it
282 MouseArea {
283 id: proximityArea
284
285 anchors {
286 fill: parent
287 leftMargin: (!isVertical) ? 0 : (frontAligned ? scrollbarArea.thickness : 0)
288 rightMargin: (!isVertical) ? 0 : (rearAligned ? scrollbarArea.thickness : 0)
289 topMargin: (isVertical) ? 0 : (topAligned ? scrollbarArea.thickness : 0)
290 bottomMargin: (isVertical) ? 0 : (bottomAligned ? scrollbarArea.thickness : 0)
291 }
292 propagateComposedEvents: true
293 enabled: isScrollable && interactive
294 hoverEnabled: true
295 onEntered: thumb.show();
296
297 onPressed: mouse.accepted = false
298 onClicked: mouse.accepted = false
299 onReleased: mouse.accepted = false
300 }
301
302 // The presence of a mouse enables the interactive thumb
303 // FIXME: Should use form factor hints
304 InverseMouse.onEntered: interactive = true
305
306 // The slider's position represents which part of the flickable is visible.
307 // The slider's size represents the size the visible part relative to the
308 // total size of the flickable.
309 Item {
310 id: scrollCursor
311 x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - scrollCursor.width)
312 y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - scrollCursor.height)
313 width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.width)
314 height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, 0.0, flickableItem.height)
315
316 function drag() {
317 scrollbarUtils.dragAndClamp(styledItem, scrollCursor, contentSize, pageSize);
318 }
319 }
320
321 Rectangle {
322 id: slider
323
324 color: visuals.sliderColor
325
326 anchors {
327 left: (isVertical) ? scrollbarArea.left : undefined
328 right: (isVertical) ? scrollbarArea.right : undefined
329 top: (!isVertical) ? scrollbarArea.top : undefined
330 bottom: (!isVertical) ? scrollbarArea.bottom : undefined
331 }
332
333 x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.width - slider.width)
334 y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, 0.0, styledItem.height - slider.height)
335 width: (isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.width)
336 height: (!isVertical) ? scrollbarArea.thickness : scrollbarUtils.sliderSize(styledItem, minimumSliderSize, flickableItem.height)
337 radius: visuals.sliderRadius
338
339 Behavior on width {
340 enabled: (!isVertical)
341 NumberAnimation {
342 duration: visuals.sliderAnimation.duration
343 easing: visuals.sliderAnimation.easing
344 }
345 }
346 Behavior on height {
347 enabled: (isVertical)
348 NumberAnimation {
349 duration: visuals.sliderAnimation.duration
350 easing: visuals.sliderAnimation.easing
351 }
352 }
353
354 function scroll(amount) {
355 scrollAnimation.to = scrollbarUtils.scrollAndClamp(styledItem, amount, 0.0, contentSize - pageSize);
356 scrollAnimation.restart();
357 }
358 }
359
360 // The sliderThumbConnector ensures a visual connection between the slider and the thumb
361 Rectangle {
362 id: sliderThumbConnector
363
364 property real thumbConnectorMargin: visuals.thumbConnectorMargin
365 property bool isThumbAboveSlider: (isVertical) ? thumb.y < slider.y : thumb.x < slider.x
366 anchors {
367 left: (isVertical) ? scrollbarArea.left : (isThumbAboveSlider ? thumb.left : slider.right)
368 right: (isVertical) ? scrollbarArea.right : (isThumbAboveSlider ? slider.left : thumb.right)
369 top: (!isVertical) ? scrollbarArea.top : (isThumbAboveSlider ? thumb.top : slider.bottom)
370 bottom: (!isVertical) ? scrollbarArea.bottom : (isThumbAboveSlider ? slider.top : thumb.bottom)
371
372 leftMargin : (isVertical) ? 0 : (isThumbAboveSlider ? thumbConnectorMargin : 0)
373 rightMargin : (isVertical) ? 0 : (isThumbAboveSlider ? 0 : thumbConnectorMargin)
374 topMargin : (!isVertical) ? 0 : (isThumbAboveSlider ? thumbConnectorMargin : 0)
375 bottomMargin : (!isVertical) ? 0 : (isThumbAboveSlider ? 0 : thumbConnectorMargin)
376 }
377 color: visuals.thumbConnectorColor
378 opacity: thumb.shown ? 1.0 : 0.0
379 Behavior on opacity {
380 NumberAnimation {
381 duration: visuals.thumbConnectorFading.duration
382 easing: visuals.thumbConnectorFading.easing
383 }
384 }
385 }
386
387 MouseArea {
388 id: thumbArea
389
390 property point thumbPoint: mapToPoint(thumb.mapFromItem(thumbArea, mouseX, mouseY))
391 property point thumbTopPoint: mapToPoint(thumbTop.mapFromItem(thumb, thumbPoint.x, thumbPoint.y))
392 property point thumbBottomPoint: mapToPoint(thumbBottom.mapFromItem(thumb, thumbPoint.x, thumbPoint.y))
393 property bool inThumbTop: thumbTop.contains(thumbTopPoint)
394 property bool inThumbBottom: thumbBottom.contains(thumbBottomPoint)
395
396 anchors {
397 fill: scrollbarArea
398 // set margins adding 2 dp for error area
399 leftMargin: (!isVertical || frontAligned) ? 0 : units.dp(-2) - thumb.width
400 rightMargin: (!isVertical || rearAligned) ? 0 : units.dp(-2) - thumb.width
401 topMargin: (isVertical || topAligned) ? 0 : units.dp(-2) - thumb.height
402 bottomMargin: (isVertical || bottomAligned) ? 0 : units.dp(-2) - thumb.height
403 }
404 enabled: isScrollable && interactive
405 hoverEnabled: true
406 onEntered: thumb.show()
407 onPressed: {
408 if (isVertical) {
409 if (mouseY < thumb.y) {
410 thumb.placeThumbForeUnderMouse(mouse)
411 } else if (mouseY > (thumb.y + thumb.height)) {
412 thumb.placeThumbRearUnderMouse(mouse)
413 }
414 } else {
415 if (mouseX < thumb.x) {
416 thumb.placeThumbForeUnderMouse(mouse)
417 } else if (mouseX > (thumb.x + thumb.width)) {
418 thumb.placeThumbRearUnderMouse(mouse)
419 }
420 }
421 }
422 onClicked: {
423 if (inThumbBottom)
424 slider.scroll(pageSize)
425 else if (inThumbTop)
426 slider.scroll(-pageSize)
427 }
428
429 // Dragging behaviour
430 function resetDrag() {
431 thumbYStart = thumb.y
432 thumbXStart = thumb.x
433 dragYStart = drag.target.y
434 dragXStart = drag.target.x
435 }
436
437 property int thumbYStart
438 property int dragYStart
439 property int dragYAmount: thumbArea.drag.target.y - thumbArea.dragYStart
440 property int thumbXStart
441 property int dragXStart
442 property int dragXAmount: thumbArea.drag.target.x - thumbArea.dragXStart
443 drag {
444 target: scrollCursor
445 axis: (isVertical) ? Drag.YAxis : Drag.XAxis
446 minimumY: 0
447 maximumY: flickableItem.height - scrollCursor.height
448 minimumX: 0
449 maximumX: flickableItem.width - scrollCursor.width
450 onActiveChanged: {
451 if (drag.active) resetDrag()
452 }
453 }
454 // update thumb position
455 onDragYAmountChanged: {
456 if (drag.active) {
457 thumb.y = MathUtils.clamp(thumbArea.thumbYStart + thumbArea.dragYAmount, 0, thumb.maximumPos);
458 }
459 }
460 onDragXAmountChanged: {
461 if (drag.active) {
462 thumb.x = MathUtils.clamp(thumbArea.thumbXStart + thumbArea.dragXAmount, 0, thumb.maximumPos);
463 }
464 }
465
466 // drag slider and content to the proper position
467 onPositionChanged: {
468 if (pressedButtons == Qt.LeftButton) {
469 scrollCursor.drag()
470 }
471 }
472 }
473
474 Timer {
475 id: autohideTimer
476
477 interval: 1000
478 repeat: true
479 onTriggered: if (!proximityArea.containsMouse && !thumbArea.containsMouse && !thumbArea.pressed) thumb.hide()
480 }
481
482 Item {
483 id: thumb
484 objectName: "interactiveScrollbarThumb"
485
486 enabled: interactive
487
488 anchors {
489 left: frontAligned ? slider.left : undefined
490 right: rearAligned ? slider.right : undefined
491 top: topAligned ? slider.top : undefined
492 bottom: bottomAligned ? slider.bottom : undefined
493 }
494
495 width: childrenRect.width
496 height: childrenRect.height
497
498 property bool shown
499 property int maximumPos: (isVertical) ? styledItem.height - thumb.height : styledItem.width - thumb.width
500
501 /* Show the thumb as close as possible to the mouse pointer */
502 onShownChanged: {
503 if (shown) {
504 if (isVertical) {
505 var mouseY = proximityArea.containsMouse ? proximityArea.mouseY : thumbArea.mouseY;
506 y = MathUtils.clamp(mouseY - thumb.height / 2, 0, thumb.maximumPos);
507 } else {
508 var mouseX = proximityArea.containsMouse ? proximityArea.mouseX : thumbArea.mouseX;
509 x = MathUtils.clamp(mouseX - thumb.width / 2, 0, thumb.maximumPos);
510 }
511 }
512 }
513
514 function show() {
515 autohideTimer.restart();
516 shown = true;
517 }
518
519 function hide() {
520 autohideTimer.stop();
521 shown = false;
522 }
523
524 function placeThumbForeUnderMouse(mouse) {
525 var diff = (isVertical) ? mouse.y - height / 4 : mouse.x - width / 4;
526 positionAnimation.to = MathUtils.clamp(diff, 0, maximumPos);
527 positionAnimation.restart();
528 }
529
530 function placeThumbRearUnderMouse(mouse) {
531 var diff = (isVertical) ? mouse.y - height * 3 / 4 : mouse.x - width * 3 / 4;
532 positionAnimation.to = MathUtils.clamp(diff, 0, maximumPos);
533 positionAnimation.restart();
534 }
535
536 NumberAnimation {
537 id: positionAnimation
538
539 duration: 100
540 easing.type: Easing.InOutQuad
541 target: thumb
542 property: (isVertical) ? "y" : "x"
543 }
544
545 opacity: shown ? (thumbArea.containsMouse || thumbArea.drag.active ? 1.0 : 0.5) : 0.0
546 Behavior on opacity {
547 NumberAnimation {
548 duration: visuals.thumbFading.duration
549 easing: visuals.thumbFading.easing
550 }
551 }
552
553 Flow {
554 // disable mirroring as thumbs are placed in the same way no matter of RTL or LTR
555 LayoutMirroring.enabled: false
556 flow: (isVertical) ? Flow.TopToBottom : Flow.LeftToRight
557 Image {
558 id: thumbTop
559 source: thumbArea.inThumbTop && thumbArea.pressed ? visuals.backwardThumbPressed : visuals.backwardThumbReleased
560 }
561 Image {
562 id: thumbBottom
563 source: thumbArea.inThumbBottom && thumbArea.pressed ? visuals.forwardThumbPressed : visuals.forwardThumbReleased
564 }
565 }
566 }
567 }