From: Teemu Suo-Anttila Date: Mon, 7 Apr 2014 14:32:29 +0000 (+0300) Subject: Introduce a drag threshold for Drag and Drop (#13381) X-Git-Tag: 7.1.14~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7e5d44d19bd02ecc7b900ba4926380d2fa7e7668;p=vaadin-framework.git Introduce a drag threshold for Drag and Drop (#13381) Based on partial fix by Fabian Lange. Change-Id: I1a18c6ea105d87496b196b93e701aaccb987b3e7 --- diff --git a/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java b/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java index b911c28a07..47fd3c88a2 100644 --- a/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java +++ b/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java @@ -31,7 +31,6 @@ import com.google.gwt.user.client.Command; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Event.NativePreviewHandler; -import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; @@ -345,10 +344,7 @@ public class VDragAndDropManager { .addNativePreviewHandler(defaultDragAndDropEventHandler); if (dragElement != null && dragElement.getParentElement() == null) { - // deferred attaching drag image is on going, we can - // hurry with it now - lazyAttachDragElement.cancel(); - lazyAttachDragElement.run(); + attachDragElement(); } } // just capture something to prevent text selection in IE @@ -370,6 +366,13 @@ public class VDragAndDropManager { deferredStartRegistration = Event .addNativePreviewHandler(new NativePreviewHandler() { + private int startX = Util + .getTouchOrMouseClientX(currentDrag + .getCurrentGwtEvent()); + private int startY = Util + .getTouchOrMouseClientY(currentDrag + .getCurrentGwtEvent()); + @Override public void onPreviewNativeEvent( NativePreviewEvent event) { @@ -422,13 +425,23 @@ public class VDragAndDropManager { } case Event.ONMOUSEMOVE: case Event.ONTOUCHMOVE: - if (deferredStartRegistration != null) { - deferredStartRegistration.removeHandler(); - deferredStartRegistration = null; + int currentX = Util + .getTouchOrMouseClientX(event + .getNativeEvent()); + int currentY = Util + .getTouchOrMouseClientY(event + .getNativeEvent()); + if (Math.abs(startX - currentX) > 3 + || Math.abs(startY - currentY) > 3) { + if (deferredStartRegistration != null) { + deferredStartRegistration + .removeHandler(); + deferredStartRegistration = null; + } + currentDrag.setCurrentGwtEvent(event + .getNativeEvent()); + startDrag.execute(); } - currentDrag.setCurrentGwtEvent(event - .getNativeEvent()); - startDrag.execute(); break; default: // on any other events, clean up the @@ -718,16 +731,7 @@ public class VDragAndDropManager { updateDragImagePosition(); if (isStarted) { - lazyAttachDragElement.run(); - } else { - /* - * To make our default dnd handler as compatible as possible, we - * need to defer the appearance of dragElement. Otherwise events - * that are derived from sequences of other events might not - * fire as domchanged will fire between them or mouse up might - * happen on dragElement. - */ - lazyAttachDragElement.schedule(300); + attachDragElement(); } } } @@ -736,24 +740,20 @@ public class VDragAndDropManager { return dragElement; } - private final Timer lazyAttachDragElement = new Timer() { - - @Override - public void run() { - if (dragElement != null && dragElement.getParentElement() == null) { - ApplicationConnection connection = getCurrentDragApplicationConnection(); - Element dragImageParent; - if (connection == null) { - VConsole.error("Could not determine ApplicationConnection for current drag operation. The drag image will likely look broken"); - dragImageParent = RootPanel.getBodyElement(); - } else { - dragImageParent = VOverlay.getOverlayContainer(connection); - } - dragImageParent.appendChild(dragElement); + public void attachDragElement() { + if (dragElement != null && dragElement.getParentElement() == null) { + ApplicationConnection connection = getCurrentDragApplicationConnection(); + Element dragImageParent; + if (connection == null) { + VConsole.error("Could not determine ApplicationConnection for current drag operation. The drag image will likely look broken"); + dragImageParent = RootPanel.getBodyElement(); + } else { + dragImageParent = VOverlay.getOverlayContainer(connection); } - + dragImageParent.appendChild(dragElement); } - }; + + } private Command deferredCommand;