Line data Source code
1 : import { useRafFn } from '@vueuse/core'
2 : import { toValue, watch } from 'vue'
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
6 : import { useTransformState } from '@/renderer/core/layout/transform/useTransformState'
7 :
8 : /**
9 : * Invokes `onGesture` when a canvas gesture begins while `active` is true.
10 : *
11 : * Watches the canvas at the semantic layer instead of raw pointer events, so
12 : * every input device is covered: panning and zooming are observed through the
13 : * shared camera transform (mouse drag, trackpad, wheel — anything that moves
14 : * the viewport), and box selection through the canvas selection rectangle.
15 : * Intended for dismissing popups that anchor to a node but are teleported to
16 : * `document.body`, which would otherwise be left stranded as the node moves.
17 : */
18 197 : export function useDismissOnCanvasGesture(
19 : active: MaybeRefOrGetter<boolean>,
20 : onGesture: () => void
21 : ) {
22 197 : const { camera } = useTransformState()
23 197 : const canvasStore = useCanvasStore()
24 :
25 197 : watch(
26 : () => [camera.x, camera.y, camera.z],
27 : () => {
28 64 : if (toValue(active)) onGesture()
29 : }
30 : )
31 :
32 : /**
33 : * `dragging_rectangle` lives on the raw LGraphCanvas instance and is not
34 : * reactive, so it is polled per frame — same approach as
35 : * SelectionRectangle.vue. Runs only while `active`.
36 : */
37 197 : const { pause, resume } = useRafFn(
38 : () => {
39 48 : if (canvasStore.canvas?.dragging_rectangle) onGesture()
40 : },
41 : { immediate: false }
42 : )
43 :
44 197 : watch(
45 : () => toValue(active),
46 : (isActive) => (isActive ? resume() : pause()),
47 : {
48 : immediate: true
49 : }
50 : )
51 : }
|