Sources/FaceMaker/FaceMakerApp.swift

Swift source for Making faces, 72 lines. Raw file

import AppKit
import SwiftUI

extension Notification.Name {
    /// A photo opened with the app, to find the closest face to. The object is its URL.
    static let faceMakerOpenImage = Notification.Name("FaceMakerOpenImage")
}

/// Quits the app once its windows are closed, and passes on photos opened with
/// it (`open -a FaceMaker photo.jpg`, or dropped on its Dock icon), which the
/// main window treats like a photo dropped on it.
final class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        true
    }

    func application(_ application: NSApplication, open urls: [URL]) {
        for url in urls {
            NotificationCenter.default.post(name: .faceMakerOpenImage, object: url)
        }
    }
}

/// App entry point. A single main window with the face grid + controls, plus
/// a value-driven viewer window opened per face.
@main
struct FaceMakerApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
    @State private var model = AppModel()

    var body: some Scene {
        Window("FaceMaker", id: "main") {
            RootView()
                .environment(model)
                .frame(
                    minWidth: Theme.windowMinWidth,
                    minHeight: Theme.windowMinHeight)
        }
        .defaultSize(
            width: Theme.windowDefaultWidth,
            height: Theme.windowDefaultHeight)
        .windowResizability(.contentMinSize)

        // Non-modal per-face viewer. Each distinct face value gets its own
        // window; re-opening the same face focuses the existing one.
        WindowGroup(id: "viewer", for: ViewerFace.self) { $face in
            if let face {
                FaceViewerView(face: face)
                    .environment(model)  // shared instance — "Use Image" writes back
            }
        }
        .defaultSize(width: 640, height: 700)
        .handlesExternalEvents(matching: [])

        // Editable 512-slider vector for the current model.
        Window("Vector", id: "vector-editor") {
            VectorEditorView()
                .environment(model)
        }
        .defaultSize(width: 420, height: 620)
        .handlesExternalEvents(matching: [])

        // Read-only latent for one saved face.
        WindowGroup(id: "vector-viewer", for: VectorPayload.self) { $payload in
            if let payload {
                VectorViewerView(payload: payload)
            }
        }
        .defaultSize(width: 300, height: 560)
        .handlesExternalEvents(matching: [])
    }
}