Line data Source code
1 : <template>
2 92 : <PopoverRoot v-model:open="isOpen">
3 : <PopoverTrigger as-child>
4 : <Button
5 : ref="buttonRef"
6 : variant="secondary"
7 : class="group rounded-none! bg-transparent p-0 transition-none! hover:rounded-lg! hover:bg-accent!"
8 : :style="buttonStyles"
9 : :aria-label="$t('graphCanvasMenu.canvasMode')"
10 : aria-haspopup="menu"
11 : :aria-expanded="isOpen"
12 : >
13 : <div class="flex items-center gap-1 pr-0.5">
14 : <div class="rounded-lg bg-muted p-2 group-hover:bg-accent">
15 : <i :class="currentModeIcon" class="block" aria-hidden="true" />
16 : </div>
17 : <i
18 : class="icon-[lucide--chevron-down] block pr-1.5"
19 : aria-hidden="true"
20 : />
21 : </div>
22 : </Button>
23 : </PopoverTrigger>
24 :
25 : <PopoverContent
26 : side="top"
27 : :side-offset="8"
28 : :class="
29 : cn(
30 : menuContentClass,
31 : 'w-auto min-w-39 select-none data-[state=closed]:animate-none data-[state=open]:animate-none'
32 : )
33 : "
34 : @open-auto-focus.prevent="onPopoverShow"
35 : >
36 : <div
37 : ref="menuRef"
38 : class="flex flex-col"
39 : role="menu"
40 : :aria-label="$t('graphCanvasMenu.canvasMode')"
41 : >
42 : <button
43 : type="button"
44 : role="menuitemradio"
45 : :aria-checked="!isCanvasReadOnly"
46 : :tabindex="!isCanvasReadOnly ? 0 : -1"
47 : :class="cn(menuButtonClass, 'justify-between')"
48 : :aria-label="$t('graphCanvasMenu.select')"
49 : @click="setMode('select')"
50 : @keydown.arrow-down.prevent="focusNextItem"
51 : @keydown.arrow-up.prevent="focusPrevItem"
52 : @keydown.escape.prevent="closeAndRestoreFocus"
53 : >
54 : <div class="flex items-center gap-2">
55 : <i class="icon-[lucide--mouse-pointer-2]" aria-hidden="true" />
56 : <span>{{ $t('graphCanvasMenu.select') }}</span>
57 : </div>
58 : <span
59 : class="text-[9px] text-foreground"
60 : data-testid="shortcut-hint"
61 : >{{ unlockCommandText }}</span
62 : >
63 : </button>
64 :
65 : <button
66 : type="button"
67 : role="menuitemradio"
68 : :aria-checked="isCanvasReadOnly"
69 : :tabindex="isCanvasReadOnly ? 0 : -1"
70 : :class="cn(menuButtonClass, 'justify-between')"
71 : :aria-label="$t('graphCanvasMenu.hand')"
72 : @click="setMode('hand')"
73 : @keydown.arrow-down.prevent="focusNextItem"
74 : @keydown.arrow-up.prevent="focusPrevItem"
75 : @keydown.escape.prevent="closeAndRestoreFocus"
76 : >
77 : <div class="flex items-center gap-2">
78 : <i class="icon-[lucide--hand]" aria-hidden="true" />
79 : <span>{{ $t('graphCanvasMenu.hand') }}</span>
80 : </div>
81 : <span
82 : class="text-[9px] text-foreground"
83 : data-testid="shortcut-hint"
84 : >{{ lockCommandText }}</span
85 : >
86 : </button>
87 : </div>
88 : </PopoverContent>
89 : </PopoverRoot>
90 : </template>
91 :
92 : <script setup lang="ts">
93 : import { unrefElement } from '@vueuse/core'
94 : import { PopoverRoot, PopoverTrigger } from 'reka-ui'
95 : import PopoverContent from '@comfyorg/design-system/popover/PopoverContent'
96 : import type { ComponentPublicInstance } from 'vue'
97 : import { computed, nextTick, ref } from 'vue'
98 :
99 : import Button from '@comfyorg/design-system/button/Button'
100 : import { usePopoverViewportDismiss } from '@comfyorg/design-system/lib/popover/usePopoverViewportDismiss'
101 : import {
102 : menuButtonClass,
103 : menuContentClass
104 : } from '@comfyorg/design-system/lib/menu/menuStyles'
105 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
106 : import { useCommandStore } from '@/platform/keybindings/commandStore'
107 : import { cn } from '@comfyorg/tailwind-utils'
108 :
109 : interface Props {
110 : buttonStyles?: Record<string, string>
111 : }
112 :
113 : defineProps<Props>()
114 68 : const buttonRef = ref<ComponentPublicInstance | null>(null)
115 68 : const menuRef = ref<HTMLElement | null>(null)
116 68 : const isOpen = ref(false)
117 68 : usePopoverViewportDismiss(isOpen, () => unrefElement(buttonRef))
118 68 : const commandStore = useCommandStore()
119 68 : const canvasStore = useCanvasStore()
120 :
121 68 : const isCanvasReadOnly = computed(() => canvasStore.canvas?.read_only ?? false)
122 :
123 68 : const currentModeIcon = computed(() =>
124 : isCanvasReadOnly.value
125 : ? 'icon-[lucide--hand]'
126 : : 'icon-[lucide--mouse-pointer-2]'
127 : )
128 :
129 68 : const unlockCommandText = computed(() =>
130 : commandStore
131 : .formatKeySequence(commandStore.getCommand('Comfy.Canvas.Unlock'))
132 : .toUpperCase()
133 : )
134 :
135 68 : const lockCommandText = computed(() =>
136 : commandStore
137 : .formatKeySequence(commandStore.getCommand('Comfy.Canvas.Lock'))
138 : .toUpperCase()
139 : )
140 :
141 68 : const setMode = (mode: 'select' | 'hand') => {
142 4 : if (mode === 'select' && isCanvasReadOnly.value) {
143 1 : void commandStore.execute('Comfy.Canvas.Unlock')
144 3 : } else if (mode === 'hand' && !isCanvasReadOnly.value) {
145 2 : void commandStore.execute('Comfy.Canvas.Lock')
146 : }
147 4 : isOpen.value = false
148 : }
149 :
150 16 : async function onPopoverShow() {
151 16 : await nextTick()
152 : const checkedItem = menuRef.value?.querySelector<HTMLElement>(
153 : '[aria-checked="true"]'
154 : )
155 16 : checkedItem?.focus()
156 : }
157 :
158 2 : function closeAndRestoreFocus() {
159 2 : isOpen.value = false
160 2 : unrefElement(buttonRef)?.focus()
161 : }
162 :
163 2 : function focusNextItem(event: KeyboardEvent) {
164 2 : const items = getMenuItems(event)
165 : const index = items.indexOf(event.target as HTMLElement)
166 2 : items[(index + 1) % items.length]?.focus()
167 : }
168 :
169 0 : function focusPrevItem(event: KeyboardEvent) {
170 0 : const items = getMenuItems(event)
171 : const index = items.indexOf(event.target as HTMLElement)
172 0 : items[(index - 1 + items.length) % items.length]?.focus()
173 : }
174 :
175 2 : function getMenuItems(event: KeyboardEvent): HTMLElement[] {
176 2 : const menu = (event.target as HTMLElement).closest('[role="menu"]')
177 2 : if (!menu) return []
178 2 : return Array.from(menu.querySelectorAll('[role="menuitemradio"]'))
179 : }
180 : </script>
|