|
| 1 | +/** |
| 2 | + * Keyboard shortcut handling for file lists |
| 3 | + * |
| 4 | + * Provides centralized logic for handling keyboard navigation shortcuts |
| 5 | + * across different view modes (Brief and Full). |
| 6 | + */ |
| 7 | + |
| 8 | +export interface NavigationResult { |
| 9 | + /** The new index to select */ |
| 10 | + newIndex: number |
| 11 | + /** Whether the event was handled */ |
| 12 | + handled: boolean |
| 13 | +} |
| 14 | + |
| 15 | +export interface NavigationContext { |
| 16 | + currentIndex: number |
| 17 | + totalCount: number |
| 18 | + /** For Brief mode: items per column */ |
| 19 | + itemsPerColumn?: number |
| 20 | + /** For Brief mode: number of visible columns (for PageUp/PageDown) */ |
| 21 | + visibleColumns?: number |
| 22 | + /** For Full mode: number of visible items (for PageUp/PageDown) */ |
| 23 | + visibleItems?: number |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Handles keyboard navigation shortcuts for file lists. |
| 28 | + * Returns the new index and whether the event was handled. |
| 29 | + */ |
| 30 | +export function handleNavigationShortcut(event: KeyboardEvent, context: NavigationContext): NavigationResult | null { |
| 31 | + const { currentIndex, totalCount, itemsPerColumn, visibleColumns, visibleItems } = context |
| 32 | + |
| 33 | + // Home/End shortcuts (both Option+Arrow and Fn+Arrow) |
| 34 | + // Option+Up or Fn+Left = Home (go to first item) |
| 35 | + if ((event.altKey && event.key === 'ArrowUp') || (event.key === 'Home' && !event.metaKey)) { |
| 36 | + return { newIndex: 0, handled: true } |
| 37 | + } |
| 38 | + |
| 39 | + // Option+Down or Fn+Right = End (go to last item) |
| 40 | + if ((event.altKey && event.key === 'ArrowDown') || (event.key === 'End' && !event.metaKey)) { |
| 41 | + return { newIndex: Math.max(0, totalCount - 1), handled: true } |
| 42 | + } |
| 43 | + |
| 44 | + // Page Up/Down shortcuts (Fn+Up/Down) |
| 45 | + // In Brief mode: move horizontally by (visibleColumns - 1) and go to bottommost item |
| 46 | + // if near edge, go to first/last item instead |
| 47 | + // In Full mode: move vertically by (visibleItems - 1) |
| 48 | + if (event.key === 'PageUp') { |
| 49 | + if (visibleColumns !== undefined && itemsPerColumn !== undefined) { |
| 50 | + // Brief mode: horizontal page navigation |
| 51 | + const columnsToMove = Math.max(1, visibleColumns - 1) |
| 52 | + const currentColumn = Math.floor(currentIndex / itemsPerColumn) |
| 53 | + const targetColumn = currentColumn - columnsToMove |
| 54 | + |
| 55 | + // If we'd go to or past the leftmost column, jump to first item |
| 56 | + if (targetColumn <= 0) { |
| 57 | + return { newIndex: 0, handled: true } |
| 58 | + } |
| 59 | + |
| 60 | + // Otherwise, go to the bottommost item in the target column |
| 61 | + const targetColumnStart = targetColumn * itemsPerColumn |
| 62 | + const targetColumnEnd = Math.min(totalCount - 1, targetColumnStart + itemsPerColumn - 1) |
| 63 | + return { newIndex: targetColumnEnd, handled: true } |
| 64 | + } else { |
| 65 | + // Full mode: vertical page navigation by (visible items - 1) |
| 66 | + const pageSize = visibleItems ? Math.max(1, visibleItems - 1) : 20 |
| 67 | + const newIndex = Math.max(0, currentIndex - pageSize) |
| 68 | + return { newIndex, handled: true } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (event.key === 'PageDown') { |
| 73 | + if (visibleColumns !== undefined && itemsPerColumn !== undefined) { |
| 74 | + // Brief mode: horizontal page navigation |
| 75 | + const columnsToMove = Math.max(1, visibleColumns - 1) |
| 76 | + const currentColumn = Math.floor(currentIndex / itemsPerColumn) |
| 77 | + const totalColumns = Math.ceil(totalCount / itemsPerColumn) |
| 78 | + const targetColumn = currentColumn + columnsToMove |
| 79 | + |
| 80 | + // If we'd go to or past the rightmost column, jump to last item |
| 81 | + if (targetColumn >= totalColumns - 1) { |
| 82 | + return { newIndex: totalCount - 1, handled: true } |
| 83 | + } |
| 84 | + |
| 85 | + // Otherwise, go to the bottommost item in the target column |
| 86 | + const targetColumnStart = targetColumn * itemsPerColumn |
| 87 | + const targetColumnEnd = Math.min(totalCount - 1, targetColumnStart + itemsPerColumn - 1) |
| 88 | + return { newIndex: targetColumnEnd, handled: true } |
| 89 | + } else { |
| 90 | + // Full mode: vertical page navigation by (visible items - 1) |
| 91 | + const pageSize = visibleItems ? Math.max(1, visibleItems - 1) : 20 |
| 92 | + const newIndex = Math.min(totalCount - 1, currentIndex + pageSize) |
| 93 | + return { newIndex, handled: true } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Not a handled shortcut |
| 98 | + return null |
| 99 | +} |
0 commit comments