]> source.dussan.org Git - vaadin-framework.git/commitdiff
Introduce a drag threshold for Drag and Drop (#13381)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Mon, 7 Apr 2014 14:32:29 +0000 (17:32 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 11 Apr 2014 12:36:39 +0000 (12:36 +0000)
Based on partial fix by Fabian Lange.

Change-Id: I1a18c6ea105d87496b196b93e701aaccb987b3e7

client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java

index b911c28a071187d65571033aecb351e553607415..47fd3c88a2eadd12a7380a5df93b5ae5215d9803 100644 (file)
@@ -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;