|
| 1 | +import type { InjectionKey } from 'vue' |
| 2 | + |
| 3 | +import type { Point } from '@/renderer/core/layout/types' |
| 4 | + |
| 5 | +/** |
| 6 | + * Lightweight, injectable transform state used by layout-aware components. |
| 7 | + * |
| 8 | + * Consumers use this interface to convert coordinates between LiteGraph's |
| 9 | + * canvas space and the DOM's screen space, access the current pan/zoom |
| 10 | + * (camera), and perform basic viewport culling checks. |
| 11 | + * |
| 12 | + * Coordinate mapping: |
| 13 | + * - screen = (canvas + offset) * scale |
| 14 | + * - canvas = screen / scale - offset |
| 15 | + * |
| 16 | + * The full implementation and additional helpers live in |
| 17 | + * `useTransformState()`. This interface deliberately exposes only the |
| 18 | + * minimal surface needed outside that composable. |
| 19 | + * |
| 20 | + * @example |
| 21 | + * const state = inject(TransformStateKey)! |
| 22 | + * const screen = state.canvasToScreen({ x: 100, y: 50 }) |
| 23 | + */ |
| 24 | +interface TransformState { |
| 25 | + /** Convert a screen-space point (CSS pixels) to canvas space. */ |
| 26 | + screenToCanvas: (p: Point) => Point |
| 27 | + /** Convert a canvas-space point to screen space (CSS pixels). */ |
| 28 | + canvasToScreen: (p: Point) => Point |
| 29 | + /** Current pan/zoom; `x`/`y` are offsets, `z` is scale. */ |
| 30 | + camera?: { x: number; y: number; z: number } |
| 31 | + /** |
| 32 | + * Test whether a node's rectangle intersects the (expanded) viewport. |
| 33 | + * Handy for viewport culling and lazy work. |
| 34 | + * |
| 35 | + * @param nodePos Top-left in canvas space `[x, y]` |
| 36 | + * @param nodeSize Size in canvas units `[width, height]` |
| 37 | + * @param viewport Screen-space viewport `{ width, height }` |
| 38 | + * @param margin Optional fractional margin (e.g. `0.2` = 20%) |
| 39 | + */ |
| 40 | + isNodeInViewport?: ( |
| 41 | + nodePos: ArrayLike<number>, |
| 42 | + nodeSize: ArrayLike<number>, |
| 43 | + viewport: { width: number; height: number }, |
| 44 | + margin?: number |
| 45 | + ) => boolean |
| 46 | +} |
| 47 | + |
| 48 | +export const TransformStateKey: InjectionKey<TransformState> = |
| 49 | + Symbol('transformState') |
0 commit comments