Skip to content

Commit 59ce169

Browse files
authored
Add selection changed state watcher (#3899)
1 parent 4294b2c commit 59ce169

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/components/graph/SelectionOverlay.vue

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
<script setup lang="ts">
1616
import { createBounds } from '@comfyorg/litegraph'
17-
import type { LGraphCanvas } from '@comfyorg/litegraph'
1817
import { whenever } from '@vueuse/core'
1918
import { ref, watch } from 'vue'
2019
2120
import { useAbsolutePosition } from '@/composables/element/useAbsolutePosition'
22-
import { useChainCallback } from '@/composables/functional/useChainCallback'
2321
import { useCanvasStore } from '@/stores/graphStore'
2422
2523
const canvasStore = useCanvasStore()
@@ -28,8 +26,8 @@ const { style, updatePosition } = useAbsolutePosition()
2826
const visible = ref(false)
2927
const showBorder = ref(false)
3028
31-
const positionSelectionOverlay = (canvas: LGraphCanvas) => {
32-
const selectedItems = canvas.selectedItems
29+
const positionSelectionOverlay = () => {
30+
const { selectedItems } = canvasStore.getCanvas()
3331
showBorder.value = selectedItems.size > 1
3432
3533
if (!selectedItems.size) {
@@ -48,26 +46,20 @@ const positionSelectionOverlay = (canvas: LGraphCanvas) => {
4846
}
4947
5048
// Register listener on canvas creation.
51-
watch(
52-
() => canvasStore.canvas as LGraphCanvas | null,
53-
(canvas: LGraphCanvas | null) => {
54-
if (!canvas) return
55-
56-
canvas.onSelectionChange = useChainCallback(
57-
canvas.onSelectionChange,
58-
// Wait for next frame as sometimes the selected items haven't been
59-
// rendered yet, so the boundingRect is not available on them.
60-
() => requestAnimationFrame(() => positionSelectionOverlay(canvas))
61-
)
49+
whenever(
50+
() => canvasStore.getCanvas().state.selectionChanged,
51+
() => {
52+
requestAnimationFrame(() => {
53+
positionSelectionOverlay()
54+
canvasStore.getCanvas().state.selectionChanged = false
55+
})
6256
},
6357
{ immediate: true }
6458
)
6559
66-
whenever(
67-
() => canvasStore.getCanvas().ds.state,
68-
() => positionSelectionOverlay(canvasStore.getCanvas()),
69-
{ deep: true }
70-
)
60+
whenever(() => canvasStore.getCanvas().ds.state, positionSelectionOverlay, {
61+
deep: true
62+
})
7163
7264
watch(
7365
() => canvasStore.canvas?.state?.draggingItems,
@@ -77,10 +69,10 @@ watch(
7769
// the correct position.
7870
// https://github.com/Comfy-Org/ComfyUI_frontend/issues/2656
7971
if (draggingItems === false) {
80-
setTimeout(() => {
72+
requestAnimationFrame(() => {
8173
visible.value = true
82-
positionSelectionOverlay(canvasStore.getCanvas())
83-
}, 100)
74+
positionSelectionOverlay()
75+
})
8476
} else {
8577
// Selection change update to visible state is delayed by a frame. Here
8678
// we also delay a frame so that the order of events is correct when

0 commit comments

Comments
 (0)