feat(desktop): rebindable keyboard shortcuts panel
Add a central keybind registry + nanostore so desktop hotkeys are discoverable and user-rebindable. A titlebar ⌨ button (and ⌘/) opens a collapsible map grouped by Composer (read-only) / Profiles / Session / Navigation / View; click any chip to capture a new combo. Overrides persist to localStorage as a delta against shipped defaults, so future default changes aren't shadowed by a stored snapshot. Migrates the previously scattered inline listeners (palette, command center, new session, sidebar, theme) into the registry, and adds profile switch/cycle/create + default-profile hotkeys.
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import { atom, computed } from 'nanostores'
|
||||
|
||||
import {
|
||||
defaultBindings,
|
||||
KEYBIND_ACTION_IDS,
|
||||
keybindAction,
|
||||
type KeybindBindings
|
||||
} from '@/lib/keybinds/actions'
|
||||
import { arraysEqual, persistString, storedString } from '@/lib/storage'
|
||||
|
||||
const STORAGE_KEY = 'hermes.desktop.keybinds'
|
||||
|
||||
// Defaults overlaid with the user's stored overrides. Unknown / stale action ids
|
||||
// are dropped; actions added in a later release pick up their shipped default.
|
||||
function loadBindings(): KeybindBindings {
|
||||
const base = defaultBindings()
|
||||
const raw = storedString(STORAGE_KEY)
|
||||
|
||||
if (!raw) {
|
||||
return base
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>
|
||||
|
||||
for (const id of KEYBIND_ACTION_IDS) {
|
||||
const value = parsed[id]
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
base[id] = value.filter((combo): combo is string => typeof combo === 'string')
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Corrupt storage falls back to defaults.
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
// Persist only the actions whose combos differ from their shipped default, so
|
||||
// changing a default never gets shadowed by a stored snapshot.
|
||||
function persistBindings(bindings: KeybindBindings): void {
|
||||
const defaults = defaultBindings()
|
||||
const diff: KeybindBindings = {}
|
||||
|
||||
for (const id of KEYBIND_ACTION_IDS) {
|
||||
const current = bindings[id] ?? []
|
||||
|
||||
if (!arraysEqual(current, defaults[id] ?? [])) {
|
||||
diff[id] = current
|
||||
}
|
||||
}
|
||||
|
||||
persistString(STORAGE_KEY, JSON.stringify(diff))
|
||||
}
|
||||
|
||||
export const $bindings = atom<KeybindBindings>(loadBindings())
|
||||
|
||||
$bindings.subscribe(persistBindings)
|
||||
|
||||
// Reverse lookup combo → actionId for dispatch. First action wins on conflict;
|
||||
// the panel/edit overlay surface conflicts so users can resolve them.
|
||||
export const $comboIndex = computed($bindings, bindings => {
|
||||
const index = new Map<string, string>()
|
||||
|
||||
for (const id of KEYBIND_ACTION_IDS) {
|
||||
for (const combo of bindings[id] ?? []) {
|
||||
if (!index.has(combo)) {
|
||||
index.set(combo, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return index
|
||||
})
|
||||
|
||||
export function setBinding(actionId: string, combos: string[]): void {
|
||||
if (!keybindAction(actionId)) {
|
||||
return
|
||||
}
|
||||
|
||||
$bindings.set({ ...$bindings.get(), [actionId]: [...combos] })
|
||||
}
|
||||
|
||||
export function resetBinding(actionId: string): void {
|
||||
const action = keybindAction(actionId)
|
||||
|
||||
if (!action) {
|
||||
return
|
||||
}
|
||||
|
||||
$bindings.set({ ...$bindings.get(), [actionId]: [...action.defaults] })
|
||||
}
|
||||
|
||||
export function resetAllBindings(): void {
|
||||
$bindings.set(defaultBindings())
|
||||
}
|
||||
|
||||
// Other actions that already use `combo` (excluding `actionId` itself).
|
||||
export function conflictsFor(actionId: string, combo: string): string[] {
|
||||
const bindings = $bindings.get()
|
||||
|
||||
return KEYBIND_ACTION_IDS.filter(id => id !== actionId && (bindings[id] ?? []).includes(combo))
|
||||
}
|
||||
|
||||
// ── Capture ─────────────────────────────────────────────────────────────────
|
||||
// `$capture` is the action currently listening for its next keypress (a panel
|
||||
// row armed for rebinding). Session-only — never persisted.
|
||||
|
||||
export const $capture = atom<string | null>(null)
|
||||
|
||||
export function beginCapture(actionId: string): void {
|
||||
$capture.set(actionId)
|
||||
}
|
||||
|
||||
export function endCapture(): void {
|
||||
$capture.set(null)
|
||||
}
|
||||
|
||||
// ── Panel ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export const $keybindPanelOpen = atom(false)
|
||||
|
||||
export function openKeybindPanel(): void {
|
||||
$keybindPanelOpen.set(true)
|
||||
}
|
||||
|
||||
export function closeKeybindPanel(): void {
|
||||
$keybindPanelOpen.set(false)
|
||||
$capture.set(null)
|
||||
}
|
||||
|
||||
export function toggleKeybindPanel(): void {
|
||||
if ($keybindPanelOpen.get()) {
|
||||
closeKeybindPanel()
|
||||
} else {
|
||||
openKeybindPanel()
|
||||
}
|
||||
}
|
||||
@@ -288,6 +288,72 @@ export function setShowAllProfiles(value: boolean): void {
|
||||
$showAllProfiles.set(value)
|
||||
}
|
||||
|
||||
export function toggleShowAllProfiles(): void {
|
||||
$showAllProfiles.set(!$showAllProfiles.get())
|
||||
}
|
||||
|
||||
// ── Hotkey-driven profile switching ────────────────────────────────────────
|
||||
// Positional + relative navigation for the rail, used by the keybind runtime.
|
||||
// The ordered list is [default, ...named-in-rail-order]; switching is a no-op
|
||||
// when the slot is empty so unused ⌘N keys stay harmless.
|
||||
|
||||
function orderedProfileKeys(): string[] {
|
||||
const profiles = $profiles.get()
|
||||
|
||||
const named = sortByProfileOrder(
|
||||
profiles.filter(profile => !profile.is_default),
|
||||
$profileOrder.get()
|
||||
).map(profile => normalizeProfileKey(profile.name))
|
||||
|
||||
const hasDefault = profiles.some(profile => profile.is_default)
|
||||
|
||||
return hasDefault ? ['default', ...named] : named
|
||||
}
|
||||
|
||||
// Switch to the default (root ~/.hermes) profile — bound to ⌘1.
|
||||
export function switchToDefaultProfile(): void {
|
||||
const def = $profiles.get().find(profile => profile.is_default)
|
||||
|
||||
selectProfile(def ? def.name : 'default')
|
||||
}
|
||||
|
||||
// Switch to the Nth named (non-default) profile in rail order (1-based).
|
||||
export function switchProfileToSlot(slot: number): void {
|
||||
const named = sortByProfileOrder(
|
||||
$profiles.get().filter(profile => !profile.is_default),
|
||||
$profileOrder.get()
|
||||
)
|
||||
|
||||
const target = named[slot - 1]
|
||||
|
||||
if (target) {
|
||||
selectProfile(target.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Step to the next/previous profile in the rail, wrapping around.
|
||||
export function cycleProfile(direction: 1 | -1): void {
|
||||
const keys = orderedProfileKeys()
|
||||
|
||||
if (keys.length < 2) {
|
||||
return
|
||||
}
|
||||
|
||||
const current = $showAllProfiles.get() ? -1 : keys.indexOf(normalizeProfileKey($activeGatewayProfile.get()))
|
||||
const start = current < 0 ? (direction === 1 ? -1 : 0) : current
|
||||
const next = (start + direction + keys.length) % keys.length
|
||||
|
||||
selectProfile(keys[next])
|
||||
}
|
||||
|
||||
// Bumped to ask the rail to open its "create profile" dialog (the dialog state
|
||||
// is local to the rail component; this lets a global hotkey trigger it).
|
||||
export const $profileCreateRequest = atom(0)
|
||||
|
||||
export function requestProfileCreate(): void {
|
||||
$profileCreateRequest.set($profileCreateRequest.get() + 1)
|
||||
}
|
||||
|
||||
// Keepalive ping for the active pool backend so the main-process idle reaper
|
||||
// (which can't see the direct renderer↔backend WS) spares it. No-op for the
|
||||
// primary/default backend, which is never pooled.
|
||||
|
||||
Reference in New Issue
Block a user