]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Grid flick scrolling on iOS devices (#16762)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Thu, 26 Feb 2015 15:21:23 +0000 (17:21 +0200)
committerVaadin Code Review <review@vaadin.com>
Mon, 2 Mar 2015 12:59:56 +0000 (12:59 +0000)
Change-Id: Iee64f1dc5ce91472c66f43a6269cb1eccb2f9b52

client/src/com/vaadin/client/widgets/Escalator.java

index 129100e0733431743983832b9c332df4bf985c01..0e34d9846608a5e8d405d6c9127512142935a777 100644 (file)
@@ -335,7 +335,6 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
             private double touches = 0;
             private int lastX = 0;
             private int lastY = 0;
-            private double lastTime = 0;
             private boolean snappedScrollEnabled = true;
             private double deltaX = 0;
             private double deltaY = 0;
@@ -345,7 +344,10 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
             private CustomTouchEvent latestTouchMoveEvent;
 
             /** The timestamp of {@link #flickPageX1} and {@link #flickPageY1} */
-            private double flickTimestamp = Double.MIN_VALUE;
+            private double flickStartTime = 0;
+
+            /** The timestamp of {@link #flickPageX2} and {@link #flickPageY2} */
+            private double flickTimestamp = 0;
 
             /** The most recent flick touch reference Y */
             private double flickPageY1 = -1;
@@ -368,6 +370,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
              * over here.
              */
             private AnimationCallback mover = new AnimationCallback() {
+
                 @Override
                 public void execute(double timestamp) {
                     if (touches != 1) {
@@ -378,17 +381,16 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
                     final int y = latestTouchMoveEvent.getPageY();
 
                     /*
-                     * Check if we need a new flick coordinate sample (more than
-                     * FLICK_POLL_FREQUENCY ms have passed since the last
-                     * sample)
+                     * Check if we need a new flick coordinate sample ( more
+                     * than FLICK_POLL_FREQUENCY ms have passed since the last
+                     * sample )
                      */
-                    if (timestamp - flickTimestamp > FLICK_POLL_FREQUENCY) {
-                        flickTimestamp = timestamp;
-                        flickPageY2 = flickPageY1;
-                        flickPageY1 = y;
+                    if (System.currentTimeMillis() - flickTimestamp > FLICK_POLL_FREQUENCY) {
 
-                        flickPageX2 = flickPageX1;
-                        flickPageX1 = x;
+                        flickTimestamp = System.currentTimeMillis();
+                        // Set target coordinates
+                        flickPageY2 = y;
+                        flickPageX2 = x;
                     }
 
                     deltaX = x - lastX;
@@ -396,12 +398,6 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
                     lastX = x;
                     lastY = y;
 
-                    /*
-                     * Instead of using the provided arbitrary timestamp, let's
-                     * use a known-format and reproducible timestamp.
-                     */
-                    lastTime = Duration.currentTimeMillis();
-
                     // snap the scroll to the major axes, at first.
                     if (snappedScrollEnabled) {
                         final double oldDeltaX = deltaX;
@@ -477,6 +473,14 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
                 lastX = event.getPageX();
                 lastY = event.getPageY();
 
+                // Reset flick parameters
+                flickPageX1 = lastX;
+                flickPageX2 = -1;
+                flickPageY1 = lastY;
+                flickPageY2 = -1;
+                flickStartTime = System.currentTimeMillis();
+                flickTimestamp = 0;
+
                 snappedScrollEnabled = true;
             }
 
@@ -514,25 +518,18 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
 
                     final double finalPageY;
                     final double finalPageX;
-                    double deltaT = lastTime - flickTimestamp;
+                    double deltaT = flickTimestamp - flickStartTime;
                     boolean onlyOneSample = flickPageX2 < 0 || flickPageY2 < 0;
-                    if (onlyOneSample || deltaT > FLICK_POLL_FREQUENCY / 3) {
-                        finalPageY = flickPageY1;
-                        finalPageX = flickPageX1;
+                    if (onlyOneSample) {
+                        finalPageX = latestTouchMoveEvent.getPageX();
+                        finalPageY = latestTouchMoveEvent.getPageY();
                     } else {
-                        deltaT += FLICK_POLL_FREQUENCY;
                         finalPageY = flickPageY2;
                         finalPageX = flickPageX2;
                     }
 
-                    flickPageY1 = -1;
-                    flickPageY2 = -1;
-                    flickTimestamp = Double.MIN_VALUE;
-
-                    double deltaX = latestTouchMoveEvent.getPageX()
-                            - finalPageX;
-                    double deltaY = latestTouchMoveEvent.getPageY()
-                            - finalPageY;
+                    double deltaX = finalPageX - flickPageX1;
+                    double deltaY = finalPageY - flickPageY1;
 
                     escalator.scroller
                             .handleFlickScroll(deltaX, deltaY, deltaT);