SPB Git

Repository Public MIT

RootView.swift 6.4 KB 141 lines swift

1// ORIX Calc2// File: App/Sources/RootView.swift3// Description: The adaptive shell — a mode strip on iPhone, a sidebar with an optional companion calculator on iPad.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import ORIXUI9import ORIXFeatureCalculator1011struct RootView: View {12    @Environment(AppModel.self) private var app13    @Environment(\.horizontalSizeClass) private var horizontalSizeClass14    let calculator: CalculatorModel15    let options: LaunchOptions1617    @State private var showHistory = false18    @State private var showSettings = false19    @State private var showCamera = false20    @State private var notice: String?21    @State private var companion = false22    @State private var sidebarExpanded = true2324    var body: some View {25        @Bindable var app = app26        Group {27            if horizontalSizeClass == .regular { regularLayout } else { compactLayout }28        }29        .background(ORIXColor.background.ignoresSafeArea())30        .environment(\.graphPreview, FeatureRegistry.graphPreview)31        .tint(app.mode.tint)32        .sheet(item: $app.presentedDetail) { ResultDetailView($0).presentationDetents([.medium, .large]) }33        .sheet(isPresented: $showHistory) { HistoryView().environment(app) }34        .sheet(isPresented: $showSettings) { SettingsView().environment(app) }35        .sheet(isPresented: $showCamera) { MathCameraView().environment(app) }36        .alert("ORIX Calc", isPresented: Binding(get: { notice != nil }, set: { if !$0 { notice = nil } })) {37            Button("OK", role: .cancel) {}38        } message: { Text(notice ?? "") }39        .onReceive(NotificationCenter.default.publisher(for: .orixShowHistory)) { _ in showHistory = true }40        .onReceive(NotificationCenter.default.publisher(for: .orixNotice)) { notice = $0.object as? String }41        .onReceive(NotificationCenter.default.publisher(for: Notification.Name("orix.requestedMode"))) { note in42            if let raw = note.object as? String, let mode = CalcMode(rawValue: raw) { app.mode = mode }43            UserDefaults.standard.removeObject(forKey: "orix.calc.requestedMode")44        }45        .onAppear {46            companion = options.companion47            if let n = app.storeNotice { notice = n }48            if let raw = UserDefaults.standard.string(forKey: "orix.calc.requestedMode"), let mode = CalcMode(rawValue: raw) {49                app.mode = mode50                UserDefaults.standard.removeObject(forKey: "orix.calc.requestedMode")51            }52        }53    }5455    private func content(_ mode: CalcMode) -> some View {56        FeatureRegistry.screen(for: mode, calculator: calculator, options: options)57            .id(mode)58            .transition(.opacity)59    }6061    // MARK: iPhone6263    private var compactLayout: some View {64        VStack(spacing: 0) {65            ModeStrip(showHistory: $showHistory, showSettings: $showSettings, showCamera: $showCamera)66            // Every visited environment stays alive so that switching back restores its exact state.67            PersistentModeStack(calculator: calculator, options: options)68        }69    }7071    // MARK: iPad7273    private var regularLayout: some View {74        GeometryReader { geo in75            regularContent76                // In portrait the full sidebar would squeeze the multi-zone environments: start as an icon rail there.77                .onAppear { if geo.size.width < 1100 { sidebarExpanded = false } }78                .onChange(of: geo.size.width < 1100) { _, narrow in withAnimation(ORIXMotion.mode) { sidebarExpanded = !narrow } }79        }80    }8182    private var regularContent: some View {83        HStack(spacing: 0) {84            // The sidebar never disappears: it collapses to an icon rail, so every environment stays one tap away in any orientation.85            ModeSidebar(expanded: $sidebarExpanded, showHistory: $showHistory, showSettings: $showSettings, showCamera: $showCamera, companion: $companion)86                .frame(width: sidebarExpanded ? 272 : 68)87            Divider()88            let isCalculator = app.mode == .standard || app.mode == .scientific89            if companion && !isCalculator {90                CalculatorScreen(model: calculator, kind: .scientific, compactPane: true)91                    .frame(width: 370)92                    .transition(.move(edge: .leading).combined(with: .opacity))93                Divider()94            }95            Group {96                if isCalculator {97                    // A calculator alone on a 13-inch screen would be absurdly large: keep it at a comfortable size beside the history.98                    HStack(spacing: 0) {99                        CalculatorScreen(model: calculator, kind: app.mode == .standard ? .standard : .scientific, compactPane: true)100                            .frame(maxWidth: 520)101                            .frame(maxWidth: .infinity)102                        Divider()103                        HistoryView(embedded: true).frame(width: 340)104                    }105                } else {106                    PersistentModeStack(calculator: calculator, options: options)107                }108            }109            .frame(maxWidth: .infinity, maxHeight: .infinity)110        }111        .animation(ORIXMotion.mode, value: companion)112        .animation(ORIXMotion.mode, value: sidebarExpanded)113    }114}115116/// Keeps each environment that has been opened in the hierarchy (hidden when inactive) so that its state, scroll position117/// and in-flight work survive a switch. Calculators are cheap and stateless outside their shared model, so they are rebuilt.118struct PersistentModeStack: View {119    @Environment(AppModel.self) private var app120    let calculator: CalculatorModel121    let options: LaunchOptions122    @State private var visited: Set<CalcMode> = []123124    var body: some View {125        ZStack {126            ForEach(CalcMode.allCases) { mode in127                if mode == app.mode || visited.contains(mode) {128                    FeatureRegistry.screen(for: mode, calculator: calculator, options: options)129                        .opacity(mode == app.mode ? 1 : 0)130                        .allowsHitTesting(mode == app.mode)131                        .accessibilityHidden(mode != app.mode)132                        .zIndex(mode == app.mode ? 1 : 0)133                }134            }135        }136        .animation(ORIXMotion.mode, value: app.mode)137        .onAppear { visited.insert(app.mode) }138        .onChange(of: app.mode) { _, new in visited.insert(new) }139    }140}141
Files main
Menu

Theme