Repository Public
1import SwiftUI23/// User-configurable shortcuts. Defaults live here; overrides are stored in `AppSettings.keybindings`4/// as strings such as "cmd+shift+t" and applied to the menu at launch (menus are built once by SwiftUI).5struct KeyBindingDefinition: Identifiable {6 let id: String7 let title: String8 let `default`: String9}1011enum KeyBindings {12 static let definitions: [KeyBindingDefinition] = [13 .init(id: "window.new", title: "New Window", default: "cmd+n"),14 .init(id: "tab.new", title: "New Tab", default: "cmd+t"),15 .init(id: "pane.close", title: "Close Pane", default: "cmd+w"),16 .init(id: "tab.close", title: "Close Tab", default: "cmd+shift+w"),17 .init(id: "split.right", title: "Split Right", default: "cmd+d"),18 .init(id: "split.down", title: "Split Down", default: "cmd+shift+d"),19 .init(id: "term.clear", title: "Clear Screen and Scrollback", default: "cmd+k"),20 .init(id: "term.find", title: "Find", default: "cmd+f"),21 .init(id: "pane.next", title: "Focus Next Pane", default: "cmd+]"),22 .init(id: "pane.previous", title: "Focus Previous Pane", default: "cmd+["),23 .init(id: "pane.maximize", title: "Maximize / Restore Pane", default: "cmd+shift+return"),24 .init(id: "pane.newwindow", title: "Move Pane to New Window", default: "cmd+option+n"),25 .init(id: "claude.new", title: "New Claude Session in Tab", default: "cmd+option+c"),26 .init(id: "palette", title: "Command Palette", default: "cmd+shift+p"),27 .init(id: "view.sidebar", title: "Show/Hide Sidebar", default: "cmd+control+s"),28 .init(id: "view.inspector", title: "Show/Hide Inspector", default: "cmd+control+i"),29 .init(id: "view.git", title: "Show Git Panel", default: "cmd+control+g"),30 .init(id: "view.focus", title: "Focus Mode", default: "cmd+control+f"),31 ]3233 /// Parses "cmd+shift+t", "ctrl+cmd+f", "cmd+return", "cmd+]" … Returns nil for unparsable strings.34 static func parse(_ text: String) -> (KeyEquivalent, EventModifiers)? {35 let parts = text.lowercased().split(separator: "+").map { $0.trimmingCharacters(in: .whitespaces) }36 guard let keyText = parts.last, !keyText.isEmpty else { return nil }37 var modifiers: EventModifiers = []38 for p in parts.dropLast() {39 switch p {40 case "cmd", "command", "⌘": modifiers.insert(.command)41 case "shift", "⇧": modifiers.insert(.shift)42 case "option", "opt", "alt", "⌥": modifiers.insert(.option)43 case "ctrl", "control", "⌃": modifiers.insert(.control)44 default: return nil45 }46 }47 let key: KeyEquivalent48 switch keyText {49 case "return", "enter": key = .return50 case "escape", "esc": key = .escape51 case "space": key = .space52 case "tab": key = .tab53 case "delete", "backspace": key = .delete54 case "up": key = .upArrow55 case "down": key = .downArrow56 case "left": key = .leftArrow57 case "right": key = .rightArrow58 default:59 guard keyText.count == 1, let c = keyText.first else { return nil }60 key = KeyEquivalent(c)61 }62 // All NEXTERM shortcuts must carry ⌘ so they never reach the shell.63 guard modifiers.contains(.command) else { return nil }64 return (key, modifiers)65 }6667 /// Effective shortcut for an action: user override when valid, else the default.68 static func shortcut(_ id: String, overrides: [String: String]) -> KeyboardShortcut? {69 guard let def = definitions.first(where: { $0.id == id }) else { return nil }70 if let custom = overrides[id], let parsed = parse(custom) { return KeyboardShortcut(parsed.0, modifiers: parsed.1) }71 guard let parsed = parse(def.default) else { return nil }72 return KeyboardShortcut(parsed.0, modifiers: parsed.1)73 }7475 /// Finds duplicate assignments (two actions bound to the same key) for the settings UI.76 static func conflicts(overrides: [String: String]) -> [String] {77 var seen: [String: String] = [:]78 var dupes: [String] = []79 for def in definitions {80 let text = (overrides[def.id].flatMap { parse($0) != nil ? $0 : nil } ?? def.default).lowercased()81 if let other = seen[text] { dupes.append("\(def.title) and \(other) both use \(text)") } else { seen[text] = def.title }82 }83 return dupes84 }85}8687struct KeybindingSettings: View {88 @Binding var settings: AppSettings8990 var body: some View {91 Form {92 Text("Shortcuts must include ⌘ so they never collide with shell or TUI keys. Changes apply to the menus after the app is relaunched.")93 .font(.system(size: 11)).foregroundStyle(.secondary)94 ForEach(KeyBindings.definitions) { def in95 HStack {96 Text(def.title).frame(width: 220, alignment: .leading)97 TextField(def.default, text: Binding(98 get: { settings.keybindings[def.id] ?? "" },99 set: { settings.keybindings[def.id] = $0.isEmpty ? nil : $0 }))100 .textFieldStyle(.roundedBorder).frame(width: 160)101 if let custom = settings.keybindings[def.id], KeyBindings.parse(custom) == nil {102 Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(.orange).help("Not a valid ⌘ shortcut; default is used")103 .accessibilityLabel("invalid shortcut")104 }105 }106 }107 let conflicts = KeyBindings.conflicts(overrides: settings.keybindings)108 if !conflicts.isEmpty {109 ForEach(conflicts, id: \.self) { Text("Conflict: \($0)").font(.system(size: 11)).foregroundStyle(.orange) }110 }111 Button("Reset to Defaults") { settings.keybindings = [:] }112 }113 .padding()114 }115}116