2 * Copyright (C) 2013-2014 Canonical Ltd.
3 * Copyright (C) 2020 UBports Foundation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19import Lomiri.Components 1.3
24 implicitWidth: row.width
25 implicitHeight: units.gu(3)
27 property bool hideRow: false
28 property QtObject model: null
29 property real overFlowWidth: width
30 property bool expanded: false
31 readonly property alias currentItem: row.currentItem
32 readonly property alias currentItemIndex: row.currentIndex
34 property real unitProgress: 0.0
35 property real selectionChangeBuffer: units.gu(2)
36 property bool enableLateralChanges: false
37 property bool lightMode: false
38 readonly property color hightlightColor: lightMode ? "#000000" : "#ffffff"
40 property alias delegate: row.delegate
41 property alias contentX: row.contentX
43 property real lateralPosition: -1
44 onLateralPositionChanged: {
45 updateItemFromLateralPosition();
48 onEnableLateralChangesChanged: {
49 updateItemFromLateralPosition();
52 function updateItemFromLateralPosition() {
53 if (currentItem && !enableLateralChanges) return;
54 if (lateralPosition === -1) return;
57 selectItemAt(lateralPosition);
61 var maximumBufferOffset = selectionChangeBuffer * unitProgress;
62 var proposedItem = indicatorAt(lateralPosition, 0);
64 var bufferExceeded = false;
66 if (proposedItem !== currentItem) {
67 // Proposed item is not directly adjacent to current?
68 if (Math.abs(proposedItem.ownIndex - currentItem.ownIndex) > 1) {
69 bufferExceeded = true;
71 var currentItemLateralPosition = root.mapToItem(proposedItem, lateralPosition, 0).x;
73 // Is the distance into proposed item greater than max buffer?
74 // Proposed item is before current item
75 if (proposedItem.x < currentItem.x) {
76 bufferExceeded = (proposedItem.width - currentItemLateralPosition) > maximumBufferOffset;
78 bufferExceeded = currentItemLateralPosition > maximumBufferOffset;
82 selectItemAt(lateralPosition);
86 selectItemAt(lateralPosition);
90 function indicatorAt(x, y) {
91 var item = row.itemAt(x + row.contentX, y);
92 return item && item.hasOwnProperty("ownIndex") ? item : null;
95 function resetCurrentItem() {
96 d.firstItemSwitch = true;
97 d.previousItem = null;
98 row.currentIndex = -1;
101 function selectPreviousItem() {
102 var indexToSelect = currentItemIndex - 1;
103 while (indexToSelect >= 0) {
104 if (setCurrentItemIndex(indexToSelect))
106 indexToSelect = indexToSelect - 1;
110 function selectNextItem() {
111 var indexToSelect = currentItemIndex + 1;
112 while (indexToSelect < row.contentItem.children.length) {
113 if (setCurrentItemIndex(indexToSelect))
115 indexToSelect = indexToSelect + 1;
119 function setCurrentItemIndex(index) {
120 for (var i = 0; i < row.contentItem.children.length; i++) {
121 var item = row.contentItem.children[i];
122 if (item.hasOwnProperty("ownIndex") && item.ownIndex === index && item.enabled) {
123 if (currentItem !== item) {
124 row.currentIndex = index;
132 function selectItemAt(lateralPosition) {
133 var item = indicatorAt(lateralPosition, 0);
134 if (item && item.opacity > 0 && item.enabled) {
135 row.currentIndex = item.ownIndex;
137 // Select default item.
138 var searchIndex = lateralPosition >= width ? row.count - 1 : 0;
140 for (var i = 0; i < row.contentItem.children.length; i++) {
141 if (row.contentItem.children[i].hasOwnProperty("ownIndex") &&
142 row.contentItem.children[i].ownIndex === searchIndex &&
143 row.contentItem.children[i].enabled)
145 item = row.contentItem.children[i];
149 if (item && currentItem !== item) {
150 row.currentIndex = item.ownIndex;
157 property bool firstItemSwitch: true
158 property var previousItem
159 property bool forceAlignmentAnimationDisabled: false
162 onCurrentItemChanged: {
163 if (d.previousItem) {
164 d.firstItemSwitch = false;
166 d.previousItem = currentItem;
171 objectName: "panelRow"
172 orientation: ListView.Horizontal
174 opacity: hideRow ? 0 : 1
175 // dont set visible on basis of opacity; otherwise width will not be calculated correctly
178 bottom: parent.bottom
181 // TODO: make this better
182 // when the width changes, the highlight will lag behind due to animation, so we need to disable the animation
183 // and adjust the highlight X immediately.
184 width: contentItem.width
186 SequentialAnimation {
189 d.forceAlignmentAnimationDisabled = true;
190 highlight.currentItemX = Qt.binding(function() { return currentItem ? currentItem.x - row.contentX : 0 });
191 d.forceAlignmentAnimationDisabled = false;
197 Behavior on opacity { NumberAnimation { duration: LomiriAnimation.SnapDuration } }
202 objectName: "highlight"
204 anchors.bottom: row.bottom
206 color: root.hightlightColor
207 visible: currentItem !== null
210 width: currentItem ? currentItem.width : 0
212 enabled: !d.firstItemSwitch && expanded
213 LomiriNumberAnimation { duration: LomiriAnimation.FastDuration; easing: LomiriAnimation.StandardEasing }
216 // micromovements of the highlight line when user moves the finger across the items while pulling
217 // the handle downwards.
218 property real highlightCenterOffset: {
219 if (!currentItem || lateralPosition == -1 || !enableLateralChanges) return 0;
221 var itemMapped = root.mapToItem(currentItem, lateralPosition, 0);
223 var distanceFromCenter = itemMapped.x - currentItem.width / 2;
224 if (distanceFromCenter > 0) {
225 distanceFromCenter = Math.max(0, distanceFromCenter - currentItem.width / 8);
227 distanceFromCenter = Math.min(0, distanceFromCenter + currentItem.width / 8);
230 if (currentItem && currentItem.ownIndex === 0 && distanceFromCenter < 0) {
232 } else if (currentItem && currentItem.ownIndex === row.count-1 & distanceFromCenter > 0) {
235 return (distanceFromCenter / (currentItem.width / 4)) * units.gu(1);
237 Behavior on highlightCenterOffset {
238 NumberAnimation { duration: LomiriAnimation.FastDuration; easing: LomiriAnimation.StandardEasing }
241 property real currentItemX: currentItem ? currentItem.x - row.contentX : 0
242 Behavior on currentItemX {
243 id: currentItemXBehavior
244 enabled: !d.firstItemSwitch && expanded && !d.forceAlignmentAnimationDisabled
245 NumberAnimation { duration: LomiriAnimation.FastDuration; easing: LomiriAnimation.StandardEasing }
247 x: currentItemX + highlightCenterOffset
258 PropertyChanges { target: highlight; opacity: 0.9 }
265 properties: "opacity";
266 duration: LomiriAnimation.SnapDuration
267 easing: LomiriAnimation.StandardEasing