Skip to content

Commit

Permalink
Safer check for dblclick management in window applications, fixes #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Aioros committed Jul 22, 2024
1 parent ee3dbd9 commit cd55704
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"discord": "MrAioros"
}
],
"version": "2.0.10",
"version": "2.0.11",
"compatibility": {
"minimum": "11",
"verified": "12.329"
Expand All @@ -37,7 +37,7 @@
}
],
"url": "https://github.com/Oromis/touch-vtt",
"download": "https://github.com/Oromis/touch-vtt/releases/download/v2.0.10/touch-vtt-v2.0.10.zip",
"download": "https://github.com/Oromis/touch-vtt/releases/download/v2.0.11/touch-vtt-v2.0.11.zip",
"manifest": "https://raw.githubusercontent.com/Oromis/touch-vtt/main/module.json",
"license": "https://www.gnu.org/licenses/gpl-3.0.en.html",
"readme": "README.md",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "touch-vtt",
"version": "2.0.10",
"version": "2.0.11",
"description": "Adds touch support to FoundryVTT",
"main": "src/touch-vtt.js",
"scripts": {
Expand Down
30 changes: 15 additions & 15 deletions src/logic/WindowAppAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function createStyleElement() {

class WindowAppAdapter {
constructor() {
this.lastClickInfo = {target: null, time: 0}
this.lastClickInfo = {target: null, time: 0, touch: false}

/*** Double-click management - Start ***/
// In both v11 and v12 (but in an especially weird way in v11) double clicks on app windows are triggered inconsistently for touch events
Expand All @@ -32,16 +32,17 @@ class WindowAppAdapter {
// but after double touching a different section of the window, the behavior becomes the same as v12
// The brutal approach here is to just hijack and cancel any dblclick event on an app, and create our own as best as we can

// Reminder: this would be cleaner using evt.sourceCapabilities.firesTouchEvents, but it's not supported by Firefox and Safari yet.
// If updated in the future, we don't need to keep track of lastClickInfo.touch anymore, and we just filter by that in both listeners.

// Cancel any native dblclick event on apps
document.body.addEventListener("dblclick", (evt) => {
if (evt.sourceCapabilities?.firesTouchEvents) { // We try to only do this for touch-based dblclick
const isInApp = !!evt.target.closest(".app")
if (evt.isTrusted && isInApp) {
evt.preventDefault()
evt.stopImmediatePropagation()
evt.stopPropagation()
return false
}
const isInApp = !!evt.target.closest(".app, .application")
if (evt.isTrusted && isInApp && this.lastClickInfo.touch) { // we only cancel native dblclick if the last click we received was touch-based
evt.preventDefault()
evt.stopImmediatePropagation()
evt.stopPropagation()
return false
}
}, true)

Expand All @@ -62,13 +63,12 @@ class WindowAppAdapter {
}

manageTouchDblClick(clickEvent) {
if (clickEvent.originalEvent.sourceCapabilities?.firesTouchEvents) { // We try to only do this for touch-based dblclick
if (clickEvent.pointerType == "touch" && Date.now() - this.lastClickInfo.time < 500 && clickEvent.target == this.lastClickInfo.target) {
dispatchModifiedEvent(clickEvent, "dblclick")
this.lastClickInfo = {target: null, time: 0}
}
this.lastClickInfo = {target: clickEvent.target, time: Date.now()}
const isTouch = ["touch", "pen"].includes(clickEvent.pointerType)
if (isTouch && Date.now() - this.lastClickInfo.time < 500 && clickEvent.target == this.lastClickInfo.target) {
dispatchModifiedEvent(clickEvent, "dblclick")
this.lastClickInfo = {target: null, time: 0, touch: isTouch}
}
this.lastClickInfo = {target: clickEvent.target, time: Date.now(), touch: isTouch}
}
}

Expand Down

0 comments on commit cd55704

Please sign in to comment.