2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
18 import Ubuntu.Components 1.3
19 import "../Components"
23 property alias expanded: row.expanded
24 property alias interactive: flickable.interactive
25 property alias indicatorsModel: row.indicatorsModel
26 property alias unitProgress: row.unitProgress
27 property alias enableLateralChanges: row.enableLateralChanges
28 property alias overFlowWidth: row.overFlowWidth
29 readonly property alias currentItemIndex: row.currentItemIndex
30 property real lateralPosition: -1
31 readonly property string currentIndicator: row.currentItem ? row.currentItem.identifier : ""
33 function selectItemAt(lateralPosition) {
35 row.resetCurrentItem();
37 var mapped = root.mapToItem(row, lateralPosition, 0);
38 row.selectItemAt(mapped.x);
41 function setCurrentItemIndex(index) {
43 row.resetCurrentItem();
45 row.setCurrentItemIndex(index);
48 function addScrollOffset(scrollAmmout) {
49 if (scrollAmmout < 0) { // left scroll
50 if (flickable.contentX + flickable.width > row.width) return; // already off the left.
52 if (flickable.contentX + flickable.width - scrollAmmout > row.width) { // going to be off the right
53 scrollAmmout = (flickable.contentX + flickable.width) - row.width;
55 } else { // right scroll
56 if (flickable.contentX < 0) return; // already off the right.
57 if (flickable.contentX - scrollAmmout < 0) scrollAmmout = flickable.contentX; // going to be off the right
59 d.scrollOffset = d.scrollOffset + scrollAmmout;
64 property var initialItem
65 // the non-expanded distance from row offset to center of initial item
66 property real originalDistanceFromRight: -1
68 // calculate the distance from row offset to center of initial item
69 property real distanceFromRight: {
70 if (originalDistanceFromRight == -1) return 0;
71 if (!initialItem) return 0;
72 return row.width - initialItem.x - initialItem.width /2;
75 // offset to the intially selected expanded item
76 property real rowOffset: 0
77 property real scrollOffset: 0
78 property real alignmentAdjustment: 0
79 property real combinedOffset: 0
81 // when the scroll offset changes, we need to reclaculate the relative lateral position
82 onScrollOffsetChanged: root.lateralPositionChanged()
84 onInitialItemChanged: {
85 originalDistanceFromRight = initialItem ? (row.width - initialItem.x - initialItem.width/2) : -1;
88 Behavior on alignmentAdjustment {
89 NumberAnimation { duration: UbuntuAnimation.BriskDuration; easing: UbuntuAnimation.StandardEasing}
92 function alignIndicators() {
93 flickable.resetContentXComponents();
95 if (expanded && !flickable.moving) {
96 // gap between left and row?
97 if (flickable.contentX + flickable.width > row.width) {
98 // row width is less than flickable
99 if (row.width < flickable.width) {
100 d.alignmentAdjustment -= flickable.contentX;
102 d.alignmentAdjustment -= ((flickable.contentX + flickable.width) - row.width);
105 // gap between right and row?
106 } else if (flickable.contentX < 0) {
107 d.alignmentAdjustment -= flickable.contentX;
109 // current item overlap on left
110 } else if (row.currentItem && (flickable.contentX + flickable.width) < (row.width - row.currentItem.x)) {
111 d.alignmentAdjustment += ((row.width - row.currentItem.x) - (flickable.contentX + flickable.width));
113 // current item overlap on right
114 } else if (row.currentItem && flickable.contentX > (row.width - row.currentItem.x - row.currentItem.width)) {
115 d.alignmentAdjustment -= flickable.contentX - (row.width - row.currentItem.x - row.currentItem.width);
125 anchors.bottom: parent.bottom
128 opacity: expanded ? 1.0 : 0.0
129 Behavior on opacity { NumberAnimation { duration: UbuntuAnimation.SnapDuration } }
135 clip: expanded || row.width > rowContainer.width
139 objectName: "flickable"
141 // we rotate it because we want the Flickable to align its content item
142 // on the right instead of on the left
146 contentWidth: row.width
147 contentX: d.combinedOffset
150 // contentX can change by user interaction as well as user offset changes
151 // This function re-aligns the offsets so that the offsets match the contentX
152 function resetContentXComponents() {
153 d.scrollOffset += d.combinedOffset - flickable.contentX;
156 rebound: Transition {
160 easing.type: Easing.OutCubic
166 objectName: "indicatorItemRow"
169 bottom: parent.bottom
172 // Compensate for the Flickable rotation (ie, counter-rotate)
176 if (root.lateralPosition == -1) return -1;
178 var mapped = root.mapToItem(row, root.lateralPosition, 0);
179 return Math.min(Math.max(mapped.x, 0), row.width);
182 onCurrentItemChanged: {
183 if (!currentItem) d.initialItem = undefined;
184 else if (!d.initialItem) d.initialItem = currentItem;
189 enabled: root.expanded
191 row.selectItemAt(mouse.x);
202 interval: UbuntuAnimation.FastDuration // enough for row animation.
205 onTriggered: d.alignIndicators();
216 alignmentAdjustment: 0
218 restoreEntryValues: false
223 when: expanded && !interactive
227 combinedOffset: rowOffset + alignmentAdjustment - scrollOffset
232 if (!initialItem) return 0;
233 if (distanceFromRight - initialItem.width <= 0) return 0;
235 var rowOffset = distanceFromRight - originalDistanceFromRight;
238 restoreEntryValues: false
243 when: expanded && interactive
247 // don't use row offset anymore.
248 d.scrollOffset -= d.rowOffset;
250 d.initialItem = undefined;
251 alignmentTimer.start();
256 combinedOffset: rowOffset + alignmentAdjustment - scrollOffset
257 restoreEntryValues: false
268 properties: "rowOffset, scrollOffset, alignmentAdjustment"
273 properties: "combinedOffset"
274 duration: UbuntuAnimation.SnapDuration
275 easing: UbuntuAnimation.StandardEasing