Files
OSGKeyboard/OSGKeyboardMac/OSGKeyboardMacApp.swift
T
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

276 lines
12 KiB
Swift

// OSGKeyboardMacApp.swift
// OSGKeyboard · Mac
//
// Entry point. A borderless, System-Settings-style main window plus a
// rock-solid AppKit status-bar item (NSStatusItem) with a dictation popover.
// Light / dark follows the user's Appearance preference (Settings ▸ General).
import AppKit
import SwiftUI
@main
struct OSGKeyboardMacApp: App {
@NSApplicationDelegateAdaptor(MacAppDelegate.self) private var appDelegate
@StateObject private var viewModel = MacDictationViewModel.shared
// Optional managed account: Sign in with Apple, server-held credits, and
// the invitation link. Unlike iOS there is no managed-gateway credential
// handoff to reset on sign-out — the Mac app is BYOK-only for providers,
// so the account only carries identity, credits and referrals.
@StateObject private var accountSession = AccountSessionCoordinator(
dependencies: LiveAccountDependencyFactory.make(),
arePurchasesAvailable: false
)
// Mac-local appearance preference. Drives both the SwiftUI colour scheme
// and — via `applyToApp` — the AppKit window chrome / popover.
@AppStorage(MacAppearancePreference.storageKey) private var appearanceRaw = MacAppearancePreference.system.rawValue
@AppStorage(MacOnboardingState.storageKey) private var hasCompletedMacOnboarding = false
private var appearance: MacAppearancePreference {
MacAppearancePreference(rawValue: appearanceRaw) ?? .system
}
var body: some Scene {
Window("OSGKeyboard", id: "main") {
Group {
if hasCompletedMacOnboarding {
MacRootView(viewModel: viewModel)
} else {
MacOnboardingView(
viewModel: viewModel,
hasCompletedOnboarding: $hasCompletedMacOnboarding
)
}
}
.macSystemPalette()
.environment(\.locale, viewModel.config.uiLanguage.swiftUILocale)
.environmentObject(accountSession)
.preferredColorScheme(appearance.colorScheme)
.task { await viewModel.onAppear() }
.task { await accountSession.restoreIfNeeded() }
.onAppear { MacAppearancePreference.applyToApp(appearance) }
.onChange(of: appearanceRaw) { MacAppearancePreference.applyToApp(appearance) }
.onReceive(NotificationCenter.default.publisher(for: .settingsDidSyncFromCloud)) { _ in
viewModel.reloadConfigFromCloud()
}
.onReceive(NotificationCenter.default.publisher(for: .personalDictionaryDidSyncFromCloud)) { _ in
viewModel.refreshDictionaryFromCloud()
}
.onReceive(NotificationCenter.default.publisher(for: .usageStatisticsDidSyncFromCloud)) { _ in
viewModel.usageStatistics.reloadFromDisk()
}
.onReceive(NotificationCenter.default.publisher(for: .speechHistoryDidSyncFromCloud)) { _ in
viewModel.speechHistory.reloadFromDisk()
}
.onOpenURL { url in
// Invitation links (https://osglab.com/i/<code>). Reaching
// the app this way additionally needs the
// `applinks:osglab.com` associated-domains entitlement on
// the Mac App ID; without it the handler is simply never
// called and sharing your own link still works.
if accountSession.handleIncomingURL(url) {
// Account lives in Settings on Mac (first card).
viewModel.selectedSection = .settings
return
}
#if DEBUG
guard url.scheme == "osgkeyboard", url.host == "seed-demo" else { return }
DemoDataSeeder.seedRichPlaceholderData(
defaults: .standard,
historyDefaults: .standard
)
viewModel.reloadConfigFromCloud()
viewModel.refreshDictionaryFromCloud()
viewModel.usageStatistics.reloadFromDisk()
viewModel.speechHistory.reloadFromDisk()
hasCompletedMacOnboarding = true
#endif
}
}
// Borderless titlebar → content (sidebar + traffic lights) runs to the
// very top, matching macOS System Settings.
.windowStyle(.hiddenTitleBar)
.windowResizability(.contentMinSize)
// Open larger than the minimum: onboarding's hero + step card +
// progress dots + bottom bar do not fit in `windowMin*`, which pushed
// the primary button off screen on first launch.
.defaultSize(width: MacMetrics.windowDefaultWidth, height: MacMetrics.windowDefaultHeight)
}
}
// MARK: - Reopening the main window from AppKit
/// Bridges SwiftUI's `openWindow` action out to AppKit code (the status-bar
/// popover) that has no access to the scene environment.
@MainActor
final class MacWindowBridge {
static let shared = MacWindowBridge()
var open: (() -> Void)?
private init() {}
}
@MainActor
enum MacMainWindow {
/// Bring the app forward and show the main window, recreating it if the
/// user had closed it.
static func open() {
NSApp.activate(ignoringOtherApps: true)
MacWindowBridge.shared.open?()
}
}
// MARK: - Status-bar item (AppKit)
/// Owns the menu-bar `NSStatusItem` and its dictation popover. Implemented in
/// AppKit rather than SwiftUI's `MenuBarExtra` because the latter is flaky
/// when combined with a primary `Window` scene (the icon can silently vanish).
@MainActor
final class MacAppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
private var statusItem: NSStatusItem?
private let popover = NSPopover()
func applicationDidFinishLaunching(_ notification: Notification) {
MacAppearancePreference.applyToApp(.current)
MacTextInsertionService.beginTrackingFrontmostApp()
configurePopover()
configureStatusItem()
MacDictationOverlayController.shared.start(observing: MacDictationViewModel.shared)
CustomLanguageModelManager.shared.prepareInBackgroundIfNeeded()
// The menu bar always follows the *system* appearance, so the status
// item must ignore the app's forced light/dark override. Re-pin the
// button appearance whenever the system theme flips.
DistributedNotificationCenter.default.addObserver(
self,
selector: #selector(systemAppearanceDidChange),
name: NSNotification.Name("AppleInterfaceThemeChangedNotification"),
object: nil
)
}
deinit {
DistributedNotificationCenter.default.removeObserver(self)
}
/// Keep the app alive after the last window closes — it lives in the menu bar.
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
false
}
private func configureStatusItem() {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = item.button {
button.image = Self.makeStatusBarImage()
button.image?.accessibilityDescription = "OSGKeyboard"
button.action = #selector(togglePopover(_:))
button.target = self
}
statusItem = item
applyStatusItemAppearance()
}
/// Builds the menu-bar glyph from the dedicated horizontal status mark.
/// Height is pinned to the tallest practical menu-bar slot so the logo reads
/// clearly; width follows the asset's aspect ratio.
private static func makeStatusBarImage() -> NSImage? {
guard let image = NSImage(named: "OSGStatusMark")
?? NSImage(named: "OSGBrandMark")
?? NSImage(systemSymbolName: "mic.circle.fill", accessibilityDescription: "OSGKeyboard") else {
return nil
}
let height: CGFloat = 9
let aspect = max(image.size.width / max(image.size.height, 1), 1)
image.size = NSSize(width: height * aspect, height: height)
image.isTemplate = true
return image
}
/// Pins the status-bar button to the current *system* appearance so its
/// template image tint matches the real menu-bar background — regardless of
/// the in-app light/dark preference forced on `NSApp.appearance`.
private func applyStatusItemAppearance() {
guard let button = statusItem?.button else { return }
let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")?
.lowercased().contains("dark") ?? false
button.appearance = NSAppearance(named: isDark ? .darkAqua : .aqua)
}
@objc private func systemAppearanceDidChange() {
// The global-domain default lags the notification by a hair; hop to the
// next runloop tick so `AppleInterfaceStyle` reflects the new value.
DispatchQueue.main.async { [weak self] in
self?.applyStatusItemAppearance()
}
}
private func configurePopover() {
popover.delegate = self
popover.behavior = .transient
popover.animates = true
popover.contentSize = NSSize(width: 340, height: 420)
popover.contentViewController = NSHostingController(rootView: MacMenuBarPopover())
}
@objc private func togglePopover(_ sender: Any?) {
guard let button = statusItem?.button else { return }
if popover.isShown {
popover.performClose(sender)
} else {
// Capture before activation: once the popover becomes key,
// NSWorkspace reports OSGKeyboard instead of the user's target.
MacDictationViewModel.shared.prepareForPopoverPresentation()
NSApp.activate(ignoringOtherApps: true)
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
popover.contentViewController?.view.window?.makeKey()
}
}
func popoverDidClose(_ notification: Notification) {
MacDictationViewModel.shared.clearPreparedPopoverTarget()
}
}
/// SwiftUI content hosted inside the status-bar popover. Shares the single
/// view model and follows the same appearance preference as the main window.
private struct MacMenuBarPopover: View {
@ObservedObject private var viewModel = MacDictationViewModel.shared
@AppStorage(MacAppearancePreference.storageKey) private var appearanceRaw = MacAppearancePreference.system.rawValue
@AppStorage(MacOnboardingState.storageKey) private var hasCompletedMacOnboarding = false
var body: some View {
Group {
if hasCompletedMacOnboarding {
MacContentView(viewModel: viewModel)
} else {
onboardingPrompt
}
}
.frame(width: 340)
.macSystemPalette()
.environment(\.locale, viewModel.config.uiLanguage.swiftUILocale)
.preferredColorScheme(MacAppearancePreference(rawValue: appearanceRaw)?.colorScheme ?? nil)
}
private var onboardingPrompt: some View {
VStack(spacing: Spacing.md) {
Image(systemName: "sparkles")
.font(TypeStyle.pageTitle)
.foregroundStyle(.accent)
Text(MacL10n.string("mac.onboarding.popover.title", language: viewModel.config.uiLanguage))
.font(TypeStyle.headline)
Text(MacL10n.string("mac.onboarding.popover.subtitle", language: viewModel.config.uiLanguage))
.font(TypeStyle.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
Button(MacL10n.string("mac.openWindow", language: viewModel.config.uiLanguage)) {
MacMainWindow.open()
}
.buttonStyle(.borderedProminent)
}
.padding(Spacing.lg)
}
}