Skip to content

Commit

Permalink
fix: custom shortcuts can use arrow keys (closes #1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Apr 30, 2022
1 parent 254f238 commit bb1de75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/ui/generic-components/CustomRecorderControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/ui/preferences-window/tabs/ControlsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: ""))
Expand All @@ -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) }
Expand Down

0 comments on commit bb1de75

Please sign in to comment.