Skip to content

Commit d883448

Browse files
authored
Fix incorrect touch event handling by accurately tracking touch points (#3622)
1 parent 1c59e3b commit d883448

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

src/extensions/core/simpleTouchSupport.ts

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,35 @@ import { LGraphCanvas, LiteGraph } from '@comfyorg/litegraph'
22

33
import { app } from '../../scripts/app'
44

5-
// @ts-expect-error fixme ts strict error
6-
let touchZooming
5+
let touchZooming = false
76
let touchCount = 0
87

98
app.registerExtension({
109
name: 'Comfy.SimpleTouchSupport',
1110
setup() {
12-
// @ts-expect-error fixme ts strict error
13-
let touchDist
14-
// @ts-expect-error fixme ts strict error
15-
let touchTime
16-
// @ts-expect-error fixme ts strict error
17-
let lastTouch
18-
// @ts-expect-error fixme ts strict error
19-
let lastScale
20-
// @ts-expect-error fixme ts strict error
21-
function getMultiTouchPos(e) {
11+
let touchDist: number | null = null
12+
let touchTime: Date | null = null
13+
let lastTouch: { clientX: number; clientY: number } | null = null
14+
let lastScale: number | null = null
15+
function getMultiTouchPos(e: TouchEvent) {
2216
return Math.hypot(
2317
e.touches[0].clientX - e.touches[1].clientX,
2418
e.touches[0].clientY - e.touches[1].clientY
2519
)
2620
}
2721

28-
// @ts-expect-error fixme ts strict error
29-
function getMultiTouchCenter(e) {
22+
function getMultiTouchCenter(e: TouchEvent) {
3023
return {
3124
clientX: (e.touches[0].clientX + e.touches[1].clientX) / 2,
3225
clientY: (e.touches[0].clientY + e.touches[1].clientY) / 2
3326
}
3427
}
3528

36-
// @ts-expect-error fixme ts strict error
37-
app.canvasEl.parentElement.addEventListener(
29+
app.canvasEl.parentElement?.addEventListener(
3830
'touchstart',
3931
(e: TouchEvent) => {
40-
touchCount++
32+
touchCount += e.changedTouches.length
33+
4134
lastTouch = null
4235
lastScale = null
4336
if (e.touches?.length === 1) {
@@ -59,35 +52,34 @@ app.registerExtension({
5952
true
6053
)
6154

62-
// @ts-expect-error fixme ts strict error
63-
app.canvasEl.parentElement.addEventListener('touchend', (e: TouchEvent) => {
64-
touchCount--
65-
66-
if (e.touches?.length !== 1) touchZooming = false
67-
// @ts-expect-error fixme ts strict error
68-
if (touchTime && !e.touches?.length) {
69-
if (new Date().getTime() - touchTime > 600) {
70-
if (e.target === app.canvasEl) {
71-
app.canvasEl.dispatchEvent(
72-
new PointerEvent('pointerdown', {
73-
button: 2,
74-
clientX: e.changedTouches[0].clientX,
75-
clientY: e.changedTouches[0].clientY
76-
})
77-
)
78-
e.preventDefault()
55+
app.canvasEl.parentElement?.addEventListener(
56+
'touchend',
57+
(e: TouchEvent) => {
58+
touchCount -= e.changedTouches.length
59+
60+
if (e.touches?.length !== 1) touchZooming = false
61+
if (touchTime && !e.touches?.length) {
62+
if (new Date().getTime() - touchTime.getTime() > 600) {
63+
if (e.target === app.canvasEl) {
64+
app.canvasEl.dispatchEvent(
65+
new PointerEvent('pointerdown', {
66+
button: 2,
67+
clientX: e.changedTouches[0].clientX,
68+
clientY: e.changedTouches[0].clientY
69+
})
70+
)
71+
e.preventDefault()
72+
}
7973
}
74+
touchTime = null
8075
}
81-
touchTime = null
8276
}
83-
})
77+
)
8478

85-
// @ts-expect-error fixme ts strict error
86-
app.canvasEl.parentElement.addEventListener(
79+
app.canvasEl.parentElement?.addEventListener(
8780
'touchmove',
8881
(e) => {
8982
touchTime = null
90-
// @ts-expect-error fixme ts strict error
9183
if (e.touches?.length === 2 && lastTouch && !e.ctrlKey && !e.shiftKey) {
9284
e.preventDefault() // Prevent browser from zooming when two textareas are touched
9385
app.canvas.pointer.isDown = false
@@ -100,7 +92,7 @@ app.registerExtension({
10092

10193
const center = getMultiTouchCenter(e)
10294

103-
// @ts-expect-error fixme ts strict error
95+
if (lastScale === null || touchDist === null) return
10496
let scale = (lastScale * newTouchDist) / touchDist
10597

10698
const newX = (center.clientX - lastTouch.clientX) / scale
@@ -124,8 +116,7 @@ app.registerExtension({
124116

125117
const newScale = app.canvas.ds.scale
126118

127-
// @ts-expect-error fixme ts strict error
128-
const convertScaleToOffset = (scale) => [
119+
const convertScaleToOffset = (scale: number) => [
129120
center.clientX / scale - app.canvas.ds.offset[0],
130121
center.clientY / scale - app.canvas.ds.offset[1]
131122
]
@@ -147,22 +138,18 @@ app.registerExtension({
147138
})
148139

149140
const processMouseDown = LGraphCanvas.prototype.processMouseDown
150-
LGraphCanvas.prototype.processMouseDown = function () {
151-
// @ts-expect-error fixme ts strict error
141+
LGraphCanvas.prototype.processMouseDown = function (e: PointerEvent) {
152142
if (touchZooming || touchCount) {
153143
return
154144
}
155145
app.canvas.pointer.isDown = false // Prevent context menu from opening on second tap
156-
// @ts-expect-error fixme ts strict error
157-
return processMouseDown.apply(this, arguments)
146+
return processMouseDown.apply(this, [e])
158147
}
159148

160149
const processMouseMove = LGraphCanvas.prototype.processMouseMove
161-
LGraphCanvas.prototype.processMouseMove = function () {
162-
// @ts-expect-error fixme ts strict error
150+
LGraphCanvas.prototype.processMouseMove = function (e: PointerEvent) {
163151
if (touchZooming || touchCount > 1) {
164152
return
165153
}
166-
// @ts-expect-error fixme ts strict error
167-
return processMouseMove.apply(this, arguments)
154+
return processMouseMove.apply(this, [e])
168155
}

0 commit comments

Comments
 (0)