Line data Source code
1 : import { SELECTION_BOUNDS_PADDING } from '@/base/common/selectionBounds'
2 : import type { LGraphCanvas, Rectangle } from '@/lib/litegraph/src/litegraph'
3 : import { createBounds } from '@/lib/litegraph/src/litegraph'
4 : import { app } from '@/scripts/app'
5 : import type { ComfyExtension } from '@/types/comfy'
6 :
7 : /**
8 : * Draws a dashed border around selected items that maintains constant pixel size
9 : * regardless of zoom level, similar to the DOM selection overlay.
10 : */
11 31004 : function drawSelectionBorder(
12 : ctx: CanvasRenderingContext2D,
13 : canvas: LGraphCanvas
14 : ) {
15 31004 : const selectedItems = canvas.selectedItems
16 :
17 : // Only draw if multiple items selected
18 31004 : if (selectedItems.size <= 1) return
19 :
20 : // Use the same bounds calculation as the toolbox
21 543 : const bounds = createBounds(selectedItems, SELECTION_BOUNDS_PADDING)
22 2466 : if (!bounds) return
23 :
24 543 : const [x, y, width, height] = bounds
25 :
26 : // Save context state
27 543 : ctx.save()
28 :
29 : // Set up dashed line style that doesn't scale with zoom
30 : const borderWidth = 2 / canvas.ds.scale // Constant 2px regardless of zoom
31 : ctx.lineWidth = borderWidth
32 31004 : ctx.strokeStyle =
33 : getComputedStyle(document.documentElement)
34 : .getPropertyValue('--border')
35 : .trim() || '#ffffff66'
36 :
37 : // Create dash pattern that maintains visual size
38 31004 : const dashSize = 5 / canvas.ds.scale
39 31004 : ctx.setLineDash([dashSize, dashSize])
40 :
41 : // Draw the border using the bounds directly
42 31004 : ctx.beginPath()
43 31004 : ctx.roundRect(x, y, width, height, 8 / canvas.ds.scale)
44 31004 : ctx.stroke()
45 :
46 : // Restore context
47 31004 : ctx.restore()
48 : }
49 :
50 : /**
51 : * Extension that adds a dashed selection border for multiple selected nodes
52 : */
53 2355 : export const selectionBorderExtension: ComfyExtension = {
54 : name: 'Comfy.SelectionBorder',
55 :
56 : async init() {
57 : // Hook into the canvas drawing
58 2355 : const originalDrawForeground = app.canvas.onDrawForeground
59 :
60 2355 : app.canvas.onDrawForeground = function (
61 : ctx: CanvasRenderingContext2D,
62 : visibleArea: Rectangle
63 : ) {
64 : // Call original if it exists
65 31004 : originalDrawForeground?.call(this, ctx, visibleArea)
66 :
67 : // Draw our selection border
68 31004 : drawSelectionBorder(ctx, app.canvas)
69 : }
70 : }
71 : }
|