Line data Source code
1 : import { useElementBounding, useEventListener } from '@vueuse/core'
2 : import { computed, onUnmounted, ref, watch, watchEffect } from 'vue'
3 : import type { Ref } from 'vue'
4 :
5 : import { useSelectedLiteGraphItems } from '@/composables/canvas/useSelectedLiteGraphItems'
6 : import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
7 : import { onCanvasFrame } from '@/lib/litegraph/src/canvas/canvasFrame'
8 : import type {
9 : ReadOnlyRect,
10 : Rect,
11 : Point2D as Point
12 : } from '@comfyorg/shared-frontend-utils/geometry'
13 : import { getSelectedLinksAnchor } from '@/lib/litegraph/src/canvas/linkSelection'
14 : import { LGraphGroup, LGraphNode } from '@/lib/litegraph/src/litegraph'
15 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
16 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
17 : import { useLinkSelectionStore } from '@/stores/linkSelectionStore'
18 : import { isLGraphGroup, isLGraphNode } from '@/utils/litegraphUtil'
19 : import { computeUnionBounds } from '@/utils/mathUtil'
20 :
21 : /**
22 : * Manages the position of the selection toolbox independently.
23 : * Uses CSS custom properties for performant transform updates.
24 : */
25 :
26 : // Shared signals for auxiliary UI (e.g., MoreOptions) to coordinate hide/restore
27 2355 : const moreOptionsOpen = ref(false)
28 2355 : const forceCloseMoreOptionsSignal = ref(0)
29 2355 : const restoreMoreOptionsSignal = ref(0)
30 2355 : const moreOptionsRestorePending = ref(false)
31 2355 : let moreOptionsWasOpenBeforeDrag = false
32 2355 : let moreOptionsSelectionSignature: string | null = null
33 :
34 0 : function buildSelectionSignature(
35 : store: ReturnType<typeof useCanvasStore>
36 : ): string | null {
37 0 : const c = store.canvas
38 0 : if (!c) return null
39 0 : const items = Array.from(c.selectedItems)
40 0 : if (items.length !== 1) return null
41 0 : const item = items[0]
42 0 : if (isLGraphNode(item)) return `N:${item.id}`
43 0 : if (isLGraphGroup(item)) return `G:${item.id}`
44 0 : return null
45 : }
46 :
47 6 : function currentSelectionMatchesSignature(
48 : store: ReturnType<typeof useCanvasStore>
49 : ) {
50 6 : if (!moreOptionsSelectionSignature) return false
51 0 : return buildSelectionSignature(store) === moreOptionsSelectionSignature
52 : }
53 :
54 362 : function getSelectionBounds(item: LGraphNode | LGraphGroup): ReadOnlyRect {
55 362 : if (item instanceof LGraphGroup) return item.boundingRect
56 :
57 347 : const bounds: Rect = [0, 0, 0, 0]
58 347 : item.measure(bounds)
59 347 : return bounds
60 : }
61 :
62 2355 : const SELECTION_BOUNDS_PADDING = 10
63 2355 : const LINK_ANCHOR_GAP = 10
64 :
65 109 : export function useSelectionToolboxPosition(
66 : toolboxRef: Ref<HTMLElement | undefined>
67 : ) {
68 109 : const canvasStore = useCanvasStore()
69 109 : const lgCanvas = canvasStore.getCanvas()
70 109 : const { getSelectableItems } = useSelectedLiteGraphItems()
71 109 : const { shouldRenderVueNodes } = useVueFeatureFlags()
72 109 : const isDraggingLiteGraphItems = ref(lgCanvas.isDragging)
73 109 : const linkSelectionStore = useLinkSelectionStore()
74 :
75 109 : useEventListener(
76 : lgCanvas.canvas,
77 : 'litegraph:dragging-items-changed',
78 : (event: CustomEvent<{ dragging: boolean }>) => {
79 9 : isDraggingLiteGraphItems.value = event.detail.dragging
80 : }
81 : )
82 :
83 109 : const worldPosition = ref({ x: 0, y: 0 })
84 :
85 109 : const visible = ref(false)
86 :
87 109 : const { left: canvasLeft, top: canvasTop } = useElementBounding(
88 : lgCanvas.canvas
89 : )
90 :
91 109 : const isDragging = computed((): boolean => {
92 122 : const vueNodeDragging =
93 : shouldRenderVueNodes.value && layoutStore.isDraggingVueNodes.value
94 122 : return isDraggingLiteGraphItems.value || vueNodeDragging
95 : })
96 :
97 282 : function itemsAnchor(items: Set<unknown>): Point | undefined {
98 282 : const bounds = computeUnionBounds(
99 : [...items].flatMap((item) =>
100 : item instanceof LGraphNode || item instanceof LGraphGroup
101 : ? [getSelectionBounds(item)]
102 : : []
103 : )
104 : )
105 282 : return bounds
106 : ? {
107 : x: bounds.x + bounds.width / 2,
108 : y: bounds.y - SELECTION_BOUNDS_PADDING
109 : }
110 : : undefined
111 : }
112 :
113 945 : function selectionAnchor(): Point | undefined {
114 945 : const items = getSelectableItems()
115 945 : if (items.size) return itemsAnchor(items)
116 945 : const linkAnchor = lgCanvas.graph && getSelectedLinksAnchor(lgCanvas.graph)
117 945 : return linkAnchor
118 : ? { x: linkAnchor.x, y: linkAnchor.y - LINK_ANCHOR_GAP }
119 : : undefined
120 : }
121 :
122 109 : const updateSelectionBounds = () => {
123 945 : const anchor = selectionAnchor()
124 :
125 945 : if (!anchor || isDragging.value) {
126 718 : visible.value = false
127 718 : return
128 : }
129 :
130 227 : visible.value = true
131 227 : worldPosition.value = anchor
132 :
133 227 : updateTransform()
134 : }
135 :
136 109 : const updateTransform = () => {
137 2268 : if (!visible.value) return
138 :
139 2268 : const { scale, offset } = lgCanvas.ds
140 :
141 2268 : const screenX =
142 : (worldPosition.value.x + offset[0]) * scale + canvasLeft.value
143 2268 : const screenY =
144 : (worldPosition.value.y + offset[1]) * scale + canvasTop.value
145 :
146 2268 : if (toolboxRef.value) {
147 2268 : toolboxRef.value.style.setProperty('--tb-x', `${screenX}px`)
148 2268 : toolboxRef.value.style.setProperty('--tb-y', `${screenY}px`)
149 : }
150 : }
151 :
152 109 : watchEffect((onCleanup) => {
153 268 : if (!visible.value) return
154 119 : onCleanup(onCanvasFrame(lgCanvas, 'present', updateTransform))
155 : })
156 :
157 109 : const handleSelectionMembershipChange = () => {
158 735 : if (!moreOptionsRestorePending.value && !moreOptionsSelectionSignature)
159 735 : return
160 :
161 0 : moreOptionsRestorePending.value = false
162 0 : moreOptionsWasOpenBeforeDrag = false
163 735 : moreOptionsSelectionSignature = moreOptionsOpen.value
164 : ? buildSelectionSignature(canvasStore)
165 : : null
166 : }
167 :
168 109 : watch(
169 : [
170 : () => canvasStore.selectedItems,
171 : () => layoutStore.layoutVersion,
172 : () => linkSelectionStore.selection
173 : ],
174 : ([selectedItems], [previousSelectedItems]) => {
175 939 : if (selectedItems !== previousSelectedItems)
176 735 : handleSelectionMembershipChange()
177 939 : updateSelectionBounds()
178 : },
179 : { immediate: true }
180 : )
181 109 : watch(
182 : () => moreOptionsOpen.value,
183 : (v) => {
184 0 : if (v) {
185 0 : moreOptionsSelectionSignature = buildSelectionSignature(canvasStore)
186 0 : } else if (!isDraggingLiteGraphItems.value) {
187 0 : moreOptionsSelectionSignature = null
188 0 : if (moreOptionsRestorePending.value)
189 0 : moreOptionsRestorePending.value = false
190 : }
191 : }
192 : )
193 :
194 109 : const handleDragStateChange = (dragging: boolean) => {
195 13 : if (dragging) {
196 7 : handleDragStart()
197 7 : return
198 : }
199 :
200 6 : handleDragEnd()
201 : }
202 :
203 109 : const handleDragStart = () => {
204 7 : visible.value = false
205 :
206 7 : if (!moreOptionsOpen.value) {
207 7 : moreOptionsRestorePending.value = false
208 7 : moreOptionsWasOpenBeforeDrag = false
209 7 : return
210 : }
211 :
212 : const currentSig = buildSelectionSignature(canvasStore)
213 : const selectionChanged = currentSig !== moreOptionsSelectionSignature
214 :
215 0 : if (selectionChanged) {
216 0 : moreOptionsSelectionSignature = null
217 : }
218 0 : moreOptionsOpen.value = false
219 0 : moreOptionsWasOpenBeforeDrag = true
220 0 : moreOptionsRestorePending.value = !!moreOptionsSelectionSignature
221 :
222 0 : if (moreOptionsRestorePending.value) {
223 0 : forceCloseMoreOptionsSignal.value++
224 0 : return
225 : }
226 :
227 0 : moreOptionsWasOpenBeforeDrag = false
228 : }
229 :
230 109 : const handleDragEnd = () => {
231 6 : requestAnimationFrame(() => {
232 6 : updateSelectionBounds()
233 :
234 6 : const selectionMatches = currentSelectionMatchesSignature(canvasStore)
235 6 : const shouldRestore =
236 : moreOptionsWasOpenBeforeDrag &&
237 : visible.value &&
238 : moreOptionsRestorePending.value &&
239 : selectionMatches
240 :
241 6 : moreOptionsRestorePending.value =
242 : shouldRestore && moreOptionsRestorePending.value
243 6 : moreOptionsWasOpenBeforeDrag = false
244 :
245 6 : if (shouldRestore) {
246 0 : restoreMoreOptionsSignal.value++
247 : }
248 : })
249 : }
250 :
251 109 : watch(isDragging, handleDragStateChange)
252 :
253 109 : onUnmounted(() => {
254 0 : resetMoreOptionsState()
255 : })
256 :
257 109 : return {
258 : visible
259 : }
260 : }
261 :
262 : // External cleanup utility to be called when SelectionToolbox component unmounts
263 0 : function resetMoreOptionsState() {
264 0 : moreOptionsOpen.value = false
265 0 : moreOptionsRestorePending.value = false
266 0 : moreOptionsWasOpenBeforeDrag = false
267 0 : moreOptionsSelectionSignature = null
268 : }
|