@lattices/cli 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +155 -0
  2. package/app/Lattices.app/Contents/Info.plist +24 -0
  3. package/app/Package.swift +13 -0
  4. package/app/Sources/AccessibilityTextExtractor.swift +111 -0
  5. package/app/Sources/ActionRow.swift +61 -0
  6. package/app/Sources/App.swift +10 -0
  7. package/app/Sources/AppDelegate.swift +242 -0
  8. package/app/Sources/AppShellView.swift +62 -0
  9. package/app/Sources/AppTypeClassifier.swift +70 -0
  10. package/app/Sources/AppWindowShell.swift +63 -0
  11. package/app/Sources/CheatSheetHUD.swift +332 -0
  12. package/app/Sources/CommandModeState.swift +1362 -0
  13. package/app/Sources/CommandModeView.swift +1405 -0
  14. package/app/Sources/CommandModeWindow.swift +192 -0
  15. package/app/Sources/CommandPaletteView.swift +307 -0
  16. package/app/Sources/CommandPaletteWindow.swift +134 -0
  17. package/app/Sources/DaemonProtocol.swift +101 -0
  18. package/app/Sources/DaemonServer.swift +414 -0
  19. package/app/Sources/DesktopModel.swift +149 -0
  20. package/app/Sources/DesktopModelTypes.swift +71 -0
  21. package/app/Sources/DiagnosticLog.swift +271 -0
  22. package/app/Sources/EventBus.swift +30 -0
  23. package/app/Sources/HotkeyManager.swift +254 -0
  24. package/app/Sources/HotkeyStore.swift +338 -0
  25. package/app/Sources/InventoryManager.swift +35 -0
  26. package/app/Sources/InventoryPath.swift +43 -0
  27. package/app/Sources/KeyRecorderView.swift +210 -0
  28. package/app/Sources/LatticesApi.swift +1234 -0
  29. package/app/Sources/LayerBezel.swift +203 -0
  30. package/app/Sources/MainView.swift +479 -0
  31. package/app/Sources/MainWindow.swift +83 -0
  32. package/app/Sources/OcrModel.swift +430 -0
  33. package/app/Sources/OcrStore.swift +329 -0
  34. package/app/Sources/OmniSearchState.swift +283 -0
  35. package/app/Sources/OmniSearchView.swift +288 -0
  36. package/app/Sources/OmniSearchWindow.swift +105 -0
  37. package/app/Sources/OrphanRow.swift +129 -0
  38. package/app/Sources/PaletteCommand.swift +419 -0
  39. package/app/Sources/PermissionChecker.swift +125 -0
  40. package/app/Sources/Preferences.swift +99 -0
  41. package/app/Sources/ProcessModel.swift +199 -0
  42. package/app/Sources/ProcessQuery.swift +151 -0
  43. package/app/Sources/Project.swift +28 -0
  44. package/app/Sources/ProjectRow.swift +368 -0
  45. package/app/Sources/ProjectScanner.swift +128 -0
  46. package/app/Sources/ScreenMapState.swift +2387 -0
  47. package/app/Sources/ScreenMapView.swift +2820 -0
  48. package/app/Sources/ScreenMapWindowController.swift +89 -0
  49. package/app/Sources/SessionManager.swift +72 -0
  50. package/app/Sources/SettingsView.swift +1064 -0
  51. package/app/Sources/SettingsWindow.swift +20 -0
  52. package/app/Sources/TabGroupRow.swift +178 -0
  53. package/app/Sources/Terminal.swift +259 -0
  54. package/app/Sources/TerminalQuery.swift +156 -0
  55. package/app/Sources/TerminalSynthesizer.swift +200 -0
  56. package/app/Sources/Theme.swift +163 -0
  57. package/app/Sources/TilePickerView.swift +209 -0
  58. package/app/Sources/TmuxModel.swift +53 -0
  59. package/app/Sources/TmuxQuery.swift +81 -0
  60. package/app/Sources/WindowTiler.swift +1778 -0
  61. package/app/Sources/WorkspaceManager.swift +575 -0
  62. package/bin/client.js +4 -0
  63. package/bin/daemon-client.js +187 -0
  64. package/bin/lattices-app.js +221 -0
  65. package/bin/lattices.js +1551 -0
  66. package/docs/api.md +924 -0
  67. package/docs/app.md +297 -0
  68. package/docs/concepts.md +135 -0
  69. package/docs/config.md +245 -0
  70. package/docs/layers.md +410 -0
  71. package/docs/ocr.md +185 -0
  72. package/docs/overview.md +94 -0
  73. package/docs/quickstart.md +75 -0
  74. package/package.json +42 -0
@@ -0,0 +1,419 @@
1
+ import AppKit
2
+ import Foundation
3
+
4
+ struct PaletteCommand: Identifiable {
5
+ let id: String
6
+ let title: String
7
+ let subtitle: String
8
+ let icon: String
9
+ let category: Category
10
+ let badge: String?
11
+ let action: () -> Void
12
+
13
+ enum Category: String, CaseIterable {
14
+ case project = "Projects"
15
+ case window = "Window"
16
+ case app = "App"
17
+
18
+ var icon: String {
19
+ switch self {
20
+ case .project: return "terminal"
21
+ case .window: return "macwindow"
22
+ case .app: return "gearshape"
23
+ }
24
+ }
25
+ }
26
+
27
+ /// Fuzzy match score — higher is better, 0 means no match
28
+ func matchScore(query: String) -> Int {
29
+ let q = query.lowercased()
30
+ let t = title.lowercased()
31
+ let s = subtitle.lowercased()
32
+
33
+ // Exact prefix match on title — best
34
+ if t.hasPrefix(q) { return 100 }
35
+ // Word-boundary prefix (e.g. "set" matches "Open Settings")
36
+ let words = t.split(separator: " ").map(String.init)
37
+ if words.contains(where: { $0.hasPrefix(q) }) { return 80 }
38
+ // Contains in title
39
+ if t.contains(q) { return 60 }
40
+ // Subtitle prefix
41
+ if s.hasPrefix(q) { return 50 }
42
+ // Subtitle contains
43
+ if s.contains(q) { return 40 }
44
+ // Subsequence match on title
45
+ if isSubsequence(q, of: t) { return 20 }
46
+ return 0
47
+ }
48
+
49
+ private func isSubsequence(_ needle: String, of haystack: String) -> Bool {
50
+ var it = haystack.makeIterator()
51
+ for ch in needle {
52
+ while let next = it.next() {
53
+ if next == ch { break }
54
+ }
55
+ // If iterator is exhausted before matching all chars, not a subsequence
56
+ // (handled by the while loop returning nil)
57
+ }
58
+ // Verify: re-check properly
59
+ var hi = haystack.startIndex
60
+ for ch in needle {
61
+ guard let found = haystack[hi...].firstIndex(of: ch) else { return false }
62
+ hi = haystack.index(after: found)
63
+ }
64
+ return true
65
+ }
66
+ }
67
+
68
+ // MARK: - Command Builder
69
+
70
+ enum CommandBuilder {
71
+ static func build(scanner: ProjectScanner) -> [PaletteCommand] {
72
+ var projectCmds: [PaletteCommand] = []
73
+ var windowCmds: [PaletteCommand] = []
74
+ let terminal = Preferences.shared.terminal
75
+
76
+ for project in scanner.projects {
77
+ if project.isRunning {
78
+ // Project actions
79
+ projectCmds.append(PaletteCommand(
80
+ id: "attach-\(project.id)",
81
+ title: "Attach \(project.name)",
82
+ subtitle: "Open terminal to running session",
83
+ icon: "play.fill",
84
+ category: .project,
85
+ badge: "running",
86
+ action: { SessionManager.launch(project: project) }
87
+ ))
88
+ // Window actions
89
+ windowCmds.append(PaletteCommand(
90
+ id: "goto-\(project.id)",
91
+ title: "Go to \(project.name)",
92
+ subtitle: "Focus the terminal window",
93
+ icon: "macwindow",
94
+ category: .window,
95
+ badge: nil,
96
+ action: {
97
+ WindowTiler.navigateToWindow(
98
+ session: project.sessionName,
99
+ terminal: terminal
100
+ )
101
+ }
102
+ ))
103
+ windowCmds.append(PaletteCommand(
104
+ id: "tile-left-\(project.id)",
105
+ title: "Tile \(project.name) Left",
106
+ subtitle: "Snap window to left half",
107
+ icon: "rectangle.lefthalf.filled",
108
+ category: .window,
109
+ badge: nil,
110
+ action: {
111
+ WindowTiler.tile(session: project.sessionName, terminal: terminal, to: .left)
112
+ }
113
+ ))
114
+ windowCmds.append(PaletteCommand(
115
+ id: "tile-right-\(project.id)",
116
+ title: "Tile \(project.name) Right",
117
+ subtitle: "Snap window to right half",
118
+ icon: "rectangle.righthalf.filled",
119
+ category: .window,
120
+ badge: nil,
121
+ action: {
122
+ WindowTiler.tile(session: project.sessionName, terminal: terminal, to: .right)
123
+ }
124
+ ))
125
+ windowCmds.append(PaletteCommand(
126
+ id: "tile-max-\(project.id)",
127
+ title: "Maximize \(project.name)",
128
+ subtitle: "Expand window to fill screen",
129
+ icon: "rectangle.fill",
130
+ category: .window,
131
+ badge: nil,
132
+ action: {
133
+ WindowTiler.tile(session: project.sessionName, terminal: terminal, to: .maximize)
134
+ }
135
+ ))
136
+ windowCmds.append(PaletteCommand(
137
+ id: "detach-\(project.id)",
138
+ title: "Detach \(project.name)",
139
+ subtitle: "Disconnect clients, keep session alive",
140
+ icon: "eject.fill",
141
+ category: .window,
142
+ badge: nil,
143
+ action: { SessionManager.detach(project: project) }
144
+ ))
145
+ windowCmds.append(PaletteCommand(
146
+ id: "kill-\(project.id)",
147
+ title: "Kill \(project.name)",
148
+ subtitle: "Terminate the tmux session",
149
+ icon: "xmark.circle.fill",
150
+ category: .window,
151
+ badge: nil,
152
+ action: { SessionManager.kill(project: project) }
153
+ ))
154
+ // Recovery commands
155
+ projectCmds.append(PaletteCommand(
156
+ id: "sync-\(project.id)",
157
+ title: "Sync \(project.name)",
158
+ subtitle: "Reconcile session to declared config",
159
+ icon: "arrow.triangle.2.circlepath",
160
+ category: .project,
161
+ badge: nil,
162
+ action: { SessionManager.sync(project: project) }
163
+ ))
164
+ // Per-pane restart commands
165
+ for paneName in project.paneNames {
166
+ projectCmds.append(PaletteCommand(
167
+ id: "restart-\(paneName)-\(project.id)",
168
+ title: "Restart \(paneName) in \(project.name)",
169
+ subtitle: "Kill and re-run the \(paneName) pane",
170
+ icon: "arrow.counterclockwise",
171
+ category: .project,
172
+ badge: nil,
173
+ action: { SessionManager.restart(project: project, paneName: paneName) }
174
+ ))
175
+ }
176
+ } else {
177
+ projectCmds.append(PaletteCommand(
178
+ id: "launch-\(project.id)",
179
+ title: "Launch \(project.name)",
180
+ subtitle: project.paneSummary.isEmpty
181
+ ? (project.devCommand ?? project.path)
182
+ : project.paneSummary,
183
+ icon: "play.circle",
184
+ category: .project,
185
+ badge: nil,
186
+ action: { SessionManager.launch(project: project) }
187
+ ))
188
+ }
189
+ }
190
+
191
+ // Move-to-space commands for running projects
192
+ let allSpaces = WindowTiler.getDisplaySpaces().flatMap(\.spaces)
193
+ if allSpaces.count > 1 {
194
+ for project in scanner.projects where project.isRunning {
195
+ let tag = Terminal.windowTag(for: project.sessionName)
196
+ var windowSpaces: [Int] = []
197
+ if let (w, _) = WindowTiler.findWindow(tag: tag) {
198
+ windowSpaces = WindowTiler.getSpacesForWindow(w)
199
+ }
200
+
201
+ for space in allSpaces {
202
+ let isCurrentSpace = windowSpaces.contains(space.id)
203
+ windowCmds.append(PaletteCommand(
204
+ id: "move-space\(space.index)-\(project.id)",
205
+ title: "Move \(project.name) to Space \(space.index)",
206
+ subtitle: isCurrentSpace ? "Window is already here" : "Move window to Space \(space.index)",
207
+ icon: "rectangle.on.rectangle",
208
+ category: .window,
209
+ badge: isCurrentSpace ? "current" : nil,
210
+ action: {
211
+ let result = WindowTiler.moveWindowToSpace(
212
+ session: project.sessionName,
213
+ terminal: terminal,
214
+ spaceId: space.id
215
+ )
216
+ if case .success = result {
217
+ WindowTiler.switchToSpace(spaceId: space.id)
218
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
219
+ WindowTiler.highlightWindow(session: project.sessionName)
220
+ }
221
+ }
222
+ }
223
+ ))
224
+ }
225
+ }
226
+ }
227
+
228
+ var commands = projectCmds + windowCmds
229
+
230
+ // Layer commands (focus + launch)
231
+ let workspace = WorkspaceManager.shared
232
+ if let wsConfig = workspace.config {
233
+ for (i, layer) in (wsConfig.layers ?? []).enumerated() {
234
+ let layerIndex = i
235
+ let isActive = i == workspace.activeLayerIndex
236
+ let counts = workspace.layerRunningCount(index: i)
237
+ commands.append(PaletteCommand(
238
+ id: "layer-focus-\(layer.id)",
239
+ title: "Focus Layer: \(layer.label)",
240
+ subtitle: "\(counts.running)/\(counts.total) running \u{2014} \u{2325}\(i + 1)",
241
+ icon: "square.stack.3d.up",
242
+ category: .app,
243
+ badge: isActive ? "active" : nil,
244
+ action: { workspace.tileLayer(index: layerIndex) }
245
+ ))
246
+ commands.append(PaletteCommand(
247
+ id: "layer-launch-\(layer.id)",
248
+ title: "Launch Layer: \(layer.label)",
249
+ subtitle: "Start all \(layer.projects.count) project\(layer.projects.count == 1 ? "" : "s")",
250
+ icon: "play.circle",
251
+ category: .app,
252
+ badge: isActive ? "active" : nil,
253
+ action: { workspace.tileLayer(index: layerIndex, launch: true) }
254
+ ))
255
+ }
256
+
257
+ // Tab group commands
258
+ for group in wsConfig.groups ?? [] {
259
+ let isRunning = workspace.isGroupRunning(group)
260
+
261
+ if isRunning {
262
+ commands.append(PaletteCommand(
263
+ id: "group-attach-\(group.id)",
264
+ title: "Attach \(group.label)",
265
+ subtitle: "\(group.tabs.count) tabs",
266
+ icon: "rectangle.stack",
267
+ category: .project,
268
+ badge: "group",
269
+ action: {
270
+ if let firstTab = group.tabs.first {
271
+ let session = WorkspaceManager.sessionName(for: firstTab.path)
272
+ let terminal = Preferences.shared.terminal
273
+ terminal.focusOrAttach(session: session)
274
+ }
275
+ }
276
+ ))
277
+
278
+ // Per-tab focus commands
279
+ for (idx, tab) in group.tabs.enumerated() {
280
+ let tabLabel = tab.label ?? (tab.path as NSString).lastPathComponent
281
+ let tabIndex = idx
282
+ commands.append(PaletteCommand(
283
+ id: "group-tab-\(group.id)-\(idx)",
284
+ title: "\(group.label): \(tabLabel)",
285
+ subtitle: "Focus tab \(idx + 1) in group",
286
+ icon: "rectangle.topthird.inset.filled",
287
+ category: .project,
288
+ badge: nil,
289
+ action: {
290
+ workspace.focusTab(group: group, tabIndex: tabIndex)
291
+ }
292
+ ))
293
+ }
294
+
295
+ commands.append(PaletteCommand(
296
+ id: "group-kill-\(group.id)",
297
+ title: "Kill \(group.label) Group",
298
+ subtitle: "Terminate the group session",
299
+ icon: "xmark.circle.fill",
300
+ category: .window,
301
+ badge: nil,
302
+ action: {
303
+ workspace.killGroup(group)
304
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
305
+ scanner.refreshStatus()
306
+ }
307
+ }
308
+ ))
309
+ } else {
310
+ commands.append(PaletteCommand(
311
+ id: "group-launch-\(group.id)",
312
+ title: "Launch \(group.label)",
313
+ subtitle: "\(group.tabs.count) tabs \u{2014} \(group.tabs.map { $0.label ?? ($0.path as NSString).lastPathComponent }.joined(separator: ", "))",
314
+ icon: "rectangle.stack",
315
+ category: .project,
316
+ badge: "group",
317
+ action: { workspace.launchGroup(group) }
318
+ ))
319
+ }
320
+ }
321
+ }
322
+
323
+ // Orphan session commands
324
+ let inventory = InventoryManager.shared
325
+ for orphan in inventory.orphans {
326
+ commands.append(PaletteCommand(
327
+ id: "orphan-attach-\(orphan.name)",
328
+ title: "Attach \(orphan.name)",
329
+ subtitle: "\(orphan.panes.count) pane\(orphan.panes.count == 1 ? "" : "s") \u{2014} \(orphan.panes.prefix(3).map(\.currentCommand).joined(separator: ", "))",
330
+ icon: "play.fill",
331
+ category: .project,
332
+ badge: "orphan",
333
+ action: {
334
+ let terminal = Preferences.shared.terminal
335
+ terminal.focusOrAttach(session: orphan.name)
336
+ }
337
+ ))
338
+ commands.append(PaletteCommand(
339
+ id: "orphan-kill-\(orphan.name)",
340
+ title: "Kill \(orphan.name)",
341
+ subtitle: "Terminate unmanaged tmux session",
342
+ icon: "xmark.circle.fill",
343
+ category: .window,
344
+ badge: "orphan",
345
+ action: {
346
+ SessionManager.killByName(orphan.name)
347
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
348
+ inventory.refresh()
349
+ }
350
+ }
351
+ ))
352
+ }
353
+
354
+ // App actions
355
+ commands.append(PaletteCommand(
356
+ id: "app-settings",
357
+ title: "Settings",
358
+ subtitle: "Terminal, scan root, mode",
359
+ icon: "gearshape",
360
+ category: .app,
361
+ badge: nil,
362
+ action: {
363
+ SettingsWindowController.shared.show()
364
+ }
365
+ ))
366
+
367
+ commands.append(PaletteCommand(
368
+ id: "app-windows-list",
369
+ title: "Windows List",
370
+ subtitle: "Browse all windows across displays",
371
+ icon: "rectangle.split.2x1",
372
+ category: .app,
373
+ badge: nil,
374
+ action: { CommandModeWindow.shared.show() }
375
+ ))
376
+
377
+ commands.append(PaletteCommand(
378
+ id: "app-screen-map",
379
+ title: "Window Map",
380
+ subtitle: "Visual window editor",
381
+ icon: "rectangle.3.group",
382
+ category: .app,
383
+ badge: nil,
384
+ action: { ScreenMapWindowController.shared.show() }
385
+ ))
386
+
387
+ commands.append(PaletteCommand(
388
+ id: "app-diagnostics",
389
+ title: "Diagnostics",
390
+ subtitle: "View logs and debug info",
391
+ icon: "stethoscope",
392
+ category: .app,
393
+ badge: nil,
394
+ action: { DiagnosticWindow.shared.show() }
395
+ ))
396
+
397
+ commands.append(PaletteCommand(
398
+ id: "app-refresh",
399
+ title: "Refresh Projects",
400
+ subtitle: "Re-scan for .lattices.json configs",
401
+ icon: "arrow.clockwise",
402
+ category: .app,
403
+ badge: nil,
404
+ action: { scanner.scan() }
405
+ ))
406
+
407
+ commands.append(PaletteCommand(
408
+ id: "app-quit",
409
+ title: "Quit Lattices",
410
+ subtitle: "Exit the menu bar app",
411
+ icon: "power",
412
+ category: .app,
413
+ badge: nil,
414
+ action: { NSApp.terminate(nil) }
415
+ ))
416
+
417
+ return commands
418
+ }
419
+ }
@@ -0,0 +1,125 @@
1
+ import AppKit
2
+ import SwiftUI
3
+ import Combine
4
+
5
+ final class PermissionChecker: ObservableObject {
6
+ static let shared = PermissionChecker()
7
+
8
+ @Published var accessibility: Bool = false
9
+ @Published var screenRecording: Bool = false
10
+
11
+ private var pollTimer: Timer?
12
+ private var hasLoggedInitial = false
13
+
14
+ var allGranted: Bool { accessibility && screenRecording }
15
+
16
+ /// Check current permission state, prompting on first launch if not granted.
17
+ func check() {
18
+ let diag = DiagnosticLog.shared
19
+
20
+ let ax = AXIsProcessTrusted()
21
+ let sr = CGPreflightScreenCaptureAccess()
22
+
23
+ // First check: log identity info and prompt if needed
24
+ if !hasLoggedInitial {
25
+ hasLoggedInitial = true
26
+ let bundleId = Bundle.main.bundleIdentifier ?? "<no bundle id>"
27
+ let execPath = Bundle.main.executablePath ?? ProcessInfo.processInfo.arguments.first ?? "<unknown>"
28
+ let pid = ProcessInfo.processInfo.processIdentifier
29
+ diag.info("PermissionChecker: bundleId=\(bundleId) pid=\(pid)")
30
+ diag.info("PermissionChecker: exec=\(execPath)")
31
+ diag.info("AXIsProcessTrusted() → \(ax)")
32
+ diag.info("CGPreflightScreenCaptureAccess() → \(sr)")
33
+
34
+ // Prompt for missing permissions on first check
35
+ if !ax {
36
+ requestAccessibility()
37
+ return
38
+ }
39
+ if !sr {
40
+ requestScreenRecording()
41
+ return
42
+ }
43
+ }
44
+
45
+ // Log on state changes
46
+ if ax != accessibility || sr != screenRecording {
47
+ diag.info("Permissions: Accessibility \(ax ? "✓" : "✗"), Screen Recording \(sr ? "✓" : "✗")")
48
+ }
49
+
50
+ accessibility = ax
51
+ screenRecording = sr
52
+
53
+ // If not all granted, start polling so we detect changes while user is in Settings.
54
+ // Once all granted, stop polling.
55
+ if allGranted {
56
+ stopPolling()
57
+ } else {
58
+ startPolling()
59
+ }
60
+ }
61
+
62
+ /// Request Accessibility permission — shows the system dialog if not yet granted,
63
+ /// which adds lattices to the Accessibility list and asks the user to toggle it on.
64
+ func requestAccessibility() {
65
+ let diag = DiagnosticLog.shared
66
+ let beforeCheck = AXIsProcessTrusted()
67
+ diag.info("requestAccessibility: before=\(beforeCheck), prompting…")
68
+ let opts = [kAXTrustedCheckOptionPrompt.takeUnretainedValue(): true] as CFDictionary
69
+ let result = AXIsProcessTrustedWithOptions(opts)
70
+ diag.info("AXIsProcessTrustedWithOptions(prompt) → \(result)")
71
+ accessibility = result
72
+ if !result {
73
+ diag.warn("Accessibility not granted — opening System Settings. Toggle ON in Privacy → Accessibility.")
74
+ openAccessibilitySettings()
75
+ startPolling()
76
+ }
77
+ }
78
+
79
+ /// Request Screen Recording permission — triggers the system prompt on first call,
80
+ /// which adds lattices to the Screen Recording list. The user toggles it on in Settings.
81
+ func requestScreenRecording() {
82
+ let diag = DiagnosticLog.shared
83
+ let beforeCheck = CGPreflightScreenCaptureAccess()
84
+ diag.info("requestScreenRecording: before=\(beforeCheck), prompting…")
85
+ let result = CGRequestScreenCaptureAccess()
86
+ diag.info("CGRequestScreenCaptureAccess() → \(result)")
87
+ screenRecording = result
88
+ if !result {
89
+ diag.warn("Screen Recording not granted — opening System Settings. Toggle ON in Privacy → Screen Recording.")
90
+ openScreenRecordingSettings()
91
+ startPolling()
92
+ }
93
+ }
94
+
95
+ /// Opens System Settings → Privacy & Security → Accessibility
96
+ func openAccessibilitySettings() {
97
+ if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility") {
98
+ NSWorkspace.shared.open(url)
99
+ }
100
+ }
101
+
102
+ /// Opens System Settings → Privacy & Security → Screen Recording
103
+ func openScreenRecordingSettings() {
104
+ if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture") {
105
+ NSWorkspace.shared.open(url)
106
+ }
107
+ }
108
+
109
+ // MARK: - Polling
110
+
111
+ /// Poll every 2 seconds to detect permission changes made in System Settings.
112
+ private func startPolling() {
113
+ guard pollTimer == nil else { return }
114
+ pollTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in
115
+ DispatchQueue.main.async {
116
+ self?.check()
117
+ }
118
+ }
119
+ }
120
+
121
+ private func stopPolling() {
122
+ pollTimer?.invalidate()
123
+ pollTimer = nil
124
+ }
125
+ }
@@ -0,0 +1,99 @@
1
+ import Foundation
2
+
3
+ enum InteractionMode: String {
4
+ case learning = "learning"
5
+ case auto = "auto"
6
+ }
7
+
8
+ class Preferences: ObservableObject {
9
+ static let shared = Preferences()
10
+
11
+ @Published var terminal: Terminal {
12
+ didSet { UserDefaults.standard.set(terminal.rawValue, forKey: "terminal") }
13
+ }
14
+
15
+ @Published var scanRoot: String {
16
+ didSet { UserDefaults.standard.set(scanRoot, forKey: "scanRoot") }
17
+ }
18
+
19
+ @Published var mode: InteractionMode {
20
+ didSet { UserDefaults.standard.set(mode.rawValue, forKey: "mode") }
21
+ }
22
+
23
+ // MARK: - Search & OCR
24
+
25
+ @Published var ocrEnabled: Bool {
26
+ didSet { UserDefaults.standard.set(!ocrEnabled, forKey: "ocr.disabled") }
27
+ }
28
+
29
+ @Published var ocrQuickInterval: Double {
30
+ didSet { UserDefaults.standard.set(ocrQuickInterval, forKey: "ocr.interval") }
31
+ }
32
+
33
+ @Published var ocrDeepInterval: Double {
34
+ didSet { UserDefaults.standard.set(ocrDeepInterval, forKey: "ocr.deepInterval") }
35
+ }
36
+
37
+ @Published var ocrQuickLimit: Int {
38
+ didSet { UserDefaults.standard.set(ocrQuickLimit, forKey: "ocr.quickLimit") }
39
+ }
40
+
41
+ @Published var ocrDeepLimit: Int {
42
+ didSet { UserDefaults.standard.set(ocrDeepLimit, forKey: "ocr.deepLimit") }
43
+ }
44
+
45
+ @Published var ocrDeepBudget: Int {
46
+ didSet { UserDefaults.standard.set(ocrDeepBudget, forKey: "ocr.deepBudget") }
47
+ }
48
+
49
+ @Published var ocrAccuracy: String {
50
+ didSet { UserDefaults.standard.set(ocrAccuracy, forKey: "ocr.accuracy") }
51
+ }
52
+
53
+ init() {
54
+ if let saved = UserDefaults.standard.string(forKey: "terminal"),
55
+ let t = Terminal(rawValue: saved), t.isInstalled {
56
+ self.terminal = t
57
+ } else {
58
+ self.terminal = Terminal.installed.first ?? .terminal
59
+ }
60
+
61
+ let savedRoot = UserDefaults.standard.string(forKey: "scanRoot") ?? ""
62
+ if savedRoot.isEmpty {
63
+ // Auto-detect a reasonable default
64
+ let home = NSHomeDirectory()
65
+ let candidates = ["\(home)/dev", "\(home)/Developer", "\(home)/projects", "\(home)/src"]
66
+ self.scanRoot = candidates.first { FileManager.default.fileExists(atPath: $0) } ?? ""
67
+ } else {
68
+ self.scanRoot = savedRoot
69
+ }
70
+
71
+ if let saved = UserDefaults.standard.string(forKey: "mode"),
72
+ let m = InteractionMode(rawValue: saved) {
73
+ self.mode = m
74
+ } else {
75
+ self.mode = .learning
76
+ }
77
+
78
+ // Search & OCR
79
+ self.ocrEnabled = !UserDefaults.standard.bool(forKey: "ocr.disabled")
80
+
81
+ let savedInterval = UserDefaults.standard.double(forKey: "ocr.interval")
82
+ self.ocrQuickInterval = savedInterval > 0 ? savedInterval : 60
83
+
84
+ let savedDeep = UserDefaults.standard.double(forKey: "ocr.deepInterval")
85
+ self.ocrDeepInterval = savedDeep > 0 ? savedDeep : 7200
86
+
87
+ let savedQL = UserDefaults.standard.integer(forKey: "ocr.quickLimit")
88
+ self.ocrQuickLimit = savedQL > 0 ? savedQL : 5
89
+
90
+ let savedDL = UserDefaults.standard.integer(forKey: "ocr.deepLimit")
91
+ self.ocrDeepLimit = savedDL > 0 ? savedDL : 15
92
+
93
+ let savedBudget = UserDefaults.standard.integer(forKey: "ocr.deepBudget")
94
+ self.ocrDeepBudget = savedBudget > 0 ? savedBudget : 3
95
+
96
+ let savedAcc = UserDefaults.standard.string(forKey: "ocr.accuracy") ?? "accurate"
97
+ self.ocrAccuracy = savedAcc
98
+ }
99
+ }