Эх сурвалжийг харах

Merge pull request #272 from samhed/doubleclick

Allow for double taps to trigger double clicks even if the second touch is a slight bit off
Joel Martin 12 жил өмнө
parent
commit
f3ff971db9
1 өөрчлөгдсөн 34 нэмэгдсэн , 0 устгасан
  1. 34 0
      include/input.js

+ 34 - 0
include/input.js

@@ -1,6 +1,7 @@
 /*
 /*
  * noVNC: HTML5 VNC client
  * noVNC: HTML5 VNC client
  * Copyright (C) 2012 Joel Martin
  * Copyright (C) 2012 Joel Martin
+ * Copyright (C) 2013 Samuel Mannehed for Cendio AB
  * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
  * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
  */
  */
 
 
@@ -489,6 +490,9 @@ var that           = {},  // Public API methods
     conf           = {},  // Configuration attributes
     conf           = {},  // Configuration attributes
     mouseCaptured  = false;
     mouseCaptured  = false;
 
 
+var doubleClickTimer = null,
+    lastTouchPos = null;
+
 // Configuration attributes
 // Configuration attributes
 Util.conf_defaults(conf, that, defaults, [
 Util.conf_defaults(conf, that, defaults, [
     ['target',         'ro', 'dom',  document, 'DOM element that captures mouse input'],
     ['target',         'ro', 'dom',  document, 'DOM element that captures mouse input'],
@@ -521,6 +525,10 @@ function releaseMouse() {
 // Private functions
 // Private functions
 //
 //
 
 
+function resetDoubleClickTimer() {
+    doubleClickTimer = null;
+}
+
 function onMouseButton(e, down) {
 function onMouseButton(e, down) {
     var evt, pos, bmask;
     var evt, pos, bmask;
     if (! conf.focused) {
     if (! conf.focused) {
@@ -528,8 +536,34 @@ function onMouseButton(e, down) {
     }
     }
     evt = (e ? e : window.event);
     evt = (e ? e : window.event);
     pos = Util.getEventPosition(e, conf.target, conf.scale);
     pos = Util.getEventPosition(e, conf.target, conf.scale);
+
     if (e.touches || e.changedTouches) {
     if (e.touches || e.changedTouches) {
         // Touch device
         // Touch device
+
+        // When two touches occur within 500 ms of each other and are
+        // closer than 20 pixels together a double click is triggered.
+        if (down == 1) {
+            if (doubleClickTimer == null) {
+                lastTouchPos = pos;
+            } else {
+                clearTimeout(doubleClickTimer); 
+
+                // When the distance between the two touches is small enough
+                // force the position of the latter touch to the position of
+                // the first.
+
+                var xs = lastTouchPos.x - pos.x;
+                var ys = lastTouchPos.y - pos.y;
+                var d = Math.sqrt((xs * xs) + (ys * ys));
+
+                // The goal is to trigger on a certain physical width, the
+                // devicePixelRatio brings us a bit closer but is not optimal.
+                if (d < 20 * window.devicePixelRatio) {
+                    pos = lastTouchPos;
+                }
+            }
+            doubleClickTimer = setTimeout(resetDoubleClickTimer, 500);
+        }
         bmask = conf.touchButton;
         bmask = conf.touchButton;
         // If bmask is set
         // If bmask is set
     } else if (evt.which) {
     } else if (evt.which) {