From bb1de75128e791bc6acb085f09430561045b789f Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Sun, 1 May 2022 00:27:30 +0900 Subject: [PATCH] fix: custom shortcuts can use arrow keys (closes #1376) --- docs/Contributors.md | 2 +- .../CustomRecorderControl.swift | 17 ++++++++++++----- .../preferences-window/tabs/ControlsTab.swift | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/Contributors.md b/docs/Contributors.md index d1817b0c..9330241a 100644 --- a/docs/Contributors.md +++ b/docs/Contributors.md @@ -38,7 +38,7 @@ They helped [localize the app](https://poeditor.com/join/project/8AOEZ0eAZE): * Albert Abdilim * Ali Gokmen * Allen Guan -* Anurag +* Anurag Roy * anushree b * Arthur * Ash diff --git a/src/ui/generic-components/CustomRecorderControl.swift b/src/ui/generic-components/CustomRecorderControl.swift index 87ca959c..571de1c9 100644 --- a/src/ui/generic-components/CustomRecorderControl.swift +++ b/src/ui/generic-components/CustomRecorderControl.swift @@ -49,11 +49,12 @@ class CustomRecorderControl: RecorderControl, RecorderControlDelegate { } func alertIfSameShortcutAlreadyAssigned(_ shortcut: Shortcut, _ shortcutAlreadyAssigned: ATShortcut) { - let existing = ControlsTab.shortcutControls[shortcutAlreadyAssigned.id]! + let isArrowKeys = ["←", "→", "↑", "↓"].contains(shortcutAlreadyAssigned.id) + let existing = ControlsTab.shortcutControls[shortcutAlreadyAssigned.id] let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Conflicting shortcut", comment: "") - alert.informativeText = String(format: NSLocalizedString("Shortcut already assigned to another action: %@", comment: ""), existing.1.replacingOccurrences(of: " ", with: "\u{00A0}")) + alert.informativeText = String(format: NSLocalizedString("Shortcut already assigned to another action: %@", comment: ""), (isArrowKeys ? "Arrow keys" : existing!.1).replacingOccurrences(of: " ", with: "\u{00A0}")) if !id.starts(with: "holdShortcut") { alert.addButton(withTitle: NSLocalizedString("Unassign existing shortcut and continue", comment: "")).setAccessibilityFocused(true) } @@ -64,9 +65,15 @@ class CustomRecorderControl: RecorderControl, RecorderControlDelegate { } let userChoice = alert.runModal() if !id.starts(with: "holdShortcut") && userChoice == .alertFirstButtonReturn { - existing.0.objectValue = nil - ControlsTab.shortcutChangedCallback(existing.0) - LabelAndControl.controlWasChanged(existing.0, shortcutAlreadyAssigned.id) + if isArrowKeys { + ControlsTab.arrowKeysCheckbox.state = .off + ControlsTab.arrowKeysEnabledCallback(ControlsTab.arrowKeysCheckbox) + LabelAndControl.controlWasChanged(ControlsTab.arrowKeysCheckbox, nil) + } else { + existing!.0.objectValue = nil + ControlsTab.shortcutChangedCallback(existing!.0) + LabelAndControl.controlWasChanged(existing!.0, shortcutAlreadyAssigned.id) + } ControlsTab.shortcutControls[id]!.0.objectValue = shortcut ControlsTab.shortcutChangedCallback(self) LabelAndControl.controlWasChanged(self, id) diff --git a/src/ui/preferences-window/tabs/ControlsTab.swift b/src/ui/preferences-window/tabs/ControlsTab.swift index 95f600b8..fe87873e 100644 --- a/src/ui/preferences-window/tabs/ControlsTab.swift +++ b/src/ui/preferences-window/tabs/ControlsTab.swift @@ -21,6 +21,7 @@ class ControlsTab { "quitAppShortcut": { App.app.quitSelectedApp() }, "hideShowAppShortcut": { App.app.hideShowSelectedApp() }, ] + static var arrowKeysCheckbox: NSButton! static func initTab() -> NSView { let focusWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Focus selected window", comment: ""), "focusWindowShortcut", Preferences.focusWindowShortcut, labelPosition: .right) @@ -31,6 +32,7 @@ class ControlsTab { let quitAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Quit app", comment: ""), "quitAppShortcut", Preferences.quitAppShortcut, labelPosition: .right) let hideShowAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Hide/Show app", comment: ""), "hideShowAppShortcut", Preferences.hideShowAppShortcut, labelPosition: .right) let enableArrows = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Arrow keys", comment: ""), "arrowKeysEnabled", extraAction: ControlsTab.arrowKeysEnabledCallback, labelPosition: .right) + arrowKeysCheckbox = enableArrows[0] as! NSButton let enableMouse = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Mouse hover", comment: ""), "mouseHoverEnabled", labelPosition: .right) let enableCursorFollowFocus = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Cursor follows focus", comment: ""), "cursorFollowFocusEnabled", labelPosition: .right) let selectWindowcheckboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Also select windows using:", comment: "")) @@ -43,7 +45,7 @@ class ControlsTab { let (holdShortcut2, nextWindowShortcut2, tab2View) = toShowSection("2") let tabView = TabView([(NSLocalizedString("Shortcut 1", comment: ""), tab1View), (NSLocalizedString("Shortcut 2", comment: ""), tab2View)]) - ControlsTab.arrowKeysEnabledCallback(enableArrows[0] as! NSControl) + ControlsTab.arrowKeysEnabledCallback(arrowKeysCheckbox) // trigger shortcutChanged for these shortcuts to trigger .restrictModifiers [holdShortcut, holdShortcut2].forEach { ControlsTab.shortcutChangedCallback($0[1] as! NSControl) } [nextWindowShortcut, nextWindowShortcut2].forEach { ControlsTab.shortcutChangedCallback($0[0] as! NSControl) }