The swiss army knife of lossless video/audio editing
present keyboard shortcuts layout mapped #2515
Mikael Finstad25110782b284
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsxindex 4eb222ac..f249f0a0 100644--- a/src/renderer/src/App.tsx+++ b/src/renderer/src/App.tsx@@ -24,7 +24,7 @@ import useFrameCapture from './hooks/useFrameCapture';import useSegments from './hooks/useSegments';import useDirectoryAccess from './hooks/useDirectoryAccess';-import { UserSettingsContext, SegColorsContext, UserSettingsContextType, AppContext } from './contexts';+import { UserSettingsContext, SegColorsContext, UserSettingsContextType, AppContext, AppContextType } from './contexts';import NoFileLoaded from './NoFileLoaded';import MediaSourcePlayer from './MediaSourcePlayer';@@ -459,13 +459,6 @@ function App() {setStoreProjectInWorkingDir(newValue);}, [ensureAccessToSourceDir, getProjectFileSavePath, setStoreProjectInWorkingDir, storeProjectInWorkingDir]);- const appContext = useMemo(() => ({- working,- setWorking,- handleError,- showGenericDialog,- }), [handleError, setWorking, showGenericDialog, working]);-const userSettingsContext = useMemo<UserSettingsContextType>(() => {const { settings, ...rest } = allUserSettings;@@ -2135,7 +2128,7 @@ function App() {return true; // bubble the event}, [closeExportConfirm, concatSheetOpen, exportConfirmOpen, genericDialog, getKeyboardAction, keyboardShortcutsVisible, onExportConfirm, toggleKeyboardShortcuts]);- useKeyboard({ keyBindings, onKeyDown, onKeyUp });+ const { keyboardLayoutMap, updateKeyboardLayout } = useKeyboard({ keyBindings, onKeyDown, onKeyUp });useEffect(() => {// eslint-disable-next-line unicorn/prefer-add-event-listener@@ -2291,7 +2284,7 @@ function App() {await (fn as (...args2: unknown[]) => Promise<void>)(...args);},] as const),- // all main actions (no arguments, so simulate keyup):+ // all main actions (no arguments):...Object.entries(mainActions).map(([key, fn]) => [key,// eslint-disable-next-line @typescript-eslint/no-unused-vars@@ -2300,13 +2293,7 @@ function App() {},] as const),// also called from menu:- [- 'toggleKeyboardShortcuts',- // eslint-disable-next-line @typescript-eslint/no-unused-vars- async () => {- toggleKeyboardShortcuts();- },- ] as const,+ ['toggleKeyboardShortcuts', async () => toggleKeyboardShortcuts()] as const,];const allActionsMap = Object.fromEntries(allActions);@@ -2428,6 +2415,16 @@ function App() {return () => window.removeEventListener('keydown', handleKeydown);}, [haveBoundAlt]);+ const appContext = useMemo<AppContextType>(() => ({+ working,+ setWorking,+ handleError,+ showGenericDialog,+ keyboardLayoutMap,+ updateKeyboardLayout,+ }), [handleError, keyboardLayoutMap, setWorking, showGenericDialog, updateKeyboardLayout, working]);++const showLeftBar = batchFiles.length > 0;function renderSubtitles() {diff --git a/src/renderer/src/NoFileLoaded.tsx b/src/renderer/src/NoFileLoaded.tsxindex c18b29a7..d988c80d 100644--- a/src/renderer/src/NoFileLoaded.tsx+++ b/src/renderer/src/NoFileLoaded.tsx@@ -8,8 +8,9 @@ import SimpleModeButton from './components/SimpleModeButton';import useUserSettings from './hooks/useUserSettings';import { StateSegment } from './types';import { KeyBinding } from '../../../types';-import { formatKeyboardKey, splitKeyboardKeys } from './util';+import { splitKeyboardKeys } from './util';import { getModifier } from './hooks/useTimelineScroll';+import Kbd from './components/Kbd';const electron = window.require('electron');@@ -19,7 +20,7 @@ function Keys({ keys }: { keys: string | undefined }) {}const split = splitKeyboardKeys(keys);return split.map((key, i) => (- <Fragment key={key}><kbd>{formatKeyboardKey(key)}</kbd>{i < split.length - 1 && <span style={{ fontSize: '.7em', marginLeft: '-.2em', marginRight: '-.2em' }}>{' + '}</span>}</Fragment>+ <Fragment key={key}><Kbd code={key} />{i < split.length - 1 && <span style={{ fontSize: '.7em', marginLeft: '-.2em', marginRight: '-.2em' }}>{' + '}</span>}</Fragment>));}diff --git a/src/renderer/src/components/Kbd.tsx b/src/renderer/src/components/Kbd.tsxnew file mode 100644index 00000000..6f1f8fcf--- /dev/null+++ b/src/renderer/src/components/Kbd.tsx@@ -0,0 +1,118 @@+import { HTMLAttributes, useMemo } from 'react';++import { useAppContext } from '../contexts';+import { getMetaKeyName } from '../util';+++export default function Kbd({ code, ...props }: { code: string } & HTMLAttributes<HTMLElement>) {+ const { keyboardLayoutMap } = useAppContext();++ const keyName = useMemo(() => {+ const map: Record<string, string> = {+ Escape: 'Esc',+ Digit1: '1',+ Digit2: '2',+ Digit3: '3',+ Digit4: '4',+ Digit5: '5',+ Digit6: '6',+ Digit7: '7',+ Digit8: '8',+ Digit9: '9',+ Digit0: '0',+ KeyQ: 'Q',+ KeyW: 'W',+ KeyE: 'E',+ KeyR: 'R',+ KeyT: 'T',+ KeyY: 'Y',+ KeyU: 'U',+ KeyI: 'I',+ KeyO: 'O',+ KeyP: 'P',+ KeyA: 'A',+ KeyS: 'S',+ KeyD: 'D',+ KeyF: 'F',+ KeyG: 'G',+ KeyH: 'H',+ KeyJ: 'J',+ KeyK: 'K',+ KeyL: 'L',+ KeyZ: 'Z',+ KeyX: 'X',+ KeyC: 'C',+ KeyV: 'V',+ KeyB: 'B',+ KeyN: 'N',+ KeyM: 'M',+ Minus: '-',+ Equal: '=',+ BracketLeft: '[',+ BracketRight: ']',+ Semicolon: ';',+ Quote: '\'',+ Backquote: '`',+ Backslash: '\\',+ Comma: ',',+ Period: '.',+ Slash: '/',+ F1: 'F1',+ F2: 'F2',+ F3: 'F3',+ F4: 'F4',+ F5: 'F5',+ F6: 'F6',+ F7: 'F7',+ F8: 'F8',+ F9: 'F9',+ F10: 'F10',+ F11: 'F11',+ F12: 'F12',+ F13: 'F13',+ F14: 'F14',+ F15: 'F15',+ F16: 'F16',+ F17: 'F17',+ F18: 'F18',+ F19: 'F19',+ F20: 'F20',+ F21: 'F21',+ F22: 'F22',+ F23: 'F23',+ F24: 'F24',+ NumpadParenLeft: '(',+ NumpadParenRight: ')',+ PageUp: 'PgUp',+ PageDown: 'PgDn',+ ArrowUp: '↑',+ ArrowLeft: '←',+ ArrowRight: '→',+ ArrowDown: '↓',++ ControlLeft: 'Ctrl',+ ControlRight: 'Ctrl',+ ShiftLeft: 'Shift',+ ShiftRight: 'Shift',+ AltLeft: 'Alt',+ AltRight: 'Alt',+ MetaLeft: getMetaKeyName(),+ MetaRight: getMetaKeyName(),+ };++ if (keyboardLayoutMap == null) {+ return undefined;+ }++ return keyboardLayoutMap.get(code) ?? map[code] ?? code;+ }, [code, keyboardLayoutMap]);++ if (keyName == null) { // not yet loaded keyboard map+ return null;+ }++ return (+ // eslint-disable-next-line react/jsx-props-no-spreading+ <kbd {...props}>{keyName}</kbd>+ );+}diff --git a/src/renderer/src/components/KeyboardShortcuts.tsx b/src/renderer/src/components/KeyboardShortcuts.tsxindex 0c16556e..c075dace 100644--- a/src/renderer/src/components/KeyboardShortcuts.tsx+++ b/src/renderer/src/components/KeyboardShortcuts.tsx@@ -12,12 +12,14 @@ import SegmentCutpointButton from './SegmentCutpointButton';import { getModifier } from '../hooks/useTimelineScroll';import { KeyBinding, KeyboardAction, ModifierKey } from '../../../../types';import { StateSegment } from '../types';-import { allModifiers, altModifiers, controlModifiers, formatKeyboardKey, metaModifiers, shiftModifiers, splitKeyboardKeys } from '../util';+import { allModifiers, altModifiers, controlModifiers, metaModifiers, shiftModifiers, splitKeyboardKeys } from '../util';import * as Dialog from './Dialog';import Warning from './Warning';import Button, { DialogButton } from './Button';import Action from './Action';import TextInput from './TextInput';+import Kbd from './Kbd';+import { useAppContext } from '../contexts';type Category = string;@@ -28,7 +30,7 @@ type ActionsMap = Record<KeyboardAction, { name: string, category?: Category, beconst renderKeys = (keys: string[]) => keys.map((key, i) => (<Fragment key={key}>{i > 0 && <FaPlus style={{ verticalAlign: 'middle', fontSize: '.4em', opacity: 0.8, marginLeft: '.4em', marginRight: '.4em' }} />}- <kbd>{formatKeyboardKey(key)}</kbd>+ <Kbd code={key} /></Fragment>));@@ -159,6 +161,7 @@ const KeyboardShortcuts = memo(({currentCutSeg: StateSegment | undefined,}) => {const { t } = useTranslation();+ const { updateKeyboardLayout } = useAppContext();const { mouseWheelZoomModifierKey, mouseWheelFrameSeekModifierKey, mouseWheelKeyframeSeekModifierKey, segmentMouseModifierKey } = useUserSettings();@@ -744,7 +747,7 @@ const KeyboardShortcuts = memo(({<div key="1" style={{ ...rowStyle, alignItems: 'center' }}><span>{t('Manipulate segments on timeline')}</span><div style={{ flexGrow: 1 }} />- <kbd style={{ marginRight: '.7em' }}>{segmentMouseModifierKey}</kbd>+ <kbd style={{ marginRight: '.7em' }}>{getModifier(segmentMouseModifierKey)}</kbd><FaMouse style={{ marginRight: '.3em' }} /><span>{t('Mouse click and drag')}</span></div>,@@ -757,6 +760,10 @@ const KeyboardShortcuts = memo(({};}, [currentCutSeg, mouseWheelFrameSeekModifierKey, mouseWheelKeyframeSeekModifierKey, mouseWheelZoomModifierKey, segmentMouseModifierKey, t]);+ useEffect(() => {+ updateKeyboardLayout();+ }, [updateKeyboardLayout]);+useEffect(() => {// cleanup invalid bindings, to prevent renamed actions from blocking user to rebindconst validBindings = keyBindings.filter(({ action }) => actionsMap[action]);@@ -792,7 +799,7 @@ const KeyboardShortcuts = memo(({// eslint-disable-next-line no-alertif (!window.confirm(t('Are you sure?'))) return;- console.log('delete key binding', action, keys);+ console.log('Delete key binding', action, keys);setKeyBindings((existingBindings) => existingBindings.filter((existingBinding) => !(existingBinding.keys === keys && existingBinding.action === action)));}, [setKeyBindings, t]);@@ -815,7 +822,7 @@ const KeyboardShortcuts = memo(({const onNewKeyBindingConfirmed = useCallback(async (action: KeyboardAction, keys: string[]) => {const keysStr = stringifyKeys(keys);- console.log('new key binding', action, keysStr);+ console.log('New key binding', action, keysStr);const duplicate = keyBindings.find((existingBinding) => existingBinding.keys === keysStr);let shouldReplaceDuplicate: KeyBinding | undefined;diff --git a/src/renderer/src/contexts.ts b/src/renderer/src/contexts.tsindex ebc6eebc..a264fe77 100644--- a/src/renderer/src/contexts.ts+++ b/src/renderer/src/contexts.ts@@ -3,7 +3,7 @@ import Color from 'color';import invariant from 'tiny-invariant';import { UserSettingsRoot } from './hooks/useUserSettingsRoot';-import { ExportMode, SegmentColorIndex } from './types';+import { ExportMode, KeyboardLayoutMap, SegmentColorIndex } from './types';import type useLoading from './hooks/useLoading';import { GenericError } from './components/ErrorDialog';import { ShowGenericDialog } from './components/GenericDialog';@@ -25,11 +25,13 @@ interface SegColorsContextType {export type HandleError = (error: GenericError) => void;-interface AppContextType {+export interface AppContextType {setWorking: ReturnType<typeof useLoading>['setWorking'],working: ReturnType<typeof useLoading>['working'],handleError: HandleError,showGenericDialog: ShowGenericDialog,+ keyboardLayoutMap: KeyboardLayoutMap | undefined,+ updateKeyboardLayout: () => Promise<void>,}diff --git a/src/renderer/src/hooks/useKeyboard.ts b/src/renderer/src/hooks/useKeyboard.tsindex f1d0ad61..c3f5311d 100644--- a/src/renderer/src/hooks/useKeyboard.ts+++ b/src/renderer/src/hooks/useKeyboard.ts@@ -1,7 +1,8 @@-import { useEffect, useRef } from 'react';+import { useCallback, useEffect, useRef, useState } from 'react';import { KeyBinding, KeyboardAction } from '../../../../types';import { allModifiers, altModifiers, controlModifiers, metaModifiers, shiftModifiers } from '../util';+import { KeyboardLayoutMap } from '../types';/* Keyboard testing points (when making large changes):@@ -24,6 +25,8 @@ export default ({ keyBindings, onKeyDown: onKeyDownProp, onKeyUp: onKeyUpProp }:onKeyDown: ((a: { action: KeyboardAction }) => boolean) | ((a: { action: KeyboardAction }) => void),onKeyUp: ((a: { action: KeyboardAction }) => boolean) | ((a: { action: KeyboardAction }) => void),}) => {+ const [keyboardLayoutMap, setKeyboardLayoutMap] = useState<KeyboardLayoutMap | undefined>();+// optimization to prevent re-binding all the time:const onKeyDownRef = useRef<(a: StoredAction) => void>();useEffect(() => {@@ -119,4 +122,13 @@ export default ({ keyBindings, onKeyDown: onKeyDownProp, onKeyUp: onKeyUpProp }:document.removeEventListener('keyup', handleKeyUp);};}, [keyBindings]);++ const updateKeyboardLayout = useCallback(async () => {+ setKeyboardLayoutMap(await navigator.keyboard.getLayoutMap());+ }, [setKeyboardLayoutMap]);++ return {+ keyboardLayoutMap,+ updateKeyboardLayout,+ };};diff --git a/src/renderer/src/index.tsx b/src/renderer/src/index.tsxindex 1b11cf7b..36fa12de 100644--- a/src/renderer/src/index.tsx+++ b/src/renderer/src/index.tsx@@ -31,6 +31,7 @@ import './i18n';import './main.css';import './swal2.scss';+import { KeyboardLayoutMap } from './types';// something wrong with the tyep@@ -60,6 +61,11 @@ declare global {never);}+ interface Navigator {+ keyboard: {+ getLayoutMap(): Promise<KeyboardLayoutMap>;+ }+ }}diff --git a/src/renderer/src/main.css b/src/renderer/src/main.cssindex d6dad90e..056db754 100644--- a/src/renderer/src/main.css+++ b/src/renderer/src/main.css@@ -41,6 +41,7 @@ body {}kbd {+ white-space: nowrap;display: inline-block;padding: 3px 5px;font-size: 11px;diff --git a/src/renderer/src/types.ts b/src/renderer/src/types.tsindex 9995029a..628798da 100644--- a/src/renderer/src/types.ts+++ b/src/renderer/src/types.ts@@ -163,3 +163,5 @@ export interface BatchFile {path: string,name: string,}++export type KeyboardLayoutMap = Map<string, string>;diff --git a/src/renderer/src/util.ts b/src/renderer/src/util.tsindex 42de0a0e..e060a1fb 100644--- a/src/renderer/src/util.ts+++ b/src/renderer/src/util.ts@@ -419,102 +419,6 @@ export function getMetaKeyName() {export const dialogButtonOrder = isWindows ? 'rtl' : 'ltr'; // use ltr for mac and linux, rtl for windows-export function formatKeyboardKey(key: string) {- const map: Record<string, string> = {- Escape: 'Esc',- Digit1: '1',- Digit2: '2',- Digit3: '3',- Digit4: '4',- Digit5: '5',- Digit6: '6',- Digit7: '7',- Digit8: '8',- Digit9: '9',- Digit0: '0',- KeyQ: 'Q',- KeyW: 'W',- KeyE: 'E',- KeyR: 'R',- KeyT: 'T',- KeyY: 'Y',- KeyU: 'U',- KeyI: 'I',- KeyO: 'O',- KeyP: 'P',- KeyA: 'A',- KeyS: 'S',- KeyD: 'D',- KeyF: 'F',- KeyG: 'G',- KeyH: 'H',- KeyJ: 'J',- KeyK: 'K',- KeyL: 'L',- KeyZ: 'Z',- KeyX: 'X',- KeyC: 'C',- KeyV: 'V',- KeyB: 'B',- KeyN: 'N',- KeyM: 'M',- Minus: '-',- Equal: '=',- BracketLeft: '[',- BracketRight: ']',- Semicolon: ';',- Quote: '\'',- Backquote: '`',- Backslash: '\\',- Comma: ',',- Period: '.',- Slash: '/',- F1: 'F1',- F2: 'F2',- F3: 'F3',- F4: 'F4',- F5: 'F5',- F6: 'F6',- F7: 'F7',- F8: 'F8',- F9: 'F9',- F10: 'F10',- F11: 'F11',- F12: 'F12',- F13: 'F13',- F14: 'F14',- F15: 'F15',- F16: 'F16',- F17: 'F17',- F18: 'F18',- F19: 'F19',- F20: 'F20',- F21: 'F21',- F22: 'F22',- F23: 'F23',- F24: 'F24',- NumpadParenLeft: '(',- NumpadParenRight: ')',- PageUp: 'PgUp',- PageDown: 'PgDn',- ArrowUp: '↑',- ArrowLeft: '←',- ArrowRight: '→',- ArrowDown: '↓',-- ControlLeft: 'Ctrl',- ControlRight: 'Ctrl',- ShiftLeft: 'Shift',- ShiftRight: 'Shift',- AltLeft: 'Alt',- AltRight: 'Alt',- MetaLeft: getMetaKeyName(),- MetaRight: getMetaKeyName(),- };-- return map[key] || key;-}-export function shootConfetti(options?: confetti.Options) {confetti({particleCount: 30,