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