Debian Codegraph @

src/imports/Components/Themes/Ambiance/2.0/ListItemStyle.qml

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