Files
shotdeck/Sources/Shotdeck/main.swift
Claude Fable 5 1ae8351b04 feat(app): open the session window at launch and on reopen; local install script
--window opens the Redline window at launch, so a script or a demo lands on the new UI instead of an idle menu-bar icon. Clicking the running app in Finder, or open -a Redline, now shows the window instead of doing nothing. scripts/install-local.sh installs the locally built app into /Applications, keeps the previous copy at .build/Redline-<version>.app for rollback, and launches it with the window open.
2026-11-01 08:41:03 +04:00

153 lines
6.1 KiB
Swift

import AppKit
import SwiftUI
import ShotdeckCore
// SwiftPM treats a file named main.swift as top-level code, which forbids `@main`.
// App.main() is the equivalent entry point.
if ProcessInfo.processInfo.environment["SHOTDECK_SNAPSHOT_DIR"] != nil {
MainActor.assumeIsolated { PanelSnapshot.runIfRequested() }
}
if SelfTestLaunch.phase == "e2e" {
MainActor.assumeIsolated { PanelSelfTest.runE2EIfRequested() }
}
if ProcessInfo.processInfo.environment["REDLINE_SELFTEST_PHASE"] == "panel" {
MainActor.assumeIsolated { PanelSelfTest.runIfRequested() }
}
if ProcessInfo.processInfo.environment["REDLINE_SELFTEST_PHASE"] == "onedrive" {
MainActor.assumeIsolated { PickerSelfTest.runOneDriveOnlyIfRequested() }
}
if ProcessInfo.processInfo.environment["REDLINE_SELFTEST_PHASE"] == "sendtruth" {
MainActor.assumeIsolated { PickerSelfTest.runSendTruthOnlyIfRequested() }
}
if ProcessInfo.processInfo.environment["SHOTDECK_PICKER_SELFTEST"] != nil {
MainActor.assumeIsolated { PickerSelfTest.runIfRequested() }
}
ShotdeckApp.main()
struct ShotdeckApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
var body: some Scene {
MenuBarExtra {
MenuBarView()
.environment(appDelegate.model)
} label: {
let state = appDelegate.model.iconState
let hasUpdate = appDelegate.model.updateAvailable != nil
HStack(spacing: 4) {
menuBarIcon(for: state, hasUpdate: hasUpdate)
if let count = state.countText {
Text(count).font(.system(size: 11, weight: .semibold))
}
}
.accessibilityLabel("Redline")
}
.menuBarExtraStyle(.window)
}
}
/// The menu-bar symbol for `state`, badged while an update is staged. Uses the
/// SF Symbol's own `.badge` variant when one exists; falls back to a small
/// overlaid dot on the plain symbol otherwise. The badge disappears on its own
/// once `updateAvailable` clears, since this reads live model state.
@ViewBuilder
private func menuBarIcon(for state: MenuIconState, hasUpdate: Bool) -> some View {
if hasUpdate {
let badgeName = "\(state.symbolName).badge"
if NSImage(systemSymbolName: badgeName, accessibilityDescription: nil) != nil {
Image(systemName: badgeName)
} else {
ZStack(alignment: .topTrailing) {
Image(systemName: state.symbolName)
Circle()
.frame(width: 6, height: 6)
.offset(x: 3, y: -3)
}
}
} else {
Image(systemName: state.symbolName)
}
}
@MainActor
final class AppDelegate: NSObject, NSApplicationDelegate {
let model: AppModel
override init() {
NSApplication.shared.setActivationPolicy(.accessory)
model = AppDelegate.makeLaunchModel()
super.init()
}
func applicationDidFinishLaunching(_ notification: Notification) {
Task { await model.bootstrap() }
// `--window` opens the session window at launch (scripts and demos); a normal
// launch stays quiet in the menu bar.
if CommandLine.arguments.contains("--window") {
presentSessionsWindow()
}
}
/// Clicking the app in Finder, or `open -a Redline`, while it runs shows the
/// window instead of doing nothing — standard macOS behaviour for a menu-bar app
/// with a main window.
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool {
if !hasVisibleWindows {
presentSessionsWindow()
}
return true
}
private func presentSessionsWindow() {
(model as SessionsWindowPresenting).presentSessionsWindow(focusLane: false)
}
/// Builds the model exactly the way the real app launches: paths come from
/// `TransportSettings.resolvedAppSupportPaths()` — transport-aware, so the watcher
/// this feeds is never seeded with a stale AirDrop folder while OneDrive is the
/// persisted transport (that was the BLOCKER this function used to have, when it
/// called the AirDrop-only `FolderSettings.resolvedAppSupportPaths()` instead).
/// `appSupportRoot` exists only so PickerSelfTest's relaunch-simulation sub-step can
/// point this at a temp directory instead of the real
/// ~/Library/Application Support/Shotdeck — production always calls this with no
/// argument (the real root). Internal, not private, for that same reason.
static func makeLaunchModel(
appSupportRoot: URL? = nil,
retentionDefaults: UserDefaults = .standard
) -> AppModel {
do {
let paths = try TransportSettings.resolvedAppSupportPaths(root: appSupportRoot)
return try makeModel(paths: paths, retentionDefaults: retentionDefaults)
} catch {
Log.ui.critical(
"AppModel init failed: \(String(describing: error), privacy: .public)"
)
let tmp = FileManager.default.temporaryDirectory
let fallbackRoot = tmp.appendingPathComponent("Shotdeck-fallback", isDirectory: true)
// Safe: temp-dir creation for a path this process controls cannot legitimately fail.
let fallback = try! AppSupportPaths(root: fallbackRoot, outbox: tmp, watchFolder: tmp)
let model = try! makeModel(paths: fallback, retentionDefaults: retentionDefaults)
model.setStatus("Redline could not access its storage folder. Captures will not persist.")
return model
}
}
private static func makeModel(
paths: AppSupportPaths,
retentionDefaults: UserDefaults
) throws -> AppModel {
let ledger = try ReturnLedger(paths: paths)
return AppModel(
paths: paths,
spool: try SpoolStore(paths: paths),
composer: PDFComposer(),
capturer: ScreenCapturer(),
hotkeys: HotkeyCenter(),
picker: RegionPickerController(),
ledger: ledger,
watcher: ReturnWatcher(paths: paths, ledger: ledger),
retentionDefaults: retentionDefaults
)
}
}