Files

265 lines
12 KiB
Swift

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
/// The full-screen Git view covers the columns, so their list and focus keys rest.
public var gitViewOpen: Bool
/// The keyboard sits in the Git view's console input, where ⌘↵ must not commit.
public var inGitConsole: Bool
/// The project is a Laravel app, so ⌘D has a Schema view to toggle.
public var schemaAvailable: Bool
/// The full-screen Schema view covers the columns, like the Git view.
public var schemaViewOpen: Bool
/// A table is selected in the Schema view; Esc clears it first.
public var schemaHasSelection: Bool
/// The keyboard sits in the Schema table search and it holds text; Esc clears it before closing the view.
public var schemaSearchHasText: Bool
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 = "", gitViewOpen: Bool = false, inGitConsole: Bool = false,
schemaAvailable: Bool = false, schemaViewOpen: Bool = false, schemaHasSelection: Bool = false,
schemaSearchHasText: Bool = false) {
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
self.gitViewOpen = gitViewOpen
self.inGitConsole = inGitConsole
self.schemaAvailable = schemaAvailable
self.schemaViewOpen = schemaViewOpen
self.schemaHasSelection = schemaHasSelection
self.schemaSearchHasText = schemaSearchHasText
}
}
/// The overlay kinds the router tells apart.
public enum OverlayKind: Equatable, Sendable {
case none
/// 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)
/// The search: it owns every key but Esc, ⌘G and ⌘D, so it can switch to the Git or Schema view.
case search
/// 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 .diff: self = .diff(busy: diffBusy)
case .search: self = .search
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.
/// Esc closes the topmost layer: an overlay, then the Schema selection, search and view, then the Git view,
/// else it reaches the terminal.
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 {
guard !press.command else { return .pass }
if context.schemaViewOpen { return schemaEscape(context) }
return context.gitViewOpen ? .perform(.closeGitView) : .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 .search: return searchKey(press, context)
case .other: return .pass
}
if context.schemaViewOpen { return schemaViewKey(press, context) }
if context.gitViewOpen { return gitViewKey(press, context) }
return press.command ? commandKey(press, context) : plainKey(press, context)
}
/// Over the Git view the columns' row, focus and editor keys rest; ⌘↵ commits, even from the message field.
private static func gitViewKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute {
guard press.command else {
return press.key == .character("?") && context.owner == .workspace ? .perform(.showShortcuts) : .pass
}
if press.shift { return commandKey(press, context) }
switch press.key {
case .enter: return context.inGitConsole ? .pass : .perform(.commit)
case .character("a"): return .pass
case let .character(c): return commandCharacter(c, context)
default: return .pass
}
}
private static func searchKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute {
guard press.command, !press.shift else { return .pass }
switch press.key {
case .character("g"): return .perform(.toggleGit)
case .character("d"): return context.schemaAvailable ? .perform(.toggleSchemaView) : .pass
default: return .pass
}
}
private static func schemaEscape(_ context: KeyContext) -> KeyRoute {
if context.schemaHasSelection { return .perform(.clearSchemaSelection) }
if context.schemaSearchHasText { return .perform(.clearSchemaSearch) }
return .perform(.closeSchemaView)
}
/// Over the Schema view the columns' keys rest as over the Git view; ⌘↵ and ⌘C have nothing to act on.
private static func schemaViewKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute {
guard press.command else {
return press.key == .character("?") && context.owner == .workspace ? .perform(.showShortcuts) : .pass
}
if press.shift { return commandKey(press, context) }
switch press.key {
case .character("a"), .character("c"): return .pass
case let .character(c): return commandCharacter(c, context)
default: return .pass
}
}
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 == .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 == .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 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 "s": return .perform(.save)
case "m": return .perform(.cycleMode)
case "w": return .perform(.closeFileOrWindow)
case "g": return .perform(.toggleGit)
case "d": return context.schemaAvailable ? .perform(.toggleSchemaView) : .pass
case "1": return .perform(.togglePane(.explorer))
case "2": return .perform(.togglePane(.editor))
case "3": return .perform(.togglePane(.terminal))
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
}
}
}