* mouse events into a non-browser-specific event provided by the
* Guacamole.Mouse instance.
*
- * Touch events are translated into mouse events as if the touches occurred
- * on a touchpad (drag to push the mouse pointer, tap to click).
- *
* @constructor
* @param {Element} element The Element to use to provide mouse events.
*/
e.returnValue = false;
}
- function moveMouse(pageX, pageY) {
+ function moveMouse(clientX, clientY) {
- guac_mouse.currentState.x = pageX - element.offsetLeft;
- guac_mouse.currentState.y = pageY - element.offsetTop;
+ guac_mouse.currentState.x = clientX - element.offsetLeft;
+ guac_mouse.currentState.y = clientY - element.offsetTop;
// This is all JUST so we can get the mouse position within the element
var parent = element.offsetParent;
- while (parent) {
-
- if (parent.offsetLeft)
- guac_mouse.currentState.x -= parent.offsetLeft;
-
- if (parent.offsetTop)
- guac_mouse.currentState.y -= parent.offsetTop;
+ while (parent && !(parent === document.body)) {
+ guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
+ guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
parent = parent.offsetParent;
}
+ // Offset by document scroll amount
+ var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
+ var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
+
+ guac_mouse.currentState.x -= parent.offsetLeft - documentScrollLeft;
+ guac_mouse.currentState.y -= parent.offsetTop - documentScrollTop;
+
if (guac_mouse.onmousemove)
guac_mouse.onmousemove(guac_mouse.currentState);
element.addEventListener("mousemove", function(e) {
- // Don't handle if we aren't supposed to
- if (gesture_in_progress) return;
-
cancelEvent(e);
- moveMouse(e.pageX, e.pageY);
-
- }, false);
-
- var last_touch_x = 0;
- var last_touch_y = 0;
- var last_touch_time = 0;
- var pixels_moved = 0;
-
- 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) {
-
- cancelEvent(e);
-
- var time = new Date().getTime();
-
- // If mouse already down, release anad clear timeout
- if (guac_mouse.currentState.left) {
-
- // Fire left button up event
- guac_mouse.currentState.left = false;
- if (guac_mouse.onmouseup)
- guac_mouse.onmouseup(guac_mouse.currentState);
-
- // Clear timeout, if set
- if (click_release_timeout) {
- window.clearTimeout(click_release_timeout);
- click_release_timeout = null;
- }
-
- }
-
- // If single tap detected (based on time and distance)
- if (time - last_touch_time <= 250 && pixels_moved < 10) {
-
- // Fire left button down event
- guac_mouse.currentState.left = true;
- if (guac_mouse.onmousedown)
- guac_mouse.onmousedown(guac_mouse.currentState);
-
- // Delay mouse up - mouse up should be canceled if
- // touchstart within timeout.
- click_release_timeout = window.setTimeout(function() {
-
- // Fire left button up event
- guac_mouse.currentState.left = false;
- if (guac_mouse.onmouseup)
- guac_mouse.onmouseup(guac_mouse.currentState);
-
- // Allow mouse events now that touching is over
- gesture_in_progress = false;
-
- }, 250);
-
- }
-
- }
-
- }, false);
-
- element.addEventListener("touchstart", function(e) {
-
- // Record initial touch location and time for single-touch movement
- // and tap gestures
- if (e.touches.length == 1) {
-
- cancelEvent(e);
-
- // Stop mouse events while touching
- gesture_in_progress = true;
-
- // Clear timeout, if set
- if (click_release_timeout) {
- window.clearTimeout(click_release_timeout);
- click_release_timeout = null;
- }
-
- // 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_time = new Date().getTime();
- pixels_moved = 0;
-
- // TODO: Handle different buttons
-
- }
-
- }, false);
-
- element.addEventListener("touchmove", function(e) {
-
- // Handle single-touch movement gesture (touchpad mouse move)
- if (e.touches.length == 1) {
-
- cancelEvent(e);
-
- // 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);
-
- // Update mouse location
- guac_mouse.currentState.x += delta_x;
- guac_mouse.currentState.y += delta_y;
-
- // Prevent mouse from leaving screen
-
- if (guac_mouse.currentState.x < 0)
- guac_mouse.currentState.x = 0;
- else if (guac_mouse.currentState.x >= element.offsetWidth)
- guac_mouse.currentState.x = element.offsetWidth - 1;
-
- if (guac_mouse.currentState.y < 0)
- guac_mouse.currentState.y = 0;
- else if (guac_mouse.currentState.y >= element.offsetHeight)
- guac_mouse.currentState.y = element.offsetHeight - 1;
-
- // Fire movement event, if defined
- if (guac_mouse.onmousemove)
- guac_mouse.onmousemove(guac_mouse.currentState);
-
- // Update touch location
- last_touch_x = touch.pageX;
- last_touch_y = touch.pageY;
-
- }
+ moveMouse(e.clientX, e.clientY);
}, false);
-
element.addEventListener("mousedown", function(e) {
- // Don't handle if we aren't supposed to
- if (gesture_in_progress) return;
-
cancelEvent(e);
switch (e.button) {
}, false);
-
element.addEventListener("mouseup", function(e) {
- // Don't handle if we aren't supposed to
- if (gesture_in_progress) return;
-
cancelEvent(e);
switch (e.button) {
element.addEventListener("mouseout", function(e) {
- // Don't handle if we aren't supposed to
- if (gesture_in_progress) return;
-
// Get parent of the element the mouse pointer is leaving
if (!e) e = window.event;
// Scroll wheel support
function mousewheel_handler(e) {
- // Don't handle if we aren't supposed to
- if (gesture_in_progress) return;
-
var delta = 0;
if (e.detail)
delta = e.detail;
};
+
+/**
+ * Provides cross-browser touch event translation for a given element.
+ *
+ * Touch events are translated into mouse events as if the touches occurred
+ * on a touchpad (drag to push the mouse pointer, tap to click).
+ *
+ * @constructor
+ * @param {Element} element The Element to use to provide touch events.
+ */
+Guacamole.Mouse.Touchpad = function(element) {
+
+ /**
+ * Reference to this Guacamole.Mouse.Touchpad.
+ * @private
+ */
+ var guac_touchpad = this;
+
+ /**
+ * The distance a two-finger touch must move per scrollwheel event, in
+ * pixels.
+ */
+ this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
+
+ /**
+ * 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.devicePixelRatio || 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.
+ *
+ * @type Guacamole.Mouse.State
+ */
+ this.currentState = new Guacamole.Mouse.State(
+ 0, 0,
+ false, false, false, false, false
+ );
+
+ /**
+ * Fired whenever a mouse button is effectively pressed. This can happen
+ * as part of a "click" gesture initiated by the user by tapping one
+ * or more fingers over the touchpad element, as part of a "scroll"
+ * gesture initiated by dragging two fingers up or down, etc.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousedown = null;
+
+ /**
+ * Fired whenever a mouse button is effectively released. This can happen
+ * as part of a "click" gesture initiated by the user by tapping one
+ * or more fingers over the touchpad element, as part of a "scroll"
+ * gesture initiated by dragging two fingers up or down, etc.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmouseup = null;
+
+ /**
+ * Fired whenever the user moves the mouse by dragging their finger over
+ * the touchpad element.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousemove = null;
+
+ 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_buttons = {
+ 1: "left",
+ 2: "right",
+ 3: "middle"
+ };
+
+ var gesture_in_progress = false;
+ var click_release_timeout = null;
+
+ element.addEventListener("touchend", function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ // If we're handling a gesture AND this is the last touch
+ if (gesture_in_progress && e.touches.length == 0) {
+
+ 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_touchpad.currentState[button]) {
+
+ // Fire button up event
+ guac_touchpad.currentState[button] = false;
+ if (guac_touchpad.onmouseup)
+ guac_touchpad.onmouseup(guac_touchpad.currentState);
+
+ // Clear timeout, if set
+ if (click_release_timeout) {
+ window.clearTimeout(click_release_timeout);
+ click_release_timeout = null;
+ }
+
+ }
+
+ // If single tap detected (based on time and distance)
+ if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
+ && pixels_moved < guac_touchpad.clickMoveThreshold) {
+
+ // Fire button down event
+ guac_touchpad.currentState[button] = true;
+ if (guac_touchpad.onmousedown)
+ guac_touchpad.onmousedown(guac_touchpad.currentState);
+
+ // Delay mouse up - mouse up should be canceled if
+ // touchstart within timeout.
+ click_release_timeout = window.setTimeout(function() {
+
+ // Fire button up event
+ guac_touchpad.currentState[button] = false;
+ if (guac_touchpad.onmouseup)
+ guac_touchpad.onmouseup(guac_touchpad.currentState);
+
+ // Gesture now over
+ gesture_in_progress = false;
+
+ }, guac_touchpad.clickTimingThreshold);
+
+ }
+
+ // If we're not waiting to see if this is a click, stop gesture
+ if (!click_release_timeout)
+ gesture_in_progress = false;
+
+ }
+
+ }, false);
+
+ element.addEventListener("touchstart", function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ // Track number of touches, but no more than three
+ touch_count = Math.min(e.touches.length, 3);
+
+ // Clear timeout, if set
+ if (click_release_timeout) {
+ window.clearTimeout(click_release_timeout);
+ click_release_timeout = null;
+ }
+
+ // Record initial touch location and time for touch movement
+ // and tap gestures
+ if (!gesture_in_progress) {
+
+ // Stop mouse events while touching
+ gesture_in_progress = true;
+
+ // Record touch location and time
+ var starting_touch = e.touches[0];
+ last_touch_x = starting_touch.clientX;
+ last_touch_y = starting_touch.clientY;
+ last_touch_time = new Date().getTime();
+ pixels_moved = 0;
+
+ }
+
+ }, false);
+
+ element.addEventListener("touchmove", function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ // Get change in touch location
+ var touch = e.touches[0];
+ var delta_x = touch.clientX - last_touch_x;
+ var delta_y = touch.clientY - last_touch_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) {
+
+ // Calculate average velocity in Manhatten pixels per millisecond
+ var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
+
+ // Scale mouse movement relative to velocity
+ var scale = 1 + velocity;
+
+ // Update mouse location
+ guac_touchpad.currentState.x += delta_x*scale;
+ guac_touchpad.currentState.y += delta_y*scale;
+
+ // Prevent mouse from leaving screen
+
+ if (guac_touchpad.currentState.x < 0)
+ guac_touchpad.currentState.x = 0;
+ else if (guac_touchpad.currentState.x >= element.offsetWidth)
+ guac_touchpad.currentState.x = element.offsetWidth - 1;
+
+ if (guac_touchpad.currentState.y < 0)
+ guac_touchpad.currentState.y = 0;
+ else if (guac_touchpad.currentState.y >= element.offsetHeight)
+ guac_touchpad.currentState.y = element.offsetHeight - 1;
+
+ // Fire movement event, if defined
+ if (guac_touchpad.onmousemove)
+ guac_touchpad.onmousemove(guac_touchpad.currentState);
+
+ // Update touch location
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ // Interpret two-finger swipe as scrollwheel
+ else if (touch_count == 2) {
+
+ // If change in location passes threshold for scroll
+ if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
+
+ // Decide button based on Y movement direction
+ var button;
+ if (delta_y > 0) button = "down";
+ else button = "up";
+
+ // Fire button down event
+ guac_touchpad.currentState[button] = true;
+ if (guac_touchpad.onmousedown)
+ guac_touchpad.onmousedown(guac_touchpad.currentState);
+
+ // Fire button up event
+ guac_touchpad.currentState[button] = false;
+ if (guac_touchpad.onmouseup)
+ guac_touchpad.onmouseup(guac_touchpad.currentState);
+
+ // Only update touch location after a scroll has been
+ // detected
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ }
+
+ }, false);
+
+};
+
/**
* Simple container for properties describing the state of a mouse.
*