SPB Git

Repository Public

RootView.swift 9.7 KB 247 lines swift

1import MCCCore2import SwiftUI34/// Chooses the root of a scene: start-up, invitation setup, sign-in, the shell — with the lock screen on top.5struct RootView: View {6    @Environment(AppModel.self) private var app7    @Environment(Navigator.self) private var nav8    @Environment(\.mccTheme) private var theme9    @Session private var session1011    var body: some View {12        ZStack {13            theme.canvas.ignoresSafeArea()14            content15            if app.lock.isLocked { LockScreen().transition(.opacity).zIndex(10) }16        }17        .onChange(of: app.pendingRoute, initial: true) { consumePendingRoute() }18        .onChange(of: session.phase) { _, phase in19            if phase == .signedOut { nav.reset() }20            consumePendingRoute()21        }22        .onChange(of: app.lock.isLocked) { consumePendingRoute() }23    }2425    @ViewBuilder private var content: some View {26        if let invitation = app.invitation {27            InvitationSetupScreen(model: invitation)28        } else {29            switch session.phase {30            case .starting:31                ProgressView(tr("state.loading")).accessibilityIdentifier("screen-starting")32            case .signedOut:33                SignInScreen()34            case .signedIn:35                Shell()36            }37        }38    }3940    /// A destination requested from outside is opened only by a signed-in, unlocked scene.41    private func consumePendingRoute() {42        guard let route = app.pendingRoute, session.phase == .signedIn, !app.lock.isLocked, app.invitation == nil else { return }43        app.pendingRoute = nil44        nav.sheet = nil45        nav.go(to: route)46    }47}4849/// Picks the navigation model for the space available: tabs on iPhone and in a narrow iPad window, a sidebar with50/// side-by-side panes on a regular-width iPad.51struct Shell: View {52    @Environment(\.horizontalSizeClass) private var hSize53    @Environment(Navigator.self) private var nav5455    var body: some View {56        @Bindable var nav = nav57        let pad = UIDevice.current.userInterfaceIdiom == .pad && hSize == .regular58        VStack(spacing: 0) {59            ConnectionBanner()60            if pad { PadShell() } else { PhoneShell() }61        }62        .onChange(of: pad, initial: true) { old, isPad in63            nav.layout = isPad ? .pad : .phone64            guard old != isPad else { return }65            // The window changed width (iPad multitasking): keep the same section in the other shell.66            if isPad {67                nav.section = nav.currentSectionForPhone68            } else {69                let target = nav.section70                let t = PhoneTab.tab(for: target)71                if t == .more, nav.phonePaths[.more]?.first != .section(target) { nav.phonePaths[.more] = [.section(target)] }72                nav.tab = t73            }74        }75        .sheet(item: $nav.sheet) { SheetHost(route: $0) }76    }77}7879extension Navigator {80    /// The section shown by the phone layout, whatever `layout` currently says.81    var currentSectionForPhone: AppSection {82        if let s = tab.section { return s }83        if case .section(let s)? = phonePaths[.more]?.first { return s }84        return section85    }86}8788struct PhoneShell: View {89    @Environment(Navigator.self) private var nav90    @Session private var session9192    var body: some View {93        @Bindable var nav = nav94        TabView(selection: $nav.tab) {95            ForEach(PhoneTab.allCases) { tab in96                NavigationStack(path: nav.path(for: tab)) {97                    Group {98                        if let s = tab.section { SectionRoot(section: s) } else { MoreScreen() }99                    }100                    .navigationDestination(for: Route.self) { RouteView(route: $0) }101                }102                .tabItem { Label(tab.title, systemImage: tab.symbol) }103                .tag(tab)104                .badge(tab == .alerts ? openAlertCount : 0)105                .accessibilityIdentifier("tab-" + tab.rawValue)106            }107        }108    }109110    private var openAlertCount: Int {111        guard let a = session.cluster.overview.value?.alerts else { return 0 }112        return a.critical + a.warning113    }114}115116/// "More" tab of the iPhone: the sections that are not tabs.117struct MoreScreen: View {118    @Environment(\.mccTheme) private var theme119    var body: some View {120        ScreenStack {121            ForEach(AppSection.secondary) { s in122                RouteRow(route: .section(s), identifier: "more-" + s.rawValue) {123                    Label(s.title, systemImage: s.symbol).font(.body).foregroundStyle(theme.textPrimary)124                }125            }126        }127        .screen("more", title: tr("tab.more"), load: {})128    }129}130131struct PadShell: View {132    @Environment(Navigator.self) private var nav133    @Environment(\.mccTheme) private var theme134    @Session private var session135    @State private var columns: NavigationSplitViewVisibility = .automatic136137    var body: some View {138        @Bindable var nav = nav139        NavigationSplitView(columnVisibility: $columns) {140            List {141                Section {142                    ForEach(AppSection.primary) { row($0) }143                }144                Section(tr("sidebar.operations")) {145                    ForEach(AppSection.secondary.filter { $0 != .settings }) { row($0) }146                }147                Section {148                    row(.settings)149                }150            }151            .listStyle(.sidebar)152            .navigationTitle("MacLustr")153            .accessibilityIdentifier("sidebar")154            .toolbar {155                ToolbarItem(placement: .topBarTrailing) {156                    Button { nav.sheet = .search } label: { Label(tr("commands.search"), systemImage: "magnifyingglass") }157                        .accessibilityIdentifier("sidebar-search")158                }159            }160        } detail: {161            PadSectionPanes(section: nav.section)162                .id(nav.section)163        }164        .navigationSplitViewStyle(.balanced)165    }166167    private func row(_ s: AppSection) -> some View {168        let selected = nav.section == s169        return Button { nav.show(s) } label: {170            Label(s.title, systemImage: s.symbol)171                .frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin - 12, alignment: .leading)172                .contentShape(Rectangle())173        }174        .buttonStyle(.plain)175        .foregroundStyle(selected ? theme.accent : theme.textPrimary)176        .fontWeight(selected ? .semibold : .regular)177        .listRowBackground(selected ? RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous).fill(theme.accentMuted) : nil)178        .hoverEffect(.highlight)179        .accessibilityAddTraits(selected ? [.isSelected] : [])180        .accessibilityIdentifier("sidebar-" + s.rawValue)181        .badge(s == .alerts ? alertBadge : nil)182    }183184    private var alertBadge: Text? {185        guard let a = session.cluster.overview.value?.alerts, a.critical + a.warning > 0 else { return nil }186        return Text("\(a.critical + a.warning)")187    }188}189190/// Detail column of the iPad: one pane, or list + item side by side for the master-detail sections.191struct PadSectionPanes: View {192    @Environment(Navigator.self) private var nav193    @Environment(\.mccTheme) private var theme194    let section: AppSection195196    var body: some View {197        if section.isMasterDetail {198            GeometryReader { geo in199                HStack(spacing: 0) {200                    NavigationStack { SectionRoot(section: section) }201                        .frame(width: min(420, max(320, geo.size.width * 0.38)))202                    Rectangle().fill(theme.border).frame(width: 1).ignoresSafeArea()203                    NavigationStack(path: nav.padPath(for: section)) {204                        Group {205                            if let route = nav.padSelection[section] { RouteView(route: route).id(route) } else { DetailPlaceholder(section: section).background(theme.canvas.ignoresSafeArea()) }206                        }207                        .navigationDestination(for: Route.self) { RouteView(route: $0) }208                    }209                }210            }211        } else {212            NavigationStack(path: nav.padPath(for: section)) {213                SectionRoot(section: section)214                    .navigationDestination(for: Route.self) { RouteView(route: $0) }215            }216        }217    }218}219220/// Lock screen (local convenience). Nothing of the app is visible behind it.221struct LockScreen: View {222    @Environment(AppModel.self) private var app223    @Environment(\.mccTheme) private var theme224225    var body: some View {226        ZStack {227            theme.canvas.ignoresSafeArea()228            VStack(spacing: Tokens.Space.s6) {229                Image(systemName: "lock.fill").font(.system(size: 44)).foregroundStyle(theme.accent).accessibilityHidden(true)230                Text(tr("lock.title")).font(.title2.weight(.semibold)).foregroundStyle(theme.textPrimary)231                Text(tr("lock.message")).font(.callout).foregroundStyle(theme.textSecondary).multilineTextAlignment(.center)232                if case .unavailable(let m)? = app.lock.lastError { Text(m).font(.footnote).foregroundStyle(StatusTone.warning.color(theme)) }233                if case .failed(let m)? = app.lock.lastError { Text(m).font(.footnote).foregroundStyle(StatusTone.warning.color(theme)) }234                Button { Task { await app.lock.unlock(reason: tr("lock.reason")) } } label: {235                    Label(tr("lock.unlock"), systemImage: "faceid").frame(maxWidth: 320, minHeight: Tokens.Size.touchMin)236                }237                .buttonStyle(.mccProminent)238                .disabled(app.lock.isEvaluating)239                .accessibilityIdentifier("lock-unlock")240            }241            .padding(Tokens.Space.s8)242        }243        .accessibilityIdentifier("screen-lock")244        .task { await app.lock.unlock(reason: tr("lock.reason")) }245    }246}247
Files main
Menu

Theme