SPB Git

Repository Public License

SceneRoot.swift 6.6 KB 169 lines swift

1import LunarDesign2import NavigatorCore3import SwiftUI45extension FocusedValues {6    /// The browser window of the focused scene, for menu commands.7    @Entry public var browserWindow: WindowModel?8}910/// Root view of a scene (a Mac window, an iPad window, the iPhone screen).11///12/// `windowID` is the scene's value: `nil` for a scene the system opened by itself (it then receives a restored13/// window, or a new one), or the identifier passed to `openWindow(value:)`.14public struct NavigatorSceneView: View {15    @Binding private var windowID: WindowID?16    private let environment: AppEnvironment1718    public init(windowID: Binding<WindowID?>, environment: AppEnvironment = .shared) {19        self._windowID = windowID20        self.environment = environment21    }2223    public var body: some View {24        Group {25            switch environment.phase {26            case .launching:27                LaunchView()28            case .failed(let message):29                LaunchFailureView(message: message)30            case .ready(let store):31                ReadySceneView(windowID: $windowID, environment: environment, store: store)32                    .environment(store)33            }34        }35        .lunarTheme(environment.themeFamily, appearance: environment.appearance)36        .task { environment.start() }37    }38}3940private struct LaunchView: View {41    var body: some View {42        ZStack {43            LunarWindowBackground()44            LunarMarkView(size: 88).accessibilityHidden(true)45        }46        .accessibilityIdentifier("launch")47    }48}4950private struct LaunchFailureView: View {51    let message: String5253    var body: some View {54        ZStack {55            LunarWindowBackground()56            LunarErrorState(title: L("ORIX Navigator could not open its data"), message: message)57                .padding(LunarSpacing.xl)58        }59    }60}6162private struct ReadySceneView: View {63    @Environment(\.lunarTheme) private var theme64    @Environment(\.horizontalSizeClass) private var sizeClass65    @Environment(\.openWindow) private var openWindow66    @Environment(\.scenePhase) private var scenePhase67    @Environment(\.supportsMultipleWindows) private var supportsMultipleWindows68    @Binding var windowID: WindowID?69    let environment: AppEnvironment70    let store: BrowserStore71    @State private var window: WindowModel?72    @State private var isFullScreen = false7374    var body: some View {75        ZStack {76            if let window {77                content(window)78                    // Covered by the welcome screens: not reachable by assistive technologies either.79                    .accessibilityHidden(environment.isOnboardingVisible)80                    .focusedSceneValue(\.browserWindow, window)81                    .sheet(item: Binding(get: { window.bookmarkDraft }, set: { window.bookmarkDraft = $0 })) { draft in82                        BookmarkEditor(window: window, draft: draft)83                            .environment(store)84                            .lunarTheme(theme)85                            .presentationDetents([.medium, .large])86                    }87                    #if os(iOS)88                    .sheet(item: Binding(get: { window.shareRequest }, set: { window.shareRequest = $0 })) { request in89                        ActivityView(items: [request.url]).presentationDetents([.medium, .large])90                    }91                    #endif92                WindowOverlayHost(window: window)93                    .accessibilityHidden(environment.isOnboardingVisible)94                    .zIndex(5)95            } else {96                LunarWindowBackground()97            }98            if environment.isOnboardingVisible {99                OnboardingView(environment: environment, settings: store.settings)100                    .transition(.opacity)101                    .zIndex(10)102            }103        }104        .lunarAnimation(.smooth, value: environment.isOnboardingVisible)105        .task { attach() }106        .onChange(of: scenePhase) { _, phase in107            if phase == .active, let window { store.activeWindowID = window.id }108        }109        // A platform with one scene shows a newly opened window (a private window on iPhone) by switching to it,110        // and falls back to another window when the one it shows is closed.111        .onChange(of: store.sceneSwitchRequest) { _, target in112            guard let target, let window, store.activeWindowID == window.id || store.windows.count <= 2,113                  store.window(target) != nil else { return }114            show(store.claimWindow(id: target))115            store.sceneSwitchHandled()116        }117        .onChange(of: store.windows.map(\.id)) { _, ids in118            guard let window, !ids.contains(window.id), !store.isTerminating, store.requestCloseWindow == nil else { return }119            show(store.claimWindow())120        }121    }122123    private func show(_ next: WindowModel) {124        window = next125        windowID = next.id126        store.activeWindowID = next.id127        #if os(macOS)128        environment.macSupport?.install(on: next)129        #endif130    }131132    @ViewBuilder private func content(_ window: WindowModel) -> some View {133        #if os(macOS)134        RegularBrowserView(window: window)135            .environment(\.windowControlsInset, isFullScreen ? 0 : WindowControls.reservedWidth)136            .background {137                WindowAccessor(window: window, background: NSColor(theme.windowBackground.color),138                               initialSize: environment.options.windowSize, isFullScreen: $isFullScreen)139            }140            .ignoresSafeArea()141        #else142        if sizeClass == .compact {143            PhoneBrowserView(window: window)144        } else {145            RegularBrowserView(window: window)146        }147        #endif148    }149150    private func attach() {151        guard window == nil else { return }152        let claimed = windowID.map { store.claimWindow(id: $0) } ?? store.claimWindow()153        window = claimed154        if windowID != claimed.id { windowID = claimed.id }155        store.activeWindowID = store.activeWindowID ?? claimed.id156        environment.applyLaunchPresentation(to: claimed)157        #if os(macOS)158        environment.macSupport?.install(on: claimed)159        store.requestNewScene = { id in openWindow(value: id) }160        // Windows restored from the previous session each get their own scene.161        for id in store.pendingWindowIDs { openWindow(value: id) }162        #else163        // iPad opens another scene for a new window; iPhone has one scene, which switches window instead.164        store.supportsMultipleWindows = supportsMultipleWindows165        if supportsMultipleWindows { store.requestNewScene = { id in openWindow(value: id) } }166        #endif167    }168}169
Files main
Menu

Theme