Line data Source code
1 : import { useI18n } from 'vue-i18n'
2 :
3 : import { LGraphEventMode } from '@/lib/litegraph/src/litegraph'
4 : import type { LGraphGroup } from '@/lib/litegraph/src/litegraph'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
7 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
8 :
9 : import { useCanvasRefresh } from './useCanvasRefresh'
10 : import type { MenuOption } from './menuOption'
11 : import { useNodeCustomization } from './useNodeCustomization'
12 :
13 : /**
14 : * Composable for group-related menu operations
15 : */
16 2355 : export function useGroupMenuOptions() {
17 2355 : const { t } = useI18n()
18 2355 : const canvasStore = useCanvasStore()
19 2355 : const workflowStore = useWorkflowStore()
20 2355 : const settingStore = useSettingStore()
21 2355 : const canvasRefresh = useCanvasRefresh()
22 2355 : const { shapeOptions, colorOptions, applyColor, isLightTheme } =
23 : useNodeCustomization()
24 :
25 2355 : const getFitGroupToNodesOption = (groupContext: LGraphGroup): MenuOption => ({
26 : label: 'Fit Group To Nodes',
27 : icon: 'icon-[lucide--move-diagonal-2]',
28 : action: () => {
29 0 : try {
30 0 : groupContext.recomputeInsideNodes()
31 : } catch (e) {
32 0 : console.warn('Failed to recompute nodes in group:', e)
33 0 : return
34 : }
35 :
36 0 : const padding = settingStore.get('Comfy.GroupSelectedNodes.Padding')
37 0 : groupContext.resizeTo(groupContext.children, padding)
38 0 : groupContext.graph?.change()
39 0 : canvasStore.canvas?.setDirty(true, true)
40 0 : workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
41 : }
42 : })
43 :
44 2355 : const getGroupShapeOptions = (
45 : groupContext: LGraphGroup,
46 : bump: () => void
47 : ): MenuOption => ({
48 : label: t('contextMenu.Shape'),
49 : icon: 'icon-[lucide--box]',
50 : hasSubmenu: true,
51 : submenu: shapeOptions.map((shape) => ({
52 : label: shape.localizedName,
53 : action: () => {
54 : const nodes = groupContext.nodes
55 0 : nodes.forEach((node) => (node.shape = shape.value))
56 0 : canvasRefresh.refreshCanvas()
57 0 : bump()
58 : }
59 : }))
60 : })
61 :
62 2355 : const getGroupColorOptions = (
63 : _groupContext: LGraphGroup,
64 : bump: () => void
65 : ): MenuOption => ({
66 : label: t('contextMenu.Color'),
67 : icon: 'icon-[lucide--palette]',
68 : hasSubmenu: true,
69 : isColorPicker: true,
70 : submenu: colorOptions.map((colorOption) => ({
71 : label: colorOption.localizedName,
72 : color: isLightTheme.value
73 : ? colorOption.value.light
74 : : colorOption.value.dark,
75 : action: () => {
76 2 : applyColor(colorOption.name === 'noColor' ? null : colorOption)
77 2 : bump()
78 : }
79 : }))
80 : })
81 :
82 2355 : const getGroupModeOptions = (
83 : groupContext: LGraphGroup,
84 : bump: () => void
85 : ): MenuOption[] => {
86 28 : const options: MenuOption[] = []
87 :
88 28 : try {
89 28 : groupContext.recomputeInsideNodes()
90 : } catch (e) {
91 0 : console.warn('Failed to recompute nodes in group for mode options:', e)
92 0 : return options
93 : }
94 :
95 28 : const groupNodes = groupContext.nodes
96 28 : if (!groupNodes.length) return options
97 :
98 : // Check if all nodes have the same mode
99 16 : let allSame = true
100 16 : for (let i = 1; i < groupNodes.length; i++) {
101 37 : if (groupNodes[i].mode !== groupNodes[0].mode) {
102 1 : allSame = false
103 1 : break
104 : }
105 : }
106 :
107 16 : const createModeAction = (label: string, mode: LGraphEventMode) => ({
108 : label: t(`selectionToolbox.${label}`),
109 : icon:
110 : mode === LGraphEventMode.BYPASS
111 : ? 'icon-[lucide--ban]'
112 : : mode === LGraphEventMode.NEVER
113 : ? 'icon-[lucide--zap-off]'
114 : : 'icon-[lucide--play]',
115 : action: () => {
116 0 : groupNodes.forEach((n) => {
117 0 : n.mode = mode
118 : })
119 0 : canvasStore.canvas?.setDirty(true, true)
120 0 : groupContext.graph?.change()
121 0 : workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
122 0 : bump()
123 : }
124 : })
125 :
126 16 : if (allSame) {
127 : const current = groupNodes[0].mode
128 15 : switch (current) {
129 : case LGraphEventMode.ALWAYS:
130 14 : options.push(
131 : createModeAction('Set Group Nodes to Never', LGraphEventMode.NEVER)
132 : )
133 14 : options.push(
134 : createModeAction('Bypass Group Nodes', LGraphEventMode.BYPASS)
135 : )
136 14 : break
137 : case LGraphEventMode.NEVER:
138 0 : options.push(
139 : createModeAction(
140 : 'Set Group Nodes to Always',
141 : LGraphEventMode.ALWAYS
142 : )
143 : )
144 0 : options.push(
145 : createModeAction('Bypass Group Nodes', LGraphEventMode.BYPASS)
146 : )
147 0 : break
148 : case LGraphEventMode.BYPASS:
149 1 : options.push(
150 : createModeAction(
151 : 'Set Group Nodes to Always',
152 : LGraphEventMode.ALWAYS
153 : )
154 : )
155 1 : options.push(
156 : createModeAction('Set Group Nodes to Never', LGraphEventMode.NEVER)
157 : )
158 1 : break
159 : default:
160 0 : options.push(
161 : createModeAction(
162 : 'Set Group Nodes to Always',
163 : LGraphEventMode.ALWAYS
164 : )
165 : )
166 0 : options.push(
167 : createModeAction('Set Group Nodes to Never', LGraphEventMode.NEVER)
168 : )
169 0 : options.push(
170 : createModeAction('Bypass Group Nodes', LGraphEventMode.BYPASS)
171 : )
172 : break
173 : }
174 : } else {
175 1 : options.push(
176 : createModeAction('Set Group Nodes to Always', LGraphEventMode.ALWAYS)
177 : )
178 1 : options.push(
179 : createModeAction('Set Group Nodes to Never', LGraphEventMode.NEVER)
180 : )
181 1 : options.push(
182 : createModeAction('Bypass Group Nodes', LGraphEventMode.BYPASS)
183 : )
184 : }
185 :
186 16 : return options
187 : }
188 :
189 2355 : return {
190 : getFitGroupToNodesOption,
191 : getGroupShapeOptions,
192 : getGroupColorOptions,
193 : getGroupModeOptions
194 : }
195 : }
|