From 3397a649b5a0b39df322b92dab50dd256e3fcaac Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Thu, 26 Feb 2015 17:21:23 +0200 Subject: [PATCH] Fix Grid flick scrolling on iOS devices (#16762) Change-Id: Iee64f1dc5ce91472c66f43a6269cb1eccb2f9b52 --- .../com/vaadin/client/widgets/Escalator.java | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/client/src/com/vaadin/client/widgets/Escalator.java b/client/src/com/vaadin/client/widgets/Escalator.java index 129100e073..0e34d98466 100644 --- a/client/src/com/vaadin/client/widgets/Escalator.java +++ b/client/src/com/vaadin/client/widgets/Escalator.java @@ -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); -- 2.39.5