mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
fix(shortcuts): share one shortcut label formatter between settings and the session picker
This commit is contained in:
1 parent
18102a3ce8
commit
c41eff304c
5 files changed
+43
-19
No files matched your search
@@ -578,6 +578,17 @@ describe('SessionPickerDialog', () => {
|
|||||||
expect(screen.getByText(/Ctrl\+G sessions/)).toBeInTheDocument()
|
expect(screen.getByText(/Ctrl\+G sessions/)).toBeInTheDocument()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('shows the normalized leader, arrow and key in the footer for a leader switch binding', () => {
|
||||||
|
mocks.preferences = {
|
||||||
|
keyboardShortcuts: { switchPickerTab: 'G' },
|
||||||
|
directShortcuts: [],
|
||||||
|
leaderKey: 'Ctrl+X',
|
||||||
|
}
|
||||||
|
renderPicker()
|
||||||
|
|
||||||
|
expect(screen.getByText(/Ctrl\+X → G projects/)).toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
it('no longer switches tabs on Ctrl+O', () => {
|
it('no longer switches tabs on Ctrl+O', () => {
|
||||||
renderPicker()
|
renderPicker()
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
|
|||||||
import { useShortcutActions } from '@/contexts/KeyboardShortcutsContext'
|
import { useShortcutActions } from '@/contexts/KeyboardShortcutsContext'
|
||||||
import { useSettings } from '@/hooks/useSettings'
|
import { useSettings } from '@/hooks/useSettings'
|
||||||
import { buildPinnedSessionKeys, getSessionKey } from '@/lib/sessionKey'
|
import { buildPinnedSessionKeys, getSessionKey } from '@/lib/sessionKey'
|
||||||
import { normalizeShortcut, resolveShortcutBindings, type ShortcutBindings } from '@/lib/keyboardShortcuts'
|
import { formatShortcutLabel, resolveShortcutBindings } from '@/lib/keyboardShortcuts'
|
||||||
import { cn, formatShortRelativeTime, getRepoDisplayName } from '@/lib/utils'
|
import { cn, formatShortRelativeTime, getRepoDisplayName } from '@/lib/utils'
|
||||||
import { FINE_POINTER_MEDIA_QUERY, useMediaQuery } from '@/hooks/useMediaQuery'
|
import { FINE_POINTER_MEDIA_QUERY, useMediaQuery } from '@/hooks/useMediaQuery'
|
||||||
|
|
||||||
@@ -39,14 +39,6 @@ const NEAR_BOTTOM_PX = 240
|
|||||||
|
|
||||||
type SessionPickerTab = 'sessions' | 'projects'
|
type SessionPickerTab = 'sessions' | 'projects'
|
||||||
|
|
||||||
function switchTabShortcutLabel(bindings: ShortcutBindings): string {
|
|
||||||
const keys = bindings.shortcuts.switchPickerTab
|
|
||||||
if (!keys) return ''
|
|
||||||
const normalized = normalizeShortcut(keys)
|
|
||||||
if (bindings.directShortcuts.includes('switchPickerTab')) return normalized
|
|
||||||
return `${bindings.leaderKey} ${normalized}`
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SessionPickerDialogProps {
|
interface SessionPickerDialogProps {
|
||||||
open: boolean
|
open: boolean
|
||||||
onOpenChange: (open: boolean) => void
|
onOpenChange: (open: boolean) => void
|
||||||
@@ -716,7 +708,14 @@ export function SessionPickerDialog({
|
|||||||
const hasFinePointer = useMediaQuery(FINE_POINTER_MEDIA_QUERY)
|
const hasFinePointer = useMediaQuery(FINE_POINTER_MEDIA_QUERY)
|
||||||
const { preferences } = useSettings()
|
const { preferences } = useSettings()
|
||||||
const switchHint = useMemo(
|
const switchHint = useMemo(
|
||||||
() => switchTabShortcutLabel(resolveShortcutBindings(preferences)),
|
() => {
|
||||||
|
const bindings = resolveShortcutBindings(preferences)
|
||||||
|
return formatShortcutLabel(
|
||||||
|
bindings.shortcuts.switchPickerTab,
|
||||||
|
bindings.directShortcuts.includes('switchPickerTab'),
|
||||||
|
bindings.leaderKey,
|
||||||
|
)
|
||||||
|
},
|
||||||
[preferences],
|
[preferences],
|
||||||
)
|
)
|
||||||
const [tab, setTab] = useState<SessionPickerTab>('sessions')
|
const [tab, setTab] = useState<SessionPickerTab>('sessions')
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ import { useSettings } from '@/hooks/useSettings'
|
|||||||
import { useMobile } from '@/hooks/useMobile'
|
import { useMobile } from '@/hooks/useMobile'
|
||||||
import { Loader2, X } from 'lucide-react'
|
import { Loader2, X } from 'lucide-react'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { formatEventModifiers, formatShortcutEvent, isModifierOnlyEvent, normalizeShortcut, resolveShortcutBindings } from '@/lib/keyboardShortcuts'
|
import { formatEventModifiers, formatShortcutEvent, formatShortcutLabel, isModifierOnlyEvent, normalizeShortcut, resolveShortcutBindings } from '@/lib/keyboardShortcuts'
|
||||||
import { applyTuiKeybindImport, parseTuiKeybindConfig } from '@/lib/tuiKeybindImport'
|
import { applyTuiKeybindImport, parseTuiKeybindConfig } from '@/lib/tuiKeybindImport'
|
||||||
import { showToast } from '@/lib/toast'
|
import { showToast } from '@/lib/toast'
|
||||||
|
|
||||||
const CONVERSATION_ACTIONS = ['submit', 'abort', 'clearPrompt', 'toggleMode', 'undo', 'redo', 'compact', 'fork', 'timeline', 'exportSession', 'selectModel', 'variantCycle', 'favoriteCycle', 'halfPageUp', 'halfPageDown']
|
const CONVERSATION_ACTIONS = ['submit', 'abort', 'clearPrompt', 'toggleMode', 'undo', 'redo', 'compact', 'fork', 'timeline', 'exportSession', 'selectModel', 'variantCycle', 'favoriteCycle', 'halfPageUp', 'halfPageDown']
|
||||||
const NAVIGATION_ACTIONS = ['settings', 'sessions', 'switchPickerTab', 'newSession', 'closeSession', 'toggleSidebar', 'toggleTerminal', 'toggleSourceControl']
|
const NAVIGATION_ACTIONS = ['settings', 'sessions', 'switchPickerTab', 'newSession', 'closeSession', 'toggleSidebar', 'toggleTerminal', 'toggleSourceControl']
|
||||||
|
|
||||||
const formatShortcutLabel = (action: string): string => {
|
const formatActionLabel = (action: string): string => {
|
||||||
return action.replace(/([A-Z])/g, ' $1').trim()
|
return action.replace(/([A-Z])/g, ' $1').trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ function ShortcutRow({ action, keys, isDirect, display, isRecording, currentKeys
|
|||||||
return (
|
return (
|
||||||
<div className="flex flex-wrap items-center justify-between gap-2 py-2.5 border-b border-border last:border-0">
|
<div className="flex flex-wrap items-center justify-between gap-2 py-2.5 border-b border-border last:border-0">
|
||||||
<div className="min-w-0 flex-1 basis-48 space-y-0.5">
|
<div className="min-w-0 flex-1 basis-48 space-y-0.5">
|
||||||
<p className="truncate text-sm font-medium capitalize text-foreground">{formatShortcutLabel(action)}</p>
|
<p className="truncate text-sm font-medium capitalize text-foreground">{formatActionLabel(action)}</p>
|
||||||
<button
|
<button
|
||||||
onClick={() => onToggleDirect(action)}
|
onClick={() => onToggleDirect(action)}
|
||||||
className="text-xs text-muted-foreground hover:text-foreground transition-colors text-left"
|
className="text-xs text-muted-foreground hover:text-foreground transition-colors text-left"
|
||||||
@@ -246,10 +246,7 @@ export function KeyboardShortcuts() {
|
|||||||
|
|
||||||
const formatShortcutDisplay = (action: string, keys: string) => {
|
const formatShortcutDisplay = (action: string, keys: string) => {
|
||||||
if (!keys) return 'Not set'
|
if (!keys) return 'Not set'
|
||||||
if (directShortcuts.includes(action)) {
|
return formatShortcutLabel(keys, directShortcuts.includes(action), leaderKey)
|
||||||
return normalizeShortcut(keys)
|
|
||||||
}
|
|
||||||
return `${normalizeShortcut(leaderKey)} → ${normalizeShortcut(keys)}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const toggleDirectShortcut = (action: string) => {
|
const toggleDirectShortcut = (action: string) => {
|
||||||
@@ -293,7 +290,7 @@ export function KeyboardShortcuts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const description = [
|
const description = [
|
||||||
applied.cleared.length > 0 ? `Unbound to avoid conflicts: ${applied.cleared.map((action) => formatShortcutLabel(action).toLowerCase()).join(', ')}` : undefined,
|
applied.cleared.length > 0 ? `Unbound to avoid conflicts: ${applied.cleared.map((action) => formatActionLabel(action).toLowerCase()).join(', ')}` : undefined,
|
||||||
skippedDescription ? `Skipped: ${skippedDescription}` : undefined,
|
skippedDescription ? `Skipped: ${skippedDescription}` : undefined,
|
||||||
].filter(Boolean).join('. ')
|
].filter(Boolean).join('. ')
|
||||||
showToast.success(
|
showToast.success(
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { DEFAULT_DIRECT_SHORTCUTS, DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@/api/types/settings'
|
import { DEFAULT_DIRECT_SHORTCUTS, DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@/api/types/settings'
|
||||||
import { formatShortcutEvent, getDefaultKeyboardShortcuts, normalizeShortcut, resolveDirectShortcuts, resolveShortcutBindings } from './keyboardShortcuts'
|
import { formatShortcutEvent, formatShortcutLabel, getDefaultKeyboardShortcuts, normalizeShortcut, resolveDirectShortcuts, resolveShortcutBindings } from './keyboardShortcuts'
|
||||||
|
|
||||||
describe('keyboardShortcuts', () => {
|
describe('keyboardShortcuts', () => {
|
||||||
it('normalizes key aliases, modifier order and bare symbol keys', () => {
|
it('normalizes key aliases, modifier order and bare symbol keys', () => {
|
||||||
@@ -11,6 +11,13 @@ describe('keyboardShortcuts', () => {
|
|||||||
expect(normalizeShortcut('+')).toBe('+')
|
expect(normalizeShortcut('+')).toBe('+')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('formats empty, direct and leader bindings as labels', () => {
|
||||||
|
expect(formatShortcutLabel('', true, 'Ctrl+X')).toBe('')
|
||||||
|
expect(formatShortcutLabel('', false, 'Ctrl+X')).toBe('')
|
||||||
|
expect(formatShortcutLabel('Shift+Ctrl+z', true, 'Ctrl+X')).toBe('Ctrl+Shift+Z')
|
||||||
|
expect(formatShortcutLabel('G', false, 'Ctrl+Enter')).toBe('Ctrl+Return → G')
|
||||||
|
})
|
||||||
|
|
||||||
it('reads letters from the physical key when Alt rewrites the character', () => {
|
it('reads letters from the physical key when Alt rewrites the character', () => {
|
||||||
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: '†', code: 'KeyT', altKey: true }))).toBe('Alt+T')
|
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: '†', code: 'KeyT', altKey: true }))).toBe('Alt+T')
|
||||||
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: 'Shift', shiftKey: true }))).toBeNull()
|
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: 'Shift', shiftKey: true }))).toBeNull()
|
||||||
|
|||||||
@@ -75,6 +75,16 @@ export function normalizeShortcut(shortcut: string): string {
|
|||||||
return [...orderedModifiers, mainKey].join('+')
|
return [...orderedModifiers, mainKey].join('+')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a stored binding for display: empty keys render as an empty string, a direct binding as its normalized keys,
|
||||||
|
* and a leader binding as `leader → keys`, with both the leader and the keys normalized.
|
||||||
|
*/
|
||||||
|
export function formatShortcutLabel(keys: string, isDirect: boolean, leaderKey: string): string {
|
||||||
|
if (!keys) return ''
|
||||||
|
if (isDirect) return normalizeShortcut(keys)
|
||||||
|
return `${normalizeShortcut(leaderKey)} → ${normalizeShortcut(keys)}`
|
||||||
|
}
|
||||||
|
|
||||||
export function hasCommandModifier(shortcut: string): boolean {
|
export function hasCommandModifier(shortcut: string): boolean {
|
||||||
return /(^|\+)(Ctrl|Cmd|Alt)\+./.test(shortcut)
|
return /(^|\+)(Ctrl|Cmd|Alt)\+./.test(shortcut)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user