Skip to content

Commit

Permalink
add NSMenuItem.key extension (#35)
Browse files Browse the repository at this point in the history
* add NSMenuItem.key extension

* add menuitem extension to project

* use other initializer

* add unit test for menu extension
  • Loading branch information
DivineDominion authored Apr 6, 2021
1 parent 7005837 commit b0f2963
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/Sauce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
0909EBD0245D5C9E00ABA339 /* KeyboardLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0909EBCF245D5C9E00ABA339 /* KeyboardLayoutTests.swift */; };
095EF0012456ED9A00174829 /* ModifierTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 095EF0002456ED9A00174829 /* ModifierTransformer.swift */; };
09CDC68A210F044A007DDFE4 /* TISInputSource+Property.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09CDC689210F044A007DDFE4 /* TISInputSource+Property.swift */; };
50582477261C6F1F00AD2DD8 /* NSMenuItem+Key.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50582476261C6F1F00AD2DD8 /* NSMenuItem+Key.swift */; };
5058247B261C6FA400AD2DD8 /* NSMenuItem+KeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5058247A261C6FA400AD2DD8 /* NSMenuItem+KeyTests.swift */; };
FA1E407621107B0A0016D710 /* SpecialKeyCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA1E407521107B0A0016D710 /* SpecialKeyCode.swift */; };
FAA4132A210E2C730097D522 /* Sauce.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FAA41320210E2C730097D522 /* Sauce.framework */; };
FAA41331210E2C730097D522 /* Sauce.h in Headers */ = {isa = PBXBuildFile; fileRef = FAA41323210E2C730097D522 /* Sauce.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -33,6 +35,8 @@
0909EBCF245D5C9E00ABA339 /* KeyboardLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardLayoutTests.swift; sourceTree = "<group>"; };
095EF0002456ED9A00174829 /* ModifierTransformer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModifierTransformer.swift; sourceTree = "<group>"; };
09CDC689210F044A007DDFE4 /* TISInputSource+Property.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TISInputSource+Property.swift"; sourceTree = "<group>"; };
50582476261C6F1F00AD2DD8 /* NSMenuItem+Key.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSMenuItem+Key.swift"; sourceTree = "<group>"; };
5058247A261C6FA400AD2DD8 /* NSMenuItem+KeyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSMenuItem+KeyTests.swift"; sourceTree = "<group>"; };
FA1E407521107B0A0016D710 /* SpecialKeyCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialKeyCode.swift; sourceTree = "<group>"; };
FAA41320210E2C730097D522 /* Sauce.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sauce.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FAA41323210E2C730097D522 /* Sauce.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Sauce.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +98,7 @@
FAA41366210E2EFB0097D522 /* Key.swift */,
FA1E407521107B0A0016D710 /* SpecialKeyCode.swift */,
095EF0002456ED9A00174829 /* ModifierTransformer.swift */,
50582476261C6F1F00AD2DD8 /* NSMenuItem+Key.swift */,
);
path = Sauce;
sourceTree = "<group>";
Expand All @@ -102,6 +107,7 @@
isa = PBXGroup;
children = (
0909EBCF245D5C9E00ABA339 /* KeyboardLayoutTests.swift */,
5058247A261C6FA400AD2DD8 /* NSMenuItem+KeyTests.swift */,
FAA41330210E2C730097D522 /* Info.plist */,
);
path = SauceTests;
Expand Down Expand Up @@ -217,6 +223,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
50582477261C6F1F00AD2DD8 /* NSMenuItem+Key.swift in Sources */,
09CDC68A210F044A007DDFE4 /* TISInputSource+Property.swift in Sources */,
FAA41369210E33060097D522 /* KeyboardLayout.swift in Sources */,
FAA4136B210E33CC0097D522 /* InputSource.swift in Sources */,
Expand All @@ -231,6 +238,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5058247B261C6FA400AD2DD8 /* NSMenuItem+KeyTests.swift in Sources */,
0909EBD0245D5C9E00ABA339 /* KeyboardLayoutTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
23 changes: 23 additions & 0 deletions Lib/Sauce/NSMenuItem+Key.swift
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)
}
}
29 changes: 29 additions & 0 deletions Lib/SauceTests/NSMenuItem+KeyTests.swift
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)
}
}

0 comments on commit b0f2963

Please sign in to comment.