| 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 1.3 as Toolkit |
| 19 |
import Lomiri.Components.Private 1.3 as Private |
| 20 |
|
| 21 |
/*! |
| 22 |
\qmltype Scrollbar |
| 23 |
\inqmlmodule Lomiri.Components |
| 24 |
\ingroup lomiri |
| 25 |
\brief The Scrollbar component provides scrolling functionality for |
| 26 |
scrollable views (i.e. Flickable, ListView). |
| 27 |
|
| 28 |
\b NOTE: the Scrollbar component was revamped for OTA9 and used for the implementation |
| 29 |
of \l ScrollView. Apps targeting system version OTA9 (or newer) should use ScrollView instead |
| 30 |
of Scrollbar, as it features convergent-ready features, such as (but not limited to) |
| 31 |
keyboard input handling. |
| 32 |
|
| 33 |
The Scrollbar can be set to any flickable and has built-in anchoring setup |
| 34 |
to the attached flickable's front, rear, top or bottom. The scrollbar can |
| 35 |
also be aligned using anchors, however the built-in align functionality |
| 36 |
makes sure to have the proper alignemt applied based on theme and layout |
| 37 |
direction (RTL or LTR). |
| 38 |
|
| 39 |
The content position is driven through the attached Flickable. Therefore every |
| 40 |
style implementation should drive the position through contentX/contentY |
| 41 |
properties, depending on whether the orientation is vertical or horizontal. |
| 42 |
|
| 43 |
Example: |
| 44 |
\qml |
| 45 |
Item { |
| 46 |
ListView { |
| 47 |
id: list |
| 48 |
width: units.gu(37) |
| 49 |
height: units.gu(37) |
| 50 |
model: 30 |
| 51 |
delegate: Rectangle { |
| 52 |
width: ListView.view.width |
| 53 |
height: units.gu(5) |
| 54 |
Text { |
| 55 |
anchors.fill: parent |
| 56 |
text: "Item " + modelData |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
Scrollbar { |
| 61 |
flickableItem: list |
| 62 |
align: Qt.AlignTrailing |
| 63 |
} |
| 64 |
} |
| 65 |
\endqml |
| 66 |
|
| 67 |
\section1 Vertical Scrollbar and Flickable behaviour |
| 68 |
|
| 69 |
Since Flickable's topMargin and bottomMargin are modified by Lomiri components |
| 70 |
such as \l BottomEdgeHint and \l Header in their positioning logic, the Scrollbar component |
| 71 |
uses the value of those properties to its top and bottom anchors margins, by default. This is |
| 72 |
to prevent any overlap with BottomEdgeHint or Header (see \l {Vertical Scrollbar and Header behaviour} |
| 73 |
for more info about that). In case you need to specify custom Flickable |
| 74 |
content margins (note: we're talking about content margins, not anchors ones), you will also have to |
| 75 |
override the top and bottom anchors margins of the Scrollbar accordingly. |
| 76 |
|
| 77 |
\section1 Vertical Scrollbar and Header behaviour |
| 78 |
|
| 79 |
The thumb of the Scrollbar should not move or resize unexpectedly. It would be confusing, |
| 80 |
for instance, if it moved under the user's finger (or pointer) while he's dragging it. |
| 81 |
|
| 82 |
Because the size and position of the thumb in a scrollbar is related to the size of the |
| 83 |
trough (or track) it slides in, it is important that the trough does not resize or move while the |
| 84 |
user is interacting with the component. |
| 85 |
|
| 86 |
In the context of a \l Page with a \l Header that slides in and out dynamically when the user |
| 87 |
flicks the surface (see \l {Header::flickable}), a vertical Scrollbar that is linked |
| 88 |
to the same flickable surface as the Header behaves as described below: |
| 89 |
|
| 90 |
\list |
| 91 |
\li - when the Header is exposed, the Scrollbar component sits right below it, \ |
| 92 |
thus avoiding overlaps; |
| 93 |
|
| 94 |
\li - when the Header hides due to the user either flicking the surface or dragging \ |
| 95 |
the thumb, the trough of the Scrollbar does not resize or move, thus avoiding \ |
| 96 |
unexpected changes in thumb's position or size. As a side effect, the space \ |
| 97 |
above the Scrollbar, previously occupied by Header, stays empty until the Header \ |
| 98 |
is exposed again. |
| 99 |
|
| 100 |
\endlist |
| 101 |
\br |
| 102 |
This behaviour is intended and makes for a less confusing User Experience. |
| 103 |
*/ |
| 104 |
|
| 105 |
Toolkit.StyledItem { |
| 106 |
id: scrollbar |
| 107 |
|
| 108 |
/*! |
| 109 |
\qmlproperty Flickable Scrollbar::flickableItem |
| 110 |
This property holds the flickable item (Flickable, ListView or GridView) |
| 111 |
the Scrollbar is attached to. |
| 112 |
*/ |
| 113 |
property Flickable flickableItem: null |
| 114 |
|
| 115 |
/* |
| 116 |
This property holds the other scrollbar that is attached to the same flickable, |
| 117 |
if any. For instance, if this scrollbar is horizontal, buddyScrollbar must be set |
| 118 |
to the vertical scrollbar, if any. This is to allow a correct layout of both |
| 119 |
horizontal and vertical scrollbars when a view is scrollable in both directions. |
| 120 |
*/ |
| 121 |
//can't use property Scrollbar here as it would complain "Scrollbar instantiated recursively" |
| 122 |
property var __buddyScrollbar: null |
| 123 |
|
| 124 |
/*! |
| 125 |
\qmlproperty int Scrollbar::align |
| 126 |
The property defines the alignment of the scrollbar to the flickableItem. |
| 127 |
The implementation handles the alignment as follows: |
| 128 |
\list |
| 129 |
\li Qt.AlignLeading anchors to the left on LTR and to the right on RTL layouts |
| 130 |
\li Qt.AlignTrailing anchors to the right on LTR and to the left on RTL layouts |
| 131 |
\li Qt.AlignTop anchors to the top |
| 132 |
\li Qt.AlignBottom anchors to the bottom |
| 133 |
\endlist |
| 134 |
The default value is \b Qt.AlignTrailing. |
| 135 |
*/ |
| 136 |
property int align: Qt.AlignTrailing |
| 137 |
|
| 138 |
/*! |
| 139 |
\internal |
| 140 |
This property holds whether the scrollbar is active or passive. It is present |
| 141 |
for testing purposes. |
| 142 |
*/ |
| 143 |
property bool __interactive: __styleInstance !== null && __styleInstance.interactive |
| 144 |
|
| 145 |
/*! |
| 146 |
\internal |
| 147 |
simulate the system setting (which will be implemented in lomiri, I guess) |
| 148 |
True --> Steppers style, non-overlay scrollbars |
| 149 |
False --> Indicator and Trough styles |
| 150 |
CURRENTLY UNUSED |
| 151 |
*/ |
| 152 |
property bool __alwaysOnScrollbars: false |
| 153 |
|
| 154 |
/*! internal |
| 155 |
Used by ScrollView to tweak Scrollbar's anchoring logic for the always-on scrollbars. |
| 156 |
CURRENTLY UNUSED |
| 157 |
*/ |
| 158 |
property Item __viewport: null |
| 159 |
|
| 160 |
//Disable the input handling to let the events pass through in case we have an |
| 161 |
//interactive scrollbar right below us (can happen with nested views) |
| 162 |
enabled: __interactive//&& __alwaysOnScrollbars |
| 163 |
visible: __initializedFlickable !== null |
| 164 |
|
| 165 |
implicitWidth: internals.vertical ? units.gu(3) : (__initializedFlickable ? __initializedFlickable.width : 0) |
| 166 |
implicitHeight: !internals.vertical ? units.gu(3) : (__initializedFlickable ? __initializedFlickable.height : 0) |
| 167 |
|
| 168 |
anchors { |
| 169 |
left: (__viewport || __initializedFlickable) |
| 170 |
? internals.leftAnchor(__viewport ? __viewport : __initializedFlickable) |
| 171 |
: undefined |
| 172 |
leftMargin: internals.leftAnchorMargin() |
| 173 |
right: (__viewport || __initializedFlickable) |
| 174 |
? internals.rightAnchor(__viewport ? __viewport : __initializedFlickable) |
| 175 |
: undefined |
| 176 |
rightMargin: internals.rightAnchorMargin() |
| 177 |
top: (__viewport || __initializedFlickable) |
| 178 |
? internals.topAnchor(__viewport ? __viewport : __initializedFlickable) |
| 179 |
: undefined |
| 180 |
topMargin: (__initializedFlickable ? __initializedFlickable.topMargin : 0) + internals.topAnchorMargin() |
| 181 |
bottom: (__viewport || __initializedFlickable) |
| 182 |
? internals.bottomAnchor(__viewport ? __viewport : __initializedFlickable) |
| 183 |
: undefined |
| 184 |
bottomMargin: (__initializedFlickable ? __initializedFlickable.bottomMargin : 0) + internals.bottomAnchorMargin() |
| 185 |
} |
| 186 |
|
| 187 |
/*! |
| 188 |
\internal |
| 189 |
*/ |
| 190 |
onAlignChanged: if (!internals.checkAlign()) console.log("Wrong alignment set to Scrollbar: "+align) |
| 191 |
|
| 192 |
//Don't do anything with flickableItem until its creation is complete, it would be a waste of cpu cycles |
| 193 |
//and it would block the rendering thread for much longer |
| 194 |
property var __initializedFlickable: null |
| 195 |
Connections { |
| 196 |
target: flickableItem |
| 197 |
Component.onCompleted: __initializedFlickable = flickableItem |
| 198 |
} |
| 199 |
onFlickableItemChanged: { |
| 200 |
if (!flickableItem) __initializedFlickable = null |
| 201 |
else { |
| 202 |
//can't use a binding for this, because the "completeness" of a component is |
| 203 |
//not esposed to QML, QML just gets Component.onCompleted when a component is |
| 204 |
//completed, but you won't get that if we're assigned a Flickable which is |
| 205 |
//already "complete" |
| 206 |
if (Private.PrivateScrollbarUtils.isComponentComplete(flickableItem)) { |
| 207 |
__initializedFlickable = flickableItem |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/*! |
| 213 |
\internal |
| 214 |
Internals: contains the common logic of the scrollbar like anchoring, |
| 215 |
alignemt check, scrollability check. |
| 216 |
*/ |
| 217 |
property alias __private: internals |
| 218 |
QtObject { |
| 219 |
id: internals |
| 220 |
property bool vertical: (align === Qt.AlignLeading) || (align === Qt.AlignTrailing) |
| 221 |
property bool scrollable: __initializedFlickable && __initializedFlickable.interactive && checkAlign() |
| 222 |
property real nonOverlayScrollbarMargin: __styleInstance ? __styleInstance.nonOverlayScrollbarMargin : 0 |
| 223 |
|
| 224 |
function checkAlign() |
| 225 |
{ |
| 226 |
return (align === Qt.AlignLeading) || (align === Qt.AlignTrailing) || (align === Qt.AlignTop) || (align === Qt.AlignBottom); |
| 227 |
} |
| 228 |
|
| 229 |
// LTR and RTL are provided by LayoutMirroring, so no need to check that |
| 230 |
function leftAnchor(object) |
| 231 |
{ |
| 232 |
if (!internals.vertical || (align == Qt.AlignLeading)) { |
| 233 |
return object.left; |
| 234 |
} |
| 235 |
return undefined; |
| 236 |
} |
| 237 |
function leftAnchorMargin() |
| 238 |
{ |
| 239 |
if (__styleInstance === null) return 0 |
| 240 |
|
| 241 |
switch (align) { |
| 242 |
case Qt.AlignLeading: |
| 243 |
return __alwaysOnScrollbars ? -nonOverlayScrollbarMargin : 0 |
| 244 |
case Qt.AlignBottom: |
| 245 |
case Qt.AlignTop: |
| 246 |
if (!__alwaysOnScrollbars && __buddyScrollbar !== null |
| 247 |
&& __buddyScrollbar.align === Qt.AlignLeading |
| 248 |
&& __buddyScrollbar.__styleInstance.isScrollable) |
| 249 |
return __buddyScrollbar.__styleInstance.troughThicknessSteppersStyle |
| 250 |
// *ELSE FALLTHROUGH* |
| 251 |
default: |
| 252 |
return 0 |
| 253 |
} |
| 254 |
} |
| 255 |
function rightAnchor(object) |
| 256 |
{ |
| 257 |
if (!internals.vertical || (align == Qt.AlignTrailing)) { |
| 258 |
return object.right; |
| 259 |
} |
| 260 |
return undefined; |
| 261 |
} |
| 262 |
function rightAnchorMargin() |
| 263 |
{ |
| 264 |
if (__styleInstance === null) return 0 |
| 265 |
|
| 266 |
switch (align) { |
| 267 |
case Qt.AlignTrailing: |
| 268 |
return __alwaysOnScrollbars ? -nonOverlayScrollbarMargin : 0 |
| 269 |
case Qt.AlignBottom: |
| 270 |
case Qt.AlignTop: |
| 271 |
if (!__alwaysOnScrollbars && __buddyScrollbar !== null |
| 272 |
&& __buddyScrollbar.align === Qt.AlignTrailing |
| 273 |
&& __buddyScrollbar.__styleInstance.isScrollable) |
| 274 |
return __buddyScrollbar.__styleInstance.troughThicknessSteppersStyle |
| 275 |
// *ELSE FALLTHROUGH* |
| 276 |
default: |
| 277 |
return 0 |
| 278 |
} |
| 279 |
} |
| 280 |
function topAnchor(object) |
| 281 |
{ |
| 282 |
if (internals.vertical || (align == Qt.AlignTop)) |
| 283 |
return object.top; |
| 284 |
return undefined; |
| 285 |
} |
| 286 |
function topAnchorMargin() |
| 287 |
{ |
| 288 |
if (__styleInstance === null) return 0 |
| 289 |
|
| 290 |
switch (align) { |
| 291 |
case Qt.AlignTop: |
| 292 |
return __alwaysOnScrollbars ? -nonOverlayScrollbarMargin : 0 |
| 293 |
case Qt.AlignLeading: |
| 294 |
case Qt.AlignTrailing: |
| 295 |
if (!__alwaysOnScrollbars && __buddyScrollbar !== null |
| 296 |
&& __buddyScrollbar.align === Qt.AlignTop |
| 297 |
&& __buddyScrollbar.__styleInstance.isScrollable) |
| 298 |
return __buddyScrollbar.__styleInstance.troughThicknessSteppersStyle |
| 299 |
// *ELSE FALLTHROUGH* |
| 300 |
|
| 301 |
default: |
| 302 |
return 0 |
| 303 |
} |
| 304 |
} |
| 305 |
function bottomAnchor(object) |
| 306 |
{ |
| 307 |
if (internals.vertical || (align == Qt.AlignBottom)) |
| 308 |
return object.bottom; |
| 309 |
return undefined; |
| 310 |
} |
| 311 |
function bottomAnchorMargin() |
| 312 |
{ |
| 313 |
if (__styleInstance === null) return 0 |
| 314 |
|
| 315 |
switch (align) { |
| 316 |
case Qt.AlignBottom: |
| 317 |
return __alwaysOnScrollbars ? -nonOverlayScrollbarMargin : 0 |
| 318 |
case Qt.AlignLeading: |
| 319 |
case Qt.AlignTrailing: |
| 320 |
if (!__alwaysOnScrollbars && __buddyScrollbar !== null |
| 321 |
&& __buddyScrollbar.align === Qt.AlignBottom |
| 322 |
&& __buddyScrollbar.__styleInstance.isScrollable) |
| 323 |
return __buddyScrollbar.__styleInstance.troughThicknessSteppersStyle |
| 324 |
// *ELSE FALLTHROUGH* |
| 325 |
default: |
| 326 |
return 0 |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
styleName: "ScrollbarStyle" |
| 332 |
} |