Line data Source code
1 : import type {
2 : LGraph,
3 : LGraphGroup,
4 : LGraphNode,
5 : Reroute
6 : } from '@comfyorg/graph-model/entityRegistry'
7 :
8 : import { createMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView'
9 : import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle'
10 : import type {
11 : PointTuple as LegacyPoint,
12 : SizeTuple as LegacySize,
13 : Point2D as Point,
14 : Size2D as Size
15 : } from '@comfyorg/shared-frontend-utils/geometry'
16 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
17 : import { LayoutSource } from '@/renderer/core/layout/types'
18 : import type { LayoutOperation } from '@/renderer/core/layout/types'
19 : import { UNASSIGNED_NODE_ID } from '@comfyorg/graph-model/nodeId'
20 : import type { UUID } from '@/utils/uuid'
21 :
22 : type GraphLayoutOwner = Pick<
23 : LGraph,
24 : '_nodes' | '_groups' | '_subgraphs' | 'reroutes'
25 : >
26 : type LayoutGraph = { rootGraph: { id: UUID } }
27 :
28 : interface LayoutAttachment<TId> {
29 : graphId: UUID
30 : id: TId
31 : }
32 :
33 : type NodeLayoutAttachment = LayoutAttachment<LGraphNode['id']>
34 :
35 2356 : const nodeAttachments = new WeakMap<LGraphNode, NodeLayoutAttachment>()
36 2356 : const nodeAttachmentOwners = new Map<
37 : UUID,
38 : Map<LGraphNode['id'], WeakRef<LGraphNode>>
39 : >()
40 :
41 76 : function nodeAttachmentOwner(
42 : graphId: UUID,
43 : nodeId: LGraphNode['id']
44 : ): LGraphNode | undefined {
45 76 : return nodeAttachmentOwners.get(graphId)?.get(nodeId)?.deref()
46 : }
47 :
48 23255 : function setNodeAttachmentOwner(graphId: UUID, node: LGraphNode): void {
49 23255 : let owners = nodeAttachmentOwners.get(graphId)
50 23255 : if (!owners) {
51 4056 : owners = new Map()
52 4056 : nodeAttachmentOwners.set(graphId, owners)
53 : }
54 23255 : owners.set(node.id, new WeakRef(node))
55 : }
56 :
57 10979 : function deleteNodeAttachmentOwner(graphId: UUID, node: LGraphNode): boolean {
58 10979 : const owners = nodeAttachmentOwners.get(graphId)
59 10979 : if (owners?.get(node.id)?.deref() !== node) return false
60 10979 : owners.delete(node.id)
61 10979 : if (owners.size === 0) nodeAttachmentOwners.delete(graphId)
62 10979 : return true
63 : }
64 : interface NodeGeometryProjection {
65 : buffer: Rectangle
66 : contentSizeVersion: number
67 : geometryVersion: number
68 : layoutRef: ReturnType<typeof layoutStore.getNodeLayoutRef> | undefined
69 : position: LegacyPoint
70 : positionView: LegacyPoint
71 : renderedSize: LegacySize
72 : renderedSizeDirty: boolean
73 : storedRect: Float64Array
74 : size: LegacySize
75 : sizeView: LegacySize
76 : }
77 :
78 2356 : const nodeGeometryProjections = new WeakMap<
79 : LGraphNode,
80 : NodeGeometryProjection
81 : >()
82 11862897 : function nodeGeometryProjection(node: LGraphNode): NodeGeometryProjection {
83 11862897 : const existing = nodeGeometryProjections.get(node)
84 11862897 : if (existing) return existing
85 :
86 23337 : const buffer = new Rectangle()
87 23337 : const position = buffer.pos
88 23337 : const size = buffer.size
89 23337 : const projection: NodeGeometryProjection = {
90 : buffer,
91 : contentSizeVersion: -1,
92 : geometryVersion: -1,
93 : layoutRef: undefined,
94 : position,
95 : positionView: createMutationView(position, {
96 : commit: () => commitNodePosition(node),
97 : synchronize: () => refreshNodeGeometry(node)
98 : }),
99 : renderedSize: [0, 0],
100 : renderedSizeDirty: true,
101 : storedRect: new Float64Array(4),
102 : size,
103 : sizeView: createMutationView(size, {
104 : commit: () => commitNodeSize(node),
105 : synchronize: () => refreshNodeGeometry(node)
106 : })
107 : }
108 23337 : nodeGeometryProjections.set(node, projection)
109 23337 : return projection
110 : }
111 :
112 0 : export function nodeGeometryBuffer(node: LGraphNode): Rectangle {
113 0 : return nodeGeometryProjection(node).buffer
114 : }
115 :
116 47735 : export function nodePositionBuffer(node: LGraphNode): LegacyPoint {
117 47735 : return nodeGeometryProjection(node).position
118 : }
119 :
120 46472 : export function nodeSizeBuffer(node: LGraphNode): LegacySize {
121 46472 : return nodeGeometryProjection(node).size
122 : }
123 :
124 2901863 : export function nodePositionView(node: LGraphNode): LegacyPoint {
125 2901863 : const projection = nodeGeometryProjection(node)
126 2901863 : void projection.layoutRef?.value
127 2901863 : return projection.positionView
128 : }
129 :
130 1738856 : export function nodeSizeView(node: LGraphNode): LegacySize {
131 1738856 : const projection = nodeGeometryProjection(node)
132 1738856 : void projection.layoutRef?.value
133 1738856 : return projection.sizeView
134 : }
135 :
136 6464293 : export function refreshNodeGeometry(node: LGraphNode): LegacySize {
137 6464293 : const projection = nodeGeometryProjection(node)
138 6464293 : const attachment = nodeAttachments.get(node)
139 6464293 : let geometryChanged = false
140 6464293 : if (
141 : attachment &&
142 : projection.geometryVersion !== layoutStore.nodeGeometryVersion
143 : ) {
144 156196 : projection.geometryVersion = layoutStore.nodeGeometryVersion
145 156196 : layoutStore.readNodeRect(
146 : attachment.graphId,
147 : attachment.id,
148 : projection.buffer
149 : )
150 156196 : geometryChanged = true
151 : }
152 :
153 6464293 : if (
154 : projection.contentSizeVersion !== layoutStore.contentSizeVersion ||
155 : projection.renderedSizeDirty ||
156 : geometryChanged
157 : ) {
158 333038 : const contentSize = attachment
159 : ? layoutStore.contentSizeOf(attachment.graphId, attachment.id)
160 : : undefined
161 333038 : projection.renderedSize[0] = Math.max(
162 : projection.size[0],
163 : contentSize?.width ?? 0
164 : )
165 333038 : projection.renderedSize[1] = Math.max(
166 : projection.size[1],
167 : contentSize?.height ?? 0
168 : )
169 333038 : projection.contentSizeVersion = layoutStore.contentSizeVersion
170 333038 : projection.renderedSizeDirty = false
171 : }
172 6464293 : return projection.renderedSize
173 : }
174 :
175 48268 : export function setNodePosition(node: LGraphNode, value: LegacyPoint): void {
176 48268 : const position = nodeGeometryProjection(node).position
177 48268 : position[0] = value[0]
178 48268 : position[1] = value[1]
179 48268 : commitNodePosition(node)
180 : }
181 :
182 95309 : function commitNodePosition(node: LGraphNode): void {
183 95309 : const attachment = nodeAttachments.get(node)
184 95309 : if (node.id === UNASSIGNED_NODE_ID || !attachment) return
185 69759 : const projection = nodeGeometryProjection(node)
186 69759 : const position = { x: projection.position[0], y: projection.position[1] }
187 95309 : if (
188 : layoutStore.readNodeRect(
189 : attachment.graphId,
190 : attachment.id,
191 : projection.storedRect
192 : ) &&
193 : projection.storedRect[0] === position.x &&
194 : projection.storedRect[1] === position.y
195 : )
196 23206 : return
197 :
198 46553 : moveNodeLayout(node, position)
199 46553 : projection.geometryVersion = -1
200 46553 : refreshNodeGeometry(node)
201 : }
202 :
203 239185 : export function setNodeSize(node: LGraphNode, value: LegacySize): void {
204 : const projection = nodeGeometryProjection(node)
205 239185 : const size = projection.size
206 239185 : size[0] = value[0]
207 239185 : size[1] = value[1]
208 239185 : commitNodeSize(node)
209 : }
210 :
211 272213 : function commitNodeSize(node: LGraphNode): void {
212 272213 : const projection = nodeGeometryProjection(node)
213 : // Both whole-array assignments and indexed writes through the stable
214 : // mutation view converge here. Detached nodes do not receive a layout-store
215 : // geometry revision, so invalidate their derived rendering size before the
216 : // attachment guard as well.
217 272213 : projection.renderedSizeDirty = true
218 272213 : const attachment = nodeAttachments.get(node)
219 272213 : if (node.id === UNASSIGNED_NODE_ID || !attachment) return
220 272213 : if (
221 : layoutStore.readNodeRect(
222 : attachment.graphId,
223 : attachment.id,
224 : projection.storedRect
225 : ) &&
226 : projection.storedRect[2] === projection.size[0] &&
227 : projection.storedRect[3] === projection.size[1]
228 : )
229 43815 : return
230 :
231 : // A direct resize is authoritative over the previous DOM measurement. Drop
232 : // that measurement before deriving the rendered size so the node can shrink;
233 : // ResizeObserver reports the content's new size after layout settles.
234 272213 : ;(layoutStore as Partial<typeof layoutStore>).clearContentSize?.(
235 : attachment.graphId,
236 : attachment.id
237 : )
238 272213 : resizeNodeLayout(node, {
239 : width: projection.size[0],
240 : height: projection.size[1]
241 : })
242 272213 : projection.geometryVersion = -1
243 272213 : refreshNodeGeometry(node)
244 : }
245 2356 : const groupAttachments = new WeakMap<
246 : LGraphGroup,
247 : LayoutAttachment<LGraphGroup['id']>
248 : >()
249 2356 : const rerouteAttachments = new WeakMap<
250 : Reroute,
251 : LayoutAttachment<Reroute['id']>
252 : >()
253 :
254 : /** Shared operation meta attributing the operation to the canvas. */
255 71866 : function canvasOperationMeta() {
256 71866 : return {
257 : source: LayoutSource.Canvas,
258 : timestamp: Date.now()
259 : }
260 : }
261 :
262 : /** A newly attached node stacks above those already attached. */
263 23236 : export function attachNodeLayout(graph: LayoutGraph, node: LGraphNode): void {
264 23236 : if (nodeAttachments.has(node)) detachNodeLayout(node)
265 :
266 23236 : const graphId = graph.rootGraph.id
267 23236 : if (layoutStore.getNodeLayout(graphId, node.id)) {
268 0 : adoptNodeAttachment(graphId, node)
269 0 : return
270 : }
271 :
272 23236 : const position = { x: node._pos[0], y: node._pos[1] }
273 23236 : const size = { width: node._size[0], height: node._size[1] }
274 23236 : layoutStore.applyOperation({
275 : ...canvasOperationMeta(),
276 : graphId,
277 : layout: {
278 : bounds: { ...position, ...size },
279 : id: node.id,
280 : position,
281 : size,
282 : zIndex: layoutStore.allocateZIndex(),
283 : visible: true
284 : },
285 : nodeId: node.id,
286 : type: 'createNode'
287 : })
288 23236 : adoptNodeAttachment(graphId, node)
289 : }
290 :
291 23236 : function adoptNodeAttachment(graphId: UUID, node: LGraphNode): void {
292 23236 : const projection = nodeGeometryProjection(node)
293 23236 : const geometrySynchronized = layoutStore.readNodeRect(
294 : graphId,
295 : node.id,
296 : projection.buffer
297 : )
298 23236 : nodeAttachments.set(node, { graphId, id: node.id })
299 23236 : setNodeAttachmentOwner(graphId, node)
300 23236 : projection.layoutRef = layoutStore.getNodeLayoutRef(graphId, node.id)
301 23236 : if (geometrySynchronized) {
302 23236 : projection.geometryVersion = layoutStore.nodeGeometryVersion
303 : }
304 : }
305 :
306 76 : function transferableNodeAttachment(
307 : node: LGraphNode,
308 : replacement: LGraphNode
309 : ): NodeLayoutAttachment | undefined {
310 76 : const attachment = nodeAttachments.get(node)
311 76 : if (
312 : !attachment ||
313 : nodeAttachments.has(replacement) ||
314 : attachment.id !== replacement.id ||
315 : nodeAttachmentOwner(attachment.graphId, attachment.id) !== node ||
316 : !layoutStore.getNodeLayout(attachment.graphId, attachment.id)
317 : )
318 0 : return
319 76 : return attachment
320 : }
321 :
322 57 : export function canTransferLayoutAttachment(
323 : node: LGraphNode,
324 : replacement: LGraphNode
325 : ): boolean {
326 57 : return transferableNodeAttachment(node, replacement) !== undefined
327 : }
328 :
329 19 : export function transferLayoutAttachment(
330 : node: LGraphNode,
331 : replacement: LGraphNode
332 : ): boolean {
333 19 : const attachment = transferableNodeAttachment(node, replacement)
334 19 : if (!attachment) return false
335 :
336 19 : const replacementProjection = nodeGeometryProjection(replacement)
337 19 : const geometrySynchronized = layoutStore.readNodeRect(
338 : attachment.graphId,
339 : attachment.id,
340 : replacementProjection.buffer
341 : )
342 :
343 19 : nodeAttachments.delete(node)
344 19 : nodeGeometryProjection(node).layoutRef = undefined
345 19 : nodeAttachments.set(replacement, attachment)
346 19 : setNodeAttachmentOwner(attachment.graphId, replacement)
347 19 : replacementProjection.layoutRef = layoutStore.getNodeLayoutRef(
348 : attachment.graphId,
349 : attachment.id
350 : )
351 19 : if (geometrySynchronized) {
352 19 : replacementProjection.geometryVersion = layoutStore.nodeGeometryVersion
353 : }
354 19 : return true
355 : }
356 :
357 212 : function takeNodeLayoutAttachment(
358 : node: LGraphNode
359 : ): NodeLayoutAttachment | undefined {
360 212 : const attachment = nodeAttachments.get(node)
361 212 : if (!attachment) return
362 212 : const { graphId, id: nodeId } = attachment
363 :
364 212 : const projection = nodeGeometryProjection(node)
365 212 : layoutStore.readNodeRect(graphId, nodeId, projection.buffer)
366 212 : projection.layoutRef = undefined
367 212 : nodeAttachments.delete(node)
368 212 : return deleteNodeAttachmentOwner(graphId, node)
369 : ? { graphId, id: nodeId }
370 : : undefined
371 : }
372 :
373 212 : export function detachNodeLayout(node: LGraphNode): void {
374 212 : const attachment = takeNodeLayoutAttachment(node)
375 212 : if (!attachment) return
376 212 : layoutStore.applyOperation({
377 : ...canvasOperationMeta(),
378 : graphId: attachment.graphId,
379 : nodeId: attachment.id,
380 : type: 'deleteNode'
381 : })
382 : }
383 :
384 154 : export function attachGroupLayout(
385 : graph: LayoutGraph,
386 : group: LGraphGroup
387 : ): void {
388 154 : const graphId = graph.rootGraph.id
389 154 : if (layoutStore.getGroupLayout(graphId, group.id)) {
390 0 : group.syncBoundsFromStore()
391 0 : groupAttachments.set(group, { graphId, id: group.id })
392 0 : return
393 : }
394 :
395 154 : layoutStore.applyOperation({
396 : ...canvasOperationMeta(),
397 : graphId,
398 : groupId: group.id,
399 : layout: {
400 : id: group.id,
401 : position: { x: group.pos[0], y: group.pos[1] },
402 : size: { width: group.size[0], height: group.size[1] }
403 : },
404 : type: 'createGroup'
405 : })
406 154 : groupAttachments.set(group, { graphId, id: group.id })
407 : }
408 :
409 6 : export function detachGroupLayout(group: LGraphGroup): void {
410 6 : const attachment = groupAttachments.get(group)
411 6 : if (!attachment) return
412 6 : group.syncBoundsFromStore()
413 6 : groupAttachments.delete(group)
414 6 : layoutStore.applyOperation({
415 : ...canvasOperationMeta(),
416 : graphId: attachment.graphId,
417 : groupId: attachment.id,
418 : type: 'deleteGroup'
419 : })
420 : }
421 :
422 : /** Attaches a reroute, adopting an existing store entry when one exists. */
423 41 : export function materializeRerouteLayout(
424 : graph: LayoutGraph,
425 : reroute: Reroute
426 : ): void {
427 41 : const graphId = graph.rootGraph.id
428 41 : if (layoutStore.getRerouteLayout(graphId, reroute.id)) {
429 0 : rerouteAttachments.set(reroute, { graphId, id: reroute.id })
430 0 : return
431 : }
432 :
433 41 : layoutStore.applyOperation({
434 : ...canvasOperationMeta(),
435 : graphId,
436 : position: { x: reroute.pos[0], y: reroute.pos[1] },
437 : rerouteId: reroute.id,
438 : type: 'createReroute'
439 : })
440 41 : rerouteAttachments.set(reroute, { graphId, id: reroute.id })
441 : }
442 :
443 0 : export function detachRerouteLayout(reroute: Reroute): void {
444 0 : const attachment = rerouteAttachments.get(reroute)
445 0 : if (!attachment) return
446 0 : reroute.syncPosition()
447 0 : rerouteAttachments.delete(reroute)
448 0 : layoutStore.applyOperation({
449 : ...canvasOperationMeta(),
450 : graphId: attachment.graphId,
451 : rerouteId: attachment.id,
452 : type: 'deleteReroute'
453 : })
454 : }
455 :
456 46553 : function moveNodeLayout(node: LGraphNode, position: Point): void {
457 46553 : const attachment = nodeAttachments.get(node)
458 46553 : if (!attachment) return
459 46553 : layoutStore.applyOperation({
460 : ...canvasOperationMeta(),
461 : graphId: attachment.graphId,
462 : nodeId: attachment.id,
463 : position,
464 : type: 'moveNode'
465 : })
466 : }
467 :
468 36815 : export function resizeNodeLayout(
469 : node: LGraphNode,
470 : size: Size,
471 : {
472 : position,
473 : source = LayoutSource.Canvas
474 : }: { position?: Point; source?: LayoutSource } = {}
475 : ): void {
476 36815 : const attachment = nodeAttachments.get(node)
477 36815 : if (!attachment) return
478 36815 : if (position) {
479 30 : layoutStore.batchUpdateNodeBounds(
480 : attachment.graphId,
481 : [
482 : {
483 : nodeId: attachment.id,
484 : bounds: { ...position, ...size }
485 : }
486 : ],
487 : { source }
488 : )
489 30 : return
490 : }
491 36785 : layoutStore.applyOperation({
492 : graphId: attachment.graphId,
493 : nodeId: attachment.id,
494 : size,
495 : source,
496 : timestamp: Date.now(),
497 : type: 'resizeNode'
498 : })
499 : }
500 :
501 105 : export function setGroupBoundsLayout(
502 : group: LGraphGroup,
503 : position: Point,
504 : size: Size
505 : ): void {
506 105 : const attachment = groupAttachments.get(group)
507 105 : if (!attachment) return
508 105 : layoutStore.applyOperation({
509 : ...canvasOperationMeta(),
510 : graphId: attachment.graphId,
511 : groupId: attachment.id,
512 : position,
513 : size,
514 : type: 'setGroupBounds'
515 : })
516 : }
517 :
518 5 : export function moveRerouteLayout(reroute: Reroute, position: Point): void {
519 5 : const attachment = rerouteAttachments.get(reroute)
520 5 : if (!attachment) return
521 5 : layoutStore.applyOperation({
522 : ...canvasOperationMeta(),
523 : graphId: attachment.graphId,
524 : position,
525 : rerouteId: attachment.id,
526 : type: 'moveReroute'
527 : })
528 : }
529 :
530 : /**
531 : * Invalidates every attachment a graph owns, including those inside its
532 : * subgraph definitions. Layout entries are removed in one store transaction
533 : * unless their graph bucket will be cleared separately.
534 : */
535 11737 : export function detachGraphLayouts(
536 : graphs: readonly GraphLayoutOwner[],
537 : { removeLayouts = true }: { removeLayouts?: boolean } = {}
538 : ): void {
539 11737 : const meta = removeLayouts ? canvasOperationMeta() : undefined
540 11737 : const operations: LayoutOperation[] = []
541 11737 : const visited = new Set<GraphLayoutOwner>()
542 :
543 12151 : function collect(owner: GraphLayoutOwner): void {
544 12151 : if (visited.has(owner)) return
545 12151 : visited.add(owner)
546 12151 : for (const node of owner._nodes) {
547 10767 : const attachment = nodeAttachments.get(node)
548 10767 : if (!attachment) continue
549 10767 : layoutStore.readNodeRect(
550 : attachment.graphId,
551 : attachment.id,
552 : nodeGeometryProjection(node).buffer
553 : )
554 10767 : nodeAttachments.delete(node)
555 10767 : const owned = deleteNodeAttachmentOwner(attachment.graphId, node)
556 10767 : if (meta && owned) {
557 36 : operations.push({
558 : ...meta,
559 : graphId: attachment.graphId,
560 : nodeId: attachment.id,
561 : type: 'deleteNode'
562 : })
563 : }
564 : }
565 12151 : for (const group of owner._groups) {
566 49 : const attachment = groupAttachments.get(group)
567 49 : if (!attachment) continue
568 49 : group.syncBoundsFromStore()
569 49 : groupAttachments.delete(group)
570 49 : if (meta) {
571 0 : operations.push({
572 : ...meta,
573 : graphId: attachment.graphId,
574 : groupId: attachment.id,
575 : type: 'deleteGroup'
576 : })
577 : }
578 : }
579 12151 : for (const reroute of owner.reroutes.values()) {
580 16 : const attachment = rerouteAttachments.get(reroute)
581 16 : if (!attachment) continue
582 16 : reroute.syncPosition()
583 16 : rerouteAttachments.delete(reroute)
584 16 : if (meta) {
585 0 : operations.push({
586 : ...meta,
587 : graphId: attachment.graphId,
588 : rerouteId: attachment.id,
589 : type: 'deleteReroute'
590 : })
591 : }
592 : }
593 12151 : for (const subgraph of owner._subgraphs.values()) collect(subgraph)
594 : }
595 :
596 11737 : for (const graph of graphs) collect(graph)
597 11737 : if (removeLayouts) layoutStore.applyOperations(operations)
598 : }
|