prole/prole-tools-app/Sources/HotKeyManager.swift
chrisfu 0e01595de0 refactor: switch to environment wrapper; standardize env.sh usage and improve $PROLE_HOME management
- Replace direct script invocation with `$PROLE_HOME/env.sh` wrapper for consistent environment setup across all components.
- Update runtime processes to dynamically resolve `$PROLE_HOME` or fallback to `$HOME/.prole`.
- Deprecate `init-port-forwards.sh` script bundling; remove from LaunchAgentManager and build system.
- Overhaul `env.sh` generation to include execution capability, customizable paths, and improved error handling.
- Standardize app name and paths to "Prole Tools" across all files and UI elements.
- Adjust build output to align with new structure, including executable naming and resource packaging.
2025-12-15 22:00:32 -08:00

35 lines
1.5 KiB
Swift

import AppKit
import Carbon.HIToolbox
final class HotKeyManager {
private var hotKeyRef: EventHotKeyRef?
enum Key: UInt32 { case p = 35 /* US keyboard virtual keycode for P */ }
struct Modifiers: OptionSet {
let rawValue: UInt32
static let command = Modifiers(rawValue: UInt32(cmdKey))
static let option = Modifiers(rawValue: UInt32(optionKey))
static let shift = Modifiers(rawValue: UInt32(shiftKey))
}
func registerGlobalHotKey(modifiers: Modifiers, key: Key, handler: @escaping () -> Void) {
var eventSpec = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed))
InstallEventHandler(GetApplicationEventTarget(), { (_, _, userData) -> OSStatus in
let handler = Unmanaged<HotKeyHandlerWrapper>.fromOpaque(userData!).takeUnretainedValue()
handler.handler()
return noErr
}, 1, &eventSpec, Unmanaged.passRetained(HotKeyHandlerWrapper(handler: handler)).toOpaque(), nil)
let hotKeyID = EventHotKeyID(signature: OSType(UInt32(bitPattern: Int32(bitPattern: 0x50524F4C))), id: 1) // 'PROL'
RegisterEventHotKey(UInt32(key.rawValue), modifiers.rawValue, hotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef)
}
deinit { if let ref = hotKeyRef { UnregisterEventHotKey(ref) } }
}
private final class HotKeyHandlerWrapper {
let handler: () -> Void
init(handler: @escaping () -> Void) { self.handler = handler }
}