Skip to content

Commit

Permalink
Add description to KeyCombo
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Aug 26, 2019
1 parent 14b32e7 commit 2c864fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 0 additions & 2 deletions Sources/HotKey/HotKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public final class HotKey {

public typealias Handler = () -> Void


// MARK: - Properties

let identifier = UUID()
Expand All @@ -25,7 +24,6 @@ public final class HotKey {
}
}


// MARK: - Initializers

public init(keyCombo: KeyCombo, keyDownHandler: Handler? = nil, keyUpHandler: Handler? = nil) {
Expand Down
3 changes: 0 additions & 3 deletions Sources/HotKey/HotKeysController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final class HotKeysController {
}
}


// MARK: - Properties

static var hotKeys = [UInt32: HotKeyBox]()
Expand All @@ -39,7 +38,6 @@ final class HotKeysController {

private static var eventHandler: EventHandlerRef?


// MARK: - Registration

static func register(_ hotKey: HotKey) {
Expand Down Expand Up @@ -182,7 +180,6 @@ final class HotKeysController {
}
}


private func hotKeyEventHandler(eventHandlerCall: EventHandlerCallRef?, event: EventRef?, userData: UnsafeMutableRawPointer?) -> OSStatus {
return HotKeysController.handleCarbonEvent(event)
}
18 changes: 10 additions & 8 deletions Sources/HotKey/KeyCombo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AppKit

public struct KeyCombo {
public struct KeyCombo: Equatable {

// MARK: - Properties

Expand Down Expand Up @@ -31,7 +31,6 @@ public struct KeyCombo {
return carbonKeyCode >= 0
}


// MARK: - Initializers

public init(carbonKeyCode: UInt32, carbonModifiers: UInt32 = 0) {
Expand All @@ -44,15 +43,13 @@ public struct KeyCombo {
self.carbonModifiers = modifiers.carbonFlags
}


// MARK: - Converting Keys

public static func carbonKeyCodeToString(_ carbonKeyCode: UInt32) -> String? {
return nil
}
}


extension KeyCombo {
public var dictionary: [String: Any] {
return [
Expand All @@ -72,9 +69,14 @@ extension KeyCombo {
}
}

extension KeyCombo: CustomStringConvertible {
public var description: String {
var output = modifiers.description

extension KeyCombo: Equatable {
public static func == (lhs: KeyCombo, rhs: KeyCombo) -> Bool {
return lhs.carbonKeyCode == rhs.carbonKeyCode && lhs.carbonModifiers == rhs.carbonModifiers
}
if let keyDescription = key?.description {
output += keyDescription
}

return output
}
}
24 changes: 24 additions & 0 deletions Sources/HotKey/NSEventModifierFlags+HotKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ extension NSEvent.ModifierFlags {
}
}
}

extension NSEvent.ModifierFlags: CustomStringConvertible {
public var description: String {
var output = ""

if contains(.control) {
output += Key.control.description
}

if contains(.option) {
output += Key.option.description
}

if contains(.shift) {
output += Key.shift.description
}

if contains(.command) {
output += Key.command.description
}

return output
}
}
5 changes: 5 additions & 0 deletions Tests/HotKeyTests/KeyComboTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ final class KeyComboTests: XCTestCase {
XCTAssertEqual(keyCombo.key, keyCombo2.key)
XCTAssertEqual(keyCombo.modifiers, keyCombo2.modifiers)
}

func testDescription() {
XCTAssertEqual("⌘C", KeyCombo(key: .c, modifiers: .command).description)
XCTAssertEqual("⌃⌥⇧⌘C", KeyCombo(key: .c, modifiers: [.command, .shift, .control, .option]).description)
}
}

0 comments on commit 2c864fc

Please sign in to comment.