Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Apps/LocalChatMac/LocalChatMacApp.swift3// Description: Local Chat for macOS: multi-window scenes, menu commands, menu bar extra, settings window.4import SwiftUI5import LocalChatCore6import LocalChatUI78@main9struct LocalChatMacApp: App {10 @NSApplicationDelegateAdaptor(AppDelegate.self) private var delegate11 @State private var app = AppBootstrap.make()1213 var body: some Scene {14 WindowGroup("Local Chat", id: "main") {15 AppBootstrap.root(app)16 .frame(minWidth: 860, minHeight: 560)17 }18 .defaultSize(width: 1240, height: 800)19 .windowToolbarStyle(.unified)20 .commands { LocalChatCommands(app: app.model) }2122 MenuBarExtra {23 if let model = app.model { MenuBarPanel().environment(model) } else { Text("Local Chat could not start.") }24 } label: {25 Image(systemName: (app.model?.isAnyGenerationRunning ?? false) ? "sparkles" : "sparkle")26 }27 .menuBarExtraStyle(.window)2829 Settings {30 if let model = app.model { SettingsScreen().environment(model).frame(minWidth: 820, minHeight: 560) }31 }32 }33}3435final class AppDelegate: NSObject, NSApplicationDelegate {36 func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false } // the menu bar item and the API server keep running37 func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { true }38}3940struct LocalChatCommands: Commands {41 let app: AppModel?42 @Environment(\.openWindow) private var openWindow4344 var body: some Commands {45 CommandGroup(replacing: .newItem) {46 Button("New Window") { openWindow(id: "main") }.keyboardShortcut("n", modifiers: [.command, .shift])47 }48 CommandMenu("Model") {49 Button("Unload All Models") { Task { await app?.engine.unloadAll() } }.keyboardShortcut("u", modifiers: [.command, .shift])50 Button("Refresh Remote Nodes") { Task { await app?.refreshNodes() } }51 Divider()52 if let app {53 ForEach(app.snapshot.loaded) { loaded in54 Button("Unload \(loaded.descriptor.name)") { Task { await app.engine.unload(loaded.id) } }55 }56 }57 }58 }59}6061/// Quick status from the menu bar: what is loaded, what is generating, measured memory — and shortcuts.62struct MenuBarPanel: View {63 @Environment(AppModel.self) private var app64 @Environment(\.openWindow) private var openWindow6566 var body: some View {67 VStack(alignment: .leading, spacing: 10) {68 HStack {69 Image(systemName: "sparkles").foregroundStyle(DS.G.aurora)70 Text("Local Chat").font(DS.F.headline)71 Spacer()72 if app.server.isRunning { Pill("API on", symbol: "network", tone: .ok) }73 }74 if let active = app.snapshot.active.first {75 HStack {76 ThinkingDots()77 Text("Generating with \(active.modelName)").font(DS.F.callout).lineLimit(1)78 Spacer()79 if let rate = active.liveRate { Text("\(MetricFormat.rate(rate)) /s").font(DS.F.mono(11)).foregroundStyle(DS.C.accent) }80 }81 }82 if app.snapshot.loaded.isEmpty {83 Text("No model in memory").font(DS.F.caption).foregroundStyle(DS.C.muted)84 } else {85 ForEach(app.snapshot.loaded) { loaded in86 HStack {87 Circle().fill(DS.C.ok).frame(width: 6, height: 6)88 Text(loaded.descriptor.name).font(DS.F.callout).lineLimit(1)89 Spacer()90 Text(MetricFormat.memory(loaded.info.weightsMemoryBytes)).font(DS.F.mono(10.5)).foregroundStyle(DS.C.muted)91 Button { Task { await app.engine.unload(loaded.id) } } label: { Image(systemName: "eject") }.buttonStyle(.plain).help("Unload")92 }93 }94 }95 if let footprint = app.snapshot.processFootprintBytes {96 Text("Memory in use by Local Chat: \(ByteFormat.memory(footprint))").font(DS.F.micro).foregroundStyle(DS.C.muted)97 }98 Divider()99 HStack {100 Button("Open Local Chat") { openWindow(id: "main"); NSApp.activate(ignoringOtherApps: true) }.buttonStyle(.lcPrimaryCompact)101 Spacer()102 Button("Quit") { NSApp.terminate(nil) }.buttonStyle(.lcSecondaryCompact)103 }104 }105 .padding(14).frame(width: 330)106 }107}108