import Foundation import HelderCore /// A key press, reduced to what the router matches on. public struct KeyPress: Equatable, Sendable { public enum Key: Equatable, Sendable { /// Lowercased, as typed without modifiers; `?` arrives as itself. case character(String) case up, down, left, right case enter case escape case other } public var key: Key public var command: Bool public var shift: Bool public var option: Bool public var control: Bool public init(_ key: Key, command: Bool = false, shift: Bool = false, option: Bool = false, control: Bool = false) { self.key = key self.command = command self.shift = shift self.option = option self.control = control } static func cmd(_ key: Key, shift: Bool = false) -> KeyPress { KeyPress(key, command: true, shift: shift) } } /// Who holds the keyboard when the key arrives. public enum KeyboardOwner: Equatable, Sendable { /// No text input: the workspace itself. case workspace /// A text field or text view, and the column it sits in. case text(in: Panel?) /// A terminal pane. case terminal } /// The state a routing decision depends on; built from the window and the model at key time. public struct KeyContext: Equatable, Sendable { public var hasProject: Bool public var overlay: OverlayKind public var owner: KeyboardOwner /// The focused text view or terminal has a non-empty selection. public var hasTextSelection: Bool /// The focused text view's selection, when it sits in the editor column. public var editorTextSelection: TextSelectionInfo? /// The editor's read-only panes hold a multi-line selection. public var hasEditorLineSelection: Bool public var activePanel: Panel? public var focusZone: FocusZone public var recentFileCount: Int /// The seed ⌘F uses: the first line of the current selection. public var searchSeed: String public init(hasProject: Bool = true, overlay: OverlayKind = .none, owner: KeyboardOwner = .workspace, hasTextSelection: Bool = false, editorTextSelection: TextSelectionInfo? = nil, hasEditorLineSelection: Bool = false, activePanel: Panel? = nil, focusZone: FocusZone = .neutral, recentFileCount: Int = 0, searchSeed: String = "") { self.hasProject = hasProject self.overlay = overlay self.owner = owner self.hasTextSelection = hasTextSelection self.editorTextSelection = editorTextSelection self.hasEditorLineSelection = hasEditorLineSelection self.activePanel = activePanel self.focusZone = focusZone self.recentFileCount = recentFileCount self.searchSeed = searchSeed } } /// The overlay kinds the router tells apart. public enum OverlayKind: Equatable, Sendable { case none case note /// The full-screen Diff: the workspace shortcuts keep working over it. `busy` while its own /// menu or pass-on prompt is open; those then own every key. case diff(busy: Bool) /// Any other overlay: it owns every key but Esc. case other public init(_ overlay: Overlay, diffBusy: Bool = false) { switch overlay { case .none: self = .none case .note: self = .note case .diff: self = .diff(busy: diffBusy) default: self = .other } } } public enum KeyRoute: Equatable { /// The key belongs to someone else: a text field, a terminal, an overlay's own handler, or a menu. case pass case perform(Command) } /// Maps a key press to a command. A key that a focused field or terminal needs is never taken: /// arrows, ↵, `?`, ⌘A and ⌘C/⌘←/⌘→ go to the field; ⌘O, ⌘R and ⇧⌘N are left to the menu bar. public enum KeyRouter { public static func route(_ press: KeyPress, _ context: KeyContext) -> KeyRoute { if press.control || press.option { return .pass } if !context.hasProject { return launcherKey(press) } switch context.overlay { case .diff(busy: true): return .pass case .none: if press.key == .escape { return .pass } default: if press.key == .escape && !press.command { return .perform(.dismissOverlay) } } switch context.overlay { case .none: break case .diff: if press == .cmd(.character("a")) { return .pass } case .note: return press == .cmd(.character("p")) ? .perform(.passNote) : .pass case .other: return .pass } return press.command ? commandKey(press, context) : plainKey(press, context) } private static func launcherKey(_ press: KeyPress) -> KeyRoute { guard !press.shift else { return .pass } switch press.key { case .up: return .perform(.launcherMove(-1)) case .down: return .perform(.launcherMove(1)) case .enter where !press.command: return .perform(.launcherActivate) default: return .pass } } private static func plainKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute { let inList = context.owner == .workspace && (context.activePanel == .sourceControl || context.activePanel == .explorer) switch press.key { case .up where inList && !press.shift: return .perform(.moveRowCursor(-1)) case .down where inList && !press.shift: return .perform(.moveRowCursor(1)) case .enter where inList && !press.shift: return .perform(.openSelectedRow) case .character("?") where context.owner == .workspace: return .perform(.showShortcuts) default: return .pass } } private static func commandKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute { let owner = context.owner if press.shift { return press.key == .character("o") ? .perform(.showRecentProjects) : .pass } switch press.key { case .up, .down: guard context.focusZone == .editor, context.recentFileCount > 0 else { return .pass } return .perform(.showRecentFiles(initialSelection: ListCursor.recentFilesStart(movingDown: press.key == .down, count: context.recentFileCount))) case .right: if owner == .workspace, context.activePanel == .sourceControl || context.activePanel == .explorer { return .perform(.openRowMenu) } if let selection = context.editorTextSelection { return .perform(.passSelection(selection)) } if context.hasEditorLineSelection && owner != .terminal { return .perform(.passSelection(nil)) } return owner == .workspace ? .perform(.focusNeighbour(step: 1)) : .pass case .left: return owner == .workspace ? .perform(.focusNeighbour(step: -1)) : .pass case .enter: return owner == .text(in: .sourceControl) ? .pass : .perform(.commit) case let .character(c): return commandCharacter(c, context) default: return .pass } } private static func commandCharacter(_ c: String, _ context: KeyContext) -> KeyRoute { switch c { case "f": return .perform(.showSearch(seed: context.searchSeed)) case "p": return .perform(.push) case "n": return .perform(.showNote) case "s": return .perform(.save) case "m": return .perform(.cycleMode) case "w": return .perform(.closeFileOrWindow) case "g": return .perform(.toggleGit) case "l": return .perform(.toggleAutoFit) case ".": return .perform(.toggleHidden) case "a": return context.owner == .workspace ? .perform(.selectAllInFile) : .pass case "c": if context.owner == .terminal || context.hasTextSelection || context.hasEditorLineSelection { return .pass } return .perform(.focusCommitMessage) default: return .pass } } }