Skip to content
Browse files

Refactor ghost click code

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...
1 parent eb1c9d1 commit d96917a07708703087ba1f48c3f7ac744f54e502 @azakus azakus committed
Showing with 18 additions and 16 deletions.
  1. +18 −16 src/standard/gestures.html
View
34 src/standard/gestures.html
@@ -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
@@ -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
}
};

0 comments on commit d96917a

Please sign in to comment.
Something went wrong with that request. Please try again.