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