Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.3/ScrollbarStyle.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 QtQml 2.15
19 import Lomiri.Components 1.3
20
21 /*
22 The visuals handle both active and passive modes. This behavior is driven yet by
23 the styledItem's __interactive property, however should be detected upon runtime based on
24 the device type.
25 On active scrollbars, positioning is handled so that the logic updates the flickable's
26 X/Y content positions, which is then synched with the contentPosition by the main
27 element.
28
29 Style properties used:
30 - interactive: bool - drives the interactive behavior of the scrollbar
31 - minimumSliderSize: real - specifies the minimum size of the slider
32 * overlay
33 - overlay: bool - true if the scrollbar is overlay type
34 - overlayOpacityWhenHidden: opacity when hidden
35 - overlayOpacityWhenShown: opacity when shown
36 * trough
37 - troughThicknessIndicatorStyle: real - thickness of the thumb in Indicator style
38 - troughThicknessThumbStyle: real - thickness of the thumb in Thumb style
39 - troughThicknessSteppersStyle: real - thickness of the thumb in Steppers style
40 - troughColorThumbStyle: color - color of the trough in Thumb style
41 - troughColorSteppersStyle: color - color of the trough in Steppers style
42 * slider
43 - sliderColor: color - color for the slider
44 - sliderRadius: real - radius of of the slider rectangle
45 - thumbThickness: real - thickness of the slider in Thumb style
46 - indicatorThickness: real - thickness of the slider in Indicator style
47 * animations - where duration and easing properties are used only
48 - scrollbarThicknessAnimation: PropertyAnimation - animation used when changing
49 the thickness of the slider
50 - scrollbarFadeInAnimation: PropertyAnimation - animation used when fading in
51 - scrollbarFadeOutAnimation: PropertyAnimation - animation used when fading out
52 - scrollbarFadeOutPause: int - milliseconds to pause before fading out
53 - scrollbarCollapsePause: int - milliseconds to pause at the beginning of the
54 transition from Thumb to Indicator style
55 * scrolling UX - the values are relative to the size of the view
56 - shortScrollingRatio: real - how much to scroll when using arrow keys and steppers
57 - longScrollingRatio: real - how much to scroll when using PageUp/Down
58 * other styling properties
59 - hintingStyle: string - the style to use for the hinting feature
60 - thumbsExtremesMargin: real - This is the top/bottom (for the vertical scrollbar)
61 and left/right (for horiz scrollbar) margin of the slider
62 from the trough.
63 It is supposed to be the same margin as the one that
64 separates the thumb from the left/right (or top/bottom
65 for horizontal scrollbar) edges of the screen.
66 */
67
68 Item {
69 id: visuals
70
71 property bool initialized: false
72
73 /*****************************************************
74 * STYLING PROPERTIES *
75 *****************************************************/
76 property bool interactive: isMouseConnected || veryLongContentItem
77 property real minimumSliderSize: __canFitSteppersAndTallerThumb
78 ? __minSliderSizeWhenUnrestrictedSize
79 : __minSliderSizeWhenRestrictedSize
80
81 property bool overlay: !alwaysOnScrollbars
82 property real overlayOpacityWhenShown: 1.0
83 property real overlayOpacityWhenHidden: 0.0
84
85 property real troughThicknessSteppersStyle : units.dp(14)
86 property real troughThicknessThumbStyle : units.dp(14)
87 property real troughThicknessIndicatorStyle : units.dp(9)
88 property color troughColorThumbStyle: theme.palette.normal.foreground
89 property color troughColorSteppersStyle: theme.palette.normal.foreground
90
91 property color sliderColor: theme.palette.normal.overlayText
92
93 //used for hover and pressed states, otherwise they're transparent in order to show the same
94 //background as the trough
95 property color stepperBgColor: theme.palette.normal.base
96 property real sliderRadius: units.dp(3)
97 property real thumbThickness: units.gu(1)
98 property real indicatorThickness : units.dp(3)
99
100 property PropertyAnimation scrollbarThicknessAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
101 property PropertyAnimation scrollbarFadeInAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SlowDuration }
102 property PropertyAnimation scrollbarFadeOutAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SlowDuration }
103 property int scrollbarFadeOutPause: 3000
104 property int scrollbarCollapsePause: 1000
105
106 //The ratio of page to scroll when using pg_down/up
107 //and arrow_down/up keyboard keys (and steppers)
108 property real shortScrollingRatio: 0.1
109 property real longScrollingRatio: 0.9
110
111 property string hintingStyle: veryLongContentItem ? 'thumb' : 'indicator'
112 property real thumbsExtremesMargin: units.dp(4)
113
114 /*****************************************************
115 * HELPER PROPERTIES *
116 *****************************************************/
117 property alias thumb: slider
118 property alias trough: trough
119
120 //helper properties to ease code readability
121 property bool isScrollable: styledItem.__private.scrollable && pageSize > 0.0
122 && contentSize > 0.0 && totalContentSize > pageSize
123 property bool isVertical: (styledItem.align === Qt.AlignLeading) || (styledItem.align === Qt.AlignTrailing)
124 property bool frontAligned: (styledItem.align === Qt.AlignLeading)
125 property bool rearAligned: (styledItem.align === Qt.AlignTrailing)
126 property bool topAligned: (styledItem.align === Qt.AlignTop)
127 property bool bottomAligned: (styledItem.align === Qt.AlignBottom)
128
129 //flickable helper properties
130 //Don't do anything with the flickable until its Component.onCompleted is called, it's a waste of cycles
131 property Flickable flickableItem: styledItem.__initializedFlickable
132 property real pageSize: flickableItem
133 ? (isVertical ? flickableItem.height : flickableItem.width)
134 : 0
135 property real contentSize: flickableItem
136 ? (isVertical ? flickableItem.contentHeight : flickableItem.contentWidth)
137 : 0
138 property real leadingContentMargin: flickableItem
139 ? (isVertical ? flickableItem.topMargin : flickableItem.leftMargin)
140 : 0
141 property real trailingContentMargin: flickableItem
142 ? (isVertical ? flickableItem.bottomMargin : flickableItem.rightMargin)
143 : 0
144 //this size includes content margins
145 property real totalContentSize: contentSize + leadingContentMargin + trailingContentMargin
146
147
148 /*****************************************************
149 * INTERNAL PROPERTIES AND FUNCTIONS *
150 *****************************************************/
151
152 //special handling of situations where we don't have enough room to show both thumb and steppers
153 property bool __canFitSteppers: visuals[scrollbarUtils.propSize] >= 2*__idealStepperSize
154 property bool __canFitShorterThumb: visuals[scrollbarUtils.propSize] >= (__minSliderSizeWhenRestrictedSize
155 + 2*thumbsExtremesMargin)
156 property bool __canFitSteppersAndShorterThumb: visuals[scrollbarUtils.propSize] >= (2*__idealStepperSize
157 + __minSliderSizeWhenRestrictedSize
158 + 2*thumbsExtremesMargin)
159 property bool __canFitSteppersAndTallerThumb: visuals[scrollbarUtils.propSize] >= (2*__idealStepperSize
160 + __minSliderSizeWhenUnrestrictedSize
161 + 2*thumbsExtremesMargin)
162
163 //the minimum slider size for common usecases, i.e. when there is enough space to show thumb + steppers
164 property real __minSliderSizeWhenUnrestrictedSize: 3*thumbThickness
165 property real __minSliderSizeWhenRestrictedSize: thumbThickness
166
167 property real __targetStepperSize: !__canFitSteppers
168 ? visuals[scrollbarUtils.propSize]/2
169 : __idealStepperSize
170 property real __idealStepperSize: troughThicknessSteppersStyle
171 property real __stepperImgOpacityOnPressed: 1.0
172 property real __stepperImgOpacityOnHover: 0.7
173 property real __stepperImgOpacityNormal: 0.4
174 property real __stepperImgOpacityDisabled: 0.2
175
176 property real __stepperBgOpacityOnPressed: 1.0
177 property real __stepperBgOpacityOnHover: 0.63
178
179 //the stepper arrow svg is designed to scale nicely to the lowest
180 //px-per-GU display (i.e. 8px/GU). Please consider updating the asset if
181 //you change this size to avoid blurry steppers on low px-per-GU displays.
182 property real __stepperAssetWidth: units.dp(8)
183
184 //UX requested to disable this feature for the moment
185 property bool __disableDragResetFeature: true
186
187 property bool __recursionGuard: false
188 property bool __disableStateBinding: false
189 property alias __overshootTimer: overshootTimer
190 property bool __hinting: false
191 property bool draggingThumb: thumbArea.drag.active || slider.mouseDragging || slider.touchDragging
192 //this is the condition that triggers "Thumb Style"
193 //only show the thumb if the page AND the view is moving
194 property bool thumbStyleFlag: veryLongContentItem && (flickableItem.moving || scrollAnimation.running)
195 //we show thumb style instead of indicator style if the content item is very long on *any* of the 2 axes
196 property bool veryLongContentItem: flickableItem && initialized
197 && ((flickableItem.contentHeight > flickableItem.height * 10)
198 || (flickableItem.contentWidth > flickableItem.width * 10))
199
200 //this will eventually come from QInputInfo
201 property bool isMouseConnected: true
202 //used to offset viewport and scrollbars to make space for non-overlay scrollbars
203 property real nonOverlayScrollbarMargin: troughThicknessSteppersStyle
204 //TODO: move to Scrollbar.qml?
205 property bool useSteppers: true
206 //the thumb will have a touchDragStartMargin margin on both sides
207 //to be more easily draggable via touch
208 property real touchDragStartMargin: units.gu(2)
209 //NOTE: this is not used for Qt's drag.treshold, just for our logic!
210 property real dragThreshold: units.dp(2)
211 //this could eventually become a system setting
212 //True --> Steppers style, non-overlay scrollbars
213 //False --> Indicator, Trough and Steppers styles, overlaid to the content
214 property bool alwaysOnScrollbars: styledItem.__alwaysOnScrollbars
215
216 function scrollBy(amount, animate) {
217 if (isNaN(amount)) {
218 console.log("BUG: Invalid scrolling delta.")
219 return
220 }
221 scrollTo(scrollbarUtils.scrollAndClamp(
222 styledItem, amount, -leadingContentMargin,
223 Math.max(contentSize + trailingContentMargin - pageSize,
224 -leadingContentMargin))
225 , animate)
226 }
227 function scrollTo(value, animate) {
228 if (isNaN(value)) {
229 console.log("BUG: Invalid scrolling value.")
230 return
231 }
232 //don't setup an animation with duration 0, if we just don't want the animation
233 if (animate) {
234 scrollAnimation.to = value
235 scrollAnimation.restart()
236 } else {
237 if (scrollAnimation.running) scrollAnimation.stop()
238 if (flickableItem) {
239 flickableItem[scrollbarUtils.propContent] = value
240 flickableItem.returnToBounds()
241 }
242 }
243 }
244 function scrollToBeginning(animate) {
245 if (!flickableItem) return
246 scrollTo(flickableItem[scrollbarUtils.propOrigin] - visuals.leadingContentMargin, animate)
247 }
248 function scrollToEnd(animate) {
249 if (!flickableItem) return
250 scrollTo((flickableItem[scrollbarUtils.propOrigin]
251 + totalContentSize - visuals.leadingContentMargin - pageSize), animate)
252 }
253 function drag() {
254 scrollbarUtils.dragAndClamp(styledItem, slider.relThumbPosition, totalContentSize, pageSize);
255 }
256 function resetScrollingToPreDrag() {
257 thumbArea.resetFlickableToPreDragState()
258 }
259 //As per spec, we show a hint of the scrollbars in indicator style whenever the
260 //flickable item changes its size
261 //NOTE: THIS IS ASSUMING THAT the transition to hidden state has a PauseAnimation,
262 //otherwise the hinting would be too quick!
263 function flashScrollbar() {
264 //only show the hint if the scrollbar is currently hidden or its already showing the hint
265 //(it could happen that while it is showing the hint the size of the flickable changes enough
266 //to trigger the transition to thumb style, and in that case we want to hint again using thumb
267 //style)
268 if (initialized && isScrollable && !draggingThumb && !pressHoldTimer.running
269 && (state == '' || state === 'hidden' || (__disableStateBinding && visuals.state !== hintingStyle))) {
270 __disableStateBinding = true
271 __hinting = true
272 visuals.state = hintingStyle
273 hintingTimer.restart()
274 }
275 }
276 //The definition of isScrollable includes a binding on totalContentSize:
277 //when totalContentSizeChanged triggers flashScrollbar, isScrollable may
278 //have not been reevaluated yet using the new totalContentSize value, and
279 //that could make the logic ignore the hinting request.
280 //We perform the checks inside the flashScrollbar function instead of each
281 //time before calling flashScrollbar (though this comes at slightly higher cost)
282 onIsScrollableChanged: flashScrollbar()
283 onTotalContentSizeChanged: flashScrollbar()
284 onHintingStyleChanged: flashScrollbar()
285
286 anchors.fill: parent
287 opacity: overlayOpacityWhenHidden
288 Component.onCompleted: initialized = true
289
290 //we can't use onRunningChanged in the state transitions to enable the state binding again
291 //after hinting, because that was causing
292 //"Can't apply a state change as part of a state definition." when the state it was transitioning
293 //to was different from the one the binding evaluationg was returning
294 //Instead we use a timer, tune it so that it lasts as long as needed, and then we reenable the
295 //state binding once the transition has completed
296 Timer {
297 id: hintingTimer
298 repeat: false
299 //we assume thickness animation duration is enough to complete the
300 //transition to the hinting style
301 interval: scrollbarThicknessAnimation.duration
302 onTriggered: {
303 __hinting = false
304 __disableStateBinding = false
305 }
306 }
307
308 /*!
309 \internal
310 Object storing property names used in calculations.
311 */
312 QtObject {
313 id: scrollbarUtils
314 objectName: "scrollbarUtils"
315 property string propOrigin: (isVertical) ? "originY" : "originX"
316 property string propContent: (isVertical) ? "contentY" : "contentX"
317 property string propPosRatio: (isVertical) ? "yPosition" : "xPosition"
318 property string propSizeRatio: (isVertical) ? "heightRatio" : "widthRatio"
319 property string propCoordinate: (isVertical) ? "y" : "x"
320 property string otherPropCoordinate: (isVertical) ? "x" : "y"
321 property string propSize: (isVertical) ? "height" : "width"
322 property string otherPropSize: (isVertical) ? "width" : "height"
323 property string propAtBeginning: (isVertical) ? "atYBeginning" : "atXBeginning"
324 property string propAtEnd: (isVertical) ? "atYEnd" : "atXEnd"
325
326 /*!
327 Calculates the slider position based on the visible area's ratios.
328 */
329 function sliderPos(scrollbar, min, max) {
330 if (!scrollbar.__initializedFlickable) return min
331
332 //the margin between the trough and the thumb min and max values
333 var margin = scrollbar.__styleInstance.thumbsExtremesMargin
334
335 //The total length of the path where the thumb can be positioned, from its min to its max value
336 var draggableLength = visuals.trough[propSize] - margin*2
337 var maxPosRatio = 1.0 - (scrollbar.__initializedFlickable ? scrollbar.__initializedFlickable.visibleArea[propSizeRatio] : 1.0)
338
339 //Example with x/width, same applies to y/height
340 //xPosition is in the range [0...1 - widthRatio]
341 //and we want the following mapping (xPosition --> thumb.x):
342 // 0 ---> margin (margin is the min position for the thumb)
343 // 1 - widthRatio ---> (draggableLength - scrollbar.__styleInstance.thumb[propSize]) + margin
344 //So we scale the maximum thumb position by xPosition. But that's not enough, because that would mean
345 //the maxPosition is reached when xPosition becomes 1, and that never happens. To compensate that, we
346 //scale xPosition by ( 1 / ( 1 - widthRatio) ). This way, when xPosition reaches its max ( 1 - widthRatio )
347 //we get a multiplication factor of 1
348 return MathUtils.clamp(1.0 / maxPosRatio * (scrollbar.__initializedFlickable ? scrollbar.__initializedFlickable.visibleArea[propPosRatio] : 1.0)
349 * (draggableLength - scrollbar.__styleInstance.thumb[propSize]) + margin, min, max);
350 }
351
352 /*!
353 Calculates the slider size for ListViews based on the visible area's position
354 and size ratios, clamping it between min and max.
355
356 The function can be used in Scrollbar styles to calculate the size of the slider.
357
358 NOTE: THIS CODE IS ASSUMING THAT "MAX" IS ALSO THE "SIZE" OF THE TROUGH THAT THE
359 THUMB CAN MOVE INTO! (which is what you want in 99.9% of the cases, for a scrollbar)
360 */
361 function sliderSize(scrollbar, min, max) {
362 if (!scrollbar.__initializedFlickable) return min
363
364 var sizeRatio = scrollbar.__initializedFlickable ? scrollbar.__initializedFlickable.visibleArea[propSizeRatio] : 1.0;
365 var posRatio = scrollbar.__initializedFlickable ? scrollbar.__initializedFlickable.visibleArea[propPosRatio] : 0.0;
366
367 //(sizeRatio * max) is the current ideal size, as recommended by Flickable visibleArea props
368 var sizeUnderflow = (sizeRatio * max) < min ? min - (sizeRatio * max) : 0
369
370 //we multiply by (max - sizeUndeflow) because we want to simulate a shorter trough. This is because
371 //posRatio value is [0...1-sizeRatio] so it assumes the slider will be of size sizeRatio*size_of_the_trough
372 //(because that's the only way to make the slider fill the remaining part of the trough when posRatio is
373 //at its maximum value), while our slider could actually be bigger due to the imposed "min" value.
374 //We will compensate for this shift by adding sizeUnderflow to endPos.
375 var startPos = posRatio * (max - sizeUnderflow)
376 var endPos = (posRatio + sizeRatio) * (max - sizeUnderflow) + sizeUnderflow
377 var overshootStart = startPos < 0 ? -startPos : 0
378 var overshootEnd = endPos > max ? endPos - max : 0
379
380 // overshoot adjusted start and end
381 var adjustedStartPos = startPos + overshootStart
382 var adjustedEndPos = endPos - overshootStart - overshootEnd
383
384 // final position and size of slider
385 var position = adjustedStartPos + min > max ? max - min : adjustedStartPos
386 var result = (adjustedEndPos - position) < min ? min : (adjustedEndPos - position)
387
388 return result;
389 }
390
391 /*!
392 The function calculates and clamps the position to be scrolled to the minimum
393 and maximum values.
394
395 The scroll and drag functions require a slider that does not have any minimum
396 size set (meaning the minimum is set to 0.0). Implementations should consider
397 using an invisible cursor to drag the slider and the ListView position.
398 */
399 function scrollAndClamp(scrollbar, amount, min, max) {
400 if (!scrollbar.__initializedFlickable) return
401
402 return scrollbar.__initializedFlickable[propOrigin] +
403 MathUtils.clamp(scrollbar.__initializedFlickable[propContent]
404 - scrollbar.__initializedFlickable[propOrigin] + amount,
405 min, max);
406 }
407
408 /*!
409 The function calculates the new position of the dragged slider. The amount is
410 relative to the contentSize, which is either the flickable's contentHeight or
411 contentWidth or other calculated value, depending on its orientation. The pageSize
412 specifies the visibleArea, and it is usually the heigtht/width of the scrolling area.
413
414 //FIXME: should we change the API and remove pageSize or just pass trough's size as pageSize par?
415
416 NOTE: when flickable.topMargin is 5GU, contentY has to be -5GU (not 0!) to be at the top of the scrollable!!
417 */
418 function dragAndClamp(scrollbar, relThumbPosition, contentSize, pageSize) {
419 if (!scrollbar.__initializedFlickable) return
420
421 scrollbar.__initializedFlickable[propContent] =
422 scrollbar.__initializedFlickable[propOrigin] + relThumbPosition * (contentSize - scrollbar.__initializedFlickable[propSize]) - leadingContentMargin; //don't use pageSize, we don't know if the scrollbar is edge to edge!;
423 }
424 }
425
426 //each scrollbar connects to both width and height because
427 //we want to show both the scrollbar in both cases
428 Connections {
429 target: (flickableItem && initialized) ? flickableItem : null
430 function onHeightChanged() { flashScrollbar() }
431 function onWidthChanged() { flashScrollbar() }
432 //Use onTotalContentSizeChanged instead of binding to contentWidth/Height changes
433 //to trigger the scrollbar hinting when the scrollable content is resized
434 }
435
436 SmoothedAnimation {
437 id: scrollAnimation
438 objectName: "scrollAnimation"
439
440 //duration and easing coming from UX spec
441 duration: LomiriAnimation.SlowDuration
442 easing.type: Easing.InOutCubic
443 target: flickableItem
444 property: scrollbarUtils.propContent
445 //when the listview has variable size delegates the contentHeight estimation done by ListView
446 //could make us overshoot, especially when going from top to bottom of the list or viceversa.
447 //We call returnToBounds to mitigate that.
448 onRunningChanged: if (!running && flickableItem) {
449 flickableItem.returnToBounds()
450 }
451 }
452
453
454 // The thumb appears whenever the mouse gets close enough to the scrollbar
455 // and disappears after being for a long enough time far enough of it
456 MouseArea {
457 id: proximityArea
458
459 //like containsMouse, but unlike it this property is not true when the user
460 //taps on the area using touchscreen
461 property bool containsMouseDevice: false
462
463 anchors.fill: parent
464 propagateComposedEvents: true
465 preventStealing: false
466 enabled: isScrollable && interactive
467 hoverEnabled: isScrollable && interactive
468 Mouse.ignoreSynthesizedEvents: true
469 Mouse.onEntered: {
470 isMouseConnected = true
471 proximityArea.containsMouseDevice = true
472 }
473 Mouse.onExited: {
474 overshootTimer.restart();
475 proximityArea.containsMouseDevice = false
476 }
477 onPressed: { mouse.accepted = false }
478
479 Timer {
480 id: overshootTimer
481 interval: scrollbarThicknessAnimation.duration * 10
482 }
483
484 // represents the visible area of the scrollbar where
485 //slider and thumb connector are placed
486 Rectangle {
487 id: background
488 anchors.fill: flowContainer
489 objectName: "background"
490 visible: flowContainer.showBackground
491 }
492
493 Flow {
494 id: flowContainer
495 property bool showSteppers: false
496 property bool showBackground: false
497 property bool showCornerRect: false
498 //thickness of trough and thumb
499 property real thickness: 0
500 property real thumbThickness: 0
501
502 property real proximityThickness: (isVertical) ? styledItem.width - thickness
503 : styledItem.height - thickness
504 anchors {
505 fill: parent
506 leftMargin: (!isVertical || frontAligned) ? 0.0 : flowContainer.proximityThickness
507 rightMargin: (!isVertical || rearAligned) ? 0.0 : flowContainer.proximityThickness
508 topMargin: (isVertical || topAligned) ? 0.0 : flowContainer.proximityThickness
509 bottomMargin: (isVertical || bottomAligned) ? 0.0 : flowContainer.proximityThickness
510 }
511 clip: true
512 flow: (isVertical) ? Flow.TopToBottom : Flow.LeftToRight
513
514 Item {
515 id: trough
516 //account for 2 steppers at the end of the scrollbar
517 width: isVertical ? parent.width
518 : parent.width - steppersMouseArea.width
519 height: isVertical ? parent.height - steppersMouseArea.height
520 : parent.height
521 objectName: "trough"
522
523 Rectangle {
524 id: slider
525 objectName: "interactiveScrollbarThumb"
526
527 //this property is true if the user could be starting a drag (i.e. pressed inside the thumb)
528 //but drag.active is not true yet (because that requires dragging >0 pixels)
529 //Usecase: thumb is at the top, user drags towards the top and
530 //goes out of window -> drag.active won't trigger because the thumb can't move by even 1px,
531 //but we still want the logic to know that user could be trying to drag (by moving the
532 //mouse down again afterwards, for example)
533 //
534 //NOTE: starting a drag while the scrollbars are in steppers mode is considered a mouse drag
535 //(because we want the style to stay to steppers) even when started with touch!
536 property bool mouseDragging: false
537 property bool touchDragging: false
538
539 //the proportional position of the thumb relative to the trough it can move into
540 //It is 0 when the thumb is at its minimum value, and 1 when it is at its maximum value
541 property real relThumbPosition : isVertical
542 ? (slider.y - thumbsExtremesMargin) / (trough.height - 2*thumbsExtremesMargin - slider.height)
543 : (slider.x - thumbsExtremesMargin) / (trough.width - 2*thumbsExtremesMargin - slider.width)
544
545 //we just need to call this onPressed, so we shouldn't use a binding for it, which would get
546 //reevaluated any time one of the properties changes.
547 //+ having it as a binding has the sideeffect that when we query its value from inside onPressed
548 //it may not be using the most up-to-date value for *all* the variables it uses, and that would
549 //break the logic
550 function containsTouch() {
551 var touchX = slider.mapFromItem(thumbArea, thumbArea.mouseX, thumbArea.mouseY).x
552 var touchY = slider.mapFromItem(thumbArea, thumbArea.mouseX, thumbArea.mouseY).y
553 //relaxed checks on the position of the touch, to allow some error
554 if (touchX >= -visuals.touchDragStartMargin
555 && touchX <= slider.width + visuals.touchDragStartMargin
556 && touchY >= -visuals.touchDragStartMargin
557 && touchY <= slider.height + visuals.touchDragStartMargin) {
558 return true
559 } else {
560 return false
561 }
562 }
563
564 anchors {
565 verticalCenter: (isVertical) ? undefined : trough.verticalCenter
566 horizontalCenter: (isVertical) ? trough.horizontalCenter : undefined
567 }
568 x: (isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, thumbsExtremesMargin, trough.width - slider.width - thumbsExtremesMargin)
569 y: (!isVertical) ? 0 : scrollbarUtils.sliderPos(styledItem, thumbsExtremesMargin, trough.height - slider.height - thumbsExtremesMargin)
570 radius: visuals.sliderRadius
571 color: Qt.rgba(sliderColor.r, sliderColor.g, sliderColor.b,
572 sliderColor.a * (visuals.draggingThumb
573 ? 1.0
574 : (thumbArea.hoveringThumb ? 0.7 : 0.4 )))
575
576 //visible: HANDLED BY STATES
577
578 //This is to stop the scrollbar from changing size while being dragged when we have listviews
579 //with delegates of variable size (in those cases, contentWidth/height changes as the user scrolls
580 //because of the way ListView estimates the size of the out-of-views delegates
581 //and that would trigger resizing of the thumb)
582 //NOTE: otoh, we want the x/y binding to apply (even though it will cause a small flickering occasionally)
583 //even while dragging, because that guarantees the view is at the top when the user drags to the top
584 Binding {
585 when: !visuals.draggingThumb
586 target: slider
587 property: "width"
588 value: (isVertical) ? flowContainer.thumbThickness
589 : scrollbarUtils.sliderSize(styledItem, visuals.minimumSliderSize, trough.width - 2 * thumbsExtremesMargin)
590 restoreMode: Binding.RestoreNone
591 }
592 Binding {
593 when: !visuals.draggingThumb
594 target: slider
595 property: "height"
596 value: (!isVertical) ? flowContainer.thumbThickness
597 : scrollbarUtils.sliderSize(styledItem, visuals.minimumSliderSize, trough.height - 2 * thumbsExtremesMargin)
598 restoreMode: Binding.RestoreNone
599 }
600 }
601
602 //we reuse the MouseArea for touch interactions as well, because MultiTouchPointArea
603 //can't propagate touch events underneath, if not consumed
604 //(it would also require implementing drag as it doesn't have it)
605 MouseArea {
606 id: thumbArea
607 objectName: "thumbArea"
608
609 property real originXAtDragStart: 0
610 property real originYAtDragStart: 0
611 property real contentXAtDragStart: 0
612 property real contentYAtDragStart: 0
613
614 //cache input device position in order to ignore mouse/touch
615 //moves which are shorter than a threshold. This is needed due
616 //to the low precision of some touchscreen devices, to avoid
617 //shaking the view while slowly dragging via touch
618 property real previousX: 0
619 property real previousY: 0
620
621
622 //THIS BEHAVIOUR IS CURRENTLY DISABLED, SEE __disableDragResetFeature
623 //following UX spec, if during a drag the mouse goes far away from the scrollbar
624 //we want to temporarily lock the drag and reset the scrollbar to where it was when
625 //the drag started, WITHOUT losing the drag (if the mouse gets back within the distance
626 //threshold, the drag has to resum without the need to release and pressHold again)
627 property bool lockDrag: false
628
629 property bool hoveringThumb: false
630
631 function initDrag(isMouse) {
632 __disableStateBinding = true
633 thumbArea.saveFlickableScrollingState()
634 if (isMouse) {
635 slider.mouseDragging = true
636 visuals.state = "steppers"
637 } else {
638 slider.touchDragging = true
639 visuals.state = "thumb"
640 }
641 }
642
643 function resetDrag() {
644 if (thumbArea.lockDrag) thumbArea.lockDrag = false
645 slider.mouseDragging = false
646 slider.touchDragging = false
647 __disableStateBinding = false
648 }
649
650 function handlePress(mouseX, mouseY) {
651 if (!thumbArea.pressed) return
652
653 var mouseScrollingProp = isVertical ? mouseY : mouseX
654 if (mouseScrollingProp < slider[scrollbarUtils.propCoordinate]) {
655 scrollBy(-pageSize*visuals.longScrollingRatio, true)
656 } else if (mouseScrollingProp > slider[scrollbarUtils.propCoordinate] + slider[scrollbarUtils.propSize]) {
657 scrollBy(pageSize*visuals.longScrollingRatio, true)
658 }
659 }
660
661 //we consider hover if it's inside the TROUGH along the scrolling axis
662 //and inside the THUMB along the non-scrolling axis
663 //NOTE: mouseX and mouseY are assumed to be relative to thumbArea
664 function handleHover(mouseX, mouseY) {
665 //NOTE: we're assuming thumbArea has same size/position as the trough.
666 //Use mapToItem to map the coordinates if that assumption falls (i.e. to implement
667 //a larger touch detection area around the thumb)
668 var mouseScrollingProp = isVertical ? mouseY : mouseX
669 var otherProp = isVertical ? mouseX : mouseY
670
671 //don't count as hover if the user is already press-and-holding
672 //the trough to scroll page by page
673 hoveringThumb = !(pressHoldTimer.running && pressHoldTimer.startedBy === thumbArea)
674 && mouseScrollingProp >= slider[scrollbarUtils.propCoordinate] &&
675 mouseScrollingProp <= slider[scrollbarUtils.propCoordinate] + slider[scrollbarUtils.propSize] &&
676 otherProp >= 0 && otherProp <= trough[scrollbarUtils.otherPropSize]
677 }
678
679 function saveFlickableScrollingState() {
680 originXAtDragStart = flickableItem.originX
681 originYAtDragStart = flickableItem.originY
682 contentXAtDragStart = flickableItem.contentX
683 contentYAtDragStart = flickableItem.contentY
684 }
685
686 function resetFlickableToPreDragState() {
687 //NOTE: when dealing with ListViews which have
688 //delegates that are highly variable in size originX/Y and contentWidth/Height
689 //change based on "out-of-view delegates" size estimation done by ListView.
690 //In that case, we can't do anything to bring back the view to where it was
691 //when the drag started, so we'd better not do anything instead of doing something
692 //which we're sure will be wrong
693 if (originXAtDragStart != flickableItem.originX
694 || originYAtDragStart != flickableItem.originY) {
695 return
696 }
697 flickableItem.contentX = contentXAtDragStart
698 flickableItem.contentY = contentYAtDragStart
699 }
700
701 function cacheMousePosition(mouse) {
702 previousX = mouse.x
703 previousY = mouse.y
704 }
705
706 //NOTE: remember to update the handleHover check if you add anchor margins
707 anchors.fill: trough
708
709 enabled: isScrollable && interactive && __canFitSteppersAndShorterThumb
710 onPressed: {
711 cacheMousePosition(mouse)
712 //potentially we allow using touch to trigger mouse interactions, in case the
713 //mouse has previously hovered over the area and activated the steppers style
714 //checking for the value of visuals.state wouldn't be useful here, the value could be
715 //"hidden" even if the values have only just started transitioning from "steppers" state
716
717 if (firstStepper.visible) {
718 var mouseScrollingProp = isVertical ? mouseY : mouseX
719 //don't start the press and hold timer to avoid conflicts with the drag
720 if (mouseScrollingProp < slider[scrollbarUtils.propCoordinate] ||
721 mouseScrollingProp > (slider[scrollbarUtils.propCoordinate] + slider[scrollbarUtils.propSize])) {
722 handlePress(mouseX, mouseY)
723 pressHoldTimer.startTimer(thumbArea)
724 } else {
725 //we can't tell whether the drag is started from mouse or touch
726 //(unless we add an additional multipointtoucharea and reimplement drag
727 //but MPTArea can't ignore touch events so we wouldn't be able to propagate those)
728 //so we assume that it's a mouse drag if the mouse is within the proximity area
729 //when the mouse is dragged
730 initDrag(true)
731 }
732 } else if (visuals.veryLongContentItem && slider.containsTouch()) {
733 initDrag(false)
734 } else {
735 //propagate otherwise
736 mouse.accepted = false
737 }
738 }
739 onPositionChanged: {
740 //avoid shaking the view when the user is draggin via touch
741 if (Math.abs(mouse.x - previousX) < visuals.dragThreshold
742 && Math.abs(mouse.y - previousY) < visuals.dragThreshold) {
743 return
744 }
745 cacheMousePosition(mouse)
746
747 if (pressedButtons == Qt.LeftButton && (slider.mouseDragging || slider.touchDragging)) {
748 if (!__disableDragResetFeature &&
749 ((isVertical && Math.abs(mouse.x - thumbArea.x) >= flowContainer.thickness * 10)
750 || (!isVertical && Math.abs(mouse.y - thumbArea.y) >= flowContainer.thickness * 10))) {
751 //don't reset it if the user is already scrolling the view (via keyboard or similar)
752 if (!scrollAnimation.running) {
753 resetFlickableToPreDragState()
754 thumbArea.lockDrag = true
755 }
756 } else {
757 //don't clash with scrolling animation
758 if (scrollAnimation.running) scrollAnimation.stop()
759
760 if (thumbArea.lockDrag) thumbArea.lockDrag = false
761 visuals.drag()
762 }
763 }
764 }
765 onCanceled: {
766 hoveringThumb = false
767 resetDrag()
768 pressHoldTimer.stop()
769 }
770 onReleased: {
771 //don't call handleHover here as touch release also triggers this handler
772 //see bug #1616868
773 resetDrag()
774 pressHoldTimer.stop()
775 }
776 drag {
777 //don't start a drag while we're scrolling using press and hold
778 target: pressHoldTimer.running ? undefined : slider
779 axis: (isVertical) ? Drag.YAxis : Drag.XAxis
780 minimumY: lockDrag ? slider.y : thumbsExtremesMargin
781 maximumY: lockDrag ? slider.y : trough.height - slider.height - thumbsExtremesMargin
782 minimumX: lockDrag ? slider.x : thumbsExtremesMargin
783 maximumX: lockDrag ? slider.x : trough.width - slider.width - thumbsExtremesMargin
784 onActiveChanged: {
785 if (drag.active) {
786 //drag.active is true only when the target has been moved by >0 pixels (if threshold == 0)
787 //that means if the bar is at the top and user drags it upwards, drag.active won't change,
788 //but the user will still be able to leave the app window from above, move mouse to the left (or right)
789 //enough to trigger the drag lock logic, and then come back in the window and move downwards.
790 //At *that* point drag.active will become true, but we don't want to actually drag the thumb
791 //because the mouse is still outside the area where thumb is follow input device movements
792 if (!lockDrag) {
793 visuals.drag()
794 }
795 } else {
796 resetDrag()
797 }
798 }
799
800 //NOTE: we need threshold to be 0, otherwise it will be
801 //impossible to drag contentItems which have a size so that
802 //"flickableItem.height - contentItem.height < threshold"!!!
803 threshold: 0
804 }
805
806 //logic to support different colour on hovering
807 hoverEnabled: true
808 //This means this mouse filter will only handle real mouse events!
809 //i.e. the synthesized mouse events created when you use
810 //touchscreen will not trigger it! This way we can have logic that
811 //will not trigger when using touch
812 Mouse.ignoreSynthesizedEvents: true
813 Mouse.enabled: true
814 Mouse.onEntered: {
815 hoveringThumb = false
816 handleHover(event.x, event.y)
817 }
818 Mouse.onPositionChanged: {
819 //We need to update the hover state also onPosChanged because this area
820 //covers the whole trough, not just the thumb, so entered/exited are not enough
821 //e.g. when mouse moves from inside the thumb to inside the trough, or when you
822 //click on the trough and the thumb scrolls and goes under the mouse cursor
823 handleHover(mouse.x, mouse.y)
824 }
825 Mouse.onExited: {
826 hoveringThumb = false
827 }
828 Mouse.onReleased: {
829 handleHover(mouse.x, mouse.y)
830 }
831
832 Timer {
833 id: pressHoldTimer
834
835 //This var is needed to reuse the same timer to handle
836 //both thumb and steppers press-and-hold
837 //NOTE: the item MUST provide a handlePress method
838 property MouseArea startedBy
839 property bool firstTrigger: true
840
841 function startTimer(item) {
842 startedBy = item
843 start()
844 }
845
846 interval: firstTrigger ? 500 : 250
847 onTriggered: {
848 if (firstTrigger) firstTrigger = false
849 if (startedBy !== undefined) {
850 startedBy.handlePress(startedBy.mouseX, startedBy.mouseY)
851 } else {
852 console.log("Congrats, you have just found a bug! Press and hold timer is running without knowing the item that started it. Please report this.")
853 }
854 }
855 repeat: true
856 onRunningChanged: {
857 if (!running) {
858 firstTrigger = true
859 }
860 }
861 }
862 }
863 }
864
865 MouseArea {
866 id: steppersMouseArea
867 objectName: "steppersMouseArea"
868 //size is handled by the states
869
870 property bool hoveringFirstStepper: false
871 property bool hoveringSecondStepper: false
872
873 function handlePress() {
874 var mappedCoordFirstStepper = mapToItem(firstStepper, mouseX, mouseY)
875 var mappedCoordSecondStepper = mapToItem(secondStepper, mouseX, mouseY)
876
877 if (firstStepper.contains(Qt.point(mappedCoordFirstStepper.x, mappedCoordFirstStepper.y))) {
878 scrollBy(-pageSize * visuals.shortScrollingRatio, true)
879 } else if (secondStepper.contains(Qt.point(mappedCoordSecondStepper.x, mappedCoordSecondStepper.y))) {
880 scrollBy(pageSize * visuals.shortScrollingRatio, true)
881 }
882 }
883
884 function handleHover(event) {
885 var mappedCoordFirstStepper = mapToItem(firstStepper, event.x, event.y)
886 var mappedCoordSecondStepper = mapToItem(secondStepper, event.x, event.y)
887
888 if (firstStepper.contains(Qt.point(mappedCoordFirstStepper.x, mappedCoordFirstStepper.y))) {
889 hoveringFirstStepper = true
890 hoveringSecondStepper = false
891 } else if (secondStepper.contains(Qt.point(mappedCoordSecondStepper.x, mappedCoordSecondStepper.y))) {
892 hoveringFirstStepper = false
893 hoveringSecondStepper = true
894 } else {
895 hoveringFirstStepper = false
896 hoveringSecondStepper = false
897 }
898 }
899
900 enabled: isScrollable && interactive
901 visible: flowContainer.showSteppers
902
903 preventStealing: true
904 //Logic that handles hovering on steppers
905 hoverEnabled: true
906 Mouse.enabled: true
907 Mouse.ignoreSynthesizedEvents: true
908 Mouse.onEntered: {
909 hoveringFirstStepper = false
910 hoveringSecondStepper = false
911 handleHover(event)
912 }
913 Mouse.onPositionChanged: {
914 handleHover(mouse)
915 }
916 Mouse.onExited: {
917 hoveringFirstStepper = false
918 hoveringSecondStepper = false
919 }
920 Mouse.onPressed: {
921 //This additional trigger of the hovering logic is useful especially in testing
922 //environments, where simulating a click on the first stepper will generate onEntered,
923 //but then clicking on the second one (without a mouseMove) won't, because they're
924 //handled by the same mouse area, which is already hovered. With this trigger
925 //we ensure that the hovering logic is correct even when there are no mouse moves between
926 //clicks on different steppers (like it happens in tst_actionSteppers test).
927 handleHover(mouse)
928 }
929 Mouse.onReleased: {
930 handleHover(mouse)
931 }
932
933 //We don't change the size of the images because we let the image reader figure the size out,
934 //though that means we have to hide the images somehow while the mousearea is visible but has
935 //null size. We choose to enable clipping here instead of creating bindings on images' visible prop
936 clip: true
937 onPressed: {
938 handlePress()
939 pressHoldTimer.startTimer(steppersMouseArea)
940 }
941 onReleased: pressHoldTimer.stop()
942 onCanceled: pressHoldTimer.stop()
943
944 Rectangle {
945 id: firstStepper
946 objectName: "firstStepper"
947 anchors {
948 //it's left in both cases, otherwise using RTL
949 //would break the layout of the arrow
950 left: parent ? parent.left : undefined
951 right: isVertical ? (parent ? parent.right : undefined) : undefined
952 top: parent ? parent.top : undefined
953 bottom: !isVertical ? (parent ? parent.bottom : undefined) : undefined
954 }
955 color: (steppersMouseArea.hoveringFirstStepper && flickableItem && !flickableItem[scrollbarUtils.propAtBeginning])
956 ? Qt.rgba(stepperBgColor.r, stepperBgColor.g, stepperBgColor.b,
957 stepperBgColor.a * (steppersMouseArea.pressed ? __stepperBgOpacityOnPressed : __stepperBgOpacityOnHover))
958 : "transparent"
959 visible: parent.visible
960 clip: true
961
962 Binding {
963 target: firstStepper
964 property: "height"
965 when: isVertical
966 value: visible ? __targetStepperSize : 0
967 }
968 Binding {
969 target: firstStepper
970 property: "width"
971 when: !isVertical
972 value: visible ? __targetStepperSize : 0
973 }
974 Icon {
975 objectName: "firstStepperIcon"
976 anchors.centerIn: parent
977 width: __stepperAssetWidth
978 rotation: isVertical ? 180 : 90
979 //NOTE: Qt.resolvedUrl was removed because it does not seem to be needed and
980 //the QML profiler showed it's relatively expensive
981 source: visible ? "../artwork/toolkit_scrollbar-stepper.svg" : ""
982 asynchronous: true
983 color: Qt.rgba(sliderColor.r, sliderColor.g, sliderColor.b,
984 sliderColor.a * ((flickableItem && flickableItem[scrollbarUtils.propAtBeginning])
985 ? __stepperImgOpacityDisabled
986 : (steppersMouseArea.hoveringFirstStepper
987 ? (steppersMouseArea.pressed ? __stepperImgOpacityOnPressed : __stepperImgOpacityOnHover)
988 : __stepperImgOpacityNormal)))
989 }
990 }
991 Rectangle {
992 id: secondStepper
993 objectName: "secondStepper"
994 anchors {
995 left: isVertical ? parent.left : firstStepper.right
996 right: isVertical ? parent.right : undefined
997 top: !isVertical ? parent.top : firstStepper.bottom
998 bottom: !isVertical ? parent.bottom : undefined
999 }
1000 color: (steppersMouseArea.hoveringSecondStepper && flickableItem && !flickableItem[scrollbarUtils.propAtEnd])
1001 ? Qt.rgba(stepperBgColor.r, stepperBgColor.g, stepperBgColor.b,
1002 stepperBgColor.a * (steppersMouseArea.pressed ? __stepperBgOpacityOnPressed : __stepperBgOpacityOnHover))
1003 : "transparent"
1004 clip: true
1005 visible: parent.visible
1006
1007 Binding {
1008 target: secondStepper
1009 property: "height"
1010 when: isVertical
1011 value: visible ? __targetStepperSize : 0
1012 }
1013 Binding {
1014 target: secondStepper
1015 property: "width"
1016 when: !isVertical
1017 value: visible ? __targetStepperSize : 0
1018 }
1019 Icon {
1020 id: icon
1021 objectName: "secondStepperIcon"
1022 anchors.centerIn: parent
1023 width: __stepperAssetWidth
1024 rotation: isVertical ? 0 : -90
1025 //NOTE: Qt.resolvedUrl was removed because it does not seem to be needed and
1026 //the QML profiler showed it's relatively expensive
1027 source: visible ? "../artwork/toolkit_scrollbar-stepper.svg" : ""
1028 asynchronous: true
1029 color: Qt.rgba(sliderColor.r, sliderColor.g, sliderColor.b,
1030 sliderColor.a * ((flickableItem && flickableItem[scrollbarUtils.propAtEnd])
1031 ? __stepperImgOpacityDisabled
1032 : (steppersMouseArea.hoveringSecondStepper
1033 ? (steppersMouseArea.pressed ? __stepperImgOpacityOnPressed : __stepperImgOpacityOnHover)
1034 : __stepperImgOpacityNormal)))
1035 }
1036 }
1037 }
1038 }
1039
1040 //just a hack: a rectangle which covers the corner where scrollbars meet, when they're in steppers style
1041 Rectangle {
1042 id: cornerRect
1043 //NOTE: I tried grouping styledItem.__buddyScrollbar && styledItem.__buddyScrollbar.__styleInstance
1044 // && styledItem.__buddyScrollbar.__styleInstance.isScrollable in 1 property and use that for
1045 //all the anchors, but it turned out it makes the instantiation 2-3% slower!!!
1046 anchors.left: {
1047 if (styledItem.__buddyScrollbar && styledItem.__buddyScrollbar.__styleInstance
1048 && styledItem.__buddyScrollbar.__styleInstance.isScrollable) {
1049 if (isVertical) {
1050 return flowContainer.left
1051 } else {
1052 if (styledItem.__buddyScrollbar.align === Qt.AlignTrailing) {
1053 return flowContainer.right
1054 } else
1055 return undefined
1056 }
1057 }
1058 }
1059 anchors.right: {
1060 if (styledItem.__buddyScrollbar && styledItem.__buddyScrollbar.__styleInstance
1061 && styledItem.__buddyScrollbar.__styleInstance.isScrollable) {
1062 if (isVertical) {
1063 return flowContainer.right
1064 } else {
1065 if (styledItem.__buddyScrollbar.align === Qt.AlignLeading) {
1066 return flowContainer.left
1067 } else {
1068 return undefined
1069 }
1070 }
1071 }
1072 }
1073 anchors.top: {
1074 if (styledItem.__buddyScrollbar && styledItem.__buddyScrollbar.__styleInstance
1075 && styledItem.__buddyScrollbar.__styleInstance.isScrollable) {
1076 if (isVertical) {
1077 if (styledItem.__buddyScrollbar.align === Qt.AlignBottom) {
1078 return flowContainer.bottom
1079 } else {
1080 return undefined
1081 }
1082 } else {
1083 return undefined
1084 }
1085 }
1086 }
1087 anchors.bottom: {
1088 if (styledItem.__buddyScrollbar && styledItem.__buddyScrollbar.__styleInstance
1089 && styledItem.__buddyScrollbar.__styleInstance.isScrollable) {
1090 if (isVertical) {
1091 if (styledItem.__buddyScrollbar.align === Qt.AlignTop) {
1092 return flowContainer.top
1093 } else {
1094 return undefined
1095 }
1096 } else {
1097 return flowContainer.bottom
1098 }
1099 }
1100 }
1101 //REMEMBER: the thickness animates during state changes,
1102 //so we want to bind the right side to the thickness of the scrollbar
1103 width: isVertical ? flowContainer.thickness : troughThicknessSteppersStyle
1104 height: isVertical ? troughThicknessSteppersStyle : flowContainer.thickness
1105 color: background.color
1106 visible: flowContainer.showCornerRect
1107 && styledItem.__buddyScrollbar
1108 && styledItem.__buddyScrollbar.__styleInstance.isScrollable
1109 }
1110 }
1111
1112 Binding {
1113 when: !__disableStateBinding
1114 target: visuals
1115 property: 'state'
1116 restoreMode: Binding.RestoreNone
1117 value: {
1118 if (!isScrollable && !alwaysOnScrollbars) {
1119 return '';
1120 }
1121 else if (overlay) {
1122 //we use the check on running to avoid to make it so that the scrollbar completes the transition
1123 //to the steppers (or thumb) state even when the mouse exits the area prematurely (e.g. when the mouse
1124 //overshoots, without the .running check the scrollbar would stop the growing transition halfway and
1125 //go back to hidden mode)
1126 // NOTE: we use steppersMouseArea.pressed to keep steppers state even when the user presses on the steppers
1127 // then holds and moves the pointer outside the window (probably by mistake) without releasing
1128 if (proximityArea.containsMouseDevice || steppersMouseArea.pressed
1129 //if the user is press-and-holding on the trough to scroll page by page
1130 || (pressHoldTimer.running && pressHoldTimer.startedBy === thumbArea)
1131 || (draggingThumb && slider.mouseDragging) || __overshootTimer.running) {
1132 return 'steppers'
1133 } else if (thumbStyleFlag
1134 || (draggingThumb && slider.touchDragging) || __overshootTimer.running) {
1135 return 'thumb'
1136 } else if ((flickableItem && flickableItem.moving) || scrollAnimation.running || transitionToIndicator.running) {
1137 return 'indicator';
1138 } else return 'hidden';
1139 } else {
1140 return 'steppers'
1141 }
1142 }
1143 }
1144
1145 states: [
1146 State {
1147 name: 'hidden'
1148 extend: ''
1149 PropertyChanges {
1150 target: visuals
1151 opacity: overlayOpacityWhenHidden
1152 }
1153 },
1154 State {
1155 name: 'shown'
1156 PropertyChanges {
1157 target: visuals
1158 opacity: overlayOpacityWhenShown
1159 }
1160 PropertyChanges {
1161 target: slider
1162 visible: __canFitShorterThumb
1163 }
1164 },
1165 State {
1166 name: 'indicator'
1167 extend: 'shown'
1168 PropertyChanges {
1169 target: flowContainer
1170 thickness: troughThicknessIndicatorStyle
1171 thumbThickness: indicatorThickness
1172 showSteppers: false
1173 showBackground: alwaysOnScrollbars
1174 showCornerRect: false
1175 }
1176 PropertyChanges {
1177 target: steppersMouseArea
1178 width: 0
1179 height: 0
1180 }
1181 PropertyChanges {
1182 target: background
1183 color: Qt.rgba(troughColorThumbStyle.r,
1184 troughColorThumbStyle.g,
1185 troughColorThumbStyle.b,
1186 troughColorThumbStyle.a * 0.2)
1187 }
1188 },
1189 State {
1190 name: 'thumb'
1191 extend: 'shown'
1192 PropertyChanges {
1193 target: flowContainer
1194 thickness: visuals.troughThicknessThumbStyle
1195 thumbThickness: visuals.thumbThickness
1196 showSteppers: false
1197 showBackground: alwaysOnScrollbars
1198 showCornerRect: false
1199 }
1200 PropertyChanges {
1201 target: steppersMouseArea
1202 width: 0
1203 height: 0
1204 }
1205 PropertyChanges {
1206 target: background
1207 color: Qt.rgba(troughColorThumbStyle.r,
1208 troughColorThumbStyle.g,
1209 troughColorThumbStyle.b,
1210 troughColorThumbStyle.a * 0.2)
1211 }
1212 },
1213 State {
1214 name: 'steppers'
1215 extend: 'shown'
1216 PropertyChanges {
1217 target: flowContainer
1218 thickness: visuals.troughThicknessSteppersStyle
1219 thumbThickness: visuals.thumbThickness
1220 showSteppers: visuals.useSteppers
1221 showBackground: true
1222 showCornerRect: true
1223 }
1224 PropertyChanges {
1225 target: steppersMouseArea
1226 width: isVertical ? flowContainer.width
1227 : (firstStepper.visible ? firstStepper.width : 0)
1228 + (secondStepper.visible ? secondStepper.width : 0)
1229 height: isVertical ? (firstStepper.visible ? firstStepper.height : 0)
1230 + (secondStepper.visible ? secondStepper.height : 0)
1231 : trough.height
1232 }
1233 PropertyChanges {
1234 target: background
1235 color: Qt.rgba(troughColorSteppersStyle.r,
1236 troughColorSteppersStyle.g,
1237 troughColorSteppersStyle.b,
1238 troughColorSteppersStyle.a * 0.9)
1239 }
1240 PropertyChanges {
1241 target: slider
1242 visible: __canFitSteppersAndShorterThumb
1243 }
1244 }
1245 ]
1246 transitions: [
1247 Transition {
1248 from: ''
1249 to: 'shown'
1250 NumberAnimation {
1251 target: visuals
1252 property: "opacity"
1253 duration: scrollbarFadeInAnimation.duration
1254 easing: scrollbarFadeInAnimation.easing
1255 }
1256 },
1257 Transition {
1258 from: 'thumb,steppers'
1259 to: 'indicator'
1260 id: collapsingTransition
1261 SequentialAnimation {
1262 //don't pause the animation if we're showing the *hint* and the hintingStyle changes from
1263 //thumb/steppers to indicator
1264 PauseAnimation { id: scrollbarCollapseAnim; duration: __disableStateBinding ? 0 : scrollbarCollapsePause }
1265 PropertyAction {
1266 target: steppersMouseArea
1267 properties: isVertical ? "width" : "height"
1268 }
1269 ParallelAnimation {
1270 NumberAnimation {
1271 target: flowContainer
1272 properties: "thickness,thumbThickness"
1273 duration: scrollbarThicknessAnimation.duration
1274 easing: scrollbarThicknessAnimation.easing
1275 }
1276 NumberAnimation {
1277 target: steppersMouseArea
1278 properties: isVertical ? "height" : "width"
1279 duration: scrollbarThicknessAnimation.duration
1280 easing: scrollbarThicknessAnimation.easing
1281 }
1282 ColorAnimation { target: background; duration: scrollbarThicknessAnimation.duration }
1283 }
1284 PropertyAction {
1285 target: flowContainer
1286 properties: "showSteppers,showBackground,showCornerRect"
1287 }
1288 PropertyAction {
1289 target: slider
1290 properties: "visible"
1291 }
1292 }
1293 },
1294 Transition {
1295 id: transitionToIndicator
1296 from: '*'
1297 to: 'indicator'
1298 SequentialAnimation {
1299 ParallelAnimation {
1300 NumberAnimation {
1301 target: visuals
1302 property: "opacity"
1303 duration: scrollbarThicknessAnimation.duration
1304 easing: scrollbarThicknessAnimation.easing
1305 }
1306 NumberAnimation {
1307 target: flowContainer
1308 properties: "thickness,thumbThickness"
1309 duration: scrollbarThicknessAnimation.duration
1310 easing: scrollbarThicknessAnimation.easing
1311 }
1312 ColorAnimation { target: background; duration: scrollbarThicknessAnimation.duration }
1313 }
1314 //this covers the case when scrollbar is still transitioning from steppers style to hidden,
1315 //but the transition is not yet finished, and we flick the view. At that point the state is "hidden"
1316 //but the properties are still transitioning from steppers style to hidden. If a change to "indicator"
1317 //style is triggered while the properties are still transitioning, we have to handle those properties
1318 //as well to avoid immediate changes in the UI.
1319 PropertyAction {
1320 target: flowContainer
1321 properties: "showSteppers,showBackground,showCornerRect"
1322 }
1323 PropertyAction {
1324 target: slider
1325 properties: "visible"
1326 }
1327 }
1328 },
1329 Transition {
1330 id: growingTransition
1331 objectName: "indicatorToThumbSteppersTransition"
1332 from: 'indicator'
1333 to: 'thumb,steppers'
1334 reversible: true
1335 SequentialAnimation {
1336 PropertyAction {
1337 target: flowContainer
1338 properties: "showSteppers,showBackground,showCornerRect"
1339 }
1340 PropertyAction {
1341 target: steppersMouseArea
1342 properties: isVertical ? "width" : "height"
1343 }
1344 ParallelAnimation {
1345 NumberAnimation {
1346 target: visuals
1347 property: "opacity"
1348 duration: scrollbarThicknessAnimation.duration
1349 easing: scrollbarThicknessAnimation.easing
1350 }
1351 NumberAnimation {
1352 target: flowContainer
1353 properties: "thickness,thumbThickness"
1354 duration: scrollbarThicknessAnimation.duration
1355 easing: scrollbarThicknessAnimation.easing
1356 }
1357 NumberAnimation {
1358 target: steppersMouseArea
1359 properties: isVertical ? "height" : "width"
1360 duration: scrollbarThicknessAnimation.duration
1361 easing: scrollbarThicknessAnimation.easing
1362 }
1363 ColorAnimation { target: background; duration: scrollbarThicknessAnimation.duration }
1364 }
1365 PropertyAction {
1366 target: slider
1367 properties: "visible"
1368 }
1369 }
1370 },
1371 Transition {
1372 id: steppersTransition
1373 objectName: "anythingToThumbSteppersTransition"
1374 from: '*'
1375 to: 'thumb,steppers'
1376 SequentialAnimation {
1377 PropertyAction {
1378 target: flowContainer
1379 properties: "showSteppers,showBackground,showCornerRect"
1380 }
1381 PropertyAction {
1382 target: steppersMouseArea
1383 properties: isVertical ? "width" : "height"
1384 }
1385 //hide the thumb (if needed) before the steppers expand (otherwise you'd see the
1386 //thumb sliding out of screen before hiding)
1387 PropertyAction {
1388 target: slider
1389 properties: "visible"
1390 }
1391 ParallelAnimation {
1392 NumberAnimation {
1393 target: visuals
1394 property: "opacity"
1395 duration: scrollbarThicknessAnimation.duration
1396 easing: scrollbarThicknessAnimation.easing
1397 }
1398 NumberAnimation {
1399 target: flowContainer
1400 properties: "thickness,thumbThickness"
1401 //the check on opacity is needed because this transition is also run when a transition to "hidden"
1402 //is still in progress! For example: transition from indicator to hidden is started, the state is set to "hidden"
1403 //immediately, but the transition still has to happen. If, in this situation, we trigger a state change from "hidden"
1404 //to another state (thumb/steppers) this transition is fired, but the fact that state is "hidden" is not
1405 //really informative, we need to know if the stuff is actually visible on screen, because we want different
1406 //animation based on whether we're animating from visible or invisible content, even if in both cases the state is "hidden"!
1407 duration: visuals.opacity > 0 ? scrollbarThicknessAnimation.duration : 0
1408 easing: scrollbarThicknessAnimation.easing
1409 }
1410 NumberAnimation {
1411 target: steppersMouseArea
1412 properties: isVertical ? "height" : "width"
1413 duration: visuals.opacity > 0 ? scrollbarThicknessAnimation.duration : 0
1414 easing: scrollbarThicknessAnimation.easing
1415 }
1416 ColorAnimation { target: background; duration: visuals.opacity > 0 ? scrollbarThicknessAnimation.duration : 0 }
1417 }
1418 }
1419 },
1420 Transition {
1421 id: hidingAnimation
1422 from: '*'
1423 to: 'hidden'
1424 SequentialAnimation {
1425 PauseAnimation { id: fadeOutPauseAnim; duration: scrollbarFadeOutPause }
1426 NumberAnimation {
1427 target: visuals
1428 property: "opacity"
1429 duration: scrollbarFadeOutAnimation.duration
1430 easing: scrollbarFadeOutAnimation.easing
1431 }
1432 PropertyAction {
1433 target: steppersMouseArea
1434 properties: "width,height"
1435 }
1436 PropertyAction {
1437 target: flowContainer
1438 properties: "thickness,thumbThickness,showSteppers,showBackground,showCornerRect"
1439 }
1440 PropertyAction {
1441 target: background
1442 properties: "color"
1443 }
1444 PropertyAction {
1445 target: slider
1446 properties: "visible"
1447 }
1448 PropertyAction {
1449 target: visuals
1450 properties: "state"
1451 }
1452 }
1453 }
1454 ]
1455 }