Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404public import Foundation
public struct KeyModifiers: OptionSet, Sendable, Hashable, CustomStringConvertible { public let rawValue: UInt8
public init(rawValue: UInt8) { precondition( rawValue & 0b1110_0000 == 0, String( localized: "Key modifiers must use only the five supported bits.", table: "Keybindings", bundle: .module ) ) self.rawValue = rawValue }
public static let ctrl = KeyModifiers(rawValue: 1 << 0) public static let alt = KeyModifiers(rawValue: 1 << 1) public static let cmd = KeyModifiers(rawValue: 1 << 2) public static let shift = KeyModifiers(rawValue: 1 << 3) public static let fn = KeyModifiers(rawValue: 1 << 4) public static let hyper: KeyModifiers = [.ctrl, .alt, .cmd, .shift]
public init(parsing text: String) throws(KeyChordParseError) { let text = KeyChordSyntax.normalize(text) if text.isEmpty { self = [] return } self = try Self.parse(KeyChordSyntax.components(text)) }
public var description: String { let names: [(Self, String)] = [(.ctrl, "ctrl"), (.alt, "alt"), (.cmd, "cmd"), (.shift, "shift"), (.fn, "fn")] return names.filter { contains($0.0) }.map(\.1).joined(separator: "-") }
fileprivate static func named(_ name: String) -> Self? { switch name { case "ctrl", "control": .ctrl case "alt", "opt", "option": .alt case "cmd", "command": .cmd case "shift": .shift case "fn": .fn case "hyper": .hyper default: nil } }
fileprivate static func parse(_ components: [String]) throws(KeyChordParseError) -> Self { var modifiers: Self = [] for component in components { guard !component.isEmpty else { throw .emptyComponent } guard let modifier = named(component) else { throw .unknownModifier(component) } guard modifiers.intersection(modifier).isEmpty else { throw .duplicateModifier(component) } modifiers.formUnion(modifier) } return modifiers }}
public struct MouseModifiers: OptionSet, Sendable, Hashable, CustomStringConvertible { public let rawValue: UInt8
public init(rawValue: UInt8) { precondition( rawValue & 0b1111_0000 == 0, String( localized: "Mouse modifiers must use only the four supported bits.", table: "Keybindings", bundle: .module ) ) self.rawValue = rawValue }
public static let control = MouseModifiers(rawValue: KeyModifiers.ctrl.rawValue) public static let option = MouseModifiers(rawValue: KeyModifiers.alt.rawValue) public static let command = MouseModifiers(rawValue: KeyModifiers.cmd.rawValue) public static let shift = MouseModifiers(rawValue: KeyModifiers.shift.rawValue)
public init(parsing text: String) throws(MouseModifiersParseError) { let text = KeyChordSyntax.normalize(text) guard !text.isEmpty else { throw .emptyModifiers } var modifiers: Self = [] for component in KeyChordSyntax.components(text) { guard !component.isEmpty else { throw .emptyComponent } guard let keyModifier = KeyModifiers.named(component), !keyModifier.contains(.fn) else { throw .unknownModifier(component) } let modifier = Self(rawValue: keyModifier.rawValue) guard modifiers.intersection(modifier).isEmpty else { throw .duplicateModifier(component) } modifiers.formUnion(modifier) } self = modifiers }
public var description: String { KeyModifiers(rawValue: rawValue).description }}
public enum MouseModifiersParseError: Error, LocalizedError, Sendable, Equatable, CustomStringConvertible { case emptyModifiers case emptyComponent case unknownModifier(String) case duplicateModifier(String)
public var errorDescription: String? { description }
public var description: String { switch self { case .emptyModifiers: String(localized: "Mouse modifiers cannot be empty.", table: "Keybindings", bundle: .module) case .emptyComponent: String(localized: "Mouse modifiers contain an empty modifier.", table: "Keybindings", bundle: .module) case .unknownModifier(let token): String( localized: "Unknown mouse modifier ‘\(token)’. Use ctrl, alt (or opt), cmd, shift, or hyper.", table: "Keybindings", bundle: .module ) case .duplicateModifier(let token): String( localized: "Modifier ‘\(token)’ repeats a modifier already present.", table: "Keybindings", bundle: .module ) } }}
/// Physical ANSI positions; letters do not depend on the active keyboard layout.public enum ANSIKey: String, CaseIterable, Sendable, CustomStringConvertible { case a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z case zero = "0" case one = "1" case two = "2" case three = "3" case four = "4" case five = "5" case six = "6" case seven = "7" case eight = "8" case nine = "9" case minus case equal = "=" case leftBracket = "[" case rightBracket = "]" case backslash = "\\" case semicolon = ";" case quote = "'" case comma = "," case period = "." case slash = "/" case grave = "`" case `return`, space, tab, escape, delete case forwardDelete = "forwarddelete" case capsLock = "capslock" case home, end, help case pageUp = "pageup" case pageDown = "pagedown" case left, right, up, down case f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20 case keypad0, keypad1, keypad2, keypad3, keypad4, keypad5, keypad6, keypad7, keypad8, keypad9 case keypadDecimal = "keypaddecimal" case keypadMultiply = "keypadmultiply" case keypadPlus = "keypadplus" case keypadClear = "keypadclear" case keypadDivide = "keypaddivide" case keypadEnter = "keypadenter" case keypadMinus = "keypadminus" case keypadEqual = "keypadequal"
public var description: String { rawValue }
public var keyCode: UInt16 { switch self { case .a: 0 case .s: 1 case .d: 2 case .f: 3 case .h: 4 case .g: 5 case .z: 6 case .x: 7 case .c: 8 case .v: 9 case .b: 11 case .q: 12 case .w: 13 case .e: 14 case .r: 15 case .y: 16 case .t: 17 case .one: 18 case .two: 19 case .three: 20 case .four: 21 case .six: 22 case .five: 23 case .equal: 24 case .nine: 25 case .seven: 26 case .minus: 27 case .eight: 28 case .zero: 29 case .rightBracket: 30 case .o: 31 case .u: 32 case .leftBracket: 33 case .i: 34 case .p: 35 case .return: 36 case .l: 37 case .j: 38 case .quote: 39 case .k: 40 case .semicolon: 41 case .backslash: 42 case .comma: 43 case .slash: 44 case .n: 45 case .m: 46 case .period: 47 case .tab: 48 case .space: 49 case .grave: 50 case .delete: 51 case .escape: 53 case .capsLock: 57 case .f17: 64 case .keypadDecimal: 65 case .keypadMultiply: 67 case .keypadPlus: 69 case .keypadClear: 71 case .keypadDivide: 75 case .keypadEnter: 76 case .keypadMinus: 78 case .f18: 79 case .f19: 80 case .keypadEqual: 81 case .keypad0: 82 case .keypad1: 83 case .keypad2: 84 case .keypad3: 85 case .keypad4: 86 case .keypad5: 87 case .keypad6: 88 case .keypad7: 89 case .f20: 90 case .keypad8: 91 case .keypad9: 92 case .f5: 96 case .f6: 97 case .f7: 98 case .f3: 99 case .f8: 100 case .f9: 101 case .f11: 103 case .f13: 105 case .f16: 106 case .f14: 107 case .f10: 109 case .f12: 111 case .f15: 113 case .help: 114 case .home: 115 case .pageUp: 116 case .forwardDelete: 117 case .f4: 118 case .end: 119 case .f2: 120 case .pageDown: 121 case .f1: 122 case .left: 123 case .right: 124 case .down: 125 case .up: 126 } }
fileprivate static func parse(_ token: String) throws(KeyChordParseError) -> (Self, KeyModifiers) { if let key = Self(rawValue: token) { return (key, []) } switch token { case "-", "hyphen": return (.minus, []) case "equal", "equals": return (.equal, []) case "leftbracket": return (.leftBracket, []) case "rightbracket": return (.rightBracket, []) case "backslash": return (.backslash, []) case "semicolon": return (.semicolon, []) case "quote", "apostrophe": return (.quote, []) case "comma": return (.comma, []) case "period", "dot": return (.period, []) case "slash": return (.slash, []) case "grave", "backtick": return (.grave, []) case "enter": return (.return, []) case "esc": return (.escape, []) case "backspace": return (.delete, []) case "!": return (.one, .shift) case "@": return (.two, .shift) case "#": return (.three, .shift) case "$": return (.four, .shift) case "%": return (.five, .shift) case "^": return (.six, .shift) case "&": return (.seven, .shift) case "*": return (.eight, .shift) case "(": return (.nine, .shift) case ")": return (.zero, .shift) case "_": return (.minus, .shift) case "+": return (.equal, .shift) case "{": return (.leftBracket, .shift) case "}": return (.rightBracket, .shift) case "|": return (.backslash, .shift) case ":": return (.semicolon, .shift) case "\"": return (.quote, .shift) case "<": return (.comma, .shift) case ">": return (.period, .shift) case "?": return (.slash, .shift) case "~": return (.grave, .shift) default: throw .unknownKey(token) } }}
public struct KeyChord: Sendable, Hashable, CustomStringConvertible { public let modifiers: KeyModifiers public let key: ANSIKey
public init(modifiers: KeyModifiers = [], key: ANSIKey) { self.modifiers = modifiers self.key = key }
public init(parsing text: String) throws(KeyChordParseError) { let text = KeyChordSyntax.normalize(text) guard !text.isEmpty else { throw .emptyChord } var components = KeyChordSyntax.components(text) // A doubled separator spells a literal minus; printing uses its unambiguous name. if text == "-" { components = ["minus"] } else if components.count >= 3 && components.suffix(2).allSatisfy(\.isEmpty) { components.removeLast(2) components.append("minus") } guard let keyToken = components.popLast(), !keyToken.isEmpty else { throw .missingKey } let modifiers = try KeyModifiers.parse(components) guard KeyModifiers.named(keyToken) == nil else { throw .missingKey } let (key, impliedModifiers) = try ANSIKey.parse(keyToken) self.init(modifiers: modifiers.union(impliedModifiers), key: key) }
public var keyCode: UInt16 { key.keyCode }
public var description: String { modifiers.isEmpty ? key.description : "\(modifiers)-\(key)" }}
public enum KeyChordParseError: Error, LocalizedError, Sendable, Equatable, CustomStringConvertible { case emptyChord case emptyComponent case missingKey case unknownModifier(String) case duplicateModifier(String) case unknownKey(String)
public var errorDescription: String? { description }
public var description: String { switch self { case .emptyChord: String(localized: "A key chord cannot be empty.", table: "Keybindings", bundle: .module) case .emptyComponent: String(localized: "A key chord contains an empty modifier.", table: "Keybindings", bundle: .module) case .missingKey: String(localized: "A key chord needs a key after its modifiers.", table: "Keybindings", bundle: .module) case .unknownModifier(let token): String( localized: "Unknown modifier ‘\(token)’. Use ctrl, alt (or opt), cmd, shift, fn, or hyper.", table: "Keybindings", bundle: .module ) case .duplicateModifier(let token): String( localized: "Modifier ‘\(token)’ repeats a modifier already present.", table: "Keybindings", bundle: .module ) case .unknownKey(let token): String( localized: "Unknown ANSI key ‘\(token)’. Use a letter, digit, punctuation, named key, or f1–f20.", table: "Keybindings", bundle: .module ) } }}
private enum KeyChordSyntax { static func normalize(_ text: String) -> String { let bytes = text.trimmingCharacters(in: .whitespacesAndNewlines).utf8.map { byte in (65...90).contains(byte) ? byte + 32 : byte } return String(decoding: bytes, as: UTF8.self) }
static func components(_ text: String) -> [String] { text.split(separator: "-", omittingEmptySubsequences: false) .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } }}