-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMenuBarManager.swift
More file actions
521 lines (455 loc) · 20.1 KB
/
Copy pathMenuBarManager.swift
File metadata and controls
521 lines (455 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//
// MenuBarManager.swift
// HyperVibe
//
// Manages the menu bar icon and menu
//
import AppKit
import Carbon.HIToolbox
// Button actions that can be assigned
enum ButtonAction: String, CaseIterable {
case enterKey = "Enter: Submit prompt"
case upKey = "Up: Navigate Up"
case downKey = "Down: Navigate Down"
case escKey = "Esc: Navigate Back"
case ctrlC = "Control + C: Cancel Prompt"
case spaceKey = "Space: Claude Voice Dictation"
case rightCmd = "Right Command: 3rd-party Voice Dictation"
case rightOpt = "Right Option: 3rd-party Voice Dictation"
case trackpadClick = "Mouse Click"
case none = "None"
/// Push-to-talk dictation needs the virtual key held for the full press duration.
/// Only a subset of HID buttons emit reliable release events, so these actions are
/// only offered for hold-capable buttons.
var requiresHold: Bool {
switch self {
case .spaceKey, .rightCmd, .rightOpt: return true
default: return false
}
}
}
/// HID buttons whose driver emits both press (value=1) and release (value=0) — verified via /tmp/hypervibe.log.
/// menu/tv/select are excluded: menu/tv are press-only on the Siri Remote, select is handled separately for click/drag.
let holdCapableButtons: Set<String> = ["playPause", "volumeUp", "volumeDown", "siri"]
/// Trackpad swipe directions (single-finger flicks). Detection happens in TouchHandler;
/// execution is dispatched here so mappings live alongside button mappings.
enum SwipeDirection: String, CaseIterable {
case up, down, left, right
}
/// Action a swipe can trigger. Slash-command cases type the raw value (without Enter — user
/// presses Enter themselves). `leftArrow`/`rightArrow` send virtual arrow keys instead of text.
/// `init` is a Swift keyword so the case name is backtick-escaped; rawValue "/init" is what displays.
enum SwipeAction: String, CaseIterable {
// Priority order: direction-matched arrow (filtered per submenu), then Mode Switching,
// then ultrathink, then slash commands alphabetically, None last.
case leftArrow = "Left: Navigate Left"
case rightArrow = "Right: Navigate Right"
case modeSwitch = "Mode Switching (Shift + Tab)"
case ultrathink = "ultrathink"
case btw = "/btw"
case compact = "/compact"
case config = "/config"
case context = "/context"
case effort = "/effort"
case `init` = "/init"
case model = "/model"
case remoteControl = "/remote-control"
case schedule = "/schedule"
case tasks = "/tasks"
case usage = "/usage"
case none = "None"
}
// Scroll speed options
enum ScrollSpeed: String, CaseIterable {
case slow = "Slow"
case medium = "Medium"
case fast = "Fast"
var scale: CGFloat {
switch self {
case .slow: return 150.0
case .medium: return 300.0
case .fast: return 500.0
}
}
}
class MenuBarManager {
private let statusItem: NSStatusItem
private let menu: NSMenu
private let statusMenuItem: NSMenuItem
// Button mappings (stored in UserDefaults)
private var buttonMappings: [String: ButtonAction] = [:]
// Swipe gesture mappings (stored in UserDefaults under "swipeMappings").
private var swipeMappings: [SwipeDirection: SwipeAction] = [:]
private static let defaultSwipeMappings: [SwipeDirection: SwipeAction] = [
.up: .usage,
.down: .compact,
.left: .model,
.right: .modeSwitch,
]
// Scroll speed (used for trackpad scroll scale; no menu, native multitouch)
private(set) var scrollSpeed: ScrollSpeed = .medium
/// Set by app delegate so menu bar can delegate media actions to MediaController.
var mediaController: MediaController?
init(statusItem: NSStatusItem) {
self.statusItem = statusItem
self.menu = NSMenu()
self.statusMenuItem = NSMenuItem(title: "Status: Disconnected", action: nil, keyEquivalent: "")
loadMappings()
loadSwipeMappings()
setupMenuBar()
}
private func loadMappings() {
// Default mappings (only used on first launch / after schema upgrade)
let defaultMappings: [String: ButtonAction] = [
"playPause": .enterKey,
"menu": .escKey,
"select": .trackpadClick,
"volumeUp": .upKey,
"volumeDown": .downKey,
"siri": .spaceKey,
"tv": .ctrlC
]
// Schema version bumps:
// v3: old media-key actions removed — drop all saved button mappings
// v4: "select" default changed from .enterKey to .trackpadClick — reset just that entry
let currentSchema = 4
let savedSchema = UserDefaults.standard.integer(forKey: "buttonMappingsSchema")
if savedSchema < 3 {
UserDefaults.standard.removeObject(forKey: "buttonMappings")
} else if savedSchema < 4 {
// Targeted migration: reset "select" so the new default applies, preserve others.
if var saved = UserDefaults.standard.dictionary(forKey: "buttonMappings") as? [String: String] {
saved.removeValue(forKey: "select")
UserDefaults.standard.set(saved, forKey: "buttonMappings")
}
}
if savedSchema < currentSchema {
UserDefaults.standard.set(currentSchema, forKey: "buttonMappingsSchema")
}
if let saved = UserDefaults.standard.dictionary(forKey: "buttonMappings") as? [String: String] {
for (button, actionRaw) in saved {
if let action = ButtonAction(rawValue: actionRaw) {
buttonMappings[button] = action
}
}
for (button, action) in defaultMappings {
if buttonMappings[button] == nil {
buttonMappings[button] = action
}
}
// Defensive: if a hold-required action got persisted against a tap-only button, reset to none.
for (button, action) in buttonMappings where action.requiresHold && !holdCapableButtons.contains(button) {
buttonMappings[button] = ButtonAction.none
}
} else {
buttonMappings = defaultMappings
saveMappings()
}
}
private func saveMappings() {
var toSave: [String: String] = [:]
for (button, action) in buttonMappings {
toSave[button] = action.rawValue
}
UserDefaults.standard.set(toSave, forKey: "buttonMappings")
}
/// Procedurally draw the menu-bar icon — a walkie-talkie glyph mirroring the
/// Figma reference (36-unit viewBox: antenna + body with display + speaker
/// holes via even-odd fill). 2× centered scale matches the menu-bar reading
/// size; overflow clips at the canvas edges by design.
private static func makeWaveIcon() -> NSImage {
let pt: CGFloat = 18
let image = NSImage(size: NSSize(width: pt, height: pt), flipped: true) { rect in
guard let ctx = NSGraphicsContext.current?.cgContext else { return false }
let s = rect.width
ctx.translateBy(x: s / 2, y: s / 2)
ctx.scaleBy(x: 2, y: 2)
ctx.translateBy(x: -s / 2, y: -s / 2)
ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1))
let antenna = CGRect(x: 0.5260 * s, y: 0.1944 * s,
width: 0.0638 * s, height: 0.1594 * s)
let body = CGRect(x: 0.3348 * s, y: 0.3538 * s,
width: 0.3187 * s, height: 0.4462 * s)
let display = CGRect(x: 0.3986 * s, y: 0.6406 * s,
width: 0.1911 * s, height: 0.0956 * s)
let speakerR: CGFloat = 0.0956 * s
let speaker = CGRect(x: 0.4942 * s - speakerR, y: 0.5131 * s - speakerR,
width: 2 * speakerR, height: 2 * speakerR)
let path = CGMutablePath()
path.addPath(CGPath(roundedRect: antenna,
cornerWidth: 0.0278 * s, cornerHeight: 0.0278 * s, transform: nil))
path.addPath(CGPath(roundedRect: body,
cornerWidth: 0.0556 * s, cornerHeight: 0.0556 * s, transform: nil))
path.addPath(CGPath(roundedRect: display,
cornerWidth: 0.0278 * s, cornerHeight: 0.0278 * s, transform: nil))
path.addEllipse(in: speaker)
ctx.addPath(path)
ctx.fillPath(using: .evenOdd)
return true
}
image.isTemplate = true
return image
}
private func setupMenuBar() {
// Configure the button (the visible icon in menu bar)
guard let button = statusItem.button else {
return
}
button.image = Self.makeWaveIcon()
button.title = ""
rebuildMenu()
statusItem.menu = menu
}
private func rebuildMenu() {
menu.removeAllItems()
// Title
let titleItem = NSMenuItem(title: "Siri Remote", action: nil, keyEquivalent: "")
titleItem.isEnabled = false
menu.addItem(titleItem)
menu.addItem(NSMenuItem.separator())
// Status
statusMenuItem.isEnabled = false
menu.addItem(statusMenuItem)
menu.addItem(NSMenuItem.separator())
// Button Mappings submenu
let mappingsItem = NSMenuItem(title: "Button Mappings", action: nil, keyEquivalent: "")
let mappingsSubmenu = NSMenu()
let buttons = [
("select", "Trackpad Click"),
("menu", "Menu Button"),
("tv", "TV Button"),
("siri", "Siri Button"),
("playPause", "Play/Pause Button"),
("volumeUp", "Volume Up"),
("volumeDown", "Volume Down"),
]
for (key, label) in buttons {
let buttonItem = NSMenuItem(title: label, action: nil, keyEquivalent: "")
let actionSubmenu = NSMenu()
let canHold = holdCapableButtons.contains(key)
for action in ButtonAction.allCases {
// Voice-dictation actions require press+release tracking; hide them on tap-only buttons.
if action.requiresHold && !canHold { continue }
// Mouse Click is only meaningful for the trackpad click button.
if action == .trackpadClick && key != "select" { continue }
let actionItem = NSMenuItem(title: action.rawValue, action: #selector(changeMapping(_:)), keyEquivalent: "")
actionItem.target = self
actionItem.representedObject = (key, action)
if buttonMappings[key] == action {
actionItem.state = .on
}
actionSubmenu.addItem(actionItem)
}
buttonItem.submenu = actionSubmenu
mappingsSubmenu.addItem(buttonItem)
}
mappingsItem.submenu = mappingsSubmenu
menu.addItem(mappingsItem)
// Swipe Gestures submenu
let swipeItem = NSMenuItem(title: "Swipe Gestures", action: nil, keyEquivalent: "")
let swipeSubmenu = NSMenu()
let swipes: [(SwipeDirection, String)] = [
(.up, "Swipe Up"),
(.down, "Swipe Down"),
(.left, "Swipe Left"),
(.right, "Swipe Right"),
]
for (direction, label) in swipes {
let dirItem = NSMenuItem(title: label, action: nil, keyEquivalent: "")
let actionsMenu = NSMenu()
for action in SwipeAction.allCases {
// Each arrow-key action only appears on its matching swipe direction.
if action == .leftArrow && direction != .left { continue }
if action == .rightArrow && direction != .right { continue }
let actionItem = NSMenuItem(title: action.rawValue, action: #selector(changeSwipeMapping(_:)), keyEquivalent: "")
actionItem.target = self
actionItem.representedObject = (direction, action)
if swipeMappings[direction] == action {
actionItem.state = .on
}
actionsMenu.addItem(actionItem)
}
dirItem.submenu = actionsMenu
swipeSubmenu.addItem(dirItem)
}
swipeItem.submenu = swipeSubmenu
menu.addItem(swipeItem)
menu.addItem(NSMenuItem.separator())
// Quit
let quitItem = NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q")
quitItem.target = self
menu.addItem(quitItem)
}
@objc private func changeMapping(_ sender: NSMenuItem) {
guard let (buttonKey, action) = sender.representedObject as? (String, ButtonAction) else {
return
}
buttonMappings[buttonKey] = action
saveMappings()
rebuildMenu()
}
@objc private func changeSwipeMapping(_ sender: NSMenuItem) {
guard let (direction, action) = sender.representedObject as? (SwipeDirection, SwipeAction) else {
return
}
swipeMappings[direction] = action
saveSwipeMappings()
rebuildMenu()
}
func updateConnectionStatus(connected: Bool) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.statusMenuItem.title = connected ? "Status: Connected ✓" : "Status: Disconnected"
self.statusItem.button?.appearsDisabled = !connected
}
}
func getMapping(for button: String) -> ButtonAction {
return buttonMappings[button] ?? .none
}
// Map HID codes to button names
private let hidCodeToButton: [String: String] = [
"0x000C:0x00CD": "playPause", // Play/Pause
"0x000C:0x00B5": "nextTrack", // Next (not a physical button but for mapping)
"0x000C:0x00B6": "prevTrack", // Previous (not a physical button but for mapping)
"0x000C:0x00E9": "volumeUp", // Volume Up
"0x000C:0x00EA": "volumeDown", // Volume Down
"0x0001:0x0086": "menu", // Menu button (System Menu Main)
"0x000C:0x0080": "select", // Select button
"0x000C:0x0040": "menu", // Menu (alternate)
"0x000C:0x0223": "menu", // Home
"0x000C:0x0224": "back", // Back
]
/// Get the action name for a given HID code (for event interception)
func getMappingForHIDCode(_ hidCode: String) -> String? {
guard let buttonName = hidCodeToButton[hidCode],
let action = buttonMappings[buttonName] else {
return nil
}
return action.rawValue
}
private func loadSwipeMappings() {
if let saved = UserDefaults.standard.dictionary(forKey: "swipeMappings") as? [String: String] {
for (dirRaw, actionRaw) in saved {
if let dir = SwipeDirection(rawValue: dirRaw),
let act = SwipeAction(rawValue: actionRaw) {
swipeMappings[dir] = act
}
}
}
// Fill any missing directions with defaults.
for (dir, act) in Self.defaultSwipeMappings where swipeMappings[dir] == nil {
swipeMappings[dir] = act
}
}
private func saveSwipeMappings() {
var toSave: [String: String] = [:]
for (dir, act) in swipeMappings {
toSave[dir.rawValue] = act.rawValue
}
UserDefaults.standard.set(toSave, forKey: "swipeMappings")
}
func getSwipeMapping(for direction: SwipeDirection) -> SwipeAction {
return swipeMappings[direction] ?? .none
}
/// Execute the action bound to a swipe direction. Slash-command actions type text
/// (no Enter — user presses Enter themselves). Arrow/modifier actions send key events.
func executeSwipe(_ direction: SwipeDirection) {
let action = swipeMappings[direction] ?? SwipeAction.none
switch action {
case .none:
break
case .leftArrow:
sendKey(kVK_LeftArrow)
case .rightArrow:
sendKey(kVK_RightArrow)
case .modeSwitch:
sendKey(kVK_Tab, flags: .maskShift)
case .btw, .schedule, .ultrathink:
// Trailing space: user typically continues with an argument or prose.
typeString(action.rawValue + " ")
case .compact, .config, .context, .effort, .`init`,
.model, .remoteControl, .tasks, .usage:
// No trailing space: these commands stand alone or open an interactive picker.
typeString(action.rawValue)
}
}
/// Post the given string as a single keyboard event via `keyboardSetUnicodeString`.
/// Works across terminals and most text fields; bypasses layout-specific key codes.
private func typeString(_ s: String) {
let utf16 = Array(s.utf16)
let count = utf16.count
guard count > 0 else { return }
utf16.withUnsafeBufferPointer { buffer in
guard let base = buffer.baseAddress else { return }
let src = CGEventSource(stateID: .hidSystemState)
let down = CGEvent(keyboardEventSource: src, virtualKey: 0, keyDown: true)
down?.keyboardSetUnicodeString(stringLength: count, unicodeString: base)
down?.post(tap: .cghidEventTap)
usleep(5000)
let up = CGEvent(keyboardEventSource: src, virtualKey: 0, keyDown: false)
up?.keyboardSetUnicodeString(stringLength: count, unicodeString: base)
up?.post(tap: .cghidEventTap)
}
}
/// Execute an action by name
func executeAction(_ actionName: String) {
guard let action = ButtonAction(rawValue: actionName) else { return }
switch action {
case .none:
break
case .enterKey:
sendKey(kVK_Return)
case .upKey:
sendKey(kVK_UpArrow)
case .downKey:
sendKey(kVK_DownArrow)
case .escKey:
sendKey(kVK_Escape)
case .ctrlC:
sendKey(kVK_ANSI_C, flags: .maskControl)
case .spaceKey:
sendKey(kVK_Space)
case .rightCmd:
sendModifierTap(kVK_RightCommand, flag: .maskCommand)
case .rightOpt:
sendModifierTap(kVK_RightOption, flag: .maskAlternate)
case .trackpadClick:
performClick()
}
}
private func performClick() {
let pos = NSEvent.mouseLocation
let screenH = NSScreen.main?.frame.height ?? 0
let cgPos = CGPoint(x: pos.x, y: screenH - pos.y)
let down = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: cgPos, mouseButton: .left)
let up = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: cgPos, mouseButton: .left)
down?.post(tap: .cghidEventTap)
usleep(10000)
up?.post(tap: .cghidEventTap)
}
private func sendKey(_ keyCode: Int, flags: CGEventFlags = []) {
let src = CGEventSource(stateID: .hidSystemState)
let down = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(keyCode), keyDown: true)
let up = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(keyCode), keyDown: false)
down?.flags = flags
up?.flags = flags
down?.post(tap: .cghidEventTap)
usleep(10000)
up?.post(tap: .cghidEventTap)
}
/// Tap a modifier key alone (e.g. Right Command) — used to trigger push-to-talk dictation.
private func sendModifierTap(_ keyCode: Int, flag: CGEventFlags) {
let src = CGEventSource(stateID: .hidSystemState)
let down = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(keyCode), keyDown: true)
down?.flags = flag
down?.post(tap: .cghidEventTap)
usleep(10000)
let up = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(keyCode), keyDown: false)
up?.flags = []
up?.post(tap: .cghidEventTap)
}
@objc private func quitApp() {
NSStatusBar.system.removeStatusItem(statusItem)
NSApp.terminate(nil)
}
}