LCOV - code coverage report
Current view: top level - src/platform/keybindings/components - KeybindingPanel.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 97.7 % 131 128
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1           16 : <template>
       2              :   <div class="keybinding-panel flex min-w-0 flex-col gap-2 overflow-x-hidden">
       3              :     <Teleport defer to="#keybinding-panel-header">
       4              :       <SearchInput
       5              :         v-model="searchQuery"
       6              :         :clear-label="$t('g.clear')"
       7              :         :accessible-label="
       8              :           $t('g.searchPlaceholder', { subject: $t('g.keybindings') })
       9              :         "
      10              :         class="max-w-96"
      11              :         size="lg"
      12              :         autofocus
      13              :         :placeholder="
      14              :           $t('g.searchPlaceholder', { subject: $t('g.keybindings') })
      15              :         "
      16              :       />
      17              :     </Teleport>
      18              : 
      19              :     <Teleport defer to="#keybinding-panel-actions">
      20              :       <div class="flex items-center gap-2">
      21              :         <KeybindingPresetToolbar
      22              :           :preset-names
      23              :           @presets-changed="refreshPresetList"
      24              :         />
      25              :         <Menu :items="menuEntries" to="#keybinding-panel-actions" align="end">
      26              :           <template #trigger>
      27              :             <Button
      28              :               size="icon-lg"
      29              :               data-testid="keybinding-preset-menu"
      30              :               icon="icon-[lucide--ellipsis]"
      31              :               :aria-label="$t('g.more')"
      32              :             />
      33              :           </template>
      34              :         </Menu>
      35              :       </div>
      36              :     </Teleport>
      37              : 
      38              :     <ContextMenu :model="rowMenuItems" @close-auto-focus="restoreRowFocus">
      39              :       <Table
      40              :         data-testid="keybinding-table-container"
      41              :         @pointerdown.capture="contextMenuTarget = null"
      42              :         @contextmenu.capture="contextMenuTarget = null"
      43              :       >
      44              :         <TableHeader>
      45              :           <TableRow>
      46              :             <TableSortHead v-model:direction="commandSortDirection">
      47              :               {{ $t('g.command') }}
      48              :             </TableSortHead>
      49              :             <TableHead class="w-3/10">{{ $t('g.keybinding') }}</TableHead>
      50              :             <TableHead class="w-4/25">{{ $t('g.source') }}</TableHead>
      51              :             <TableHead class="w-36">
      52              :               <span class="sr-only">{{ $t('g.actions') }}</span>
      53              :             </TableHead>
      54              :           </TableRow>
      55              :         </TableHeader>
      56              :         <RovingFocusGroup
      57              :           v-model:current-tab-stop-id="currentRowTabStopId"
      58              :           as-child
      59              :           orientation="vertical"
      60              :         >
      61              :           <TableBody>
      62         4604 :             <KeybindingCommandRows
      63              :               v-for="command in visibleCommands"
      64              :               :key="command.id"
      65              :               :command
      66              :               :expanded="expandedCommandIds.has(command.id)"
      67              :               :selected="selectedCommandId === command.id"
      68              :               @activate="activateRow(command)"
      69              :               @row-dblclick="handleRowDblClick(command)"
      70              :               @row-contextmenu="handleRowContextMenu($event, command)"
      71              :               @edit="editKeybinding(command, $event)"
      72              :               @add="addKeybinding(command)"
      73              :               @reset="resetKeybinding(command)"
      74              :               @remove="handleRemoveKeybindingFromMenu(command)"
      75              :               @remove-single="removeSingleKeybinding(command, $event)"
      76              :             />
      77              :           </TableBody>
      78              :         </RovingFocusGroup>
      79              :       </Table>
      80              :     </ContextMenu>
      81              :     <AppPagination
      82              :       v-if="filteredCommands.length > commandsPerPageOptions[0]"
      83              :       v-model:page="currentPage"
      84              :       v-model:items-per-page="commandsPerPage"
      85              :       :total="filteredCommands.length"
      86              :       :items-per-page-options="commandsPerPageOptions"
      87              :     />
      88              : 
      89              :     <Button
      90              :       :tooltip="$t('g.resetAllKeybindingsTooltip')"
      91              :       class="mt-4 w-full"
      92              :       variant="destructive-textonly"
      93              :       @click="resetAllKeybindings"
      94              :     >
      95              :       <i class="icon-[lucide--rotate-ccw]" />
      96              :       {{ $t('g.resetAll') }}
      97              :     </Button>
      98              :   </div>
      99              : </template>
     100              : 
     101              : <script setup lang="ts">
     102              : import type { ComponentProps } from 'vue-component-type-helpers'
     103              : import { computed, onMounted, ref, watch } from 'vue'
     104              : import { useI18n } from 'vue-i18n'
     105              : import { RovingFocusGroup } from 'reka-ui'
     106              : 
     107              : import { showConfirmDialog } from '@/components/dialog/confirm/confirmDialog'
     108              : import Button from '@comfyorg/design-system/button/Button'
     109              : import { useToast } from '@/components/ui/toast/toastStore'
     110              : import ContextMenu from '@comfyorg/design-system/menu/ContextMenu'
     111              : import Menu from '@comfyorg/design-system/menu/Menu'
     112              : import type { MenuItem } from '@comfyorg/design-system/lib/menu/types'
     113              : import AppPagination from '@/components/common/AppPagination.vue'
     114              : import SearchInput from '@comfyorg/design-system/search-input/SearchInput'
     115              : import Table from '@comfyorg/design-system/table/Table'
     116              : import TableBody from '@comfyorg/design-system/table/TableBody'
     117              : import TableHead from '@comfyorg/design-system/table/TableHead'
     118              : import TableHeader from '@comfyorg/design-system/table/TableHeader'
     119              : import TableRow from '@comfyorg/design-system/table/TableRow'
     120              : import TableSortHead from '@comfyorg/design-system/table/TableSortHead'
     121              : import {
     122              :   filterByQuery,
     123              :   sortByText
     124              : } from '@comfyorg/design-system/lib/table/tableUtils'
     125              : import type { TableSortDirection } from '@comfyorg/design-system/lib/table/tableUtils'
     126              : import { useEditKeybindingDialog } from '@/platform/keybindings/composables/useEditKeybindingDialog'
     127              : import type { KeybindingImpl } from '@comfyorg/keybindings/keybinding'
     128              : import { useKeybindingService } from '@/platform/keybindings/keybindingService'
     129              : import { useKeybindingStore } from '@comfyorg/keybindings/keybindingStore'
     130              : import { useKeybindingPresetService } from '@/platform/keybindings/presetService'
     131              : import { useSettingStore } from '@/platform/settings/settingStore'
     132              : import { useCommandStore } from '@/platform/keybindings/commandStore'
     133              : import { useDialogStore } from '@/stores/dialogStore'
     134              : import { normalizeI18nKey } from '@/utils/formatUtil'
     135              : 
     136              : import KeybindingCommandRows from './KeybindingCommandRows.vue'
     137              : import KeybindingPresetToolbar from './KeybindingPresetToolbar.vue'
     138              : 
     139              : type KeybindingCommand = ComponentProps<typeof KeybindingCommandRows>['command']
     140              : 
     141           39 : const searchQuery = ref('')
     142              : 
     143           39 : const keybindingStore = useKeybindingStore()
     144           39 : const keybindingService = useKeybindingService()
     145           39 : const presetService = useKeybindingPresetService()
     146           39 : const settingStore = useSettingStore()
     147           39 : const commandStore = useCommandStore()
     148           39 : const dialogStore = useDialogStore()
     149           39 : const { t } = useI18n()
     150           39 : const toast = useToast()
     151              : 
     152           39 : const presetNames = ref<string[]>([])
     153              : 
     154           46 : async function refreshPresetList() {
     155           46 :   presetNames.value = (await presetService.listPresets()) ?? []
     156              : }
     157              : 
     158           39 : async function initPresets() {
     159           39 :   await refreshPresetList()
     160           39 :   const currentName = settingStore.get('Comfy.Keybinding.CurrentPreset')
     161           39 :   if (currentName !== 'default') {
     162            1 :     const preset = await presetService.loadPreset(currentName)
     163            1 :     if (preset) {
     164            1 :       keybindingStore.savedPresetData = preset
     165            1 :       keybindingStore.currentPresetName = currentName
     166              :     } else {
     167            0 :       await presetService.switchToDefaultPreset()
     168              :     }
     169              :   }
     170              : }
     171              : 
     172           39 : onMounted(() => initPresets())
     173              : 
     174              : // "..." menu entries (teleported to header)
     175            1 : async function saveAsNewPreset() {
     176            1 :   await presetService.promptAndSaveNewPreset()
     177            1 :   refreshPresetList()
     178              : }
     179              : 
     180            1 : async function handleDeletePreset() {
     181            1 :   await presetService.deletePreset(keybindingStore.currentPresetName)
     182            1 :   refreshPresetList()
     183              : }
     184              : 
     185            4 : async function handleImportPreset() {
     186            4 :   await presetService.importPreset()
     187            4 :   refreshPresetList()
     188              : }
     189              : 
     190           39 : const showSaveAsNew = computed(
     191              :   () =>
     192              :     keybindingStore.currentPresetName !== 'default' ||
     193              :     keybindingStore.isCurrentPresetModified
     194              : )
     195              : 
     196           39 : const menuEntries = computed<MenuItem[]>(() => [
     197              :   ...(showSaveAsNew.value
     198              :     ? [
     199              :         {
     200              :           label: t('g.keybindingPresets.saveAsNewPreset'),
     201              :           icon: 'icon-[lucide--save]',
     202              :           command: saveAsNewPreset
     203              :         }
     204              :       ]
     205              :     : []),
     206              :   {
     207              :     label: t('g.keybindingPresets.resetToDefault'),
     208              :     icon: 'icon-[lucide--rotate-cw]',
     209              :     command: () =>
     210              :       presetService.switchPreset('default').then(() => refreshPresetList())
     211              :   },
     212              :   {
     213              :     label: t('g.keybindingPresets.deletePreset'),
     214              :     icon: 'icon-[lucide--trash-2]',
     215              :     disabled: keybindingStore.currentPresetName === 'default',
     216              :     command: handleDeletePreset
     217              :   },
     218              :   {
     219              :     label: t('g.keybindingPresets.importPreset'),
     220              :     icon: 'icon-[lucide--file-input]',
     221              :     command: handleImportPreset
     222              :   },
     223              :   {
     224              :     label: t('g.keybindingPresets.exportPreset'),
     225              :     icon: 'icon-[lucide--file-output]',
     226              :     command: () => presetService.exportPreset()
     227              :   }
     228              : ])
     229              : 
     230              : // Keybinding table logic
     231           39 : const commandsData = computed<KeybindingCommand[]>(() => {
     232           57 :   return Object.values(commandStore.commands).map((command) => {
     233         6254 :     const keybindings = keybindingStore.getKeybindingsByCommandId(command.id)
     234         6254 :     return {
     235              :       expandable: keybindings.length >= 2,
     236              :       id: command.id,
     237              :       isModified: keybindingStore.isCommandKeybindingModified(command.id),
     238              :       keybindings,
     239              :       label: t(
     240              :         `commands.${normalizeI18nKey(command.id)}.label`,
     241              :         command.label ?? command.id
     242              :       ),
     243              :       rowId: `keybinding-row-${command.id}`,
     244              :       source: command.source
     245              :     }
     246              :   })
     247              : })
     248              : 
     249           39 : const commandSortDirection = ref<TableSortDirection | null>(null)
     250           39 : const currentPage = ref(1)
     251           39 : const commandsPerPage = ref(50)
     252           39 : const commandsPerPageOptions = [25, 50, 100]
     253           39 : const filteredCommands = computed(() => {
     254           87 :   const filtered = filterByQuery(
     255              :     commandsData.value,
     256              :     searchQuery.value,
     257              :     (command) => [command.id, command.label]
     258              :   )
     259           87 :   return sortByText(
     260              :     filtered,
     261              :     commandSortDirection.value,
     262              :     (command) => command.label
     263              :   )
     264              : })
     265           39 : const visibleCommands = computed(() => {
     266           87 :   const start = (currentPage.value - 1) * commandsPerPage.value
     267           87 :   return filteredCommands.value.slice(start, start + commandsPerPage.value)
     268              : })
     269              : 
     270           39 : watch(commandSortDirection, () => {
     271            0 :   currentPage.value = 1
     272              : })
     273              : 
     274           39 : const focusedRowTabStopId = ref<string | null>(null)
     275           39 : const currentRowTabStopId = computed({
     276              :   get: () => {
     277          110 :     const rowIds = visibleCommands.value.map((command) => command.rowId)
     278          110 :     const focused = focusedRowTabStopId.value
     279          110 :     return focused && rowIds.includes(focused) ? focused : (rowIds[0] ?? null)
     280              :   },
     281              :   set: (rowId: string | null) => {
     282           60 :     focusedRowTabStopId.value = rowId
     283              :   }
     284              : })
     285              : 
     286           39 : const expandedCommandIds = ref<Set<string>>(new Set())
     287              : 
     288            7 : function toggleExpanded(commandId: string) {
     289            7 :   if (expandedCommandIds.value.has(commandId)) {
     290            2 :     expandedCommandIds.value.delete(commandId)
     291              :   } else {
     292            5 :     expandedCommandIds.value.add(commandId)
     293              :   }
     294              : }
     295              : 
     296           39 : watch(searchQuery, () => {
     297           30 :   currentPage.value = 1
     298           30 :   expandedCommandIds.value.clear()
     299              : })
     300              : 
     301           39 : const selectedCommandId = ref<string | null>(null)
     302           39 : const editKeybindingDialog = useEditKeybindingDialog()
     303              : 
     304           39 : const contextMenuTarget = ref<KeybindingCommand | null>(null)
     305           39 : let rowMenuOrigin: HTMLElement | null = null
     306           39 : const rowMenuItems = computed<MenuItem[]>(() => {
     307           48 :   const target = contextMenuTarget.value
     308           48 :   if (!target) return []
     309            9 :   const hasBindings = target.keybindings.length > 0
     310            9 :   return [
     311              :     {
     312              :       label: t('g.changeKeybinding'),
     313              :       icon: 'icon-[lucide--pencil]',
     314              :       disabled: !hasBindings,
     315              :       command: () => changeKeybinding(target)
     316              :     },
     317              :     {
     318              :       label: t('g.addNewKeybinding'),
     319              :       icon: 'icon-[lucide--plus]',
     320              :       command: () => addKeybinding(target)
     321              :     },
     322              :     { separator: true },
     323              :     {
     324              :       label: t('g.resetToDefault'),
     325              :       icon: 'icon-[lucide--rotate-ccw]',
     326              :       disabled: !target.isModified,
     327              :       command: () => resetKeybinding(target)
     328              :     },
     329              :     {
     330              :       label: t('g.removeKeybinding'),
     331              :       icon: 'icon-[lucide--trash-2]',
     332              :       disabled: !hasBindings,
     333              :       command: () => handleRemoveKeybindingFromMenu(target)
     334              :     }
     335              :   ]
     336              : })
     337              : 
     338            4 : function editKeybinding(command: KeybindingCommand, binding: KeybindingImpl) {
     339            4 :   editKeybindingDialog.show({
     340              :     commandId: command.id,
     341              :     commandLabel: command.label,
     342              :     currentCombo: binding.combo,
     343              :     mode: 'edit',
     344              :     existingBinding: binding
     345              :   })
     346              : }
     347              : 
     348            9 : function addKeybinding(command: KeybindingCommand) {
     349            9 :   editKeybindingDialog.show({
     350              :     commandId: command.id,
     351              :     commandLabel: command.label,
     352              :     currentCombo: null,
     353              :     mode: 'add'
     354              :   })
     355              : }
     356              : 
     357           13 : function activateRow(command: KeybindingCommand) {
     358           13 :   selectedCommandId.value = command.id
     359           13 :   if (command.expandable || expandedCommandIds.value.has(command.id)) {
     360            7 :     toggleExpanded(command.id)
     361              :   }
     362              : }
     363              : 
     364            2 : function handleRowDblClick(command: KeybindingCommand) {
     365            2 :   if (command.keybindings.length === 0) {
     366            1 :     addKeybinding(command)
     367            1 :   } else if (command.keybindings.length === 1) {
     368            1 :     editKeybinding(command, command.keybindings[0])
     369              :   }
     370              : }
     371              : 
     372            9 : function handleRowContextMenu(event: Event, command: KeybindingCommand) {
     373            9 :   selectedCommandId.value = command.id
     374            9 :   contextMenuTarget.value = command
     375            9 :   rowMenuOrigin =
     376              :     event.currentTarget instanceof HTMLElement ? event.currentTarget : null
     377              : }
     378              : 
     379            8 : function restoreRowFocus(event: Event) {
     380            8 :   event.preventDefault()
     381            8 :   const focused = document.activeElement
     382            8 :   if (focused === document.body || focused?.closest('[role="menu"]')) {
     383            8 :     rowMenuOrigin?.focus()
     384              :   }
     385            8 :   rowMenuOrigin = null
     386              : }
     387              : 
     388            2 : async function removeSingleKeybinding(
     389              :   command: KeybindingCommand,
     390              :   index: number
     391              : ) {
     392            2 :   const binding = command.keybindings[index]
     393            2 :   if (binding) {
     394            2 :     keybindingStore.unsetKeybinding(binding)
     395            2 :     if (command.keybindings.length <= 2) {
     396            2 :       expandedCommandIds.value.delete(command.id)
     397              :     }
     398            2 :     await keybindingService.persistUserKeybindings()
     399              :   }
     400              : }
     401              : 
     402            2 : function handleRemoveAllKeybindings(command: KeybindingCommand) {
     403            2 :   const dialog = showConfirmDialog({
     404              :     headerProps: { title: t('g.removeAllKeybindingsTitle') },
     405              :     props: { promptText: t('g.removeAllKeybindingsMessage') },
     406              :     footerProps: {
     407              :       cancelText: t('g.cancel'),
     408              :       confirmText: t('g.removeAll'),
     409              :       confirmVariant: 'destructive',
     410              :       onCancel: () => dialogStore.closeDialog(dialog),
     411              :       onConfirm: async () => {
     412            1 :         keybindingStore.removeAllKeybindingsForCommand(command.id)
     413            1 :         try {
     414            1 :           await keybindingService.persistUserKeybindings()
     415              :         } finally {
     416            1 :           dialogStore.closeDialog(dialog)
     417              :         }
     418              :       }
     419              :     }
     420              :   })
     421              : }
     422              : 
     423            3 : function handleRemoveKeybindingFromMenu(command: KeybindingCommand) {
     424            3 :   if (command.expandable) {
     425            2 :     handleRemoveAllKeybindings(command)
     426              :   } else {
     427            1 :     removeSingleKeybinding(command, 0)
     428              :   }
     429              : }
     430              : 
     431            2 : function changeKeybinding(command: KeybindingCommand) {
     432            2 :   if (command.keybindings.length === 1) {
     433            1 :     editKeybinding(command, command.keybindings[0])
     434              :   } else {
     435            1 :     expandedCommandIds.value.add(command.id)
     436              :   }
     437              : }
     438              : 
     439            2 : async function resetKeybinding(command: KeybindingCommand) {
     440            2 :   if (keybindingStore.resetKeybindingForCommand(command.id)) {
     441            2 :     expandedCommandIds.value.delete(command.id)
     442            2 :     await keybindingService.persistUserKeybindings()
     443              :   } else {
     444            0 :     console.warn(
     445              :       `No changes made when resetting keybinding for command: ${command.id}`
     446              :     )
     447              :   }
     448              : }
     449              : 
     450            4 : function resetAllKeybindings() {
     451            4 :   const dialog = showConfirmDialog({
     452              :     headerProps: {
     453              :       title: t('g.resetAllKeybindingsTitle')
     454              :     },
     455              :     props: {
     456              :       promptText: t('g.resetAllKeybindingsMessage')
     457              :     },
     458              :     footerProps: {
     459              :       cancelText: t('g.cancel'),
     460              :       confirmText: t('g.resetAll'),
     461              :       confirmVariant: 'destructive',
     462              :       onCancel: () => {
     463            1 :         dialogStore.closeDialog(dialog)
     464              :       },
     465              :       onConfirm: async () => {
     466            1 :         keybindingStore.resetAllKeybindings()
     467            1 :         try {
     468            1 :           await keybindingService.persistUserKeybindings()
     469              :         } finally {
     470            1 :           dialogStore.closeDialog(dialog)
     471              :         }
     472            1 :         toast.info(t('g.info'), {
     473              :           description: t('g.allKeybindingsReset'),
     474              :           duration: 3000
     475              :         })
     476              :       }
     477              :     }
     478              :   })
     479              : }
     480              : </script>
        

Generated by: LCOV version 2.0-1