Registering / Enabling shortcuts now returns a discardable Bool to know if registering worked or not

Also adds an "isEnabled" calculated property to Shortcut, and the appropriate .shortcutIsEnabled(_ : ) function to KeyboardShortcuts.

Makes var isTakenBySystem public so it can be used for "default" shortcuts created by an app.
This commit is contained in:
Matthias Gansrigler 2022-08-01 14:28:31 +02:00
parent fe481cb687
commit a9b78b6fce
3 changed files with 36 additions and 20 deletions

View File

@ -37,34 +37,32 @@ enum CarbonKeyboardShortcuts {
private static var hotKeyId = 0
private static var eventHandler: EventHandlerRef?
private static func setUpEventHandlerIfNeeded() {
guard
eventHandler == nil,
let dispatcher = GetEventDispatcherTarget()
else {
return
}
private static func setUpEventHandlerIfNeeded() -> Bool {
guard eventHandler == nil else { return true }
guard let dispatcher = GetEventDispatcherTarget() else { return false }
let eventSpecs = [
EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed)),
EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyReleased))
]
InstallEventHandler(
guard InstallEventHandler(
dispatcher,
carbonKeyboardShortcutsEventHandler,
eventSpecs.count,
eventSpecs,
nil,
&eventHandler
)
) == noErr else { return false }
return true
}
static func register(
@discardableResult static func register(
_ shortcut: KeyboardShortcuts.Shortcut,
onKeyDown: @escaping (KeyboardShortcuts.Shortcut) -> Void,
onKeyUp: @escaping (KeyboardShortcuts.Shortcut) -> Void
) {
) -> Bool {
hotKeyId += 1
var eventHotKey: EventHotKeyRef?
@ -81,7 +79,7 @@ enum CarbonKeyboardShortcuts {
registerError == noErr,
let carbonHotKey = eventHotKey
else {
return
return false
}
hotKeys[hotKeyId] = HotKey(
@ -92,7 +90,7 @@ enum CarbonKeyboardShortcuts {
onKeyUp: onKeyUp
)
setUpEventHandlerIfNeeded()
return setUpEventHandlerIfNeeded()
}
private static func unregisterHotKey(_ hotKey: HotKey) {

View File

@ -37,18 +37,22 @@ public enum KeyboardShortcuts {
*/
static var isPaused = false
private static func register(_ shortcut: Shortcut) {
@discardableResult private static func register(_ shortcut: Shortcut) -> Bool {
guard !registeredShortcuts.contains(shortcut) else {
return
return true //already registered
}
CarbonKeyboardShortcuts.register(
let keyboardShortcutRegistered = CarbonKeyboardShortcuts.register(
shortcut,
onKeyDown: handleOnKeyDown,
onKeyUp: handleOnKeyUp
)
guard keyboardShortcutRegistered else { return false }
registeredShortcuts.insert(shortcut)
return true
}
/**
@ -129,12 +133,19 @@ public enum KeyboardShortcuts {
/**
Enable a disabled keyboard shortcut.
*/
public static func enable(_ name: Name) {
@discardableResult public static func enable(_ name: Name) -> Bool {
guard let shortcut = getShortcut(for: name) else {
return
return false
}
register(shortcut)
return register(shortcut)
}
/**
Returns whether the passed-in shortcut is currently registered.
*/
public static func shortcutIsEnabled(_ shortcut: Shortcut) -> Bool {
return registeredShortcuts.contains(shortcut) || registeredShortcuts.first(where: {$0.key == shortcut.key && $0.modifiers == shortcut.modifiers}) != nil
}
/**

View File

@ -38,6 +38,13 @@ extension KeyboardShortcuts {
You most likely don't need this.
*/
public let carbonModifiers: Int
/**
Returns whether this shortcut is registered/enabled.
*/
public var isEnabled: Bool {
KeyboardShortcuts.shortcutIsEnabled(self)
}
/**
Initialize from a strongly-typed key and modifiers.
@ -97,7 +104,7 @@ extension KeyboardShortcuts.Shortcut {
/**
Check whether the keyboard shortcut is already taken by the system.
*/
var isTakenBySystem: Bool {
public var isTakenBySystem: Bool {
guard self != Self(.f12, modifiers: []) else {
return false
}