Line data Source code
1 : import type {
2 : Size2D as Viewport,
3 : Bounds as SpotlightRect
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 : import type { Placement } from '@floating-ui/vue'
6 : export type { Size2D as Viewport } from '@comfyorg/shared-frontend-utils/geometry'
7 :
8 : export interface BoxStyle {
9 : left: string
10 : top: string
11 : width: string
12 : height: string
13 : }
14 :
15 2355 : const SPOTLIGHT_EDGE_INSET = 2
16 :
17 : export const CARD_WIDTH = 300
18 : export const VIEWPORT_MARGIN = 12
19 : export const CARD_GAP = 16
20 : /** Wide enough for the cursor glyph to sit centred between card and target. */
21 : export const CURSOR_GAP = 40
22 : /** The card's travel to a new target; whatever moves that target waits it out. */
23 : export const CARD_GLIDE_MS = 300
24 : // Kept tight so the spotlight glow doesn't spill onto an adjacent clickable control.
25 : export const SPOTLIGHT_PAD = 4
26 : export type { Bounds as SpotlightRect } from '@comfyorg/shared-frontend-utils/geometry'
27 :
28 0 : export function unionRect(a: DOMRectReadOnly, b: DOMRectReadOnly): DOMRect {
29 0 : const left = Math.min(a.left, b.left)
30 0 : const top = Math.min(a.top, b.top)
31 0 : return new DOMRect(
32 : left,
33 : top,
34 : Math.max(a.right, b.right) - left,
35 : Math.max(a.bottom, b.bottom) - top
36 : )
37 : }
38 :
39 17 : export function clampSpotlightRect(
40 : r: DOMRect,
41 : pad: number,
42 : viewport: Viewport
43 : ): SpotlightRect {
44 17 : const left = Math.max(SPOTLIGHT_EDGE_INSET, r.left - pad)
45 17 : const top = Math.max(SPOTLIGHT_EDGE_INSET, r.top - pad)
46 17 : const right = Math.min(viewport.width - SPOTLIGHT_EDGE_INSET, r.right + pad)
47 17 : const bottom = Math.min(
48 : viewport.height - SPOTLIGHT_EDGE_INSET,
49 : r.bottom + pad
50 : )
51 17 : return {
52 : x: left,
53 : y: top,
54 : width: Math.max(0, right - left),
55 : height: Math.max(0, bottom - top)
56 : }
57 : }
58 :
59 0 : export function clampSpotlight(
60 : r: DOMRect,
61 : pad: number,
62 : viewport: Viewport
63 : ): BoxStyle {
64 0 : const { x, y, width, height } = clampSpotlightRect(r, pad, viewport)
65 0 : return {
66 : left: `${x}px`,
67 : top: `${y}px`,
68 : width: `${width}px`,
69 : height: `${height}px`
70 : }
71 : }
72 :
73 : /** Under `fill-rule="evenodd"`, the hole subpath is what lets input through. */
74 0 : export function hitRegionPath(
75 : viewport: Viewport,
76 : hole: SpotlightRect | null
77 : ): string {
78 0 : const outer = `M0 0H${viewport.width}V${viewport.height}H0Z`
79 0 : if (!hole) return outer
80 0 : const { x, y, width, height } = hole
81 0 : return `${outer}M${x} ${y}h${width}v${height}h${-width}Z`
82 : }
83 :
84 0 : export function noTargetCardLeft(viewportWidth: number): number {
85 0 : return Math.max(VIEWPORT_MARGIN, (viewportWidth - CARD_WIDTH) / 2)
86 : }
87 :
88 : /** Floats the cursor in the gap on the card edge facing the target. */
89 2355 : const CURSOR_EDGE_CLASS = {
90 : top: '-top-7 left-1/2 -translate-x-1/2 rotate-45',
91 : bottom: '-bottom-7 left-1/2 -translate-x-1/2 -rotate-[135deg]',
92 : left: '-left-7 top-1/2 -translate-y-1/2 -rotate-45',
93 : right: '-right-7 top-1/2 -translate-y-1/2 rotate-[135deg]'
94 : } as const
95 :
96 2355 : const TARGET_FACING_EDGE = {
97 : top: 'bottom',
98 : bottom: 'top',
99 : left: 'right',
100 : right: 'left'
101 : } as const
102 :
103 : type Side = keyof typeof TARGET_FACING_EDGE
104 :
105 0 : export function cursorEdgeClass(placement: Placement): string {
106 : const side = placement.split('-')[0] as Side
107 : return CURSOR_EDGE_CLASS[TARGET_FACING_EDGE[side]]
108 : }
109 :
110 2355 : const TOP_BAR_HEIGHT_VAR = '--comfy-topbar-height'
111 :
112 : /** The top bar's height, read from the theme token, plus the standard gap. */
113 2432 : export function topSafeInset(): number {
114 2432 : const root = document.documentElement
115 2432 : const raw = getComputedStyle(root).getPropertyValue(TOP_BAR_HEIGHT_VAR).trim()
116 2432 : const px = raw.endsWith('rem')
117 : ? parseFloat(raw) * parseFloat(getComputedStyle(root).fontSize)
118 : : parseFloat(raw)
119 2432 : return (Number.isFinite(px) ? px : 0) + CARD_GAP
120 : }
|