Skip to content

Commit

Permalink
fix: [after release: do nothing] was broken in v7.6.0 (closes #3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Dec 3, 2024
1 parent db305db commit 83b5319
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/logic/ATShortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ATShortcut {
// Another issue is events being dropped by macOS, which we never receive
// Knowing this, we handle these edge-cases by double checking if holdShortcut is UP, when any shortcut state is UP
// If it is, then we trigger the holdShortcut action
if App.app.appIsBeingUsed {
if App.app.appIsBeingUsed && Preferences.shortcutStyle[App.app.shortcutIndex] == .focusOnRelease {
if let currentHoldShortcut = ControlsTab.shortcuts[Preferences.indexToName("holdShortcut", App.app.shortcutIndex)],
id == currentHoldShortcut.id {
let currentModifiers = cocoaToCarbonFlags(ModifierFlags.current)
Expand Down
37 changes: 37 additions & 0 deletions unit-tests/KeyboardEventsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,46 @@ final class KeyboardEventsUtilsTests: XCTestCase {
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut", "closeWindowShortcut", "holdShortcut"])
}

func testOnReleaseDoNothing() throws {
resetState()
Preferences.shortcutStyle[0] = .doNothingOnRelease
ModifierFlags.current = [.option]
handleKeyboardEvent(nil, nil, nil, [.option], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, [])
handleKeyboardEvent(KeyboardEventsTestable.globalShortcutsIds["nextWindowShortcut"], .down, nil, nil, false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut"])
handleKeyboardEvent(KeyboardEventsTestable.globalShortcutsIds["nextWindowShortcut"], .up, nil, nil, false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut"])
ModifierFlags.current = []
handleKeyboardEvent(nil, nil, nil, [], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut"])
}

// alt-down > tab-down > tab-up > `-down > `-up
func testTransitionFromOneShortcutToAnother() throws {
resetState()
ModifierFlags.current = [.option]
handleKeyboardEvent(nil, nil, nil, [.option], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, [])
handleKeyboardEvent(KeyboardEventsTestable.globalShortcutsIds["nextWindowShortcut"], .down, nil, nil, false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut"])
handleKeyboardEvent(KeyboardEventsTestable.globalShortcutsIds["nextWindowShortcut"], .up, nil, nil, false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut"])
handleKeyboardEvent(nil, nil, keycodeMap["`"], [.option], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut", "nextWindowShortcut2"])
handleKeyboardEvent(nil, nil, nil, [.option], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut", "nextWindowShortcut2"])
ModifierFlags.current = []
handleKeyboardEvent(nil, nil, nil, [], false)
XCTAssertEqual(ControlsTab.shortcutsActionsTriggered, ["nextWindowShortcut", "nextWindowShortcut2", "holdShortcut2"])
}

private func resetState() {
App.app.appIsBeingUsed = false
App.app.shortcutIndex = 0
for i in 0..<Preferences.shortcutStyle.count {
Preferences.shortcutStyle[i] = .focusOnRelease
}
ControlsTab.shortcuts.values.forEach { $0.state = .up }
ControlsTab.shortcutsActionsTriggered = []
}
Expand Down
6 changes: 6 additions & 0 deletions unit-tests/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ControlsTab {
}
if action.starts(with: "nextWindowShortcut") {
App.app.appIsBeingUsed = true
App.app.shortcutIndex = Preferences.nameToIndex(action)
}
}

Expand All @@ -66,6 +67,11 @@ class Preferences {
static func indexToName(_ baseName: String, _ index: Int) -> String {
return baseName + (index == 0 ? "" : String(index + 1))
}

static func nameToIndex(_ name: String) -> Int {
guard let number = name.last?.wholeNumberValue else { return 0 }
return number - 1
}
}

enum ShortcutStylePreference: CaseIterable {
Expand Down

0 comments on commit 83b5319

Please sign in to comment.