Line data Source code
1 : import { nextTick, onScopeDispose, toValue } from 'vue'
2 : import type { MaybeRefOrGetter } from 'vue'
3 :
4 : import {
5 : isMiddleButtonEvent,
6 : isMiddleButtonHeld,
7 : isMiddlePointerInput
8 : } from '@/base/pointerUtils'
9 : import { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer'
10 : import { captureGesture } from '@/lib/litegraph/src/canvas/captureGesture'
11 : import type {
12 : GestureEffect,
13 : GestureEvent,
14 : GestureState
15 : } from '@/lib/litegraph/src/canvas/reduceGesture'
16 : import {
17 : idleGesture,
18 : reduceGesture
19 : } from '@/lib/litegraph/src/canvas/reduceGesture'
20 : import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph'
21 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
22 : import { useCanvasInteractions } from '@/renderer/core/canvas/useCanvasInteractions'
23 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
24 : import { useNodeZIndex } from '@/renderer/extensions/vueNodes/composables/useNodeZIndex'
25 : import { useNodeDrag } from '@/renderer/extensions/vueNodes/layout/useNodeDrag'
26 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
27 : import type { NodeState } from '@comfyorg/graph-model/nodeState'
28 : import { isLGraphNode } from '@/utils/litegraphUtil'
29 :
30 : interface Press {
31 : nodeId: NodeId
32 : event: PointerEvent
33 : movingNode: boolean
34 : releaseCapture?: () => void
35 : }
36 :
37 2355 : const pressCancellers = new Map<number, () => void>()
38 :
39 8015 : export function useNodePointerInteractions(
40 : nodeStateRef: MaybeRefOrGetter<NodeState>
41 : ) {
42 8015 : const canvasStore = useCanvasStore()
43 8015 : const { startDrag, endDrag, handleDrag, cancelDrag } = useNodeDrag()
44 8015 : const { forwardEventToCanvas, shouldHandleNodePointerEvents } =
45 : useCanvasInteractions()
46 8015 : const { bringNodeToFront } = useNodeZIndex()
47 :
48 8015 : let gesture: GestureState = idleGesture
49 8015 : let press: Press | null = null
50 :
51 1416 : function canDrag() {
52 1416 : return !toValue(nodeStateRef).flags.pinned
53 : }
54 :
55 1396 : function dispatch(gestureEvent: GestureEvent, event: PointerEvent) {
56 1396 : const result = reduceGesture(gesture, gestureEvent)
57 1396 : gesture = result.state
58 1396 : try {
59 1396 : for (const effect of result.effects) runEffect(effect, event)
60 : } finally {
61 1396 : if (gesture.phase === 'idle') clearPress()
62 : }
63 : }
64 :
65 534 : function clearPress() {
66 534 : const completedPress = press
67 534 : press = null
68 534 : if (
69 : completedPress &&
70 : pressCancellers.get(completedPress.event.pointerId) === cancelPress
71 : )
72 534 : pressCancellers.delete(completedPress.event.pointerId)
73 534 : completedPress?.releaseCapture?.()
74 534 : if (completedPress?.movingNode) layoutStore.isDraggingVueNodes.value = false
75 : }
76 :
77 5244 : function cancelPress() {
78 5244 : if (press) dispatch({ type: 'cancel' }, press.event)
79 : }
80 :
81 535 : function selectNode(activePress: Press, sticky = false) {
82 535 : const { canvas } = canvasStore
83 535 : const node = canvasStore.currentGraph?.getNodeById(activePress.nodeId)
84 535 : if (node) canvas?.processSelect(node, activePress.event, sticky)
85 : }
86 :
87 25 : function startNodeDrag(activePress: Press, event: PointerEvent) {
88 25 : selectNode(activePress, true)
89 25 : if (!canDrag()) return
90 22 : layoutStore.isDraggingVueNodes.value = true
91 22 : activePress.movingNode = true
92 22 : startDrag(activePress.event, activePress.nodeId, event.shiftKey)
93 : }
94 :
95 8015 : const effectHandlers: Record<
96 : GestureEffect,
97 : (activePress: Press, event: PointerEvent) => void
98 : > = {
99 : click: (activePress) => selectNode(activePress),
100 : doubleClick: (activePress) => selectNode(activePress),
101 : movePress: () => {},
102 : startDrag: startNodeDrag,
103 : moveDrag: (activePress, event) => {
104 325 : if (activePress.movingNode && canDrag())
105 300 : handleDrag(event, activePress.nodeId)
106 : },
107 : endDrag: (activePress, event) => {
108 23 : if (activePress.movingNode)
109 21 : endDrag(event, canDrag() ? activePress.nodeId : undefined)
110 : },
111 : cancelDrag: (activePress) => {
112 1 : if (activePress.movingNode) cancelDrag()
113 : }
114 : }
115 :
116 886 : function runEffect(effect: GestureEffect, event: PointerEvent) {
117 886 : if (press) effectHandlers[effect](press, event)
118 : }
119 :
120 8015 : const forwardMiddlePointerIfNeeded = (
121 : event: PointerEvent,
122 : isMiddleInput: (event: PointerEvent) => boolean
123 : ) => {
124 2827 : if (!isMiddleInput(event)) return false
125 0 : forwardEventToCanvas(event)
126 0 : return true
127 : }
128 :
129 535 : function shouldCloneOnPress(event: PointerEvent) {
130 535 : return (
131 : LiteGraph.alt_drag_do_clone_nodes &&
132 : event.altKey &&
133 : !event.ctrlKey &&
134 : !!canvasStore.canvas?.allow_interaction
135 : )
136 : }
137 :
138 0 : function cloneNode(nodeId: NodeId): NodeId | undefined {
139 0 : const node = canvasStore.currentGraph?.getNodeById(nodeId)
140 0 : const clone = node && LGraphCanvas.cloneNodes([node])?.created[0]
141 0 : return isLGraphNode(clone) ? clone.id : undefined
142 : }
143 :
144 535 : function pressNode(event: PointerEvent): NodeId {
145 535 : const nodeId = toValue(nodeStateRef).id
146 535 : const cloneId = shouldCloneOnPress(event) ? cloneNode(nodeId) : undefined
147 535 : if (cloneId === undefined) {
148 535 : if (canDrag()) bringNodeToFront(nodeId)
149 535 : return nodeId
150 : }
151 0 : void nextTick(() => bringNodeToFront(cloneId))
152 0 : return cloneId
153 : }
154 :
155 639 : function onPointerdown(event: PointerEvent) {
156 639 : if (forwardMiddlePointerIfNeeded(event, isMiddlePointerInput)) return
157 :
158 639 : if (!shouldHandleNodePointerEvents.value) {
159 0 : forwardEventToCanvas(event)
160 0 : return
161 : }
162 :
163 639 : if (event.button !== 0) return
164 639 : if (press && press.event.pointerId !== event.pointerId) return
165 639 : pressCancellers.get(event.pointerId)?.()
166 :
167 639 : const nodeId = pressNode(event)
168 639 : const captureTarget =
169 : event.target instanceof Element ? event.target : undefined
170 639 : pressCancellers.set(event.pointerId, cancelPress)
171 639 : press = {
172 : nodeId,
173 : event,
174 : movingNode: false,
175 : releaseCapture:
176 : canDrag() && captureTarget
177 : ? captureGesture(captureTarget, event.pointerId, cancelPress)
178 : : undefined
179 : }
180 639 : dispatch(
181 : {
182 : type: 'down',
183 : position: { x: event.clientX, y: event.clientY },
184 : timeStamp: event.timeStamp,
185 : policy: CanvasPointer.gesturePolicy()
186 : },
187 : event
188 : )
189 : }
190 :
191 533 : function dispatchUp(event: PointerEvent) {
192 533 : dispatch(
193 : {
194 : type: 'up',
195 : position: { x: event.clientX, y: event.clientY },
196 : acceptsDoubleClick: false
197 : },
198 : event
199 : )
200 : }
201 :
202 2190 : function isPressPointer(event: PointerEvent) {
203 2190 : return press?.event.pointerId === event.pointerId
204 : }
205 :
206 1440 : function onPointermove(event: PointerEvent) {
207 1440 : if (forwardMiddlePointerIfNeeded(event, isMiddleButtonHeld)) return
208 1440 : if (!isPressPointer(event)) return
209 610 : if (!(event.buttons & 1)) {
210 2 : dispatchUp(event)
211 2 : return
212 : }
213 327 : dispatch(
214 : {
215 : type: 'move',
216 : position: { x: event.clientX, y: event.clientY }
217 : },
218 : event
219 : )
220 : }
221 :
222 748 : function onPointerup(event: PointerEvent) {
223 748 : if (forwardMiddlePointerIfNeeded(event, isMiddleButtonEvent)) return
224 748 : if (!shouldHandleNodePointerEvents.value) {
225 1 : forwardEventToCanvas(event)
226 1 : if (!isPressPointer(event)) return
227 1 : if (press?.movingNode) dispatchUp(event)
228 0 : else cancelPress()
229 1 : return
230 : }
231 748 : if (!isPressPointer(event) || event.button !== 0) return
232 530 : dispatchUp(event)
233 : }
234 :
235 2 : function onPointercancel(event: PointerEvent) {
236 2 : if (isPressPointer(event)) cancelPress()
237 : }
238 :
239 104 : function onContextmenu(event: MouseEvent) {
240 104 : if (!press?.movingNode) return
241 0 : event.preventDefault()
242 0 : event.stopImmediatePropagation()
243 0 : cancelPress()
244 : }
245 :
246 8015 : onScopeDispose(cancelPress)
247 :
248 : const pointerHandlers = {
249 : onPointerdown,
250 : onPointermove,
251 : onPointerup,
252 : onPointercancel,
253 : onContextmenu
254 : } as const
255 :
256 8015 : return {
257 : pointerHandlers
258 : }
259 : }
|