Skip to content

Commit 720de8c

Browse files
authored
Add UI code for configuring subgraphNode widgets (#5826)
The third PR for managing display of widgets on subgraph nodes. This is the one that actually makes the functionality usable and user visible. Adds - A right-side modal for configuring which widgets are promoted, accessed by right click or selection toolbar - This menu allows for re-arranging widget order by dragging and dropping. - Indicators inside the subgraph for which widgets have been promoted. - Context menu options for promoting or demoting widget inside of a subgraph. <img width="767" height="694" alt="image" src="https://github.com/user-attachments/assets/4f78645d-7b26-48ba-8c49-78f4807e89e8" /> <img width="784" height="435" alt="image" src="https://github.com/user-attachments/assets/7005c730-a732-481e-befb-57019a8a31a7" /> Known issues - Some preview widgets are not added to a node until a draw operation occurs. The code does not yet have a way of determining which nodes should have draw operations forced to facilitate initial widget creation. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5826-Add-UI-code-for-configuring-subgraphNode-widgets-27c6d73d36508146accbf395e5bcd36a) by [Unito](https://www.unito.io)
1 parent 4833547 commit 720de8c

File tree

32 files changed

+794
-153
lines changed

32 files changed

+794
-153
lines changed
-310 Bytes
Loading
-205 Bytes
Loading
-310 Bytes
Loading

src/components/graph/SelectionToolbox.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
<ColorPickerButton v-if="showColorPicker" />
2323
<FrameNodes v-if="showFrameNodes" />
2424
<ConvertToSubgraphButton v-if="showConvertToSubgraph" />
25-
<PublishSubgraphButton v-if="showPublishSubgraph" />
25+
<ConfigureSubgraph v-if="showSubgraphButtons" />
26+
<PublishSubgraphButton v-if="showSubgraphButtons" />
2627
<MaskEditorButton v-if="showMaskEditor" />
2728
<VerticalDivider
2829
v-if="showAnyPrimaryActions && showAnyControlActions"
@@ -50,6 +51,7 @@ import { computed, ref } from 'vue'
5051
5152
import BypassButton from '@/components/graph/selectionToolbox/BypassButton.vue'
5253
import ColorPickerButton from '@/components/graph/selectionToolbox/ColorPickerButton.vue'
54+
import ConfigureSubgraph from '@/components/graph/selectionToolbox/ConfigureSubgraph.vue'
5355
import ConvertToSubgraphButton from '@/components/graph/selectionToolbox/ConvertToSubgraphButton.vue'
5456
import DeleteButton from '@/components/graph/selectionToolbox/DeleteButton.vue'
5557
import ExecuteButton from '@/components/graph/selectionToolbox/ExecuteButton.vue'
@@ -112,7 +114,7 @@ const showInfoButton = computed(() => !!nodeDef.value)
112114
const showColorPicker = computed(() => hasAnySelection.value)
113115
const showConvertToSubgraph = computed(() => hasAnySelection.value)
114116
const showFrameNodes = computed(() => hasMultipleSelection.value)
115-
const showPublishSubgraph = computed(() => isSingleSubgraph.value)
117+
const showSubgraphButtons = computed(() => isSingleSubgraph.value)
116118
117119
const showBypass = computed(
118120
() =>
@@ -130,7 +132,7 @@ const showAnyPrimaryActions = computed(
130132
showColorPicker.value ||
131133
showConvertToSubgraph.value ||
132134
showFrameNodes.value ||
133-
showPublishSubgraph.value
135+
showSubgraphButtons.value
134136
)
135137
136138
const showAnyControlActions = computed(() => showBypass.value)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<Button
3+
v-tooltip.top="{
4+
value: $t('Edit Subgraph Widgets'),
5+
showDelay: 1000
6+
}"
7+
severity="secondary"
8+
text
9+
icon="icon-[lucide--settings-2]"
10+
@click="showSubgraphNodeDialog"
11+
/>
12+
</template>
13+
<script setup lang="ts">
14+
import Button from 'primevue/button'
15+
16+
import { showSubgraphNodeDialog } from '@/core/graph/subgraph/useSubgraphNodeDialog'
17+
</script>

src/components/graph/widgets/DomWidget.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const updateDomClipping = () => {
6868
return
6969
}
7070
71-
const isSelected = selectedNode === widget.node
71+
const isSelected = selectedNode === widgetState.widget.node
7272
const renderArea = selectedNode?.renderArea
7373
const offset = lgCanvas.ds.offset
7474
const scale = lgCanvas.ds.scale

src/composables/useCoreCommands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DEFAULT_DARK_COLOR_PALETTE,
66
DEFAULT_LIGHT_COLOR_PALETTE
77
} from '@/constants/coreColorPalettes'
8+
import { promoteRecommendedWidgets } from '@/core/graph/subgraph/proxyWidgetUtils'
89
import { t } from '@/i18n'
910
import {
1011
LGraphEventMode,
@@ -909,6 +910,7 @@ export function useCoreCommands(): ComfyCommand[] {
909910

910911
const { node } = res
911912
canvas.select(node)
913+
promoteRecommendedWidgets(node)
912914
canvasStore.updateSelectedItems()
913915
}
914916
},
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
<script setup lang="ts">
2+
import { refDebounced, watchDebounced } from '@vueuse/core'
3+
import {
4+
computed,
5+
customRef,
6+
onBeforeUnmount,
7+
onMounted,
8+
ref,
9+
triggerRef
10+
} from 'vue'
11+
12+
import SearchBox from '@/components/common/SearchBox.vue'
13+
import SubgraphNodeWidget from '@/core/graph/subgraph/SubgraphNodeWidget.vue'
14+
import {
15+
type WidgetItem,
16+
demoteWidget,
17+
isRecommendedWidget,
18+
matchesPropertyItem,
19+
matchesWidgetItem,
20+
promoteWidget,
21+
widgetItemToProperty
22+
} from '@/core/graph/subgraph/proxyWidgetUtils'
23+
import {
24+
type ProxyWidgetsProperty,
25+
parseProxyWidgets
26+
} from '@/core/schemas/proxyWidget'
27+
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
28+
import { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
29+
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
30+
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
31+
import { DraggableList } from '@/scripts/ui/draggableList'
32+
import { useLitegraphService } from '@/services/litegraphService'
33+
import { useDialogStore } from '@/stores/dialogStore'
34+
35+
const canvasStore = useCanvasStore()
36+
37+
const draggableList = ref<DraggableList | undefined>(undefined)
38+
const draggableItems = ref()
39+
const searchQuery = ref<string>('')
40+
const debouncedQuery = refDebounced(searchQuery, 200)
41+
const proxyWidgets = customRef<ProxyWidgetsProperty>((track, trigger) => ({
42+
get() {
43+
track()
44+
const node = activeNode.value
45+
if (!node) return []
46+
return parseProxyWidgets(node.properties.proxyWidgets)
47+
},
48+
set(value?: ProxyWidgetsProperty) {
49+
trigger()
50+
const node = activeNode.value
51+
if (!value) return
52+
if (!node) {
53+
console.error('Attempted to toggle widgets with no node selected')
54+
return
55+
}
56+
node.properties.proxyWidgets = value
57+
}
58+
}))
59+
60+
const activeNode = computed(() => {
61+
const node = canvasStore.selectedItems[0]
62+
if (node instanceof SubgraphNode) return node
63+
useDialogStore().closeDialog()
64+
return undefined
65+
})
66+
67+
const activeWidgets = computed<WidgetItem[]>({
68+
get() {
69+
const node = activeNode.value
70+
if (!node) return []
71+
return proxyWidgets.value.flatMap(([id, name]: [string, string]) => {
72+
const wNode = node.subgraph._nodes_by_id[id]
73+
if (!wNode?.widgets) return []
74+
const w = wNode.widgets.find((w) => w.name === name)
75+
if (!w) return []
76+
return [[wNode, w]]
77+
})
78+
},
79+
set(value: WidgetItem[]) {
80+
const node = activeNode.value
81+
if (!node) {
82+
console.error('Attempted to toggle widgets with no node selected')
83+
return
84+
}
85+
//map back to id/name
86+
const widgets: ProxyWidgetsProperty = value.map(widgetItemToProperty)
87+
proxyWidgets.value = widgets
88+
}
89+
})
90+
91+
const interiorWidgets = computed<WidgetItem[]>(() => {
92+
const node = activeNode.value
93+
if (!node) return []
94+
const { updatePreviews } = useLitegraphService()
95+
const interiorNodes = node.subgraph.nodes
96+
for (const node of interiorNodes) {
97+
node.updateComputedDisabled()
98+
updatePreviews(node)
99+
}
100+
return interiorNodes
101+
.flatMap(nodeWidgets)
102+
.filter(([_, w]: WidgetItem) => !w.computedDisabled)
103+
})
104+
105+
const candidateWidgets = computed<WidgetItem[]>(() => {
106+
const node = activeNode.value
107+
if (!node) return []
108+
const widgets = proxyWidgets.value
109+
return interiorWidgets.value.filter(
110+
(widgetItem: WidgetItem) => !widgets.some(matchesPropertyItem(widgetItem))
111+
)
112+
})
113+
const filteredCandidates = computed<WidgetItem[]>(() => {
114+
const query = debouncedQuery.value.toLowerCase()
115+
if (!query) return candidateWidgets.value
116+
return candidateWidgets.value.filter(
117+
([n, w]: WidgetItem) =>
118+
n.title.toLowerCase().includes(query) ||
119+
w.name.toLowerCase().includes(query)
120+
)
121+
})
122+
123+
const recommendedWidgets = computed(() => {
124+
const node = activeNode.value
125+
if (!node) return [] //Not reachable
126+
return filteredCandidates.value.filter(isRecommendedWidget)
127+
})
128+
129+
const filteredActive = computed<WidgetItem[]>(() => {
130+
const query = debouncedQuery.value.toLowerCase()
131+
if (!query) return activeWidgets.value
132+
return activeWidgets.value.filter(
133+
([n, w]: WidgetItem) =>
134+
n.title.toLowerCase().includes(query) ||
135+
w.name.toLowerCase().includes(query)
136+
)
137+
})
138+
139+
function toKey(item: WidgetItem) {
140+
return `${item[0].id}: ${item[1].name}`
141+
}
142+
function nodeWidgets(n: LGraphNode): WidgetItem[] {
143+
if (!n.widgets) return []
144+
return n.widgets.map((w: IBaseWidget) => [n, w])
145+
}
146+
function demote([node, widget]: WidgetItem) {
147+
const subgraphNode = activeNode.value
148+
if (!subgraphNode) return []
149+
demoteWidget(node, widget, [subgraphNode])
150+
triggerRef(proxyWidgets)
151+
}
152+
function promote([node, widget]: WidgetItem) {
153+
const subgraphNode = activeNode.value
154+
if (!subgraphNode) return []
155+
promoteWidget(node, widget, [subgraphNode])
156+
triggerRef(proxyWidgets)
157+
}
158+
function showAll() {
159+
const node = activeNode.value
160+
if (!node) return //Not reachable
161+
const widgets = proxyWidgets.value
162+
const toAdd: ProxyWidgetsProperty =
163+
filteredCandidates.value.map(widgetItemToProperty)
164+
widgets.push(...toAdd)
165+
proxyWidgets.value = widgets
166+
}
167+
function hideAll() {
168+
const node = activeNode.value
169+
if (!node) return //Not reachable
170+
//Not great from a nesting perspective, but path is cold
171+
//and it cleans up potential error states
172+
proxyWidgets.value = proxyWidgets.value.filter(
173+
(widgetItem) => !filteredActive.value.some(matchesWidgetItem(widgetItem))
174+
)
175+
}
176+
function showRecommended() {
177+
const node = activeNode.value
178+
if (!node) return //Not reachable
179+
const widgets = proxyWidgets.value
180+
const toAdd: ProxyWidgetsProperty =
181+
recommendedWidgets.value.map(widgetItemToProperty)
182+
//TODO: Add sort step here
183+
//Input should always be before output by default
184+
widgets.push(...toAdd)
185+
proxyWidgets.value = widgets
186+
}
187+
188+
function setDraggableState() {
189+
draggableList.value?.dispose()
190+
if (debouncedQuery.value || !draggableItems.value?.children?.length) return
191+
draggableList.value = new DraggableList(
192+
draggableItems.value,
193+
'.draggable-item'
194+
)
195+
//Original implementation plays really poorly with vue,
196+
//It has been modified to not add/remove elements
197+
draggableList.value.applyNewItemsOrder = function () {
198+
const reorderedItems = []
199+
200+
let oldPosition = -1
201+
this.getAllItems().forEach((item, index) => {
202+
if (item === this.draggableItem) {
203+
oldPosition = index
204+
return
205+
}
206+
if (!this.isItemToggled(item)) {
207+
reorderedItems[index] = item
208+
return
209+
}
210+
const newIndex = this.isItemAbove(item) ? index + 1 : index - 1
211+
reorderedItems[newIndex] = item
212+
})
213+
214+
for (let index = 0; index < this.getAllItems().length; index++) {
215+
const item = reorderedItems[index]
216+
if (typeof item === 'undefined') {
217+
reorderedItems[index] = this.draggableItem
218+
}
219+
}
220+
const newPosition = reorderedItems.indexOf(this.draggableItem)
221+
const aw = activeWidgets.value
222+
const [w] = aw.splice(oldPosition, 1)
223+
aw.splice(newPosition, 0, w)
224+
activeWidgets.value = aw
225+
}
226+
}
227+
watchDebounced(
228+
filteredActive,
229+
() => {
230+
setDraggableState()
231+
},
232+
{ debounce: 100 }
233+
)
234+
onMounted(() => {
235+
setDraggableState()
236+
})
237+
onBeforeUnmount(() => {
238+
draggableList.value?.dispose()
239+
})
240+
</script>
241+
<template>
242+
<SearchBox
243+
v-model:model-value="searchQuery"
244+
class="p-2"
245+
:placeholder="$t('g.search') + '...'"
246+
/>
247+
<div
248+
v-if="filteredActive.length"
249+
class="pt-1 pb-4 border-b-1 border-sand-100 dark-theme:border-charcoal-600"
250+
>
251+
<div class="flex py-0 px-4 justify-between">
252+
<div class="text-slate-100 text-[9px] font-semibold uppercase">
253+
{{ $t('subgraphStore.shown') }}
254+
</div>
255+
<a
256+
class="cursor-pointer text-right text-blue-100 text-[11px] font-normal"
257+
@click.stop="hideAll"
258+
>
259+
{{ $t('subgraphStore.hideAll') }}</a
260+
>
261+
</div>
262+
<div ref="draggableItems">
263+
<div
264+
v-for="[node, widget] in filteredActive"
265+
:key="toKey([node, widget])"
266+
class="w-full draggable-item"
267+
style=""
268+
>
269+
<SubgraphNodeWidget
270+
:node-title="node.title"
271+
:widget-name="widget.name"
272+
:is-shown="true"
273+
:is-draggable="!debouncedQuery"
274+
@toggle-visibility="demote([node, widget])"
275+
/>
276+
</div>
277+
</div>
278+
</div>
279+
<div v-if="filteredCandidates.length" class="pt-1 pb-4">
280+
<div class="flex py-0 px-4 justify-between">
281+
<div class="text-slate-100 text-[9px] font-semibold uppercase">
282+
{{ $t('subgraphStore.hidden') }}
283+
</div>
284+
<a
285+
class="cursor-pointer text-right text-blue-100 text-[11px] font-normal"
286+
@click.stop="showAll"
287+
>
288+
{{ $t('subgraphStore.showAll') }}</a
289+
>
290+
</div>
291+
<div
292+
v-for="[node, widget] in filteredCandidates"
293+
:key="toKey([node, widget])"
294+
class="w-full"
295+
>
296+
<SubgraphNodeWidget
297+
:node-title="node.title"
298+
:widget-name="widget.name"
299+
@toggle-visibility="promote([node, widget])"
300+
/>
301+
</div>
302+
</div>
303+
<div
304+
v-if="recommendedWidgets.length"
305+
class="justify-center flex py-4 border-t-1 border-sand-100 dark-theme:border-charcoal-600"
306+
>
307+
<Button
308+
size="small"
309+
class="rounded border-none px-3 py-0.5"
310+
@click.stop="showRecommended"
311+
>
312+
{{ $t('subgraphStore.showRecommended') }}
313+
</Button>
314+
</div>
315+
</template>

0 commit comments

Comments
 (0)