Skip to content

Commit

Permalink
Refactor ghost click code
Browse files Browse the repository at this point in the history
Use Polymer.Debounce to properly debounce on quick taps
Use 2500ms timeout (from 0.5), 500ms is too short in the wild
  • Loading branch information
dfreedm committed May 29, 2015
1 parent eb1c9d1 commit d96917a
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/standard/gestures.html
Expand Up @@ -23,8 +23,8 @@
// number of last N track positions to keep
var TRACK_LENGTH = 2;

// Disabling "mouse" handlers for 500ms is enough
var MOUSE_TIMEOUT = 500;
// Disabling "mouse" handlers for 2500ms is enough
var MOUSE_TIMEOUT = 2500;
var MOUSE_EVENTS = ['mousedown', 'mousemove', 'mouseup', 'click'];

// touch will make synthetic mouse events
Expand All @@ -48,39 +48,41 @@
}
};

function ignoreMouse(set) {
// manual debounce
if (set && POINTERSTATE.touch.mouseIgnoreId !== -1) {
return;
}
function setupTeardownMouseCanceller(setup) {
for (var i = 0, en; i < MOUSE_EVENTS.length; i++) {
en = MOUSE_EVENTS[i];
if (set) {
if (setup) {
document.addEventListener(en, mouseCanceller, true);
} else {
document.removeEventListener(en, mouseCanceller, true);
}
}
if (set) {
// disable MOUSE_CANCELLER after MOUSE_TIMEOUT ms
POINTERSTATE.mouse.mouseIgnoreId = setTimeout(ignoreMouse, MOUSE_TIMEOUT);
} else {
POINTERSTATE.mouse.target = null;
POINTERSTATE.touch.mouseIgnoreId = -1;
}

function ignoreMouse() {
if (!POINTERSTATE.mouse.mouseIgnoreJob) {
setupTeardownMouseCanceller(true);
}
var unset = function() {
setupTeardownMouseCanceller();
POINTERSTATE.mouse.target = null;
POINTERSTATE.mouse.mouseIgnoreJob = null;
};
POINTERSTATE.mouse.mouseIgnoreJob =
Polymer.Debounce(POINTERSTATE.mouse.mouseIgnoreJob, unset, MOUSE_TIMEOUT);
}

var POINTERSTATE = {
tapPrevented: false,
mouse: {
target: null,
mouseIgnoreJob: null
},
touch: {
x: 0,
y: 0,
id: -1,
scrollDecided: false,
mouseIgnoreId: -1
scrollDecided: false
}
};

Expand Down

0 comments on commit d96917a

Please sign in to comment.