Voxley / app · voxley.si / Sources / Voxley / Shortcut.swift
| 1 | import AppKit |
| 2 | import Carbon |
| 3 | |
| 4 | struct Shortcut: Codable, Equatable, Sendable { |
| 5 | let code: UInt32 |
| 6 | let modifiers: UInt32 |
| 7 | let keyLabel: String |
| 8 | |
| 9 | static let controlShiftSpace = Shortcut( |
| 10 | code: UInt32(kVK_Space), modifiers: UInt32(controlKey | shiftKey), keyLabel: "Space" |
| 11 | ) |
| 12 | |
| 13 | /// Control–Option–R opens the Refine window. R is for Refine. |
| 14 | static let refineDefault = Shortcut( |
| 15 | code: UInt32(kVK_ANSI_R), modifiers: UInt32(controlKey | optionKey), keyLabel: "R" |
| 16 | ) |
| 17 | |
| 18 | // A modifier chord has no ordinary key code. Existing shortcut files keep their format. |
| 19 | private static let modifierOnlyCode = UInt32.max |
| 20 | private static let functionModifier: UInt32 = 1 << 24 |
| 21 | var isModifierOnly: Bool { code == Self.modifierOnlyCode } |
| 22 | |
| 23 | init?(modifierChord: UInt32) { |
| 24 | guard modifierChord.nonzeroBitCount >= 2 else { return nil } |
| 25 | self.init(code: Self.modifierOnlyCode, modifiers: modifierChord, keyLabel: "") |
| 26 | } |
| 27 | |
| 28 | var label: String { |
| 29 | Self.modifierLabel(modifiers) + keyLabel |
| 30 | } |
| 31 | |
| 32 | private static func modifierLabel(_ modifiers: UInt32) -> String { |
| 33 | let symbols = [(controlKey, "⌃"), (optionKey, "⌥"), (shiftKey, "⇧"), (cmdKey, "⌘")] |
| 34 | let standard = symbols.compactMap { modifiers & UInt32($0.0) == 0 ? nil : $0.1 }.joined() |
| 35 | return standard + (modifiers & functionModifier == 0 ? "" : "Fn") |
| 36 | } |
| 37 | |
| 38 | static func modifierLabel(for flags: NSEvent.ModifierFlags) -> String { |
| 39 | modifierLabel(chordModifiers(for: flags)) |
| 40 | } |
| 41 | |
| 42 | static func carbonModifiers(for flags: NSEvent.ModifierFlags) -> UInt32 { |
| 43 | var modifiers: UInt32 = 0 |
| 44 | if flags.contains(.control) { modifiers |= UInt32(controlKey) } |
| 45 | if flags.contains(.option) { modifiers |= UInt32(optionKey) } |
| 46 | if flags.contains(.shift) { modifiers |= UInt32(shiftKey) } |
| 47 | if flags.contains(.command) { modifiers |= UInt32(cmdKey) } |
| 48 | return modifiers |
| 49 | } |
| 50 | |
| 51 | static func chordModifiers(for flags: NSEvent.ModifierFlags) -> UInt32 { |
| 52 | carbonModifiers(for: flags) | (flags.contains(.function) ? functionModifier : 0) |
| 53 | } |
| 54 | |
| 55 | var isStandardEditingShortcut: Bool { |
| 56 | switch Int(code) { |
| 57 | case kVK_ANSI_Z: |
| 58 | modifiers == UInt32(cmdKey) || modifiers == UInt32(cmdKey | shiftKey) |
| 59 | case kVK_ANSI_A, kVK_ANSI_C, kVK_ANSI_V, kVK_ANSI_X: |
| 60 | modifiers == UInt32(cmdKey) |
| 61 | default: |
| 62 | false |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | init(code: UInt32, modifiers: UInt32, keyLabel: String) { |
| 67 | self.code = code |
| 68 | self.modifiers = modifiers |
| 69 | self.keyLabel = keyLabel |
| 70 | } |
| 71 | |
| 72 | init(from decoder: Decoder) throws { |
| 73 | if let old = try? decoder.singleValueContainer().decode(String.self) { |
| 74 | switch old { |
| 75 | case "⌃⇧Space": self = .controlShiftSpace |
| 76 | case "⌃⌥D": self.init(code: UInt32(kVK_ANSI_D), modifiers: UInt32(controlKey | optionKey), keyLabel: "D") |
| 77 | case "F9": self.init(code: UInt32(kVK_F9), modifiers: 0, keyLabel: "F9") |
| 78 | default: |
| 79 | throw DecodingError.dataCorruptedError(in: try decoder.singleValueContainer(), |
| 80 | debugDescription: "Unknown legacy shortcut") |
| 81 | } |
| 82 | return |
| 83 | } |
| 84 | let values = try decoder.container(keyedBy: CodingKeys.self) |
| 85 | self.init(code: try values.decode(UInt32.self, forKey: .code), |
| 86 | modifiers: try values.decode(UInt32.self, forKey: .modifiers), |
| 87 | keyLabel: try values.decode(String.self, forKey: .keyLabel)) |
| 88 | } |
| 89 | |
| 90 | @MainActor |
| 91 | init?(event: NSEvent) { |
| 92 | let modifiers = Self.carbonModifiers(for: event.modifierFlags) |
| 93 | |
| 94 | let code = Int(event.keyCode) |
| 95 | let functionKeys = [kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F5, kVK_F6, kVK_F7, kVK_F8, kVK_F9, kVK_F10, |
| 96 | kVK_F11, kVK_F12, kVK_F13, kVK_F14, kVK_F15, kVK_F16, kVK_F17, kVK_F18, kVK_F19, kVK_F20] |
| 97 | let functionKey = functionKeys.firstIndex(of: code) |
| 98 | guard modifiers != 0 || functionKey != nil else { return nil } |
| 99 | let specialKeys = [ |
| 100 | kVK_Space: "Space", kVK_Return: "Return", kVK_Tab: "Tab", kVK_Delete: "Delete", |
| 101 | kVK_ForwardDelete: "Forward Delete", kVK_Escape: "Esc", kVK_Home: "Home", kVK_End: "End", |
| 102 | kVK_PageUp: "Page Up", kVK_PageDown: "Page Down", kVK_LeftArrow: "←", kVK_RightArrow: "→", |
| 103 | kVK_UpArrow: "↑", kVK_DownArrow: "↓", kVK_ANSI_KeypadEnter: "Enter" |
| 104 | ] |
| 105 | let characters = event.charactersIgnoringModifiers.flatMap { characters in |
| 106 | characters.unicodeScalars.contains(where: CharacterSet.controlCharacters.contains) ? nil : characters.uppercased() |
| 107 | } |
| 108 | let name = functionKey.map { "F\($0 + 1)" } ?? specialKeys[code] ?? characters ?? "Key \(code)" |
| 109 | self.init(code: UInt32(code), modifiers: modifiers, keyLabel: name.isEmpty ? "Key \(code)" : name) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | struct ModifierChordCapture { |
| 114 | private var peak: UInt32 = 0 |
| 115 | private var usedOrdinaryKey = false |
| 116 | |
| 117 | var pending: Shortcut? { Shortcut(modifierChord: peak) } |
| 118 | |
| 119 | mutating func ordinaryKeyDown() { |
| 120 | usedOrdinaryKey = true |
| 121 | peak = 0 |
| 122 | } |
| 123 | |
| 124 | mutating func modifiersChanged(_ held: UInt32) -> Shortcut? { |
| 125 | if held == 0 { |
| 126 | let recorded = pending |
| 127 | self = Self() |
| 128 | return recorded |
| 129 | } |
| 130 | if !usedOrdinaryKey, held.nonzeroBitCount >= peak.nonzeroBitCount { peak = held } |
| 131 | return nil |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | struct DictationShortcutGesture { |
| 136 | enum Action: Equatable { |
| 137 | case start, stop, none |
| 138 | } |
| 139 | |
| 140 | static let holdThreshold: TimeInterval = 0.30 |
| 141 | private var press: (time: TimeInterval, startedRecording: Bool)? |
| 142 | |
| 143 | mutating func interrupt() -> Bool { |
| 144 | let started = press?.startedRecording == true |
| 145 | press = nil |
| 146 | return started |
| 147 | } |
| 148 | |
| 149 | mutating func update(pressed: Bool, time: TimeInterval, canStart: Bool, isRecording: Bool) -> Action { |
| 150 | if pressed { |
| 151 | guard press == nil, canStart || isRecording else { return .none } |
| 152 | press = (time, canStart) |
| 153 | return canStart ? .start : .none |
| 154 | } |
| 155 | guard let press else { return .none } |
| 156 | self.press = nil |
| 157 | guard isRecording else { return .none } |
| 158 | if !press.startedRecording || time - press.time >= Self.holdThreshold { |
| 159 | return .stop |
| 160 | } |
| 161 | return .none |
| 162 | } |
| 163 | } |