| 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.Styles 1.3 as Styles |
| 19 |
import Lomiri.Components 1.3 |
| 20 |
|
| 21 |
Styles.ListItemStyle { |
| 22 |
|
| 23 |
id: listItemStyle |
| 24 |
|
| 25 |
/* |
| 26 |
* Coloring properties |
| 27 |
*/ |
| 28 |
property color leadingPanelColor: theme.palette.normal.foreground |
| 29 |
property color trailingPanelColor: theme.palette.normal.foreground |
| 30 |
property color leadingForegroundColor: theme.palette.normal.negative |
| 31 |
property color trailingForegroundColor: theme.palette.normal.foregroundText |
| 32 |
property color leadingDisabledForegroundColor: theme.palette.disabled.negative |
| 33 |
property color trailingDisabledForegroundColor: theme.palette.disabled.foregroundText |
| 34 |
// anchoring |
| 35 |
anchors { |
| 36 |
// do not anchor fill |
| 37 |
fill: undefined |
| 38 |
top: parent ? parent.top : undefined |
| 39 |
bottom: parent ? parent.bottom : undefined |
| 40 |
bottomMargin: styledItem.divider.visible ? styledItem.divider.height : 0 |
| 41 |
left: styledItem.contentItem.left |
| 42 |
leftMargin: -styledItem.contentItem.anchors.leftMargin |
| 43 |
right: styledItem.contentItem.right |
| 44 |
rightMargin: -styledItem.contentItem.anchors.rightMargin |
| 45 |
} |
| 46 |
LayoutMirroring.childrenInherit: true |
| 47 |
|
| 48 |
// leading/trailing panels |
| 49 |
Component { |
| 50 |
id: panelComponent |
| 51 |
Rectangle { |
| 52 |
id: panel |
| 53 |
objectName: "ListItemPanel" + (leading ? "Leading" : "Trailing") |
| 54 |
// add 0.5 GUs to the panel size so we get 2GU default margin on the first action |
| 55 |
readonly property real panelWidth: actionsRow.width + units.gu(0.5) |
| 56 |
|
| 57 |
color: leading ? leadingPanelColor : trailingPanelColor |
| 58 |
anchors.fill: parent |
| 59 |
|
| 60 |
readonly property ListItemActions itemActions: leading ? styledItem.leadingActions : styledItem.trailingActions |
| 61 |
|
| 62 |
Row { |
| 63 |
id: actionsRow |
| 64 |
anchors { |
| 65 |
left: leading ? undefined : parent.left |
| 66 |
right: leading ? parent.right : undefined |
| 67 |
leftMargin: leading ? 0 : units.gu(0.5) |
| 68 |
rightMargin: leading ? units.gu(0.5) : 0 |
| 69 |
top: parent.top |
| 70 |
bottom: parent.bottom |
| 71 |
} |
| 72 |
|
| 73 |
readonly property real maxItemWidth: parent.width / itemActions.actions.length |
| 74 |
readonly property real minItemWidth: units.gu(6) // 2GU icon + 2* 2GU margin |
| 75 |
|
| 76 |
Repeater { |
| 77 |
model: itemActions.actions |
| 78 |
AbstractButton { |
| 79 |
id: actionButton |
| 80 |
action: modelData |
| 81 |
enabled: action.enabled |
| 82 |
activeFocusOnTab: false |
| 83 |
width: MathUtils.clamp(delegateLoader.item ? delegateLoader.item.width : 0, actionsRow.minItemWidth, actionsRow.maxItemWidth) |
| 84 |
anchors { |
| 85 |
top: parent ? parent.top : undefined |
| 86 |
bottom: parent ? parent.bottom : undefined |
| 87 |
} |
| 88 |
function trigger() { |
| 89 |
internals.selectedAction = modelData; |
| 90 |
listItemStyle.rebound(); |
| 91 |
} |
| 92 |
|
| 93 |
Rectangle { |
| 94 |
anchors.fill: parent |
| 95 |
color: theme.palette.highlighted.background |
| 96 |
visible: pressed |
| 97 |
} |
| 98 |
|
| 99 |
Loader { |
| 100 |
id: delegateLoader |
| 101 |
height: parent.height |
| 102 |
sourceComponent: itemActions.delegate ? itemActions.delegate : defaultDelegate |
| 103 |
property Action action: modelData |
| 104 |
property int index: listItemIndex |
| 105 |
property bool pressed: actionButton.pressed |
| 106 |
onItemChanged: { |
| 107 |
// use action's objectName to identify the visualized action |
| 108 |
if (item && item.objectName === "") { |
| 109 |
item.objectName = modelData.objectName; |
| 110 |
actionButton.objectName = "actionbutton_" + modelData.objectName |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
Component { |
| 119 |
id: defaultDelegate |
| 120 |
Item { |
| 121 |
width: actionsRow.minItemWidth |
| 122 |
Icon { |
| 123 |
width: units.gu(2) |
| 124 |
height: width |
| 125 |
name: action.iconName |
| 126 |
source: action.iconSource |
| 127 |
color: leading |
| 128 |
? (action.enabled ? leadingForegroundColor : leadingDisabledForegroundColor) |
| 129 |
: (action.enabled ? trailingForegroundColor : trailingDisabledForegroundColor) |
| 130 |
anchors.centerIn: parent |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
// the selection/multiselection panel |
| 137 |
Component { |
| 138 |
id: selectionDelegate |
| 139 |
Item { |
| 140 |
id: selectPanel |
| 141 |
objectName: "selection_panel" + listItemIndex |
| 142 |
anchors.fill: parent ? parent : undefined |
| 143 |
|
| 144 |
CheckBox { |
| 145 |
id: checkbox |
| 146 |
opacity: 0 |
| 147 |
// for unit and autopilot tests |
| 148 |
objectName: "listitem_select" |
| 149 |
anchors.centerIn: parent |
| 150 |
// for the initial value |
| 151 |
checked: styledItem.selected |
| 152 |
onCheckedChanged: styledItem.selected = checked; |
| 153 |
Binding { |
| 154 |
target: checkbox |
| 155 |
property: "checked" |
| 156 |
value: styledItem.selected |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
states: State { |
| 161 |
name: "enabled" |
| 162 |
when: loaded && styledItem.selectMode |
| 163 |
PropertyChanges { |
| 164 |
target: checkbox |
| 165 |
opacity: 1.0 |
| 166 |
} |
| 167 |
} |
| 168 |
transitions: Transition { |
| 169 |
from: "" |
| 170 |
to: "*" |
| 171 |
reversible: true |
| 172 |
enabled: listItemStyle.animatePanels |
| 173 |
OpacityAnimator { |
| 174 |
easing: LomiriAnimation.StandardEasing |
| 175 |
duration: LomiriAnimation.FastDuration |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
// drag panel |
| 181 |
Component { |
| 182 |
id: dragDelegate |
| 183 |
Item { |
| 184 |
id: dragPanel |
| 185 |
objectName: "drag_panel" + listItemIndex |
| 186 |
anchors.fill: parent ? parent : undefined |
| 187 |
Icon { |
| 188 |
objectName: "icon" |
| 189 |
id: dragIcon |
| 190 |
anchors.centerIn: parent |
| 191 |
width: units.gu(3) |
| 192 |
height: width |
| 193 |
name: "view-grid-symbolic" |
| 194 |
opacity: 0.0 |
| 195 |
scale: 0.5 |
| 196 |
color: theme.palette.normal.base |
| 197 |
} |
| 198 |
Binding { |
| 199 |
target: listItemStyle |
| 200 |
property: "dragPanel" |
| 201 |
value: dragPanel |
| 202 |
} |
| 203 |
|
| 204 |
states: State { |
| 205 |
name: "enabled" |
| 206 |
when: loaded && styledItem.dragMode |
| 207 |
PropertyChanges { |
| 208 |
target: dragIcon |
| 209 |
opacity: 1.0 |
| 210 |
scale: 1.0 |
| 211 |
} |
| 212 |
} |
| 213 |
transitions: Transition { |
| 214 |
from: "" |
| 215 |
to: "*" |
| 216 |
reversible: true |
| 217 |
enabled: listItemStyle.animatePanels |
| 218 |
ParallelAnimation { |
| 219 |
OpacityAnimator { |
| 220 |
easing: LomiriAnimation.StandardEasing |
| 221 |
duration: LomiriAnimation.FastDuration |
| 222 |
} |
| 223 |
ScaleAnimator { |
| 224 |
easing: LomiriAnimation.StandardEasing |
| 225 |
duration: LomiriAnimation.FastDuration |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// leading panel loader |
| 233 |
Loader { |
| 234 |
id: leadingLoader |
| 235 |
objectName: "leading_loader" |
| 236 |
anchors { |
| 237 |
top: parent.top |
| 238 |
bottom: parent.bottom |
| 239 |
right: parent.left |
| 240 |
} |
| 241 |
width: styledItem.width |
| 242 |
sourceComponent: styledItem.swiped && styledItem.leadingActions && styledItem.leadingActions.actions.length > 0 ? |
| 243 |
panelComponent : null |
| 244 |
// context properties used in delegates |
| 245 |
readonly property bool leading: true |
| 246 |
readonly property bool loaded: status == Loader.Ready |
| 247 |
|
| 248 |
// panel states |
| 249 |
states: [ |
| 250 |
State { |
| 251 |
name: "selectable" |
| 252 |
when: styledItem.selectMode |
| 253 |
PropertyChanges { |
| 254 |
target: leadingLoader |
| 255 |
sourceComponent: selectionDelegate |
| 256 |
width: units.gu(5) |
| 257 |
} |
| 258 |
PropertyChanges { |
| 259 |
target: listItemStyle |
| 260 |
anchors.leftMargin: 0 |
| 261 |
} |
| 262 |
PropertyChanges { |
| 263 |
target: styledItem.contentItem |
| 264 |
anchors.leftMargin: units.gu(5) |
| 265 |
} |
| 266 |
} |
| 267 |
] |
| 268 |
transitions: Transition { |
| 269 |
from: "" |
| 270 |
to: "selectable" |
| 271 |
reversible: true |
| 272 |
enabled: listItemStyle.animatePanels |
| 273 |
PropertyAnimation { |
| 274 |
target: styledItem.contentItem |
| 275 |
properties: "anchors.leftMargin" |
| 276 |
easing: LomiriAnimation.StandardEasing |
| 277 |
duration: LomiriAnimation.FastDuration |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
// trailing panel loader |
| 282 |
Loader { |
| 283 |
id: trailingLoader |
| 284 |
objectName: "trailing_loader" |
| 285 |
anchors { |
| 286 |
top: parent.top |
| 287 |
bottom: parent.bottom |
| 288 |
left: parent.right |
| 289 |
} |
| 290 |
width: styledItem.width |
| 291 |
sourceComponent: styledItem.swiped && styledItem.trailingActions && styledItem.trailingActions.actions.length > 0 ? |
| 292 |
panelComponent : null |
| 293 |
// context properties used in delegates |
| 294 |
readonly property bool leading: false |
| 295 |
readonly property bool loaded: status == Loader.Ready |
| 296 |
|
| 297 |
// panel states |
| 298 |
states: State { |
| 299 |
name: "draggable" |
| 300 |
when: styledItem.dragMode |
| 301 |
PropertyChanges { |
| 302 |
target: trailingLoader |
| 303 |
sourceComponent: dragDelegate |
| 304 |
width: units.gu(5) |
| 305 |
} |
| 306 |
PropertyChanges { |
| 307 |
target: listItemStyle |
| 308 |
anchors.rightMargin: 0 |
| 309 |
} |
| 310 |
PropertyChanges { |
| 311 |
target: styledItem.contentItem |
| 312 |
anchors.rightMargin: units.gu(5) |
| 313 |
} |
| 314 |
} |
| 315 |
transitions: Transition { |
| 316 |
from: "" |
| 317 |
to: "*" |
| 318 |
reversible: true |
| 319 |
enabled: listItemStyle.animatePanels |
| 320 |
PropertyAnimation { |
| 321 |
target: styledItem.contentItem |
| 322 |
properties: "anchors.rightMargin" |
| 323 |
easing: LomiriAnimation.StandardEasing |
| 324 |
duration: LomiriAnimation.FastDuration |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// internals |
| 330 |
QtObject { |
| 331 |
id: internals |
| 332 |
// action triggered |
| 333 |
property Action selectedAction |
| 334 |
// swipe handling |
| 335 |
readonly property Item swipedPanel: leadingPanel ? leadingLoader.item : trailingLoader.item |
| 336 |
readonly property bool leadingPanel: listItemStyle.LayoutMirroring.enabled ? (listItemStyle.x < 0) : (listItemStyle.x > 0) |
| 337 |
readonly property real swipedOffset: (leadingPanel ? listItemStyle.x : -listItemStyle.x) * |
| 338 |
(listItemStyle.LayoutMirroring.enabled ? -1 : 1) |
| 339 |
readonly property real panelWidth: swipedPanel && swipedPanel.hasOwnProperty("panelWidth") ? swipedPanel.panelWidth : 0 |
| 340 |
property real prevX: 0.0 |
| 341 |
property real snapChangerLimit: 0.0 |
| 342 |
readonly property real threshold: units.gu(1.5) |
| 343 |
property bool snapIn: false |
| 344 |
property bool completed: false |
| 345 |
|
| 346 |
// update snap direction |
| 347 |
function updateSnapDirection() { |
| 348 |
if (prevX < listItemStyle.x && (snapChangerLimit <= listItemStyle.x)) { |
| 349 |
snapIn = (listItemStyle.LayoutMirroring.enabled !== leadingPanel); |
| 350 |
snapChangerLimit = listItemStyle.x - threshold; |
| 351 |
} else if (prevX > listItemStyle.x && (listItemStyle.x < snapChangerLimit)) { |
| 352 |
snapIn = (listItemStyle.LayoutMirroring.enabled === leadingPanel); |
| 353 |
snapChangerLimit = listItemStyle.x + threshold; |
| 354 |
} |
| 355 |
prevX = listItemStyle.x; |
| 356 |
} |
| 357 |
// perform snapIn/Out |
| 358 |
function snap() { |
| 359 |
var snapPos = (swipedOffset > units.gu(2) && snapIn) ? panelWidth : 0.0; |
| 360 |
snapPos *= leadingPanel ? 1 : -1; |
| 361 |
// invert snapPos on RTL |
| 362 |
snapPos *= listItemStyle.LayoutMirroring.enabled ? -1 : 1; |
| 363 |
snapAnimation.snapTo(snapPos); |
| 364 |
} |
| 365 |
// handle elasticity on overshoot |
| 366 |
function overshoot(event) { |
| 367 |
var offset = event.content.x - styledItem.contentItem.anchors.leftMargin; |
| 368 |
offset *= leadingPanel ? 1 : -1; |
| 369 |
// invert offset on RTL |
| 370 |
offset *= listItemStyle.LayoutMirroring.enabled ? -1 : 1; |
| 371 |
if (offset > panelWidth) { |
| 372 |
// do elastic move |
| 373 |
event.content.x = styledItem.contentItem.x + (event.to.x - event.from.x) / 2; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
snapAnimation: SmoothedAnimation { |
| 378 |
objectName: "snap_animation" |
| 379 |
target: styledItem.contentItem |
| 380 |
property: "x" |
| 381 |
// use 50GU/second velocity |
| 382 |
velocity: units.gu(60) |
| 383 |
onStopped: { |
| 384 |
// trigger action |
| 385 |
if (to == styledItem.contentItem.anchors.leftMargin && internals.selectedAction) { |
| 386 |
internals.selectedAction.trigger(listItemIndex); |
| 387 |
internals.selectedAction = null; |
| 388 |
} |
| 389 |
} |
| 390 |
// animated snapping |
| 391 |
function snapTo(pos) { |
| 392 |
if (pos == to && styledItem.contentItem.x == to) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
stop(); |
| 397 |
from = styledItem.contentItem.x; |
| 398 |
// add leading margin as the snapped out item's X coordinate is the leftMargin |
| 399 |
pos += styledItem.contentItem.anchors.leftMargin; |
| 400 |
to = pos; |
| 401 |
start(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// simple drop animation |
| 406 |
dropAnimation: SmoothedAnimation { |
| 407 |
properties: "y" |
| 408 |
velocity: units.gu(60) |
| 409 |
} |
| 410 |
|
| 411 |
onXChanged: internals.updateSnapDirection() |
| 412 |
// overriding default functions |
| 413 |
function swipeEvent(event) { |
| 414 |
if (event.status == SwipeEvent.Started) { |
| 415 |
internals.prevX = x; |
| 416 |
snapAnimation.stop(); |
| 417 |
} else if (event.status == SwipeEvent.Finished) { |
| 418 |
internals.snap(); |
| 419 |
} else if (event.status == SwipeEvent.Updated) { |
| 420 |
// handle elasticity when overshooting |
| 421 |
internals.overshoot(event) |
| 422 |
} |
| 423 |
} |
| 424 |
function rebound() { |
| 425 |
snapAnimation.snapTo(0); |
| 426 |
} |
| 427 |
|
| 428 |
// expansion |
| 429 |
Component.onCompleted: internals.completed = true |
| 430 |
state: (internals.completed && styledItem.expansion.expanded) ? (listItemStyle.flickable ? "expandedWithFlickable" : "expandedNoFlickable") : "" |
| 431 |
states: [ |
| 432 |
State { |
| 433 |
name: "expandedNoFlickable" |
| 434 |
PropertyChanges { |
| 435 |
target: styledItem |
| 436 |
height: styledItem.expansion.height |
| 437 |
} |
| 438 |
}, |
| 439 |
State { |
| 440 |
name: "expandedWithFlickable" |
| 441 |
PropertyChanges { |
| 442 |
target: styledItem |
| 443 |
height: styledItem.expansion.height |
| 444 |
} |
| 445 |
PropertyChanges { |
| 446 |
target: listItemStyle.flickable |
| 447 |
// we do not need to restore the original values |
| 448 |
restoreEntryValues: false |
| 449 |
// and we should not get any binding updates even |
| 450 |
explicit: true |
| 451 |
contentY: { |
| 452 |
var bottom = styledItem.y + styledItem.expansion.height - listItemStyle.flickable.contentY + listItemStyle.flickable.originY; |
| 453 |
var dy = bottom - listItemStyle.flickable.height; |
| 454 |
if (dy > 0) { |
| 455 |
return listItemStyle.flickable.contentY + dy - listItemStyle.flickable.originY; |
| 456 |
} else { |
| 457 |
return listItemStyle.flickable.contentY; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
] |
| 463 |
transitions: [ |
| 464 |
Transition { |
| 465 |
from: "" |
| 466 |
to: "expandedWithFlickable" |
| 467 |
reversible: true |
| 468 |
enabled: listItemStyle.animatePanels |
| 469 |
ParallelAnimation { |
| 470 |
LomiriNumberAnimation { |
| 471 |
target: listItemStyle.flickable |
| 472 |
property: "contentY" |
| 473 |
} |
| 474 |
LomiriNumberAnimation { |
| 475 |
target: styledItem |
| 476 |
property: "height" |
| 477 |
} |
| 478 |
} |
| 479 |
}, |
| 480 |
Transition { |
| 481 |
from: "" |
| 482 |
to: "expandedNoFlickable" |
| 483 |
reversible: true |
| 484 |
enabled: listItemStyle.animatePanels |
| 485 |
LomiriNumberAnimation { |
| 486 |
target: styledItem |
| 487 |
property: "height" |
| 488 |
} |
| 489 |
} |
| 490 |
] |
| 491 |
} |