| 1 |
/* |
| 2 |
* Copyright (C) 2012-2014 Canonical, Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU 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 General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU 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 |
| 19 |
|
| 20 |
Item { |
| 21 |
id: root |
| 22 |
|
| 23 |
property Action leftSideAction: null |
| 24 |
property list<Action> rightSideActions |
| 25 |
property double defaultHeight: units.gu(8) |
| 26 |
property bool locked: false |
| 27 |
property Action activeAction: null |
| 28 |
property var activeItem: null |
| 29 |
property bool triggerActionOnMouseRelease: false |
| 30 |
property color color: Theme.palette.normal.background |
| 31 |
property bool selected: false |
| 32 |
property bool selectionMode: false |
| 33 |
property alias internalAnchors: mainContents.anchors |
| 34 |
property alias animated: behaviorOnX.enabled |
| 35 |
default property alias contents: mainContents.children |
| 36 |
|
| 37 |
readonly property double actionWidth: units.gu(4) |
| 38 |
readonly property double leftActionWidth: units.gu(10) |
| 39 |
readonly property double actionThreshold: actionWidth * 0.4 |
| 40 |
readonly property double threshold: 0.4 |
| 41 |
readonly property string swipeState: main.x == 0 ? "Normal" : main.x > 0 ? "LeftToRight" : "RightToLeft" |
| 42 |
readonly property alias swipping: mainItemMoving.running |
| 43 |
readonly property bool _showActions: mouseArea.pressed || swipeState != "Normal" || swipping |
| 44 |
|
| 45 |
/* internal */ |
| 46 |
property var _visibleRightSideActions: filterVisibleActions(rightSideActions) |
| 47 |
|
| 48 |
signal itemClicked(var mouse) |
| 49 |
signal itemPressAndHold(var mouse) |
| 50 |
|
| 51 |
function returnToBoundsRTL(direction) |
| 52 |
{ |
| 53 |
var actionFullWidth = actionWidth + units.gu(2) |
| 54 |
|
| 55 |
// go back to normal state if swipping reverse |
| 56 |
if (direction === "LTR") { |
| 57 |
updatePosition(0) |
| 58 |
return |
| 59 |
} else if (!triggerActionOnMouseRelease) { |
| 60 |
updatePosition(-rightActionsView.width + units.gu(2)) |
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
var xOffset = Math.abs(main.x) |
| 65 |
var index = Math.min(Math.floor(xOffset / actionFullWidth), _visibleRightSideActions.length) |
| 66 |
var newX = 0 |
| 67 |
if (index === _visibleRightSideActions.length) { |
| 68 |
newX = -(rightActionsView.width - units.gu(2)) |
| 69 |
} else if (index >= 1) { |
| 70 |
newX = -(actionFullWidth * index) |
| 71 |
} |
| 72 |
updatePosition(newX) |
| 73 |
} |
| 74 |
|
| 75 |
function returnToBoundsLTR(direction) |
| 76 |
{ |
| 77 |
var finalX = leftActionWidth |
| 78 |
if ((direction === "RTL") || (main.x <= (finalX * root.threshold))) |
| 79 |
finalX = 0 |
| 80 |
updatePosition(finalX) |
| 81 |
} |
| 82 |
|
| 83 |
function returnToBounds(direction) |
| 84 |
{ |
| 85 |
if (main.x < 0) { |
| 86 |
returnToBoundsRTL(direction) |
| 87 |
} else if (main.x > 0) { |
| 88 |
returnToBoundsLTR(direction) |
| 89 |
} else { |
| 90 |
updatePosition(0) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
function contains(item, point, marginX) |
| 95 |
{ |
| 96 |
var itemStartX = item.x - marginX |
| 97 |
var itemEndX = item.x + item.width + marginX |
| 98 |
return (point.x >= itemStartX) && (point.x <= itemEndX) && |
| 99 |
(point.y >= item.y) && (point.y <= (item.y + item.height)); |
| 100 |
} |
| 101 |
|
| 102 |
function getActionAt(point) |
| 103 |
{ |
| 104 |
if (contains(leftActionView, point, 0)) { |
| 105 |
return leftSideAction |
| 106 |
} else if (contains(rightActionsView, point, 0)) { |
| 107 |
var newPoint = root.mapToItem(rightActionsView, point.x, point.y) |
| 108 |
for (var i = 0; i < rightActionsRepeater.count; i++) { |
| 109 |
var child = rightActionsRepeater.itemAt(i) |
| 110 |
if (contains(child, newPoint, units.gu(1))) { |
| 111 |
return i |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
return -1 |
| 116 |
} |
| 117 |
|
| 118 |
function updateActiveAction() |
| 119 |
{ |
| 120 |
if (triggerActionOnMouseRelease && |
| 121 |
(main.x <= -(root.actionWidth + units.gu(2))) && |
| 122 |
(main.x > -(rightActionsView.width - units.gu(2)))) { |
| 123 |
var actionFullWidth = actionWidth + units.gu(2) |
| 124 |
var xOffset = Math.abs(main.x) |
| 125 |
var index = Math.min(Math.floor(xOffset / actionFullWidth), _visibleRightSideActions.length) |
| 126 |
index = index - 1 |
| 127 |
if (index > -1) { |
| 128 |
root.activeItem = rightActionsRepeater.itemAt(index) |
| 129 |
root.activeAction = root._visibleRightSideActions[index] |
| 130 |
} |
| 131 |
} else { |
| 132 |
root.activeAction = null |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
function resetSwipe() |
| 137 |
{ |
| 138 |
updatePosition(0) |
| 139 |
} |
| 140 |
|
| 141 |
function filterVisibleActions(actions) |
| 142 |
{ |
| 143 |
var visibleActions = [] |
| 144 |
for(var i = 0; i < actions.length; i++) { |
| 145 |
var action = actions[i] |
| 146 |
if (action.visible) { |
| 147 |
visibleActions.push(action) |
| 148 |
} |
| 149 |
} |
| 150 |
return visibleActions |
| 151 |
} |
| 152 |
|
| 153 |
function updatePosition(pos) |
| 154 |
{ |
| 155 |
if (!root.triggerActionOnMouseRelease && (pos !== 0)) { |
| 156 |
mouseArea.state = pos > 0 ? "RightToLeft" : "LeftToRight" |
| 157 |
} else { |
| 158 |
mouseArea.state = "" |
| 159 |
} |
| 160 |
main.x = pos |
| 161 |
} |
| 162 |
|
| 163 |
states: [ |
| 164 |
State { |
| 165 |
name: "select" |
| 166 |
when: selectionMode |
| 167 |
PropertyChanges { |
| 168 |
target: selectionIcon |
| 169 |
source: Qt.resolvedUrl("ListItemWithActionsCheckBox.qml") |
| 170 |
anchors.leftMargin: units.gu(2) |
| 171 |
} |
| 172 |
PropertyChanges { |
| 173 |
target: root |
| 174 |
locked: true |
| 175 |
} |
| 176 |
PropertyChanges { |
| 177 |
target: main |
| 178 |
x: 0 |
| 179 |
} |
| 180 |
} |
| 181 |
] |
| 182 |
|
| 183 |
height: defaultHeight |
| 184 |
clip: height !== defaultHeight |
| 185 |
|
| 186 |
Rectangle { |
| 187 |
id: leftActionView |
| 188 |
|
| 189 |
anchors { |
| 190 |
top: parent.top |
| 191 |
bottom: parent.bottom |
| 192 |
right: main.left |
| 193 |
} |
| 194 |
width: root.leftActionWidth + actionThreshold |
| 195 |
visible: leftSideAction |
| 196 |
color: theme.palette.normal.negative |
| 197 |
|
| 198 |
Icon { |
| 199 |
anchors { |
| 200 |
centerIn: parent |
| 201 |
horizontalCenterOffset: actionThreshold / 2 |
| 202 |
} |
| 203 |
name: leftSideAction && _showActions ? leftSideAction.iconName : "" |
| 204 |
color: Theme.palette.selected.field |
| 205 |
height: units.gu(3) |
| 206 |
width: units.gu(3) |
| 207 |
asynchronous: true |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
Rectangle { |
| 212 |
id: rightActionsView |
| 213 |
|
| 214 |
anchors { |
| 215 |
top: main.top |
| 216 |
left: main.right |
| 217 |
bottom: main.bottom |
| 218 |
} |
| 219 |
visible: _visibleRightSideActions.length > 0 |
| 220 |
width: rightActionsRepeater.count > 0 ? rightActionsRepeater.count * (root.actionWidth + units.gu(2)) + root.actionThreshold + units.gu(2) : 0 |
| 221 |
color: Theme.palette.normal.foreground |
| 222 |
Row { |
| 223 |
anchors{ |
| 224 |
top: parent.top |
| 225 |
left: parent.left |
| 226 |
leftMargin: units.gu(2) |
| 227 |
right: parent.right |
| 228 |
rightMargin: units.gu(2) |
| 229 |
bottom: parent.bottom |
| 230 |
} |
| 231 |
spacing: units.gu(2) |
| 232 |
Repeater { |
| 233 |
id: rightActionsRepeater |
| 234 |
|
| 235 |
model: _showActions ? _visibleRightSideActions : [] |
| 236 |
Item { |
| 237 |
property alias image: img |
| 238 |
|
| 239 |
height: rightActionsView.height |
| 240 |
width: root.actionWidth |
| 241 |
|
| 242 |
Icon { |
| 243 |
id: img |
| 244 |
|
| 245 |
anchors.centerIn: parent |
| 246 |
width: units.gu(3) |
| 247 |
height: units.gu(3) |
| 248 |
name: modelData.iconName |
| 249 |
color: root.activeAction === modelData ? Theme.palette.normal.activity : Theme.palette.normal.foregroundText |
| 250 |
asynchronous: true |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
Rectangle { |
| 258 |
id: main |
| 259 |
objectName: "mainItem" |
| 260 |
|
| 261 |
anchors { |
| 262 |
top: parent.top |
| 263 |
bottom: parent.bottom |
| 264 |
} |
| 265 |
|
| 266 |
width: parent.width |
| 267 |
color: root.color |
| 268 |
|
| 269 |
Loader { |
| 270 |
id: selectionIcon |
| 271 |
objectName: "selectionIcon" |
| 272 |
|
| 273 |
anchors { |
| 274 |
left: main.left |
| 275 |
verticalCenter: main.verticalCenter |
| 276 |
} |
| 277 |
width: (status === Loader.Ready) ? item.implicitWidth : 0 |
| 278 |
visible: (status === Loader.Ready) && (item.width === item.implicitWidth) |
| 279 |
Behavior on width { |
| 280 |
NumberAnimation { |
| 281 |
duration: LomiriAnimation.SnapDuration |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
Item { |
| 287 |
id: mainContents |
| 288 |
|
| 289 |
anchors { |
| 290 |
left: selectionIcon.right |
| 291 |
leftMargin: units.gu(2) |
| 292 |
top: parent.top |
| 293 |
topMargin: units.gu(1) |
| 294 |
right: parent.right |
| 295 |
rightMargin: units.gu(2) |
| 296 |
bottom: parent.bottom |
| 297 |
bottomMargin: units.gu(1) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
Behavior on x { |
| 302 |
id: behaviorOnX |
| 303 |
|
| 304 |
LomiriNumberAnimation { |
| 305 |
id: mainItemMoving |
| 306 |
|
| 307 |
easing.type: Easing.OutElastic |
| 308 |
duration: LomiriAnimation.SlowDuration |
| 309 |
} |
| 310 |
} |
| 311 |
Behavior on color { |
| 312 |
ColorAnimation {} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
SequentialAnimation { |
| 317 |
id: triggerAction |
| 318 |
|
| 319 |
property var currentItem: root.activeItem ? root.activeItem.image : null |
| 320 |
|
| 321 |
running: false |
| 322 |
ParallelAnimation { |
| 323 |
LomiriNumberAnimation { |
| 324 |
target: triggerAction.currentItem |
| 325 |
property: "opacity" |
| 326 |
from: 1.0 |
| 327 |
to: 0.0 |
| 328 |
duration: LomiriAnimation.SlowDuration |
| 329 |
easing {type: Easing.InOutBack; } |
| 330 |
} |
| 331 |
LomiriNumberAnimation { |
| 332 |
target: triggerAction.currentItem |
| 333 |
properties: "width, height" |
| 334 |
from: units.gu(3) |
| 335 |
to: root.actionWidth |
| 336 |
duration: LomiriAnimation.SlowDuration |
| 337 |
easing {type: Easing.InOutBack; } |
| 338 |
} |
| 339 |
} |
| 340 |
PropertyAction { |
| 341 |
target: triggerAction.currentItem |
| 342 |
properties: "width, height" |
| 343 |
value: units.gu(3) |
| 344 |
} |
| 345 |
PropertyAction { |
| 346 |
target: triggerAction.currentItem |
| 347 |
properties: "opacity" |
| 348 |
value: 1.0 |
| 349 |
} |
| 350 |
ScriptAction { |
| 351 |
script: { |
| 352 |
root.activeAction.triggered(root) |
| 353 |
mouseArea.state = "" |
| 354 |
} |
| 355 |
} |
| 356 |
PauseAnimation { |
| 357 |
duration: 500 |
| 358 |
} |
| 359 |
LomiriNumberAnimation { |
| 360 |
target: main |
| 361 |
property: "x" |
| 362 |
to: 0 |
| 363 |
|
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
MouseArea { |
| 368 |
id: mouseArea |
| 369 |
|
| 370 |
property bool locked: root.locked || ((root.leftSideAction === null) && (root._visibleRightSideActions.count === 0)) |
| 371 |
property bool manual: false |
| 372 |
property string direction: "None" |
| 373 |
property real lastX: -1 |
| 374 |
|
| 375 |
anchors.fill: parent |
| 376 |
z: -1 |
| 377 |
drag { |
| 378 |
target: locked ? null : main |
| 379 |
axis: Drag.XAxis |
| 380 |
minimumX: rightActionsView.visible ? -(rightActionsView.width) : 0 |
| 381 |
maximumX: leftActionView.visible ? leftActionView.width : 0 |
| 382 |
threshold: root.actionThreshold |
| 383 |
} |
| 384 |
|
| 385 |
states: [ |
| 386 |
State { |
| 387 |
name: "LeftToRight" |
| 388 |
PropertyChanges { |
| 389 |
target: mouseArea |
| 390 |
drag.maximumX: 0 |
| 391 |
} |
| 392 |
}, |
| 393 |
State { |
| 394 |
name: "RightToLeft" |
| 395 |
PropertyChanges { |
| 396 |
target: mouseArea |
| 397 |
drag.minimumX: 0 |
| 398 |
} |
| 399 |
} |
| 400 |
] |
| 401 |
|
| 402 |
onMouseXChanged: { |
| 403 |
var offset = (lastX - mouseX) |
| 404 |
if (Math.abs(offset) <= root.actionThreshold) { |
| 405 |
return |
| 406 |
} |
| 407 |
lastX = mouseX |
| 408 |
direction = offset > 0 ? "RTL" : "LTR"; |
| 409 |
} |
| 410 |
|
| 411 |
onPressed: { |
| 412 |
lastX = mouse.x |
| 413 |
} |
| 414 |
|
| 415 |
onReleased: { |
| 416 |
if (root.triggerActionOnMouseRelease && root.activeAction) { |
| 417 |
triggerAction.start() |
| 418 |
} else { |
| 419 |
root.returnToBounds(direction) |
| 420 |
root.activeAction = null |
| 421 |
} |
| 422 |
lastX = -1 |
| 423 |
direction = "None" |
| 424 |
} |
| 425 |
onClicked: { |
| 426 |
if (main.x === 0) { |
| 427 |
root.itemClicked(mouse) |
| 428 |
} else if (main.x > 0) { |
| 429 |
var action = getActionAt(Qt.point(mouse.x, mouse.y)) |
| 430 |
if (action && action !== -1) { |
| 431 |
action.triggered(root) |
| 432 |
} |
| 433 |
} else { |
| 434 |
var actionIndex = getActionAt(Qt.point(mouse.x, mouse.y)) |
| 435 |
if (actionIndex !== -1) { |
| 436 |
root.activeItem = rightActionsRepeater.itemAt(actionIndex) |
| 437 |
root.activeAction = root._visibleRightSideActions[actionIndex] |
| 438 |
triggerAction.start() |
| 439 |
return |
| 440 |
} |
| 441 |
} |
| 442 |
root.resetSwipe() |
| 443 |
} |
| 444 |
|
| 445 |
onPositionChanged: { |
| 446 |
if (mouseArea.pressed) { |
| 447 |
updateActiveAction() |
| 448 |
} |
| 449 |
} |
| 450 |
onPressAndHold: { |
| 451 |
if (main.x === 0) { |
| 452 |
root.itemPressAndHold(mouse) |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |