-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add global hotkeys for toggling MiddleDrag and menu bar visibility #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0618aa
Add global hotkeys for toggling MiddleDrag and menu bar visibility
NullPointerDepressiveDisorder e211237
Implement hotkey customization and recording functionality
NullPointerDepressiveDisorder 5c6fb73
Add applicationShouldHandleReopen method and update menu item text
NullPointerDepressiveDisorder d0b2b66
Implement cleanup for hotkey recording and improve alert handling
NullPointerDepressiveDisorder faaefbc
Refactor multitouch and hotkey handling for improved safety and funct…
NullPointerDepressiveDisorder 263a4d2
Refactor menu bar visibility handling and enhance hotkey tests
NullPointerDepressiveDisorder e2ef448
Enhance MenuBarController to skip button click during tests
NullPointerDepressiveDisorder d3f3865
Refactor MenuBarController hotkey handling for improved safety
NullPointerDepressiveDisorder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
MiddleDrag/MiddleDragTests/GlobalHotKeyManagerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import XCTest | ||
| import Carbon.HIToolbox | ||
|
|
||
| @testable import MiddleDragCore | ||
|
|
||
| @MainActor @unsafe final class GlobalHotKeyManagerTests: XCTestCase { | ||
|
|
||
| // MARK: - carbonModifiers Tests | ||
|
|
||
| func testCarbonModifiersCommand() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: .command) | ||
| XCTAssertEqual(result, UInt32(cmdKey)) | ||
| } | ||
|
|
||
| func testCarbonModifiersOption() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: .option) | ||
| XCTAssertEqual(result, UInt32(optionKey)) | ||
| } | ||
|
|
||
| func testCarbonModifiersShift() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: .shift) | ||
| XCTAssertEqual(result, UInt32(shiftKey)) | ||
| } | ||
|
|
||
| func testCarbonModifiersControl() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: .control) | ||
| XCTAssertEqual(result, UInt32(controlKey)) | ||
| } | ||
|
|
||
| func testCarbonModifiersCombined() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: [.command, .shift]) | ||
| XCTAssertEqual(result, UInt32(cmdKey) | UInt32(shiftKey)) | ||
| } | ||
|
|
||
| func testCarbonModifiersAllFour() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: [.command, .option, .shift, .control]) | ||
| let expected = UInt32(cmdKey) | UInt32(optionKey) | UInt32(shiftKey) | UInt32(controlKey) | ||
| XCTAssertEqual(result, expected) | ||
| } | ||
|
|
||
| func testCarbonModifiersEmpty() { | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: []) | ||
| XCTAssertEqual(result, 0) | ||
| } | ||
|
|
||
| func testCarbonModifiersIgnoresNonModifierFlags() { | ||
| // .capsLock is not mapped by our utility - should not appear in result | ||
| let result = GlobalHotKeyManager.carbonModifiers(from: .capsLock) | ||
| XCTAssertEqual(result, 0) | ||
| } | ||
|
|
||
| // MARK: - Singleton Tests | ||
|
|
||
| func testSharedInstanceIsSingleton() { | ||
| let a = GlobalHotKeyManager.shared | ||
| let b = GlobalHotKeyManager.shared | ||
| XCTAssertTrue(a === b) | ||
| } | ||
|
|
||
| // MARK: - Register / Unregister Tests | ||
|
|
||
| func testRegisterReturnsNonZeroID() { | ||
| // Carbon RegisterEventHotKey may fail in CI (no window server), | ||
| // but the method itself should not crash | ||
| let id = GlobalHotKeyManager.shared.register( | ||
| keyCode: UInt32(kVK_ANSI_F), | ||
| modifiers: UInt32(cmdKey) | UInt32(shiftKey) | UInt32(optionKey) | ||
| ) {} | ||
|
|
||
| // Clean up regardless of success | ||
| if id != 0 { | ||
| GlobalHotKeyManager.shared.unregister(id: id) | ||
| } | ||
| } | ||
|
|
||
| func testRegisterMultipleHotkeysReturnsDifferentIDs() { | ||
| let id1 = GlobalHotKeyManager.shared.register( | ||
| keyCode: UInt32(kVK_ANSI_J), | ||
| modifiers: UInt32(cmdKey) | UInt32(shiftKey) | UInt32(optionKey) | ||
| ) {} | ||
| let id2 = GlobalHotKeyManager.shared.register( | ||
| keyCode: UInt32(kVK_ANSI_K), | ||
| modifiers: UInt32(cmdKey) | UInt32(shiftKey) | UInt32(optionKey) | ||
| ) {} | ||
|
|
||
| // If both succeeded, IDs should differ | ||
| if id1 != 0 && id2 != 0 { | ||
| XCTAssertNotEqual(id1, id2) | ||
| } | ||
|
|
||
| // Clean up | ||
| if id1 != 0 { GlobalHotKeyManager.shared.unregister(id: id1) } | ||
| if id2 != 0 { GlobalHotKeyManager.shared.unregister(id: id2) } | ||
| } | ||
|
|
||
| func testUnregisterInvalidIDDoesNotCrash() { | ||
| // Unregistering an ID that was never registered should be safe | ||
| XCTAssertNoThrow(GlobalHotKeyManager.shared.unregister(id: 99999)) | ||
| } | ||
|
|
||
| func testUnregisterZeroIDDoesNotCrash() { | ||
| // 0 is the failure return value from register - unregistering it should be safe | ||
| XCTAssertNoThrow(GlobalHotKeyManager.shared.unregister(id: 0)) | ||
| } | ||
|
|
||
| func testDoubleUnregisterDoesNotCrash() { | ||
| let id = GlobalHotKeyManager.shared.register( | ||
| keyCode: UInt32(kVK_ANSI_L), | ||
| modifiers: UInt32(cmdKey) | UInt32(shiftKey) | UInt32(optionKey) | ||
| ) {} | ||
|
|
||
| if id != 0 { | ||
| GlobalHotKeyManager.shared.unregister(id: id) | ||
| // Second unregister of same ID should be safe | ||
| XCTAssertNoThrow(GlobalHotKeyManager.shared.unregister(id: id)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Handler Invocation Tests | ||
|
|
||
| func testRegisterStoresHandler() { | ||
| var handlerCalled = false | ||
| let id = GlobalHotKeyManager.shared.register( | ||
| keyCode: UInt32(kVK_ANSI_G), | ||
| modifiers: UInt32(cmdKey) | UInt32(shiftKey) | UInt32(optionKey) | ||
| ) { | ||
| handlerCalled = true | ||
| } | ||
|
|
||
| // We can't easily simulate a Carbon hotkey press in tests, | ||
| // but we verify the handler was stored (not called yet) | ||
| XCTAssertFalse(handlerCalled) | ||
|
|
||
| if id != 0 { GlobalHotKeyManager.shared.unregister(id: id) } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.