adds nice git interface
This commit is contained in:
@@ -55,11 +55,15 @@ public struct KeyContext: Equatable, Sendable {
|
||||
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
|
||||
|
||||
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 = "") {
|
||||
recentFileCount: Int = 0, searchSeed: String = "", gitViewOpen: Bool = false, inGitConsole: Bool = false) {
|
||||
self.hasProject = hasProject
|
||||
self.overlay = overlay
|
||||
self.owner = owner
|
||||
@@ -70,6 +74,8 @@ public struct KeyContext: Equatable, Sendable {
|
||||
self.focusZone = focusZone
|
||||
self.recentFileCount = recentFileCount
|
||||
self.searchSeed = searchSeed
|
||||
self.gitViewOpen = gitViewOpen
|
||||
self.inGitConsole = inGitConsole
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +105,7 @@ public enum KeyRoute: Equatable {
|
||||
|
||||
/// 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 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 }
|
||||
@@ -107,7 +114,10 @@ public enum KeyRouter {
|
||||
|
||||
switch context.overlay {
|
||||
case .diff(busy: true): return .pass
|
||||
case .none: if press.key == .escape { return .pass }
|
||||
case .none:
|
||||
if press.key == .escape {
|
||||
return context.gitViewOpen && !press.command ? .perform(.closeGitView) : .pass
|
||||
}
|
||||
default: if press.key == .escape && !press.command { return .perform(.dismissOverlay) }
|
||||
}
|
||||
switch context.overlay {
|
||||
@@ -117,9 +127,24 @@ public enum KeyRouter {
|
||||
case .other: return .pass
|
||||
}
|
||||
|
||||
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 launcherKey(_ press: KeyPress) -> KeyRoute {
|
||||
guard !press.shift else { return .pass }
|
||||
switch press.key {
|
||||
@@ -131,7 +156,7 @@ public enum KeyRouter {
|
||||
}
|
||||
|
||||
private static func plainKey(_ press: KeyPress, _ context: KeyContext) -> KeyRoute {
|
||||
let inList = context.owner == .workspace && (context.activePanel == .sourceControl || context.activePanel == .explorer)
|
||||
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))
|
||||
@@ -152,16 +177,12 @@ public enum KeyRouter {
|
||||
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 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 .enter:
|
||||
return owner == .text(in: .sourceControl) ? .pass : .perform(.commit)
|
||||
case let .character(c):
|
||||
return commandCharacter(c, context)
|
||||
default:
|
||||
@@ -177,6 +198,9 @@ public enum KeyRouter {
|
||||
case "m": return .perform(.cycleMode)
|
||||
case "w": return .perform(.closeFileOrWindow)
|
||||
case "g": return .perform(.toggleGit)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user