SPB Git

Repository Public

MCCApp.swift 3.6 KB 96 lines swift

1import MCCCore2import SwiftUI34@main5struct MCCApp: App {6    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate7    @State private var app = AppModel()89    var body: some Scene {10        // One scene per window: an iPad can show several, each with its own navigation state.11        WindowGroup {12            SceneRoot()13                .environment(app)14                .environment(app.session)15                .environment(\.appModel, app)16                .task {17                    AppDelegate.push = app.push18                    await app.start()19                }20        }21        .commands { AppCommands() }22    }23}2425/// Root of a scene: owns the scene's navigation, follows the scene phase, receives links.26struct SceneRoot: View {27    @Environment(AppModel.self) private var app28    @Environment(\.scenePhase) private var phase29    @State private var nav = Navigator()30    @State private var wasActive = false31    @SceneStorage("mcc.navigation") private var stored = ""3233    var body: some View {34        RootView()35            .environment(nav)36            .focusedSceneValue(\.navigator, nav)37            .preferredColorScheme(app.appearance.colorScheme)38            .environment(\.locale, Localization.shared.locale)39            .mccThemed()40            .onOpenURL { app.handle(url: $0) }41            .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { activity in42                if let url = activity.webpageURL { app.handle(url: url) }43            }44            .onAppear {45                if let s = NavigationSnapshot.decode(stored) { nav.snapshot = s }46                phaseChanged(phase)47            }48            .onChange(of: phase) { _, new in phaseChanged(new) }49            .onChange(of: nav.snapshot) { _, new in stored = new.encoded }50            .onDisappear { if wasActive { wasActive = false; app.sceneResignedActive(toBackground: true) } }51    }5253    private func phaseChanged(_ new: ScenePhase) {54        switch new {55        case .active:56            if !wasActive { wasActive = true; app.lock.appDidBecomeActive(); app.sceneBecameActive() }57        case .inactive:58            // Transient (app switcher, system sheet such as the passkey prompt): keep the stream.59            break60        case .background:61            if wasActive { wasActive = false; app.sceneResignedActive(toBackground: true) }62        @unknown default: break63        }64    }65}6667private struct NavigatorKey: FocusedValueKey { typealias Value = Navigator }68extension FocusedValues {69    var navigator: Navigator? {70        get { self[NavigatorKey.self] }71        set { self[NavigatorKey.self] = newValue }72    }73}7475/// Keyboard shortcuts (iPad with a keyboard): sections, refresh, search, quick note.76struct AppCommands: Commands {77    @FocusedValue(\.navigator) private var nav7879    var body: some Commands {80        CommandMenu(tr("commands.go")) {81            ForEach(AppSection.allCases) { s in82                if let i = s.shortcutIndex {83                    Button(s.title) { nav?.show(s) }.keyboardShortcut(KeyEquivalent(Character("\(i)")), modifiers: .command)84                } else {85                    Button(s.title) { nav?.show(s) }86                }87            }88        }89        CommandGroup(after: .toolbar) {90            Button(tr("commands.refresh")) { nav?.refreshToken += 1 }.keyboardShortcut("r", modifiers: .command)91            Button(tr("commands.search")) { nav?.sheet = .search }.keyboardShortcut("f", modifiers: .command)92            Button(tr("commands.quick_note")) { nav?.sheet = .noteEditor(NoteContext()) }.keyboardShortcut("n", modifiers: [.command, .shift])93        }94    }95}96
Files main
Menu

Theme