Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215public import Foundationimport OSLog
public struct SymbolicHotKey: Equatable, Sendable { public let id: Int public let isEnabled: Bool public let hotKey: HotKey?
public init(id: Int, isEnabled: Bool, hotKey: HotKey?) { self.id = id self.isEnabled = isEnabled self.hotKey = hotKey }}
public enum SpaceShortcutAction: Hashable, Sendable { case desktop(Int) case moveLeft case moveRight
public var symbolicID: Int? { switch self { case .moveLeft: 79 case .moveRight: 81 case .desktop(let number): switch number { case 1...16: 117 + number default: nil } } }}
public struct SpaceShortcut: Equatable, Sendable { public let action: SpaceShortcutAction public let isEnabled: Bool public let hotKey: HotKey?}
public enum SymbolicHotKeys { public static func read() throws -> [SymbolicHotKey] { guard let domain = UserDefaults.standard.persistentDomain(forName: "com.apple.symbolichotkeys") else { return [] } let data = try PropertyListSerialization.data(fromPropertyList: domain, format: .binary, options: 0) return try parse(data) }
private static let logger = Logger(subsystem: "dev.vinnymac.parcel", category: "symbolic-hotkeys")
public static func parse(_ data: Data) throws -> [SymbolicHotKey] { try parse(data) { id in logger.error("Skipping malformed system shortcut id \(id, privacy: .public)") } }
static func parse(_ data: Data, onMalformedEntry: (String) -> Void) throws -> [SymbolicHotKey] { let document = try PropertyListDecoder().decode(Document.self, from: data) var result: [SymbolicHotKey] = [] for (id, decoded) in document.entries.sorted(by: { $0.key < $1.key }) { do { guard let number = Int(id), number >= 0, case .value(let entry) = decoded else { throw InputError.malformedSymbolicHotKey(id) } result.append(try hotKey(id: number, entry: entry)) } catch InputError.malformedSymbolicHotKey(_) { onMalformedEntry(id) } } return result.sorted { $0.id < $1.id } }
private static func hotKey(id: Int, entry: Entry) throws -> SymbolicHotKey { // macOS leaves out the value for entries still on their default chord. guard let value = entry.value else { return SymbolicHotKey(id: id, isEnabled: entry.enabled, hotKey: entry.enabled ? defaultHotKeys[id] : nil) } // Non-keyboard entries include mouse shortcuts and cannot collide with a chord. guard value.type == "standard" else { return SymbolicHotKey(id: id, isEnabled: entry.enabled, hotKey: nil) } guard value.parameters.count == 3, let flags = UInt32(exactly: value.parameters[2]) else { throw InputError.malformedSymbolicHotKey(String(id)) } let code = value.parameters[1] if code == 65535 || code == -1 { return SymbolicHotKey(id: id, isEnabled: entry.enabled, hotKey: nil) } guard let keyCode = UInt16(exactly: code), keyCode <= 127 else { throw InputError.malformedSymbolicHotKey(String(id)) } return SymbolicHotKey( id: id, isEnabled: entry.enabled, hotKey: HotKey(keyCode: keyCode, modifiers: .init(eventFlags: .init(rawValue: UInt64(flags))))) }
/// The chords macOS uses for Space shortcuts it hasn't written out. Other entries stay unknown. static let defaultHotKeys: [Int: HotKey] = { var keys: [Int: HotKey] = [ 79: HotKey(keyCode: 123, modifiers: .init(eventFlags: .init(rawValue: 8_650_752))), 80: HotKey(keyCode: 123, modifiers: .init(eventFlags: .init(rawValue: 8_781_824))), 81: HotKey(keyCode: 124, modifiers: .init(eventFlags: .init(rawValue: 8_650_752))), 82: HotKey(keyCode: 124, modifiers: .init(eventFlags: .init(rawValue: 8_781_824))), ] for (offset, keyCode) in [18, 19, 20, 21, 23, 22, 26, 28, 25].enumerated() { keys[118 + offset] = HotKey(keyCode: UInt16(keyCode), modifiers: .control) } return keys }()
public static func spaceShortcut(_ action: SpaceShortcutAction, in entries: [SymbolicHotKey]) -> SpaceShortcut { let entry = action.symbolicID.flatMap { id in entries.first { $0.id == id } } return SpaceShortcut(action: action, isEnabled: entry?.isEnabled ?? false, hotKey: entry?.hotKey) }
private struct Document: Decodable { let entries: [String: DecodedEntry] enum CodingKeys: String, CodingKey { case entries = "AppleSymbolicHotKeys" } }
private enum DecodedEntry: Decodable { case value(Entry) case malformed
init(from decoder: any Decoder) throws { do { self = .value(try Entry(from: decoder)) } catch is DecodingError { self = .malformed } } }
private struct Entry: Decodable { let enabled: Bool let value: Value? }
private struct Value: Decodable { let type: String let parameters: [Int64] }}
public struct MenuShortcut: Equatable, Sendable { public let pid: Int32 public let path: [String] public let character: String? public let hotKey: HotKey
public init(pid: Int32, path: [String], character: String? = nil, hotKey: HotKey) { self.pid = pid self.path = path self.character = character self.hotKey = hotKey }}
public enum ShortcutOwner: Equatable, Sendable { case system(symbolicID: Int) case application(pid: Int32, menuPath: [String]) case binding(index: Int)}
public struct ShortcutConflict: Equatable, Sendable { public let hotKey: HotKey public let owner: ShortcutOwner}
public enum ShortcutConflicts { public static func detect( hotKeys: [HotKey], symbolicHotKeys: [SymbolicHotKey], menuShortcuts: [MenuShortcut] = [] ) -> [ShortcutConflict] { var result: [ShortcutConflict] = [] var seen: Set<HotKey> = [] for (index, key) in hotKeys.enumerated() { if !seen.insert(key).inserted { result.append(ShortcutConflict(hotKey: key, owner: .binding(index: index))) continue } result += symbolicHotKeys.filter { $0.isEnabled && $0.hotKey?.normalizingArrowFunctionContext() == key }.map { ShortcutConflict(hotKey: key, owner: .system(symbolicID: $0.id)) } result += menuShortcuts.filter { $0.hotKey.normalizingArrowFunctionContext() == key }.map { ShortcutConflict(hotKey: key, owner: .application(pid: $0.pid, menuPath: $0.path)) } } return result }
/// Candidates come from the caller's keyboard layout; the modifier prefix never changes. public static func suggestions( for hotKey: HotKey, candidateKeyCodes: [UInt16], hotKeys: [HotKey], symbolicHotKeys: [SymbolicHotKey], menuShortcuts: [MenuShortcut] = [], limit: Int = 3 ) -> [HotKey] { guard limit > 0 else { return [] } var occupied = Set(hotKeys) occupied.insert(hotKey) occupied.formUnion( symbolicHotKeys.filter(\.isEnabled).compactMap { $0.hotKey?.normalizingArrowFunctionContext() }) occupied.formUnion(menuShortcuts.map { $0.hotKey.normalizingArrowFunctionContext() }) var result: [HotKey] = [] for code in candidateKeyCodes where code <= 127 { let candidate = HotKey(keyCode: code, modifiers: hotKey.modifiers) if occupied.insert(candidate).inserted { result.append(candidate) if result.count == limit { break } } } return result }}