Line data Source code
1 : <template>
2 2770 : <div
3 : v-show="isVisible"
4 : data-testid="selection-rectangle"
5 : class="pointer-events-none absolute z-1 border border-blue-400 bg-blue-500/20"
6 : :style="rectangleStyle"
7 : />
8 : </template>
9 :
10 : <script setup lang="ts">
11 : import type { CompactRect } from '@comfyorg/shared-frontend-utils/geometry'
12 :
13 : import { computed, ref, watch } from 'vue'
14 :
15 : import { onCanvasFrame } from '@/lib/litegraph/src/canvas/canvasFrame'
16 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
17 :
18 2355 : const canvasStore = useCanvasStore()
19 :
20 2355 : const selectionRect = ref<CompactRect | null>(null)
21 :
22 2355 : watch(
23 : () => canvasStore.canvas,
24 : (canvas, _, onCleanup) => {
25 2355 : if (!canvas) {
26 0 : selectionRect.value = null
27 0 : return
28 : }
29 :
30 2355 : onCleanup(
31 : onCanvasFrame(canvas, 'update', () => {
32 122061 : const { pointer, dragging_rectangle } = canvas
33 :
34 122061 : if (dragging_rectangle && pointer.eDown && pointer.eMove) {
35 398 : const x = pointer.eDown.safeOffsetX
36 398 : const y = pointer.eDown.safeOffsetY
37 398 : const w = pointer.eMove.safeOffsetX - x
38 398 : const h = pointer.eMove.safeOffsetY - y
39 :
40 398 : selectionRect.value = { x, y, w, h }
41 : } else {
42 121663 : selectionRect.value = null
43 : }
44 : })
45 : )
46 : },
47 : { immediate: true }
48 : )
49 :
50 2355 : const isVisible = computed(() => selectionRect.value !== null)
51 :
52 2355 : const rectangleStyle = computed(() => {
53 2770 : const rect = selectionRect.value
54 2770 : if (!rect) return {}
55 :
56 2770 : const left = rect.w >= 0 ? rect.x : rect.x + rect.w
57 2770 : const top = rect.h >= 0 ? rect.y : rect.y + rect.h
58 2770 : const width = Math.abs(rect.w)
59 2770 : const height = Math.abs(rect.h)
60 :
61 2770 : return {
62 : left: `${left}px`,
63 : top: `${top}px`,
64 : width: `${width}px`,
65 : height: `${height}px`
66 : }
67 : })
68 : </script>
|