on xenial help sign in
Fix last item X position

Fixes clicking on the last item sometimes not working

commitf62d7f4d31d715ef723f48004014f0d0301023a1 authorAlbert Astals <albert.astals@canonical.com> dateFri 4 Apr 2014, 13:57 (13 years ago) parentade823f
2 files changed+38−22
qml/Components/Carousel.qml+13−19 tests/qmltests/Components/tst_Carousel.qml+25−3
qml/Components/Carousel.qml+13−19
@@ -75,6 +75,7 @@ Item {
7575 The scaling of the items is controlled by the variable continuousIndex, described below. */
7676 ListView {
7777 id: listView
78+ objectName: "listView"
7879
7980 property int highlightIndex: -1
8081 property real minimumTileWidth: 0
@@ -148,15 +149,19 @@ Item {
148149 maximumFlickVelocity: Math.max(2500 * Math.pow(realWidth / referenceWidth, 1.5), 2500) // 2500 is platform default
149150 orientation: ListView.Horizontal
150151
151− function itemClicked(index, delegateItem) {
152− listView.currentIndex = index
153− var x = CarouselJS.getXFromContinuousIndex(index,
152+ function getXFromContinuousIndex(index) {
153+ return CarouselJS.getXFromContinuousIndex(index,
154154 realWidth,
155− realContentWidth,
155+ footerItem.x,
156156 tileWidth,
157157 gapToMiddlePhase,
158158 gapToEndPhase,
159159 carousel.drawBuffer)
160+ }
161+
162+ function itemClicked(index, delegateItem) {
163+ listView.currentIndex = index
164+ var x = getXFromContinuousIndex(index);
160165
161166 if (Math.abs(x - contentX) < 1 && delegateItem !== undefined) {
162167 /* We're clicking the selected item and
@@ -174,13 +179,7 @@ Item {
174179 }
175180
176181 function itemPressAndHold(index, delegateItem) {
177− var x = CarouselJS.getXFromContinuousIndex(index,
178− realWidth,
179− realContentWidth,
180− tileWidth,
181− gapToMiddlePhase,
182− gapToEndPhase,
183− carousel.drawBuffer);
182+ var x = getXFromContinuousIndex(index);
184183
185184 if (Math.abs(x - contentX) < 1 && delegateItem !== undefined) {
186185 /* We're pressAndHold the selected item and
@@ -209,22 +208,17 @@ Item {
209208 newContentX = disabledNewContentX
210209 }
211210 onMovementEnded: {
212− if (realContentX > 0 && realContentX < realContentWidth - realWidth)
211+ if (realContentX > 0)
213212 stepAnimation.start()
214213 }
215214
216215 SmoothedAnimation {
217216 id: stepAnimation
217+ objectName: "stepAnimation"
218218
219219 target: listView
220220 property: "contentX"
221− to: CarouselJS.getXFromContinuousIndex(listView.selectedIndex,
222− listView.realWidth,
223− listView.realContentWidth,
224− listView.tileWidth,
225− listView.gapToMiddlePhase,
226− listView.gapToEndPhase,
227− carousel.drawBuffer)
221+ to: listView.getXFromContinuousIndex(listView.selectedIndex)
228222 duration: 450
229223 velocity: 200
230224 easing.type: Easing.InOutQuad
tests/qmltests/Components/tst_Carousel.qml+25−3
@@ -18,6 +18,7 @@ import QtQuick 2.0
1818import QtTest 1.0
1919import "../../../qml/Components/"
2020import "../../../qml/Components/carousel.js" as Carousel
21+import Unity.Test 0.1 as UT
2122
2223Item {
2324 width: 700
@@ -32,12 +33,13 @@ Item {
3233 property int destroyedDelegates: 0
3334
3435 itemComponent: Rectangle {
36+ border.color: "black"
3537 color: "red"
3638 Component.onCompleted: carousel.createdDelegates++
3739 Component.onDestruction: carousel.destroyedDelegates++
3840 }
3941 }
40− TestCase {
42+ UT.UnityTestCase {
4143 id: root
4244 name: "Carousel"
4345 when: windowShown
@@ -55,8 +57,11 @@ Item {
5557 property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
5658 property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
5759
58− function test_onlyNeededItemsCreated() {
59− compare(carousel.createdDelegates, 10);
60+ // This test needs to run first since it tests a corner case condition
61+ // to where the carousel is just being created
62+ // That is why it has a 1 in the name
63+ function test_1onlyNeededItemsCreated() {
64+ tryCompare(carousel, "createdDelegates", 10);
6065 compare(carousel.destroyedDelegates, 0);
6166 }
6267
@@ -216,5 +221,22 @@ Item {
216221 data.maxTranslation)
217222 compare(scale, data.result)
218223 }
224+
225+ function test_lastItemXPosition() {
226+ var carouselList = findChild(carousel, "listView");
227+ var stepAnimation = findInvisibleChild(carouselList, "stepAnimation");
228+ var stepDiff = 0;
229+ stepAnimation.onRunningChanged.connect(function() { if (stepAnimation.running) stepDiff = Math.abs(carouselList.contentX - stepAnimation.to); })
230+ while (carouselList.realContentX < carouselList.realContentWidth - carouselList.realWidth) {
231+ mouseFlick(carousel, carousel.width - units.gu(1),
232+ carousel.height / 2,
233+ units.gu(2),
234+ carousel.height / 2);
235+ wait(1000);
236+ }
237+ verify(stepDiff < 1);
238+ var x = carouselList.getXFromContinuousIndex(carousel.model - 1);
239+ verify(Math.abs(x - carouselList.contentX) < 1);
240+ }
219241 }
220242}