@arach/lattices 0.1.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 (64) hide show
  1. package/README.md +157 -0
  2. package/app/Lattices.app/Contents/Info.plist +24 -0
  3. package/app/Package.swift +13 -0
  4. package/app/Sources/App.swift +49 -0
  5. package/app/Sources/AppDelegate.swift +104 -0
  6. package/app/Sources/AppShellView.swift +62 -0
  7. package/app/Sources/AppTypeClassifier.swift +70 -0
  8. package/app/Sources/AppWindowShell.swift +63 -0
  9. package/app/Sources/CheatSheetHUD.swift +331 -0
  10. package/app/Sources/CommandModeState.swift +1341 -0
  11. package/app/Sources/CommandModeView.swift +1380 -0
  12. package/app/Sources/CommandModeWindow.swift +192 -0
  13. package/app/Sources/CommandPaletteView.swift +307 -0
  14. package/app/Sources/CommandPaletteWindow.swift +134 -0
  15. package/app/Sources/DaemonProtocol.swift +101 -0
  16. package/app/Sources/DaemonServer.swift +406 -0
  17. package/app/Sources/DesktopModel.swift +121 -0
  18. package/app/Sources/DesktopModelTypes.swift +71 -0
  19. package/app/Sources/DiagnosticLog.swift +253 -0
  20. package/app/Sources/EventBus.swift +29 -0
  21. package/app/Sources/HotkeyManager.swift +249 -0
  22. package/app/Sources/HotkeyStore.swift +330 -0
  23. package/app/Sources/InventoryManager.swift +35 -0
  24. package/app/Sources/InventoryPath.swift +43 -0
  25. package/app/Sources/KeyRecorderView.swift +210 -0
  26. package/app/Sources/LatticesApi.swift +915 -0
  27. package/app/Sources/MainView.swift +507 -0
  28. package/app/Sources/MainWindow.swift +70 -0
  29. package/app/Sources/OrphanRow.swift +129 -0
  30. package/app/Sources/PaletteCommand.swift +409 -0
  31. package/app/Sources/PermissionChecker.swift +115 -0
  32. package/app/Sources/Preferences.swift +48 -0
  33. package/app/Sources/ProcessModel.swift +199 -0
  34. package/app/Sources/ProcessQuery.swift +151 -0
  35. package/app/Sources/Project.swift +28 -0
  36. package/app/Sources/ProjectRow.swift +368 -0
  37. package/app/Sources/ProjectScanner.swift +121 -0
  38. package/app/Sources/ScreenMapState.swift +2397 -0
  39. package/app/Sources/ScreenMapView.swift +2817 -0
  40. package/app/Sources/ScreenMapWindowController.swift +89 -0
  41. package/app/Sources/SessionManager.swift +72 -0
  42. package/app/Sources/SettingsView.swift +641 -0
  43. package/app/Sources/SettingsWindow.swift +20 -0
  44. package/app/Sources/TabGroupRow.swift +178 -0
  45. package/app/Sources/Terminal.swift +259 -0
  46. package/app/Sources/TerminalQuery.swift +156 -0
  47. package/app/Sources/TerminalSynthesizer.swift +200 -0
  48. package/app/Sources/Theme.swift +124 -0
  49. package/app/Sources/TilePickerView.swift +209 -0
  50. package/app/Sources/TmuxModel.swift +53 -0
  51. package/app/Sources/TmuxQuery.swift +81 -0
  52. package/app/Sources/WindowTiler.swift +1752 -0
  53. package/app/Sources/WorkspaceManager.swift +434 -0
  54. package/bin/daemon-client.js +187 -0
  55. package/bin/lattices-app.js +205 -0
  56. package/bin/lattices.js +1295 -0
  57. package/docs/api.md +707 -0
  58. package/docs/app.md +250 -0
  59. package/docs/concepts.md +225 -0
  60. package/docs/config.md +234 -0
  61. package/docs/layers.md +317 -0
  62. package/docs/overview.md +74 -0
  63. package/docs/quickstart.md +82 -0
  64. package/package.json +38 -0
@@ -0,0 +1,409 @@
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-screen-map",
369
+ title: "Screen Map",
370
+ subtitle: "Visual window editor",
371
+ icon: "rectangle.3.group",
372
+ category: .app,
373
+ badge: nil,
374
+ action: { ScreenMapWindowController.shared.show() }
375
+ ))
376
+
377
+ commands.append(PaletteCommand(
378
+ id: "app-diagnostics",
379
+ title: "Diagnostics",
380
+ subtitle: "View logs and debug info",
381
+ icon: "stethoscope",
382
+ category: .app,
383
+ badge: nil,
384
+ action: { DiagnosticWindow.shared.show() }
385
+ ))
386
+
387
+ commands.append(PaletteCommand(
388
+ id: "app-refresh",
389
+ title: "Refresh Projects",
390
+ subtitle: "Re-scan for .lattices.json configs",
391
+ icon: "arrow.clockwise",
392
+ category: .app,
393
+ badge: nil,
394
+ action: { scanner.scan() }
395
+ ))
396
+
397
+ commands.append(PaletteCommand(
398
+ id: "app-quit",
399
+ title: "Quit Lattices",
400
+ subtitle: "Exit the menu bar app",
401
+ icon: "power",
402
+ category: .app,
403
+ badge: nil,
404
+ action: { NSApp.terminate(nil) }
405
+ ))
406
+
407
+ return commands
408
+ }
409
+ }
@@ -0,0 +1,115 @@
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 without prompting.
17
+ func check() {
18
+ let diag = DiagnosticLog.shared
19
+
20
+ let ax = AXIsProcessTrusted()
21
+ let sr = CGPreflightScreenCaptureAccess()
22
+
23
+ // First check: log detailed identity info to help debug TCC issues
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
+
35
+ // Log on state changes
36
+ if ax != accessibility || sr != screenRecording {
37
+ diag.info("Permissions: Accessibility \(ax ? "✓" : "✗"), Screen Recording \(sr ? "✓" : "✗")")
38
+ }
39
+
40
+ accessibility = ax
41
+ screenRecording = sr
42
+
43
+ // If not all granted, start polling so we detect changes while user is in Settings.
44
+ // Once all granted, stop polling.
45
+ if allGranted {
46
+ stopPolling()
47
+ } else {
48
+ startPolling()
49
+ }
50
+ }
51
+
52
+ /// Request Accessibility permission — shows the system dialog if not yet granted,
53
+ /// which adds lattices to the Accessibility list and asks the user to toggle it on.
54
+ func requestAccessibility() {
55
+ let diag = DiagnosticLog.shared
56
+ let beforeCheck = AXIsProcessTrusted()
57
+ diag.info("requestAccessibility: before=\(beforeCheck), prompting…")
58
+ let opts = [kAXTrustedCheckOptionPrompt.takeUnretainedValue(): true] as CFDictionary
59
+ let result = AXIsProcessTrustedWithOptions(opts)
60
+ diag.info("AXIsProcessTrustedWithOptions(prompt) → \(result)")
61
+ accessibility = result
62
+ if !result {
63
+ diag.warn("Accessibility not granted — opening System Settings. Toggle ON in Privacy → Accessibility.")
64
+ openAccessibilitySettings()
65
+ startPolling()
66
+ }
67
+ }
68
+
69
+ /// Request Screen Recording permission — triggers the system prompt on first call,
70
+ /// which adds lattices to the Screen Recording list. The user toggles it on in Settings.
71
+ func requestScreenRecording() {
72
+ let diag = DiagnosticLog.shared
73
+ let beforeCheck = CGPreflightScreenCaptureAccess()
74
+ diag.info("requestScreenRecording: before=\(beforeCheck), prompting…")
75
+ let result = CGRequestScreenCaptureAccess()
76
+ diag.info("CGRequestScreenCaptureAccess() → \(result)")
77
+ screenRecording = result
78
+ if !result {
79
+ diag.warn("Screen Recording not granted — opening System Settings. Toggle ON in Privacy → Screen Recording.")
80
+ openScreenRecordingSettings()
81
+ startPolling()
82
+ }
83
+ }
84
+
85
+ /// Opens System Settings → Privacy & Security → Accessibility
86
+ func openAccessibilitySettings() {
87
+ if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility") {
88
+ NSWorkspace.shared.open(url)
89
+ }
90
+ }
91
+
92
+ /// Opens System Settings → Privacy & Security → Screen Recording
93
+ func openScreenRecordingSettings() {
94
+ if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture") {
95
+ NSWorkspace.shared.open(url)
96
+ }
97
+ }
98
+
99
+ // MARK: - Polling
100
+
101
+ /// Poll every 2 seconds to detect permission changes made in System Settings.
102
+ private func startPolling() {
103
+ guard pollTimer == nil else { return }
104
+ pollTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in
105
+ DispatchQueue.main.async {
106
+ self?.check()
107
+ }
108
+ }
109
+ }
110
+
111
+ private func stopPolling() {
112
+ pollTimer?.invalidate()
113
+ pollTimer = nil
114
+ }
115
+ }
@@ -0,0 +1,48 @@
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
+ init() {
24
+ if let saved = UserDefaults.standard.string(forKey: "terminal"),
25
+ let t = Terminal(rawValue: saved), t.isInstalled {
26
+ self.terminal = t
27
+ } else {
28
+ self.terminal = Terminal.installed.first ?? .terminal
29
+ }
30
+
31
+ let savedRoot = UserDefaults.standard.string(forKey: "scanRoot") ?? ""
32
+ if savedRoot.isEmpty {
33
+ // Auto-detect a reasonable default
34
+ let home = NSHomeDirectory()
35
+ let candidates = ["\(home)/dev", "\(home)/Developer", "\(home)/projects", "\(home)/src"]
36
+ self.scanRoot = candidates.first { FileManager.default.fileExists(atPath: $0) } ?? ""
37
+ } else {
38
+ self.scanRoot = savedRoot
39
+ }
40
+
41
+ if let saved = UserDefaults.standard.string(forKey: "mode"),
42
+ let m = InteractionMode(rawValue: saved) {
43
+ self.mode = m
44
+ } else {
45
+ self.mode = .learning
46
+ }
47
+ }
48
+ }