refactor: centralize shortcut recording lifetime

This commit is contained in:
Valdemar Lorenzen
2026-10-16 10:23:20 +02:00
parent 04e505b846
commit 6725be8ad5
7 changed files with 199 additions and 30 deletions
+8 -18
View File
@@ -249,6 +249,7 @@ final class SettingsWindowController: NSWindowController, NSWindowDelegate {
private func refresh() {
refreshLogin()
recorder.isRecording = shortcuts.isRecording
commandSpaceButton.isHidden = destinationSelector.indexOfSelectedItem != 0
recorder.setAccessibilityLabel("Record \(GlobalShortcuts.destinations[destinationSelector.indexOfSelectedItem].title) global shortcut")
let name = shortcut.shortcut?.displayName
@@ -266,10 +267,9 @@ final class SettingsWindowController: NSWindowController, NSWindowDelegate {
}
@objc private func beginRecording() {
guard !recorder.isRecording else { return }
guard !shortcuts.isRecording else { return }
errorLabel.stringValue = ""
shortcuts.suspend()
recorder.isRecording = true
shortcuts.beginRecording()
refresh()
if window?.makeFirstResponder(recorder) != true {
cancelRecording()
@@ -283,9 +283,8 @@ final class SettingsWindowController: NSWindowController, NSWindowDelegate {
return
}
guard let candidate = KeyboardShortcut(keyCode: event.keyCode, modifiers: event.modifierFlags) else {
recorder.isRecording = false
errorLabel.stringValue = "Choose a supported key with Command, Option, or Control. A modifier by itself is not a shortcut."
restoreSavedShortcuts()
appendShortcutErrors(shortcuts.cancelRecording())
refresh()
return
}
@@ -293,28 +292,19 @@ final class SettingsWindowController: NSWindowController, NSWindowDelegate {
}
private func apply(_ candidate: KeyboardShortcut?) {
let wasRecording = recorder.isRecording
recorder.isRecording = false
errorLabel.stringValue = ""
do {
try shortcuts.setShortcut(candidate, at: destinationSelector.indexOfSelectedItem)
} catch {
errorLabel.stringValue = error.localizedDescription
}
if wasRecording { restoreSavedShortcuts() }
appendShortcutErrors(shortcuts.finishRecording(candidate, at: destinationSelector.indexOfSelectedItem))
refresh()
}
private func cancelRecording() {
guard recorder.isRecording else { return }
recorder.isRecording = false
guard shortcuts.isRecording else { return }
errorLabel.stringValue = ""
restoreSavedShortcuts()
appendShortcutErrors(shortcuts.cancelRecording())
refresh()
}
private func restoreSavedShortcuts() {
let errors = shortcuts.start()
private func appendShortcutErrors(_ errors: [String]) {
if !errors.isEmpty {
let message = errors.joined(separator: "\n")
errorLabel.stringValue = errorLabel.stringValue.isEmpty ? message : "\(errorLabel.stringValue)\n\(message)"