| 1 |
/* |
| 2 |
* Copyright 2016 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 |
import QtQuick 2.4 |
| 17 |
import QtQuick.Window 2.2 |
| 18 |
import Qt5Compat.GraphicalEffects |
| 19 |
import Lomiri.Components |
| 20 |
import Lomiri.Components.Private |
| 21 |
import Lomiri.Components.Styles 2.0 as Style |
| 22 |
|
| 23 |
Style.SectionsStyle { |
| 24 |
id: sectionsStyle |
| 25 |
|
| 26 |
implicitWidth: sectionsListView.contentWidth + 2 * listViewContainer.listViewMargins |
| 27 |
implicitHeight: Screen.height > units.gu(50) ? units.gu(4) : units.gu(3) |
| 28 |
|
| 29 |
sectionColor: enabled |
| 30 |
? theme.palette.normal.backgroundTertiaryText |
| 31 |
: theme.palette.disabled.backgroundTertiaryText |
| 32 |
|
| 33 |
underlineColor: theme.palette.normal.base |
| 34 |
|
| 35 |
selectedSectionColor: enabled |
| 36 |
? theme.palette.selected.positionText |
| 37 |
: theme.palette.selectedDisabled.positionText |
| 38 |
|
| 39 |
pressedBackgroundColor: theme.palette.highlighted.background |
| 40 |
|
| 41 |
textSize: Label.Medium |
| 42 |
|
| 43 |
/*! |
| 44 |
The spacing on the left and right sides of the label |
| 45 |
inside a section button. |
| 46 |
*/ |
| 47 |
property real horizontalLabelSpacing: units.gu(2) |
| 48 |
|
| 49 |
/*! |
| 50 |
The height of the bar underlining the sections. |
| 51 |
*/ |
| 52 |
property real underlineHeight: units.dp(2) |
| 53 |
|
| 54 |
// We don't clip listview on purpose, so we have to clip here to prevent Sections element |
| 55 |
// from painting outside its area. |
| 56 |
clip: true |
| 57 |
|
| 58 |
//This item is needed for the OpacityMask feature. It is needed to make sure that when we |
| 59 |
//bring a list element into view, that element won't be covered by the opacity mask. So we |
| 60 |
//disable clipping on the list but we give it margins. This way when an item is repositioned |
| 61 |
//to be within the listview, that item will not be positioned under the opacity mask. (which is |
| 62 |
//what would have happened if the listview were filling the parent) |
| 63 |
Item { |
| 64 |
id: listViewContainer |
| 65 |
anchors.fill: parent |
| 66 |
|
| 67 |
property real listViewMargins: units.gu(2) |
| 68 |
|
| 69 |
//We need to set this to 0.0 when OpacityMask will draw this listview for us. |
| 70 |
//we don't set visible: false because we still want to get the input events! |
| 71 |
opacity: 1.0 |
| 72 |
|
| 73 |
ListView { |
| 74 |
id: sectionsListView |
| 75 |
objectName: "sections_listview" |
| 76 |
|
| 77 |
property bool animateContentX: false |
| 78 |
|
| 79 |
// Position the selected item correctly. |
| 80 |
// For a scrollable ListView, if the item was already fully visible, |
| 81 |
// no repositioning is needed. If the item was (partially) invisible, |
| 82 |
// position it so that it becomes fully visible. |
| 83 |
// If the ListView is not scrollable, the first item will be aligned with |
| 84 |
// the left of the ListView. |
| 85 |
function positionItem(item) { |
| 86 |
if (item !== null) { |
| 87 |
//stop the flick before doing computations |
| 88 |
if (moving) { |
| 89 |
return; |
| 90 |
} |
| 91 |
if (dragging || flicking) { |
| 92 |
cancelFlick(); |
| 93 |
} |
| 94 |
if (contentXAnim.running) { |
| 95 |
contentXAnim.stop(); |
| 96 |
} |
| 97 |
|
| 98 |
contentXAnim.from = contentX; |
| 99 |
//make sure we don't overshoot bounds |
| 100 |
if (sectionsListView.contentWidth <= sectionsListView.width) { |
| 101 |
// No scrolling, position the sections on the left. |
| 102 |
contentXAnim.to = originX; |
| 103 |
} else { |
| 104 |
// Position the selected Item so that it is fully visible. |
| 105 |
contentXAnim.to = MathUtils.clamp(contentX, originX, originX + contentWidth - width); |
| 106 |
} |
| 107 |
if (contentXAnim.from !== contentXAnim.to) { |
| 108 |
contentXAnim.start(); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
anchors { |
| 114 |
fill: parent |
| 115 |
leftMargin: listViewContainer.listViewMargins |
| 116 |
rightMargin: listViewContainer.listViewMargins |
| 117 |
} |
| 118 |
|
| 119 |
//this is just to disable keyboard navigation to avoid messing with contentX/contentWidth while |
| 120 |
//the view is moving |
| 121 |
focus: !moving |
| 122 |
|
| 123 |
onWidthChanged: positionItem(currentItem) |
| 124 |
//make sure that the currentItem is in the middle when everything is initialized |
| 125 |
Component.onCompleted: positionItem(currentItem) |
| 126 |
|
| 127 |
orientation: ListView.Horizontal |
| 128 |
boundsBehavior: Flickable.StopAtBounds |
| 129 |
|
| 130 |
model: styledItem.model |
| 131 |
|
| 132 |
//We need this to make sure that we have delegates for the whole width, since we have |
| 133 |
//clip disabled. |
| 134 |
displayMarginBeginning: listViewContainer.listViewMargins |
| 135 |
displayMarginEnd: listViewContainer.listViewMargins |
| 136 |
|
| 137 |
currentIndex: styledItem.selectedIndex |
| 138 |
onCurrentIndexChanged: { |
| 139 |
styledItem.selectedIndex = currentIndex; |
| 140 |
} |
| 141 |
onCurrentItemChanged: { |
| 142 |
//adjust contentX so that the item is kept in the middle |
| 143 |
//don't use ListView.ApplyRange because that does an awkward animation when you select an item |
| 144 |
//*while* the current item is outside of screen |
| 145 |
positionItem(currentItem); |
| 146 |
} |
| 147 |
|
| 148 |
highlightFollowsCurrentItem: false |
| 149 |
highlight: Item { |
| 150 |
//show the highlight on top of the delegate |
| 151 |
z: 2 |
| 152 |
x: sectionsListView.currentItem ? sectionsListView.currentItem.x : -width |
| 153 |
width: sectionsListView.currentItem ? sectionsListView.currentItem.width : 0 |
| 154 |
height: sectionsListView.currentItem ? sectionsListView.currentItem.height : 0 |
| 155 |
// Hide the highlight underline when the ListItem has the focus frame enabled: |
| 156 |
visible: sectionsListView.currentItem && |
| 157 |
!sectionsListView.currentItem.keyNavigationFocus |
| 158 |
|
| 159 |
Rectangle { |
| 160 |
anchors { |
| 161 |
bottom: parent.bottom |
| 162 |
left: parent.left |
| 163 |
right: parent.right |
| 164 |
} |
| 165 |
height: sectionsStyle.underlineHeight |
| 166 |
color: sectionsStyle.selectedSectionColor |
| 167 |
} |
| 168 |
Behavior on x { LomiriNumberAnimation {} } |
| 169 |
} |
| 170 |
|
| 171 |
delegate: ListItem { |
| 172 |
id: sectionButton |
| 173 |
objectName: "section_button_" + index |
| 174 |
width: label.width + 2 * sectionsStyle.horizontalLabelSpacing |
| 175 |
height: sectionsStyle.height |
| 176 |
// No need to clip |
| 177 |
contentItem.clip: false |
| 178 |
|
| 179 |
property bool selected: index === styledItem.selectedIndex |
| 180 |
onClicked: { |
| 181 |
styledItem.selectedIndex = index; |
| 182 |
sectionsListView.forceActiveFocus(); |
| 183 |
} |
| 184 |
|
| 185 |
// FIXME: This line may be removed when bug #1573215 is fixed and |
| 186 |
// ListItems in a horizontal ListView don't show the divider by default |
| 187 |
divider.visible: false |
| 188 |
|
| 189 |
// Section title |
| 190 |
Label { |
| 191 |
id: label |
| 192 |
objectName: "section_button_label_" + index |
| 193 |
// modelData may be either a string, or an Action |
| 194 |
text: modelData.hasOwnProperty("text") ? modelData.text : modelData |
| 195 |
textSize: sectionsStyle.textSize |
| 196 |
font.weight: Font.Light |
| 197 |
anchors.centerIn: parent |
| 198 |
color: sectionButton.selected ? |
| 199 |
sectionsStyle.selectedSectionColor : |
| 200 |
sectionsStyle.sectionColor |
| 201 |
|
| 202 |
Behavior on color { |
| 203 |
ColorAnimation { duration: LomiriAnimation.SlowDuration } |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// Section title underline |
| 208 |
Rectangle { |
| 209 |
id: underline |
| 210 |
anchors { |
| 211 |
bottom: parent.bottom |
| 212 |
left: parent.left |
| 213 |
right: parent.right |
| 214 |
} |
| 215 |
height: sectionsStyle.underlineHeight |
| 216 |
color: sectionsStyle.underlineColor |
| 217 |
// Don't cover the focus frame: |
| 218 |
visible: !sectionButton.keyNavigationFocus |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
SmoothedAnimation { |
| 223 |
objectName: "sections_scroll_animation" |
| 224 |
id: contentXAnim |
| 225 |
target: sectionsListView |
| 226 |
property: "contentX" |
| 227 |
duration: LomiriAnimation.FastDuration |
| 228 |
velocity: units.gu(10) |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
MouseArea { |
| 234 |
// Detect hovering over the left and right areas to show the scrolling chevrons. |
| 235 |
id: hoveringArea |
| 236 |
|
| 237 |
property real iconsDisabledOpacity: 0.3 |
| 238 |
|
| 239 |
anchors.fill: parent |
| 240 |
hoverEnabled: true |
| 241 |
|
| 242 |
property bool pressedLeft: false |
| 243 |
property bool pressedRight: false |
| 244 |
onPressed: function(mouse) { |
| 245 |
pressedLeft = leftIcon.contains(mouse); |
| 246 |
pressedRight = rightIcon.contains(mouse); |
| 247 |
mouse.accepted = pressedLeft || pressedRight; |
| 248 |
} |
| 249 |
onClicked: function(mouse) { |
| 250 |
// positionViewAtIndex() does not provide animation |
| 251 |
var scrollDirection = 0; |
| 252 |
if (pressedLeft && leftIcon.contains(mouse)) { |
| 253 |
scrollDirection = -1; |
| 254 |
} else if (pressedRight && rightIcon.contains(mouse)) { |
| 255 |
scrollDirection = 1; |
| 256 |
} else { |
| 257 |
// User pressed on the left or right icon, and then released inside of the |
| 258 |
// MouseArea but outside of the icon that was pressed. |
| 259 |
return; |
| 260 |
} |
| 261 |
if (contentXAnim.running) contentXAnim.stop(); |
| 262 |
var newContentX = sectionsListView.contentX + (sectionsListView.width * scrollDirection); |
| 263 |
contentXAnim.from = sectionsListView.contentX; |
| 264 |
// make sure we don't overshoot bounds |
| 265 |
contentXAnim.to = MathUtils.clamp( |
| 266 |
newContentX, |
| 267 |
0.0, // estimation of originX is some times wrong when scrolling towards the beginning |
| 268 |
sectionsListView.originX + sectionsListView.contentWidth - sectionsListView.width); |
| 269 |
contentXAnim.start(); |
| 270 |
} |
| 271 |
|
| 272 |
Icon { |
| 273 |
id: leftIcon |
| 274 |
objectName: "left_scroll_icon" |
| 275 |
// return whether the pressed event was done inside the area of the icon |
| 276 |
function contains(mouse) { |
| 277 |
return (mouse.x < listViewContainer.listViewMargins && |
| 278 |
!sectionsListView.atXBeginning); |
| 279 |
} |
| 280 |
anchors { |
| 281 |
left: parent.left |
| 282 |
leftMargin: (listViewContainer.listViewMargins - width) / 2 |
| 283 |
bottom: parent.bottom |
| 284 |
bottomMargin: sectionsStyle.height < units.gu(4) ? units.gu(1) : units.gu(2) |
| 285 |
} |
| 286 |
width: units.gu(1) |
| 287 |
height: units.gu(1) |
| 288 |
visible: false |
| 289 |
asynchronous: true |
| 290 |
opacity: visible |
| 291 |
? sectionsListView.atXBeginning ? hoveringArea.iconsDisabledOpacity : 1.0 |
| 292 |
: 0.0 |
| 293 |
name: "toolkit_chevron-rtl_1gu" |
| 294 |
Behavior on opacity { |
| 295 |
LomiriNumberAnimation { |
| 296 |
duration: LomiriAnimation.FastDuration |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
Icon { |
| 302 |
id: rightIcon |
| 303 |
objectName: "right_scroll_icon" |
| 304 |
// return whether the pressed event was done inside the area of the icon |
| 305 |
function contains(mouse) : bool { |
| 306 |
return (mouse.x > (hoveringArea.width - listViewContainer.listViewMargins) && |
| 307 |
!sectionsListView.atXEnd); |
| 308 |
} |
| 309 |
anchors { |
| 310 |
right: parent.right |
| 311 |
rightMargin: (listViewContainer.listViewMargins - width) / 2 |
| 312 |
bottom: parent.bottom |
| 313 |
bottomMargin: sectionsStyle.height < units.gu(4) ? units.gu(1) : units.gu(2) |
| 314 |
} |
| 315 |
width: units.gu(1) |
| 316 |
height: units.gu(1) |
| 317 |
visible: false |
| 318 |
asynchronous: true |
| 319 |
opacity: visible |
| 320 |
? sectionsListView.atXEnd ? hoveringArea.iconsDisabledOpacity : 1.0 |
| 321 |
: 0.0 |
| 322 |
name: "toolkit_chevron-ltr_1gu" |
| 323 |
Behavior on opacity { |
| 324 |
LomiriNumberAnimation { |
| 325 |
duration: LomiriAnimation.FastDuration |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
LinearGradient { |
| 332 |
id: gradient |
| 333 |
|
| 334 |
visible: false |
| 335 |
anchors.fill: parent |
| 336 |
start: Qt.point(0,0) |
| 337 |
end: Qt.point(width,0) |
| 338 |
|
| 339 |
property real gradientWidth: listViewContainer.listViewMargins / gradient.width |
| 340 |
//the width is gradientWidth, but we want the gradient to actually start/finish at gradientSplitPosition |
| 341 |
//just to leave some margin. |
| 342 |
property real gradientSplitPosition: 2/3 * gradientWidth |
| 343 |
|
| 344 |
gradient: Gradient { |
| 345 |
//left gradient |
| 346 |
GradientStop { position: 0.0 ; color: Qt.rgba(1,1,1,0) } |
| 347 |
GradientStop { position: gradient.gradientSplitPosition ; color: Qt.rgba(1,1,1,0) } |
| 348 |
GradientStop { position: gradient.gradientWidth; color: Qt.rgba(1,1,1,1) } |
| 349 |
//right gradient |
| 350 |
GradientStop { position: 1.0 - gradient.gradientWidth; color: Qt.rgba(1,1,1,1) } |
| 351 |
GradientStop { position: 1.0 - gradient.gradientSplitPosition; color: Qt.rgba(1,1,1,0) } |
| 352 |
GradientStop { position: 1.0; color: Qt.rgba(1,1,1,0) } |
| 353 |
} |
| 354 |
|
| 355 |
Rectangle { |
| 356 |
// full opacity for the underline at the bottom in the mask below |
| 357 |
anchors { |
| 358 |
left: parent.left |
| 359 |
right: parent.right |
| 360 |
bottom: parent.bottom |
| 361 |
} |
| 362 |
height: sectionsStyle.underlineHeight |
| 363 |
color: "white" |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
OpacityMask { |
| 368 |
id: mask |
| 369 |
anchors.fill: parent |
| 370 |
visible: false |
| 371 |
source: listViewContainer |
| 372 |
maskSource: gradient |
| 373 |
} |
| 374 |
|
| 375 |
states: [ |
| 376 |
State { |
| 377 |
name: "hovering" |
| 378 |
when: hoveringArea.containsMouse |
| 379 |
PropertyChanges { target: mask; visible: true } |
| 380 |
PropertyChanges { target: listViewContainer; opacity: 0.0 } |
| 381 |
PropertyChanges { target: leftIcon; visible: true; } |
| 382 |
PropertyChanges { target: rightIcon; visible: true; } |
| 383 |
} |
| 384 |
] |
| 385 |
} |