Line data Source code
1 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
2 : import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph'
3 :
4 : import { app } from '@/scripts/app'
5 :
6 : import { canvasOrReport } from './touchCanvasAccess'
7 : import { touchGesture } from './touchPointerGuards'
8 : import type { ComfyExtension } from '@/types/comfy'
9 :
10 : type TouchPoint = { clientX: number; clientY: number }
11 :
12 2355 : const MIN_PINCH_DISTANCE = 8
13 :
14 0 : function zoomCanvasAroundPinch(
15 : canvas: LGraphCanvas,
16 : requestedScale: number,
17 : center: TouchPoint,
18 : lastTouch: TouchPoint
19 : ) {
20 0 : let scale = requestedScale
21 :
22 0 : if (scale < canvas.ds.min_scale) {
23 0 : scale = canvas.ds.min_scale
24 0 : } else if (scale > canvas.ds.max_scale) {
25 0 : scale = canvas.ds.max_scale
26 : }
27 :
28 0 : const newX = (center.clientX - lastTouch.clientX) / scale
29 0 : const newY = (center.clientY - lastTouch.clientY) / scale
30 0 : const oldScale = canvas.ds.scale
31 :
32 0 : canvas.ds.scale = scale
33 :
34 0 : if (Math.abs(canvas.ds.scale - 1) < 0.01) {
35 0 : canvas.ds.scale = 1
36 : }
37 :
38 0 : const newScale = canvas.ds.scale
39 :
40 0 : const convertScaleToOffset = (scale: number) => [
41 : center.clientX / scale - canvas.ds.offset[0],
42 : center.clientY / scale - canvas.ds.offset[1]
43 : ]
44 0 : const oldCenter = convertScaleToOffset(oldScale)
45 0 : const newCenter = convertScaleToOffset(newScale)
46 :
47 0 : canvas.ds.offset[0] += newX + newCenter[0] - oldCenter[0]
48 0 : canvas.ds.offset[1] += newY + newCenter[1] - oldCenter[1]
49 :
50 0 : lastTouch.clientX = center.clientX
51 0 : lastTouch.clientY = center.clientY
52 :
53 0 : canvas.setDirty(true, true)
54 : }
55 :
56 2355 : export const simpleTouchSupportExtension: ComfyExtension = {
57 : name: 'Comfy.SimpleTouchSupport',
58 : setup() {
59 2355 : let touchDist: number | null = null
60 2355 : let touchTime: Date | null = null
61 2355 : let lastTouch: { clientX: number; clientY: number } | null = null
62 2355 : let lastScale: number | null = null
63 0 : function getMultiTouchPos(e: TouchEvent) {
64 0 : return Math.hypot(
65 : e.touches[0].clientX - e.touches[1].clientX,
66 : e.touches[0].clientY - e.touches[1].clientY
67 : )
68 : }
69 :
70 0 : function getMultiTouchCenter(e: TouchEvent) {
71 0 : return {
72 : clientX: (e.touches[0].clientX + e.touches[1].clientX) / 2,
73 : clientY: (e.touches[0].clientY + e.touches[1].clientY) / 2
74 : }
75 : }
76 :
77 2355 : app.canvasEl.parentElement?.addEventListener(
78 : 'touchstart',
79 : (e: TouchEvent) => {
80 0 : touchGesture.count += e.changedTouches.length
81 :
82 0 : lastTouch = null
83 0 : lastScale = null
84 0 : if (e.touches.length === 1) {
85 0 : touchTime = new Date()
86 0 : lastTouch = e.touches[0]
87 : } else {
88 0 : touchTime = null
89 0 : if (e.touches.length === 2) {
90 0 : const canvas = canvasOrReport('touchstart', e)
91 0 : if (!canvas) return
92 0 : lastScale = canvas.ds.scale
93 0 : lastTouch = getMultiTouchCenter(e)
94 :
95 0 : touchDist = getMultiTouchPos(e)
96 0 : canvas.pointer.isDown = false
97 : }
98 : }
99 : },
100 : true
101 : )
102 :
103 2355 : app.canvasEl.parentElement?.addEventListener(
104 : 'touchend',
105 : (e: TouchEvent) => {
106 0 : touchGesture.count -= e.changedTouches.length
107 :
108 0 : if (e.touches.length !== 1) touchGesture.zooming = false
109 0 : if (touchTime && !e.touches.length) {
110 0 : if (new Date().getTime() - touchTime.getTime() > 600) {
111 0 : if (e.target === app.canvasEl) {
112 0 : const touch = {
113 : button: 2,
114 : clientX: e.changedTouches[0].clientX,
115 : clientY: e.changedTouches[0].clientY,
116 : pointerId: 1,
117 : isPrimary: true
118 : }
119 0 : app.canvasEl.dispatchEvent(new PointerEvent('pointerdown', touch))
120 0 : setTimeout(() => {
121 0 : app.canvasEl.dispatchEvent(new PointerEvent('pointerup', touch))
122 : })
123 0 : e.preventDefault()
124 : }
125 : }
126 0 : touchTime = null
127 : }
128 : }
129 : )
130 :
131 2355 : const resetTouchState = () => {
132 98 : touchGesture.count = 0
133 98 : touchGesture.zooming = false
134 98 : touchTime = null
135 98 : lastTouch = null
136 98 : lastScale = null
137 98 : touchDist = null
138 : }
139 :
140 2355 : document.addEventListener('visibilitychange', () => {
141 100 : if (document.hidden) {
142 98 : resetTouchState()
143 : }
144 : })
145 :
146 2355 : app.canvasEl.parentElement?.addEventListener('touchcancel', resetTouchState)
147 :
148 0 : function handlePinchMove(e: TouchEvent) {
149 0 : if (!lastTouch) return
150 0 : const canvas = canvasOrReport('touchmove', e)
151 0 : if (!canvas) return
152 0 : e.preventDefault()
153 0 : canvas.pointer.isDown = false
154 0 : touchGesture.zooming = true
155 :
156 0 : LiteGraph.closeAllContextMenus(window)
157 0 : canvas.search_box?.close()
158 0 : const newTouchDist = getMultiTouchPos(e)
159 0 : const center = getMultiTouchCenter(e)
160 :
161 0 : if (lastScale === null || touchDist === null) return
162 0 : if (touchDist < MIN_PINCH_DISTANCE) {
163 0 : touchDist = newTouchDist
164 0 : lastScale = canvas.ds.scale
165 0 : lastTouch = center
166 0 : return
167 : }
168 0 : const scale = (lastScale * newTouchDist) / touchDist
169 0 : if (!Number.isFinite(scale) || scale <= 0) return
170 :
171 0 : zoomCanvasAroundPinch(canvas, scale, center, lastTouch)
172 : }
173 :
174 2355 : app.canvasEl.parentElement?.addEventListener(
175 : 'touchmove',
176 : (e) => {
177 0 : if (touchTime && lastTouch && e.touches.length === 1) {
178 0 : const onlyTouch = e.touches[0]
179 0 : const deltaX = onlyTouch.clientX - lastTouch.clientX
180 0 : const deltaY = onlyTouch.clientY - lastTouch.clientY
181 0 : if (deltaX * deltaX + deltaY * deltaY > 30) {
182 0 : touchTime = null
183 : }
184 : }
185 0 : if (e.touches.length === 2 && !e.ctrlKey && !e.shiftKey) {
186 0 : handlePinchMove(e)
187 : }
188 : },
189 : true
190 : )
191 : }
192 : }
|