Line data Source code
1 : <script lang="ts">
2 : // A segment switch unmounts the focused toggle instance and mounts a fresh
3 : // one in the other mode's host, so the focus handoff is tracked at module
4 : // scope where both instances can reach it.
5 2355 : let restoreFocusOnMount = false
6 2355 : </script>
7 :
8 : <script setup lang="ts">
9 : import { cn } from '@comfyorg/tailwind-utils'
10 : import { snakeCase } from 'es-toolkit'
11 : import { computed, nextTick, onMounted, ref, useTemplateRef } from 'vue'
12 : import { useI18n } from 'vue-i18n'
13 :
14 : import Button from '@comfyorg/design-system/button/Button'
15 : import Menu from '@comfyorg/design-system/menu/Menu'
16 : import { useWorkflowActionsMenu } from '@/composables/useWorkflowActionsMenu'
17 : import { useKeybindingStore } from '@comfyorg/keybindings/keybindingStore'
18 : import { useTelemetry } from '@comfyorg/telemetry'
19 : import { useAppModeStore } from '@/stores/appModeStore'
20 : import { useCommandStore } from '@/platform/keybindings/commandStore'
21 : import type { ViewMode } from '@comfyorg/graph-model/appMode'
22 :
23 : interface ViewModeSegment {
24 : mode: ViewMode
25 : icon: string
26 : label: string
27 : switchLabel: string
28 : switchTooltip: string
29 : /** Drives behavior and aria; flips as soon as the mode changes. */
30 : active: boolean
31 : /** Frame-lagged mirror of {@link active} that drives the morph order. */
32 : displayActive: boolean
33 : }
34 :
35 : const { source, align = 'start' } = defineProps<{
36 : source: string
37 : align?: 'start' | 'center' | 'end'
38 : }>()
39 :
40 2093 : const { t } = useI18n()
41 2093 : const keybindingStore = useKeybindingStore()
42 2093 : const dropdownOpen = ref(false)
43 2093 : const appModeStore = useAppModeStore()
44 :
45 2093 : const { menuItems } = useWorkflowActionsMenu(
46 : () => useCommandStore().execute('Comfy.RenameWorkflow'),
47 : { isRoot: true }
48 : )
49 :
50 61 : function onMenuItemActivate(item: object) {
51 61 : if (!('id' in item) || typeof item.id !== 'string') return
52 61 : useTelemetry()?.trackUiButtonClicked({
53 : button_id: `workflow_actions_${snakeCase(item.id)}_clicked`,
54 : element_group: 'workflow_actions'
55 : })
56 : }
57 :
58 2093 : const toggleShortcut = computed(() => {
59 2111 : const shortcut = keybindingStore
60 : .getKeybindingByCommandId('Comfy.ToggleLinear')
61 : ?.combo.toString()
62 2111 : return shortcut ? t('g.shortcutSuffix', { shortcut }) : ''
63 : })
64 :
65 2093 : const segments = computed<ViewModeSegment[]>(() =>
66 : (
67 : [
68 : {
69 : mode: 'graph',
70 : icon: 'icon-[comfy--workflow]',
71 : label: t('breadcrumbsMenu.graph'),
72 : switchLabel: t('breadcrumbsMenu.enterNodeGraph'),
73 : switchTooltip:
74 : t('breadcrumbsMenu.enterNodeGraph') + toggleShortcut.value
75 : },
76 : {
77 : mode: 'app',
78 : icon: 'icon-[lucide--panels-top-left]',
79 : label: t('breadcrumbsMenu.app'),
80 : switchLabel: t('breadcrumbsMenu.enterAppMode'),
81 : switchTooltip: t('breadcrumbsMenu.enterAppMode') + toggleShortcut.value
82 : }
83 : ] as const
84 : ).map((seg) => ({
85 : ...seg,
86 : active: appModeStore.viewMode === seg.mode,
87 : displayActive: appModeStore.displayViewMode === seg.mode
88 : }))
89 : )
90 :
91 : // Display-inactive segment first (left), display-active last (right). The
92 : // reorder on mode switch is what TransitionGroup FLIP-animates.
93 2093 : const orderedSegments = computed(() => {
94 2193 : const [graph, app] = segments.value
95 2193 : return graph.displayActive ? [app, graph] : [graph, app]
96 : })
97 :
98 122 : function onOpenChange(open: boolean) {
99 122 : dropdownOpen.value = open
100 122 : if (!open) return
101 122 : useTelemetry()?.trackUiButtonClicked({
102 : button_id: source,
103 : element_group: 'workflow_actions'
104 : })
105 : }
106 :
107 2 : function switchMode() {
108 2 : dropdownOpen.value = false
109 2 : restoreFocusOnMount = true
110 2 : void useCommandStore().execute('Comfy.ToggleLinear', {
111 : metadata: { source }
112 : })
113 : }
114 :
115 4514 : function segmentLabel(seg: ViewModeSegment) {
116 4514 : return seg.active
117 : ? t('breadcrumbsMenu.activeModeWorkflowActions', { mode: seg.label })
118 : : seg.switchLabel
119 : }
120 :
121 4514 : function segmentTooltip(seg: ViewModeSegment) {
122 4514 : return seg.active ? t('breadcrumbsMenu.workflowActions') : seg.switchTooltip
123 : }
124 :
125 63 : function onSegmentClick(seg: ViewModeSegment, event: MouseEvent) {
126 63 : if (seg.active) return
127 2 : event.stopPropagation()
128 2 : switchMode()
129 : }
130 :
131 : /** Keys the reka trigger opens the menu on; other keys bubble on so app-level
132 : * keybindings keep working while a segment has focus. */
133 2093 : const MENU_TRIGGER_KEYS = new Set(['Enter', ' ', 'ArrowDown'])
134 :
135 1 : function onSegmentKeydown(seg: ViewModeSegment, event: KeyboardEvent) {
136 1 : if (!seg.active && MENU_TRIGGER_KEYS.has(event.key)) event.stopPropagation()
137 : }
138 :
139 2093 : const toggleRef = useTemplateRef<HTMLDivElement>('toggleRef')
140 :
141 : // The trigger div carries role="group" (permitting the aria-label, prohibited
142 : // on a plain div, that names the menu via reka's aria-labelledby) and
143 : // tabindex="-1" so reka's close-focus (e.g. Escape closing the menu) lands
144 : // there; its @focus forwards focus here, to the active segment. Also called
145 : // when this instance mounts to replace the one unmounted by a
146 : // segment-initiated mode switch.
147 3 : function focusActiveSegment() {
148 3 : toggleRef.value
149 : ?.querySelector<HTMLButtonElement>('button[aria-haspopup]')
150 : ?.focus()
151 : }
152 :
153 2093 : onMounted(async () => {
154 2093 : if (!restoreFocusOnMount) return
155 2 : restoreFocusOnMount = false
156 2 : await nextTick()
157 2 : focusActiveSegment()
158 : })
159 2093 : </script>
160 :
161 : <template>
162 8359 : <Menu
163 : v-model:open="dropdownOpen"
164 : :items="menuItems"
165 : :modal="false"
166 : :align
167 : :side-offset="8"
168 : :collision-padding="10"
169 : class="min-w-56"
170 : @update:open="onOpenChange"
171 : @activate="onMenuItemActivate"
172 : >
173 : <template #trigger>
174 : <div
175 : ref="toggleRef"
176 : data-testid="view-mode-toggle"
177 : role="group"
178 : tabindex="-1"
179 : :aria-label="t('breadcrumbsMenu.workflowActions')"
180 : class="group pointer-events-auto relative inline-block shrink-0 floating-panel"
181 : @focus="focusActiveSegment"
182 : >
183 : <TransitionGroup
184 : tag="div"
185 : move-class="transition-[background-color,color,transform] duration-200"
186 : class="flex items-center gap-1"
187 : >
188 4514 : <div v-for="seg in orderedSegments" :key="seg.mode" class="flex">
189 : <Button
190 : :tooltip="segmentTooltip(seg)"
191 : tooltip-side="bottom"
192 : type="button"
193 : variant="textonly"
194 : size="unset"
195 : :aria-label="segmentLabel(seg)"
196 : :aria-haspopup="seg.active ? 'menu' : undefined"
197 : :aria-expanded="seg.active ? dropdownOpen : undefined"
198 : :class="
199 : cn(
200 : 'flex h-8 gap-0 rounded-lg font-normal transition-[background-color,color,transform] duration-200',
201 : seg.displayActive
202 : ? 'bg-secondary pr-2 pl-2.5 group-data-[state=open]:bg-secondary/80 group-data-[state=open]:shadow-md hover:bg-secondary'
203 : : 'w-8 text-muted-foreground hover:bg-secondary hover:text-foreground'
204 : )
205 : "
206 : @click="onSegmentClick(seg, $event)"
207 : @keydown="onSegmentKeydown(seg, $event)"
208 : >
209 : <i :class="seg.icon" aria-hidden="true" />
210 : <span
211 : :class="
212 : cn(
213 : 'grid transition-[grid-template-columns,opacity] duration-200',
214 : seg.displayActive
215 : ? 'ml-1.5 grid-cols-[1fr] opacity-100'
216 : : 'grid-cols-[0fr] opacity-0'
217 : )
218 : "
219 : >
220 : <span
221 : class="flex min-w-0 items-center overflow-hidden text-sm leading-none whitespace-nowrap"
222 : >
223 : {{ seg.label }}
224 : <i
225 : class="ml-1 icon-[lucide--chevron-down] text-muted-foreground"
226 : aria-hidden="true"
227 : />
228 : </span>
229 : </span>
230 : </Button>
231 : </div>
232 : </TransitionGroup>
233 : </div>
234 : </template>
235 : </Menu>
236 : </template>
|