📧✨ Beautiful email. For everyone.
fix: prioritize native shortcuts in focused fields (#41)
Paul Koeck3afb76a37403
diff --git a/apps/desktop/src/renderer/src/App.tsx b/apps/desktop/src/renderer/src/App.tsxindex 7842f0b..3cf537a 100644--- a/apps/desktop/src/renderer/src/App.tsx+++ b/apps/desktop/src/renderer/src/App.tsx@@ -98,7 +98,14 @@ import { messageText } from './message-text'import { signatureBlock, signatureText } from './signature'import { textSignatureMarker } from './composer-document'import { selectConversation } from './selection'-import { ShortcutHint, useShortcutHandlers, useShortcutText, useShortcuts } from './shortcuts'+import {+ ignoreInputsForCommand,+ sameBinding,+ ShortcutHint,+ useShortcutHandlers,+ useShortcutText,+ useShortcuts,+} from './shortcuts'import { emptyListState } from './empty-states'import { AccountMark } from './AccountMark'import { lockVault, useEncryption } from './encryption-ui'@@ -981,9 +988,9 @@ export function App() {commandMenu: {run: () => setPaletteOpen((p) => !p),enabled: !composer,- ignoreInputs: false,+ ignoreInputs: ignoreInputsForCommand,},- settings: { run: () => openSettings(), ignoreInputs: false },+ settings: { run: () => openSettings(), ignoreInputs: ignoreInputsForCommand },toggleSidebar: { run: () => setCollapsed((c) => !c) },compose: { run: () => void compose() },search: { run: focusSearch },@@ -1005,6 +1012,7 @@ export function App() {previous: { run: () => step(-1) },open: { run: openFocused },back: {+ ignoreInputs: (binding) => !sameBinding(binding, ['Escape']),run: () => {if (route.thread) back()else if (draftSelected.size) setDraftSelected(new Set())diff --git a/apps/desktop/src/renderer/src/Composer.tsx b/apps/desktop/src/renderer/src/Composer.tsxindex 89b34eb..f83ab1f 100644--- a/apps/desktop/src/renderer/src/Composer.tsx+++ b/apps/desktop/src/renderer/src/Composer.tsx@@ -40,7 +40,13 @@ import { recipientProblems, useEncryption } from './encryption-ui'import { ContactKeyDialog, keysChanged, useUnlock } from './encryption-actions'import { ProtectionBar, ProtectionMenu, type Protection } from './ComposerProtection'import { formatBytes } from './mail-date'-import { bindingText, useShortcutHandlers, useShortcutText, useShortcuts } from './shortcuts'+import {+ bindingText,+ ignoreInputsForCommand,+ useShortcutHandlers,+ useShortcutText,+ useShortcuts,+} from './shortcuts'import { SignatureNode } from './signature'import { linkTarget } from './link-target'import { SignatureParagraph, hasDraftContent, replaceSignature } from './composer-document'@@ -568,25 +574,44 @@ export function Composer({}const canEdit = !linkOpen && !discardOpen && !lockeduseShortcutHandlers(shortcuts.bindings, {- // Keep the library's input-aware defaults: modifier shortcuts work while writing,- // but a custom letter or sequence must not send or discard a draft as someone types.+ // Composer commands deliberately work while writing; custom typing keys stay quiet.send: {+ ignoreInputs: ignoreInputsForCommand,run: () => void send(),enabled: !linkOpen && !discardOpen && !sending && !changingFrom,},- attachFiles: { run: () => void attachFiles(), enabled: canEdit },- showCc: { run: () => focusRecipient('cc'), enabled: canEdit },- showBcc: { run: () => focusRecipient('bcc'), enabled: canEdit },+ attachFiles: {+ run: () => void attachFiles(),+ enabled: canEdit,+ ignoreInputs: ignoreInputsForCommand,+ },+ showCc: {+ run: () => focusRecipient('cc'),+ enabled: canEdit,+ ignoreInputs: ignoreInputsForCommand,+ },+ showBcc: {+ run: () => focusRecipient('bcc'),+ enabled: canEdit,+ ignoreInputs: ignoreInputsForCommand,+ },expandComposer: {+ ignoreInputs: ignoreInputsForCommand,run: () => setExpanded((current) => !current),enabled: !linkOpen && !discardOpen,},saveClose: {+ ignoreInputs: ignoreInputsForCommand,run: () => void close(),enabled: !linkOpen && !discardOpen && !sending && !changingFrom,},- discardDraft: { run: () => setDiscardOpen(true), enabled: canEdit },+ discardDraft: {+ run: () => setDiscardOpen(true),+ enabled: canEdit,+ ignoreInputs: ignoreInputsForCommand,+ },toggleEncryption: {+ ignoreInputs: ignoreInputsForCommand,run: () => setProtection(protection === 'encrypt' ? 'none' : 'encrypt'),enabled: canEdit && !!own,},diff --git a/apps/desktop/src/renderer/src/shortcuts.tsx b/apps/desktop/src/renderer/src/shortcuts.tsxindex 2d701b4..01b0f44 100644--- a/apps/desktop/src/renderer/src/shortcuts.tsx+++ b/apps/desktop/src/renderer/src/shortcuts.tsx@@ -5,6 +5,7 @@ import {formatForDisplay,normalizeHotkey,normalizeRegisterableHotkey,+ parseHotkey,parseKeyboardEvent,useHotkeys,useHotkeySequences,@@ -279,9 +280,17 @@ export type ShortcutHandler = {run: () => void/** Defaults to the hook's `enabled`. */enabled?: boolean+ /** Defaults to true so focused fields keep their native editing shortcuts. */ignoreInputs?: boolean | ((binding: Binding) => boolean)}+/** Opt intentional commands into fields without letting custom typing keys trigger them. */+export const ignoreInputsForCommand = (binding: Binding) =>+ !binding.every((chord) => {+ const parsed = parseHotkey(chord as Hotkey, platform)+ return parsed.ctrl || parsed.meta || parsed.key === 'Escape'+ })+/** Registers every binding of each handled shortcut. */export function useShortcutHandlers(bindings: ShortcutBindings,@@ -298,12 +307,10 @@ export function useShortcutHandlers(for (const binding of bindings[id]) {const bindingOptions = {...options,- ...(handler.ignoreInputs !== undefined && {- ignoreInputs:- typeof handler.ignoreInputs === 'function'- ? handler.ignoreInputs(binding)- : handler.ignoreInputs,- }),+ ignoreInputs:+ typeof handler.ignoreInputs === 'function'+ ? handler.ignoreInputs(binding)+ : (handler.ignoreInputs ?? true),}if (binding.length === 1)hotkeys.push({diff --git a/tests/shortcuts.test.ts b/tests/shortcuts.test.tsindex c71455b..bf6d0d5 100644--- a/tests/shortcuts.test.ts+++ b/tests/shortcuts.test.ts@@ -1,12 +1,26 @@-import { describe, expect, it } from 'vitest'+import { beforeEach, describe, expect, it, vi } from 'vitest'+import {+ useHotkeys,+ useHotkeySequences,+} from '../apps/desktop/node_modules/@tanstack/react-hotkeys'import {defaultBindings,findConflicts,+ ignoreInputsForCommand,overridesFor,resolveShortcuts,shortcutDefinitions,+ useShortcutHandlers,} from '../apps/desktop/src/renderer/src/shortcuts'+vi.mock('../apps/desktop/node_modules/@tanstack/react-hotkeys', async (importOriginal) => ({+ ...(await importOriginal<+ typeof import('../apps/desktop/node_modules/@tanstack/react-hotkeys')+ >()),+ useHotkeys: vi.fn(),+ useHotkeySequences: vi.fn(),+}))+describe('shortcut defaults and saved preferences', () => {it('has no conflicting default chords or sequence prefixes', () => {const bindings = resolveShortcuts()@@ -53,3 +67,54 @@ describe('shortcut defaults and saved preferences', () => {})})})++describe('shortcut input priority', () => {+ beforeEach(() => vi.clearAllMocks())++ it('keeps select-all and undo shortcuts out of focused fields', () => {+ useShortcutHandlers(resolveShortcuts(), {+ selectAll: { run: vi.fn() },+ undo: { run: vi.fn() },+ })+ const hotkeys = vi.mocked(useHotkeys).mock.calls[0][0]+ for (const hotkey of ['Mod+A', 'Mod+Z', 'Z'])+ expect(hotkeys.find((definition) => definition.hotkey === hotkey)?.options).toMatchObject({+ ignoreInputs: true,+ enabled: true,+ })+ })++ it('also protects fields from customized modifier shortcuts and sequences', () => {+ useShortcutHandlers(+ resolveShortcuts({ trash: [['Mod+Backspace']], archive: [['Mod+E', 'A']] }),+ { trash: { run: vi.fn() }, archive: { run: vi.fn() } },+ )+ expect(vi.mocked(useHotkeys).mock.calls[0][0][0]).toMatchObject({+ hotkey: 'Mod+Backspace',+ options: { ignoreInputs: true },+ })+ expect(vi.mocked(useHotkeySequences).mock.calls[0][0][0]).toMatchObject({+ sequence: ['Mod+E', 'A'],+ options: { ignoreInputs: true },+ })+ })++ it('allows deliberate field commands while keeping their custom typing bindings quiet', () => {+ useShortcutHandlers(+ resolveShortcuts({ send: [['Mod+Enter'], ['S'], ['Shift+S'], ['Alt+S'], ['Mod+S', 'S']] }),+ { send: { run: vi.fn(), ignoreInputs: ignoreInputsForCommand } },+ )+ const hotkeys = vi.mocked(useHotkeys).mock.calls[0][0]+ expect(hotkeys.find((definition) => definition.hotkey === 'Mod+Enter')?.options).toMatchObject({+ ignoreInputs: false,+ })+ for (const hotkey of ['S', 'Shift+S', 'Alt+S'])+ expect(hotkeys.find((definition) => definition.hotkey === hotkey)?.options).toMatchObject({+ ignoreInputs: true,+ })+ expect(vi.mocked(useHotkeySequences).mock.calls[0][0][0].options?.ignoreInputs).toBe(true)+ expect(ignoreInputsForCommand(['Ctrl+Enter'])).toBe(false)+ expect(ignoreInputsForCommand(['Meta+Enter'])).toBe(false)+ expect(ignoreInputsForCommand(['Escape'])).toBe(false)+ })+})