Set scroll and click thresholds relative to screen pixel density (if readable) and...
[guacamole-common-js.git] / src / main / resources / mouse.js
index 8cb5979..5475793 100644 (file)
@@ -59,6 +59,24 @@ Guacamole.Mouse = function(element) {
     var guac_mouse = this;
 
     /**
+     * The distance a two-finger touch must move per scrollwheel event, relative
+     * to the distance between the two touches.
+     */
+    this.scrollThreshold = 0.5;
+
+    /**
+     * The maximum number of milliseconds to wait for a touch to end for the
+     * gesture to be considered a click.
+     */
+    this.clickTimingThreshold = 250;
+
+    /**
+     * The maximum number of pixels to allow a touch to move for the gesture to
+     * be considered a click.
+     */
+    this.clickMoveThreshold = 10 * (window.displayPixelRatio || 1);
+
+    /**
      * The current mouse state. The properties of this state are updated when
      * mouse events fire. This state object is also passed in as a parameter to
      * the handler of any mouse events.
@@ -111,10 +129,13 @@ Guacamole.Mouse = function(element) {
         // This is all JUST so we can get the mouse position within the element
         var parent = element.offsetParent;
         while (parent) {
-            if (parent.offsetLeft && parent.offsetTop) {
+
+            if (parent.offsetLeft)
                 guac_mouse.currentState.x -= parent.offsetLeft;
+
+            if (parent.offsetTop)
                 guac_mouse.currentState.y -= parent.offsetTop;
-            }
+
             parent = parent.offsetParent;
         }
 
@@ -140,28 +161,39 @@ Guacamole.Mouse = function(element) {
 
     }, false);
 
+    var touch_count = 0;
     var last_touch_x = 0;
     var last_touch_y = 0;
     var last_touch_time = 0;
     var pixels_moved = 0;
+    var touch_distance = 0;
+
+    var touch_buttons = {
+        1: "left",
+        2: "right",
+        3: "middle"
+    };
 
     var gesture_in_progress = false;
     var click_release_timeout = null;
 
     element.addEventListener("touchend", function(e) {
         
-        // If we're handling a gesture
-        if (gesture_in_progress) {
+        // If we're handling a gesture AND this is the last touch
+        if (gesture_in_progress && e.touches.length == 0) {
             
             cancelEvent(e);
             
             var time = new Date().getTime();
 
+            // Get corresponding mouse button
+            var button = touch_buttons[touch_count];
+
             // If mouse already down, release anad clear timeout
-            if (guac_mouse.currentState.left) {
+            if (guac_mouse.currentState[button]) {
 
-                // Fire left button up event
-                guac_mouse.currentState.left = false;
+                // Fire button up event
+                guac_mouse.currentState[button] = false;
                 if (guac_mouse.onmouseup)
                     guac_mouse.onmouseup(guac_mouse.currentState);
 
@@ -174,10 +206,11 @@ Guacamole.Mouse = function(element) {
             }
 
             // If single tap detected (based on time and distance)
-            if (time - last_touch_time <= 250 && pixels_moved < 10) {
+            if (time - last_touch_time <= guac_mouse.clickTimingThreshold
+                    && pixels_moved < guac_mouse.clickMoveThreshold) {
 
-                // Fire left button down event
-                guac_mouse.currentState.left = true;
+                // Fire button down event
+                guac_mouse.currentState[button] = true;
                 if (guac_mouse.onmousedown)
                     guac_mouse.onmousedown(guac_mouse.currentState);
 
@@ -185,15 +218,15 @@ Guacamole.Mouse = function(element) {
                 // touchstart within timeout.
                 click_release_timeout = window.setTimeout(function() {
                     
-                    // Fire left button up event
-                    guac_mouse.currentState.left = false;
+                    // Fire button up event
+                    guac_mouse.currentState[button] = false;
                     if (guac_mouse.onmouseup)
                         guac_mouse.onmouseup(guac_mouse.currentState);
                     
                     // Allow mouse events now that touching is over
                     gesture_in_progress = false;
             
-                }, 250);
+                }, guac_mouse.clickTimingThreshold);
 
             }
 
@@ -203,7 +236,10 @@ Guacamole.Mouse = function(element) {
 
     element.addEventListener("touchstart", function(e) {
 
-        // Record initial touch location and time for single-touch movement
+        // Track number of touches, but no more than three
+        touch_count = Math.min(e.touches.length, 3);
+
+        // Record initial touch location and time for touch movement
         // and tap gestures
         if (e.touches.length == 1) {
 
@@ -220,31 +256,35 @@ Guacamole.Mouse = function(element) {
 
             // Record touch location and time
             var starting_touch = e.touches[0];
-            last_touch_x = starting_touch.pageX;
-            last_touch_y = starting_touch.pageY;
+            last_touch_x = starting_touch.screenX;
+            last_touch_y = starting_touch.screenY;
             last_touch_time = new Date().getTime();
             pixels_moved = 0;
 
-            // TODO: Handle different buttons
+        }
 
+        else if (e.touches.length == 2) {
+            var diff_x = e.touches[0].screenX - e.touches[1].screenX;
+            var diff_y = e.touches[0].screenY - e.touches[1].screenY;
+            touch_distance = Math.sqrt(diff_x*diff_x + diff_y*diff_y);
         }
 
     }, false);
 
     element.addEventListener("touchmove", function(e) {
 
-        // Handle single-touch movement gesture (touchpad mouse move)
-        if (e.touches.length == 1) {
+        cancelEvent(e);
 
-            cancelEvent(e);
+        // Get change in touch location
+        var touch = e.touches[0];
+        var delta_x = touch.screenX - last_touch_x;
+        var delta_y = touch.screenY - last_touch_y;
 
-            // Get change in touch location
-            var touch = e.touches[0];
-            var delta_x = touch.pageX - last_touch_x;
-            var delta_y = touch.pageY - last_touch_y;
+        // Track pixels moved
+        pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
 
-            // Track pixels moved
-            pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
+        // If only one touch involved, this is mouse move
+        if (touch_count == 1) {
 
             // Update mouse location
             guac_mouse.currentState.x += delta_x;
@@ -267,8 +307,38 @@ Guacamole.Mouse = function(element) {
                 guac_mouse.onmousemove(guac_mouse.currentState);
 
             // Update touch location
-            last_touch_x = touch.pageX;
-            last_touch_y = touch.pageY;
+            last_touch_x = touch.screenX;
+            last_touch_y = touch.screenY;
+
+        }
+
+        // Interpret two-finger touch as scrollwheel
+        else if (touch_count == 2) {
+
+            // If change in location passes threshold for scroll
+            if (Math.abs(delta_y) >= touch_distance * guac_mouse.scrollThreshold) {
+
+                // Decide button based on Y movement direction
+                var button;
+                if (delta_y > 0) button = "down";
+                else             button = "up";
+
+                // Fire button down event
+                guac_mouse.currentState[button] = true;
+                if (guac_mouse.onmousedown)
+                    guac_mouse.onmousedown(guac_mouse.currentState);
+
+                // Fire button up event
+                guac_mouse.currentState[button] = false;
+                if (guac_mouse.onmouseup)
+                    guac_mouse.onmouseup(guac_mouse.currentState);
+
+                // Only update touch location after a scroll has been
+                // detected
+                last_touch_x = touch.screenX;
+                last_touch_y = touch.screenY;
+
+            }
 
         }
 
@@ -355,7 +425,7 @@ Guacamole.Mouse = function(element) {
                 guac_mouse.onmouseup(guac_mouse.currentState);
         }
 
-    }, true);
+    }, false);
 
     // Override selection on mouse event element.
     element.addEventListener("selectstart", function(e) {
@@ -363,7 +433,7 @@ Guacamole.Mouse = function(element) {
     }, false);
 
     // Scroll wheel support
-    element.addEventListener('DOMMouseScroll', function(e) {
+    function mousewheel_handler(e) {
 
         // Don't handle if we aren't supposed to
         if (gesture_in_progress) return;
@@ -402,7 +472,9 @@ Guacamole.Mouse = function(element) {
 
         cancelEvent(e);
 
-    }, false);
+    }
+    element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
+    element.addEventListener('mousewheel',     mousewheel_handler, false);
 
 };