Skip to content

Commit 89bf2b7

Browse files
feat(ui): add ability to globally disable hotkeys
This will both hide the hotkey from the hotkey modal and override any other enabled status it has.
1 parent 8af0550 commit 89bf2b7

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

invokeai/frontend/web/src/features/system/components/HotkeysModal/HotkeysModal.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const HotkeysModal = ({ children }: HotkeysModalProps) => {
5353
hotkeys: [],
5454
};
5555
Object.values(category.hotkeys).forEach((item) => {
56+
if (!item.isEnabled) {
57+
return;
58+
}
5659
if (!trimmedHotkeyFilter.length) {
5760
filteredGroup.hotkeys.push(item);
5861
} else if (item.title.toLowerCase().includes(trimmedHotkeyFilter)) {

invokeai/frontend/web/src/features/system/components/HotkeysModal/useHotkeyData.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type Hotkey = {
1313
desc: string;
1414
hotkeys: string[];
1515
platformKeys: string[][];
16+
isEnabled: boolean;
1617
};
1718

1819
type HotkeyCategoryData = { title: string; hotkeys: Record<string, Hotkey> };
@@ -60,14 +61,15 @@ export const useHotkeyData = (): HotkeysData => {
6061
},
6162
};
6263

63-
const addHotkey = (category: HotkeyCategory, id: string, keys: string[]) => {
64+
const addHotkey = (category: HotkeyCategory, id: string, keys: string[], isEnabled: boolean = true) => {
6465
data[category].hotkeys[id] = {
6566
id,
6667
category,
6768
title: t(`hotkeys.${category}.${id}.title`),
6869
desc: t(`hotkeys.${category}.${id}.desc`),
6970
hotkeys: keys,
7071
platformKeys: formatKeysForPlatform(keys, isMacOS),
72+
isEnabled,
7173
};
7274
};
7375

@@ -189,17 +191,28 @@ type UseRegisteredHotkeysArg = {
189191
};
190192

191193
/**
192-
* A wrapper around `useHotkeys` that registers the hotkey with the hotkey registry.
193-
*
194-
* Registered hotkeys will be displayed in the hotkeys modal.
194+
* A wrapper around `useHotkeys` that adds a handler for a registered hotkey.
195195
*/
196196
export const useRegisteredHotkeys = ({ id, category, callback, options, dependencies }: UseRegisteredHotkeysArg) => {
197197
const hotkeysData = useHotkeyData();
198-
const keys = useMemo(() => {
199-
const _keys = hotkeysData[category].hotkeys[id]?.hotkeys;
200-
assert(_keys !== undefined, `Hotkey ${category}.${id} not found`);
201-
return _keys;
198+
const data = useMemo(() => {
199+
const _data = hotkeysData[category].hotkeys[id];
200+
assert(_data !== undefined, `Hotkey ${category}.${id} not found`);
201+
return _data;
202202
}, [category, hotkeysData, id]);
203-
204-
return useHotkeys(keys, callback, options, dependencies);
203+
const _options = useMemo(() => {
204+
// If no options are provided, return the default. This includes if the hotkey is globally disabled.
205+
if (!options) {
206+
return {
207+
enabled: data.isEnabled,
208+
} satisfies Options;
209+
}
210+
// Otherwise, return the provided optiosn, but override the enabled state.
211+
return {
212+
...options,
213+
enabled: data.isEnabled ? options.enabled : false,
214+
} satisfies Options;
215+
}, [data.isEnabled, options]);
216+
217+
return useHotkeys(data.hotkeys, callback, _options, dependencies);
205218
};

0 commit comments

Comments
 (0)