Line data Source code
1 : import { computed, ref } from 'vue'
2 : import type { Ref } from 'vue'
3 :
4 : import type { LGraphGroup, LGraphNode } from '@/lib/litegraph/src/litegraph'
5 : import { useNodeOutputsExport } from '@/platform/assets/composables/useNodeOutputsExport'
6 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
7 : import {
8 : shouldHideLinkedCoreMediaInputActions,
9 : shouldHideLinkedCoreMediaInputPreview
10 : } from '@/renderer/extensions/vueNodes/utils/linkedCoreMediaUtils'
11 : import { getExtraOptionsForWidget } from '@/services/litegraphService'
12 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
13 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
14 : import { filterUnavailableCoreMediaMenuActions } from '@/utils/coreMediaMenuActionUtils'
15 : import type { CoreMediaMenuActionKind } from '@/utils/coreMediaMenuActionUtils'
16 : import { isLGraphGroup } from '@/utils/litegraphUtil'
17 :
18 : import {
19 : buildStructuredMenu,
20 : convertContextMenuToOptions
21 : } from './contextMenuConverter'
22 : import { useGroupMenuOptions } from './useGroupMenuOptions'
23 : import { useImageMenuOptions } from './useImageMenuOptions'
24 : import { useNodeMenuOptions } from './useNodeMenuOptions'
25 : import { useSelectionMenuOptions } from './useSelectionMenuOptions'
26 : import type { MenuOption } from './menuOption'
27 : import { useSelectionState } from './useSelectionState'
28 :
29 : // Global singleton for NodeOptions component reference
30 2355 : let nodeOptionsInstance: null | NodeOptionsInstance = null
31 :
32 2355 : const invocationContext = ref<{ nodeId: NodeId; widgetName?: string }>()
33 :
34 : /**
35 : * Toggle the node options popover
36 : * @param event - The trigger event
37 : */
38 29 : export function toggleNodeOptions(event: Event) {
39 29 : invocationContext.value = undefined
40 29 : if (nodeOptionsInstance?.toggle) {
41 29 : nodeOptionsInstance.toggle(event)
42 : }
43 : }
44 :
45 : /**
46 : * Show the node options popover (always shows, doesn't toggle)
47 : * Use this for contextmenu events where we always want to show at the new position
48 : * @param event - The trigger event (must be MouseEvent for position)
49 : */
50 145 : export function showNodeOptions(
51 : event: MouseEvent,
52 : context?: { nodeId: NodeId; widgetName?: string }
53 : ) {
54 145 : invocationContext.value = context
55 145 : if (nodeOptionsInstance?.show) {
56 145 : nodeOptionsInstance.show(event)
57 : }
58 : }
59 :
60 : /**
61 : * Check if the node options menu is currently open
62 : */
63 4 : export function isNodeOptionsOpen(): boolean {
64 4 : return nodeOptionsInstance?.isOpen.value ?? false
65 : }
66 :
67 : interface NodeOptionsInstance {
68 : toggle: (event: Event) => void
69 : show: (event: MouseEvent) => void
70 : hide: () => void
71 : isOpen: Ref<boolean>
72 : }
73 :
74 : /**
75 : * Register the NodeOptions component instance
76 : * @param instance - The NodeOptions component instance
77 : */
78 2355 : export function registerNodeOptionsInstance(
79 : instance: null | NodeOptionsInstance
80 : ) {
81 2355 : nodeOptionsInstance = instance
82 : }
83 :
84 : /**
85 : * Mark menu options as coming from Vue hardcoded menu
86 : */
87 13397 : function markAsVueOptions(options: MenuOption[]): MenuOption[] {
88 13397 : return options.map((opt) => {
89 : // Don't mark dividers or category labels
90 192284 : if (opt.type === 'divider' || opt.type === 'category') {
91 54160 : return opt
92 : }
93 138124 : return { ...opt, source: 'vue' }
94 : })
95 : }
96 :
97 : /**
98 : * Composable for managing the More Options menu configuration
99 : * Refactored to use smaller, focused composables for better maintainability
100 : */
101 2355 : export function useMoreOptionsMenu() {
102 2355 : const {
103 : selectedItems,
104 : selectedNodes,
105 : canOpenNodeInfo,
106 : openNodeInfo,
107 : hasSubgraphs: hasSubgraphsComputed,
108 : hasImageNode,
109 : hasOutputNodesSelected,
110 : hasMultipleSelection,
111 : isSingleNode,
112 : computeSelectionFlags
113 : } = useSelectionState()
114 :
115 2355 : const canvasStore = useCanvasStore()
116 2355 : const nodeOutputStore = useNodeOutputStore()
117 :
118 2355 : const { getImageMenuOptions, getPreviewResolutionOption } =
119 : useImageMenuOptions()
120 2355 : const { hasMultipleOutputs } = useNodeOutputsExport()
121 2355 : const {
122 : getNodeInfoOption,
123 : getNodeVisualOptions,
124 : getPinOption,
125 : getBypassOption,
126 : getRunBranchOption
127 : } = useNodeMenuOptions()
128 2355 : const {
129 : getFitGroupToNodesOption,
130 : getGroupColorOptions,
131 : getGroupModeOptions
132 : } = useGroupMenuOptions()
133 2355 : const {
134 : getBasicSelectionOptions,
135 : getMultipleNodesOptions,
136 : getSubgraphOptions,
137 : getAlignmentOptions,
138 : getDeleteOption
139 : } = useSelectionMenuOptions()
140 :
141 2355 : const hasSubgraphs = hasSubgraphsComputed
142 2355 : const hasMultipleNodes = hasMultipleSelection
143 :
144 : // Internal version to force menu rebuild after state mutations
145 2355 : const optionsVersion = ref(0)
146 2355 : const bump = () => {
147 193 : optionsVersion.value++
148 : }
149 :
150 2355 : const menuOptions = computed((): MenuOption[] => {
151 : // Reference selection flags to ensure re-computation when they change
152 13397 : void optionsVersion.value
153 13397 : const states = computeSelectionFlags()
154 :
155 : // Detect single group selection context (and no nodes explicitly selected)
156 13397 : const selectedGroups = selectedItems.value.filter(isLGraphGroup)
157 13397 : const groupContext: LGraphGroup | null =
158 : selectedGroups.length === 1 && selectedNodes.value.length === 0
159 : ? selectedGroups[0]
160 : : null
161 13397 : const hasSubgraphsSelected = hasSubgraphs.value
162 :
163 : // For single node selection, also get LiteGraph menu items to merge
164 13397 : const litegraphOptions: MenuOption[] = []
165 13397 : const node: LGraphNode | undefined =
166 : (invocationContext.value === undefined
167 : ? undefined
168 : : canvasStore.currentGraph?.getNodeById(
169 : invocationContext.value.nodeId
170 : )) ?? selectedNodes.value.at(0)
171 13397 : const hideLinkedInputActions = node
172 : ? shouldHideLinkedCoreMediaInputActions(node)
173 : : false
174 13397 : const hideLinkedInputPreview = node
175 : ? shouldHideLinkedCoreMediaInputPreview(
176 : node,
177 : nodeOutputStore.getNodeOutputs(node)
178 : )
179 : : false
180 13397 : const unavailableCoreMediaActionKinds = new Set<CoreMediaMenuActionKind>()
181 13397 : if (hideLinkedInputActions) unavailableCoreMediaActionKinds.add('input')
182 13397 : if (hideLinkedInputPreview) unavailableCoreMediaActionKinds.add('preview')
183 13397 : if (
184 : selectedNodes.value.length === 1 &&
185 : node &&
186 : !groupContext &&
187 : canvasStore.canvas
188 : ) {
189 2309 : try {
190 2309 : const rawItems = canvasStore.canvas.getNodeMenuOptions(node)
191 : // Don't apply structuring yet - we'll do it after merging with Vue options
192 2309 : litegraphOptions.push(
193 : ...convertContextMenuToOptions(
194 : filterUnavailableCoreMediaMenuActions(
195 : rawItems,
196 : unavailableCoreMediaActionKinds
197 : ),
198 : node,
199 : false
200 : )
201 : )
202 : } catch (error) {
203 0 : console.error('Error getting LiteGraph menu items:', error)
204 : }
205 : }
206 :
207 13397 : const options: MenuOption[] = []
208 :
209 : // Section 1: Basic selection operations (Rename, Copy, Duplicate)
210 13397 : const basicOps = getBasicSelectionOptions()
211 13397 : options.push(...basicOps)
212 13397 : options.push({ type: 'divider' })
213 :
214 : // Section 2: Node actions (Run Branch, Pin, Bypass, Mute)
215 13397 : if (hasOutputNodesSelected.value) {
216 189 : const runBranch = getRunBranchOption()
217 189 : options.push(runBranch)
218 : }
219 13397 : if (!groupContext) {
220 13369 : const pin = getPinOption(states, bump)
221 13369 : const bypass = getBypassOption(bump)
222 13369 : options.push(pin)
223 13369 : options.push(bypass)
224 : }
225 13397 : if (groupContext) {
226 28 : const groupModes = getGroupModeOptions(groupContext, bump)
227 28 : options.push(...groupModes)
228 : }
229 13397 : options.push({ type: 'divider' })
230 :
231 : // Section 3: Structure operations (Convert to Subgraph, Frame selection, Minimize Node)
232 13397 : options.push(
233 : ...getSubgraphOptions({
234 : hasSubgraphs: hasSubgraphsSelected,
235 : hasMultipleSelection: hasMultipleNodes.value
236 : })
237 : )
238 13397 : if (hasMultipleNodes.value) {
239 243 : options.push(...getMultipleNodesOptions())
240 243 : options.push(...getAlignmentOptions(node))
241 : }
242 13397 : if (groupContext) {
243 28 : options.push(getFitGroupToNodesOption(groupContext))
244 : } else {
245 : // Node context: Expand/Minimize
246 13369 : const visualOptions = getNodeVisualOptions(states, bump)
247 13369 : if (visualOptions.length > 0) {
248 13369 : options.push(visualOptions[0]) // Expand/Minimize (index 0)
249 : }
250 : }
251 13397 : options.push({ type: 'divider' })
252 :
253 : // Section 4: Node properties (Node Info, Shape, Color)
254 13397 : if (canOpenNodeInfo.value) {
255 1869 : options.push(getNodeInfoOption(openNodeInfo))
256 : }
257 13397 : if (groupContext) {
258 28 : options.push(getGroupColorOptions(groupContext, bump))
259 : } else {
260 : // Add shape and color options
261 13369 : const visualOptions = getNodeVisualOptions(states, bump)
262 13369 : if (visualOptions.length > 1) {
263 13369 : options.push(visualOptions[1]) // Shape (index 1)
264 : }
265 13369 : if (visualOptions.length > 2) {
266 13369 : options.push(visualOptions[2]) // Color (index 2)
267 : }
268 : }
269 13397 : options.push({ type: 'divider' })
270 :
271 : // Section 5: Image operations (image nodes, or a node with several outputs)
272 13397 : if (
273 : (hasImageNode.value && selectedNodes.value.length > 0) ||
274 : (isSingleNode.value && hasMultipleOutputs(selectedNodes.value[0]))
275 : ) {
276 167 : options.push(
277 : ...getImageMenuOptions(selectedNodes.value[0], {
278 : input: !hideLinkedInputActions,
279 : preview: !hideLinkedInputPreview
280 : })
281 : )
282 167 : options.push({ type: 'divider' })
283 : }
284 13397 : const previewResolutionOption =
285 : isSingleNode.value && !hideLinkedInputPreview
286 : ? getPreviewResolutionOption(selectedNodes.value[0])
287 : : undefined
288 13397 : if (previewResolutionOption) options.push(previewResolutionOption)
289 13397 : const widgetName = invocationContext.value?.widgetName
290 13397 : const widget = node?.widgets?.find((w) => w.name === widgetName)
291 13397 : if (node && widget) {
292 135 : const widgetOptions = convertContextMenuToOptions(
293 : getExtraOptionsForWidget(node, widget)
294 : )
295 135 : if (widgetOptions.length > 0) {
296 135 : options.push(...widgetOptions)
297 135 : options.push({ type: 'divider' })
298 : }
299 : }
300 :
301 13397 : options.push(
302 : getDeleteOption(
303 : selectedNodes.value.some(
304 : (node) => node.removable === false || node.block_delete
305 : )
306 : )
307 : )
308 :
309 : // Section 6 & 7: Extensions and Delete are handled by buildStructuredMenu
310 :
311 : // Mark all Vue options with source
312 13397 : const markedVueOptions = markAsVueOptions(options)
313 :
314 13397 : if (litegraphOptions.length > 0) {
315 : // Merge: LiteGraph options first, then Vue options (Vue will win in dedup)
316 : const merged = [...litegraphOptions, ...markedVueOptions]
317 2309 : return buildStructuredMenu(merged)
318 : }
319 : // For other cases, structure the Vue options
320 : const result = buildStructuredMenu(markedVueOptions)
321 11088 : return result
322 : })
323 :
324 : // Computed property to get only menu items with submenus
325 : const menuOptionsWithSubmenu = computed(() =>
326 : menuOptions.value.filter((option) => option.hasSubmenu && option.submenu)
327 : )
328 :
329 2355 : return {
330 : menuOptions,
331 : menuOptionsWithSubmenu,
332 : bump,
333 : hasSubgraphs,
334 : registerNodeOptionsInstance
335 : }
336 : }
|