Line data Source code
1 : import { computed } from 'vue'
2 :
3 : import {
4 : isMiddleButtonEvent,
5 : isMiddleButtonHeld,
6 : isMiddlePointerInput
7 : } from '@/base/pointerUtils'
8 : import { isCanvasGestureWheel } from '@/base/wheelGestures'
9 : import { useSettingStore } from '@/platform/settings/settingStore'
10 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
11 :
12 : /**
13 : * Composable for handling canvas interactions from Vue components.
14 : * This provides a unified way to forward events to the LiteGraph canvas.
15 : */
16 29654 : export function useCanvasInteractions() {
17 29654 : const settingStore = useSettingStore()
18 29654 : const canvasStore = useCanvasStore()
19 :
20 29654 : const isStandardNavMode = computed(
21 : () => settingStore.get('Comfy.Canvas.NavigationMode') === 'standard'
22 : )
23 :
24 : /**
25 : * Whether Vue node components should handle pointer events.
26 : * Returns false when canvas is in read-only/panning mode (e.g., space key held for panning).
27 : */
28 29654 : const shouldHandleNodePointerEvents = computed(() => !canvasStore.isReadOnly)
29 :
30 : /**
31 : * Returns true if the wheel event target is inside an element that should
32 : * capture wheel events AND that element (or a descendant) currently has focus.
33 : *
34 : * This allows two-finger panning to continue over inputs until the user has
35 : * actively focused the widget, at which point the widget can consume scroll.
36 : */
37 29654 : const wheelCapturedByFocusedElement = (event: WheelEvent): boolean => {
38 : const target = event.target as HTMLElement | null
39 0 : const captureElement = target?.closest('[data-capture-wheel="true"]')
40 0 : const active = document.activeElement
41 :
42 0 : return !!(captureElement && active && captureElement.contains(active))
43 : }
44 :
45 : /**
46 : * Forward to canvas when the event is not consumed by a focused widget,
47 : * or when it is a canvas gesture (which must override widget consumption
48 : * to prevent destructive browser defaults).
49 : */
50 29654 : const shouldForwardWheelEvent = (event: WheelEvent): boolean =>
51 : !wheelCapturedByFocusedElement(event) || isCanvasGestureWheel(event)
52 :
53 : /**
54 : * Handles wheel events from UI components that should be forwarded to canvas
55 : * when appropriate (e.g., Ctrl+wheel for zoom, two-finger pan in standard
56 : * mode; all wheel events in legacy mode).
57 : */
58 29654 : const handleWheel = (event: WheelEvent) => {
59 0 : if (!shouldForwardWheelEvent(event)) return
60 :
61 : // In standard mode, only canvas gestures (zoom/pan) are forwarded;
62 : // vertical wheel falls through so the document/widget scrolls normally.
63 : // The re-check is intentional and NOT redundant with shouldForwardWheelEvent:
64 : // that function also returns true for unfocused vertical wheel (its
65 : // `!wheelCapturedByFocusedElement` branch), which here must stay native.
66 0 : if (isStandardNavMode.value) {
67 0 : if (isCanvasGestureWheel(event)) forwardEventToCanvas(event)
68 0 : return
69 : }
70 :
71 : // In legacy mode, all forwardable wheel events go to canvas for zoom/pan.
72 0 : forwardEventToCanvas(event)
73 : }
74 :
75 : /**
76 : * Handles pointer events from media elements that should potentially
77 : * be forwarded to canvas (e.g., space+drag for panning)
78 : */
79 29654 : const forwardMiddlePointerIfNeeded = (
80 : event: PointerEvent,
81 : isMiddleInput: (event: PointerEvent) => boolean
82 : ) => {
83 0 : if (isMiddleInput(event)) {
84 0 : forwardEventToCanvas(event)
85 0 : return true
86 : }
87 :
88 0 : return false
89 : }
90 :
91 29654 : const handleLeftButtonReadOnlyPointer = (event: PointerEvent) => {
92 0 : if (canvasStore.canvas?.read_only && event.buttons === 1) {
93 0 : event.preventDefault()
94 0 : event.stopPropagation()
95 0 : forwardEventToCanvas(event)
96 : }
97 : }
98 :
99 29654 : const handlePointerDown = (event: PointerEvent) => {
100 0 : if (forwardMiddlePointerIfNeeded(event, isMiddlePointerInput)) return
101 0 : handleLeftButtonReadOnlyPointer(event)
102 : }
103 :
104 29654 : const handlePointerMove = (event: PointerEvent) => {
105 0 : if (forwardMiddlePointerIfNeeded(event, isMiddleButtonHeld)) return
106 0 : handleLeftButtonReadOnlyPointer(event)
107 : }
108 :
109 29654 : const handlePointerUp = (event: PointerEvent) => {
110 0 : if (forwardMiddlePointerIfNeeded(event, isMiddleButtonEvent)) return
111 0 : handleLeftButtonReadOnlyPointer(event)
112 : }
113 :
114 : /**
115 : * Forwards an event to the LiteGraph canvas
116 : */
117 29654 : const forwardEventToCanvas = (
118 : event: WheelEvent | PointerEvent | MouseEvent
119 : ) => {
120 : // Honor wheel capture only when the element is focused
121 3 : if (event instanceof WheelEvent && !shouldForwardWheelEvent(event)) return
122 :
123 3 : const canvasEl = canvasStore.canvas?.canvas
124 3 : if (!canvasEl) return
125 3 : event.preventDefault()
126 3 : event.stopPropagation()
127 :
128 3 : if (event instanceof WheelEvent) {
129 0 : const { clientX, clientY, deltaX, deltaY, ctrlKey, metaKey, shiftKey } =
130 : event
131 0 : canvasEl.dispatchEvent(
132 : new WheelEvent('wheel', {
133 : clientX,
134 : clientY,
135 : deltaX,
136 : deltaY,
137 : ctrlKey,
138 : metaKey,
139 : shiftKey
140 : })
141 : )
142 0 : return
143 : }
144 :
145 3 : if (event instanceof PointerEvent) {
146 3 : canvasEl.dispatchEvent(new PointerEvent(event.type, event))
147 3 : return
148 : }
149 :
150 0 : canvasEl.dispatchEvent(new MouseEvent(event.type, event))
151 : }
152 :
153 29654 : return {
154 : handleWheel,
155 : handlePointerDown,
156 : handlePointerMove,
157 : handlePointerUp,
158 : forwardEventToCanvas,
159 : shouldHandleNodePointerEvents
160 : }
161 : }
|