-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add NSMenuItem.key extension * add menuitem extension to project * use other initializer * add unit test for menu extension
- Loading branch information
1 parent
7005837
commit b0f2963
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,23 @@ | ||
// | ||
// Sauce.swift | ||
// | ||
// Sauce | ||
// GitHub: https://github.com/clipy | ||
// HP: https://clipy-app.com | ||
// | ||
// Copyright © 2015-2020 Clipy Project. | ||
// | ||
|
||
import AppKit | ||
|
||
extension NSMenuItem { | ||
/// A `Key` instance that corresponds to the menu item's shortcut. | ||
public var key: Key? { | ||
// Prefer the shortcut overrides by the user (from "System Preferences" > "Keyboard") | ||
// to the developer's default. | ||
let keyEquivalent = !self.userKeyEquivalent.isEmpty | ||
? self.userKeyEquivalent | ||
: self.keyEquivalent | ||
return Key(character: keyEquivalent, virtualKeyCode: nil) | ||
} | ||
} |
This file contains 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,29 @@ | ||
// | ||
// NSMenuItem+KeyTests.swift | ||
// | ||
// SauceTests | ||
// GitHub: https://github.com/clipy | ||
// HP: https://clipy-app.com | ||
// | ||
// Copyright © 2015-2021 Clipy Project. | ||
// | ||
|
||
import XCTest | ||
import AppKit | ||
@testable import Sauce | ||
|
||
class NSMenuItem_KeyTests: XCTestCase { | ||
func testKeyConversionIgnoresCharacterCase() { | ||
let menuItem = NSMenuItem() | ||
menuItem.keyEquivalentModifierMask = .command | ||
|
||
menuItem.keyEquivalent = "b" | ||
XCTAssertEqual(menuItem.key, .b) | ||
|
||
menuItem.keyEquivalent = "B" | ||
XCTAssertEqual(menuItem.key, .b) | ||
|
||
menuItem.keyEquivalentModifierMask = .shift | ||
XCTAssertEqual(menuItem.key, .b) | ||
} | ||
} |