Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/RootViews.swift3// Description: Root of a window (launch / sign-in / lock / main), the privacy cover of the app switcher, the lock screen and the gate that4// keeps every secondary window (composer, message, studio, settings, menu bar) closed while the app is locked, and passkey sign-in.5// The models are always passed explicitly (never read from the environment inside menus, toolbars or sheets).6import SPMailCore7import SwiftUI89struct RootView: View {10 @Bindable var app: AppModel11 let scene: MailScene12 @Environment(\.scenePhase) private var scenePhase1314 /// Nothing readable in the app switcher: whenever the privacy preference is on (independently from the lock), and always with the lock.15 private var covered: Bool { (app.settings.hideInAppSwitcher || app.settings.lockEnabled) && scenePhase != .active && app.phase == .ready }1617 var body: some View {18 ZStack {19 SPColor.bg.ignoresSafeArea()20 switch app.phase {21 case .launching: ProgressView().controlSize(.large)22 case .signedOut: SignInView(model: app)23 case .locked: LockView(app: app)24 case .ready: MainView(scene: scene)25 }26 if covered { PrivacyCover() }27 }28 .spTheme(app.settings)29 .task { await app.launch() }30 #if os(iOS)31 .onChange(of: scenePhase) { _, phase in if phase == .background { BackgroundRefresh.schedule() } }32 .background(TouchActivityObserver { app.lock.noteActivity() })33 #endif34 .alert("SP Mail", isPresented: Binding(get: { app.notice != nil && app.phase != .ready }, set: { if !$0 { app.notice = nil } })) {35 Button("OK") { app.notice = nil }36 } message: { Text(app.notice ?? "") }37 }38}3940struct PrivacyCover: View {41 var body: some View {42 ZStack {43 SPColor.bg44 VStack(spacing: 12) { Image(systemName: "envelope.fill").font(.system(size: 54)).foregroundStyle(SPColor.accent); Text("SP Mail").font(SPFont.title).foregroundStyle(SPColor.text) }45 }46 .ignoresSafeArea()47 .accessibilityHidden(true)48 }49}5051struct LockView: View {52 let app: AppModel53 var body: some View {54 VStack(spacing: 18) {55 Image(systemName: "lock.fill").font(.system(size: 44)).foregroundStyle(SPColor.accent).accessibilityHidden(true)56 Text("SP Mail est verrouillé").font(SPFont.title).foregroundStyle(SPColor.text)57 Button { Task { await app.unlock() } } label: { Label("Déverrouiller", systemImage: "faceid").frame(minWidth: 160) }58 .buttonStyle(.borderedProminent).controlSize(.large).keyboardShortcut(.defaultAction)59 }60 .padding(40)61 .frame(maxWidth: .infinity, maxHeight: .infinity)62 .background(SPColor.bg)63 .task { await app.unlock() }64 }65}6667/// Wraps the content of a secondary window: while the app is locked it shows the lock screen instead — a composer, a message window,68/// the studio or Settings never stay readable behind the lock.69struct LockGate<Content: View>: View {70 let app: AppModel71 /// False for windows that also make sense without a session (Settings).72 var requiresSession = true73 /// True for windows whose content holds unsaved state (the composer): it stays mounted behind the lock — invisible, disabled and hidden74 /// from assistive technologies — instead of being torn down.75 var keepsContentAlive = false76 @ViewBuilder var content: () -> Content7778 /// What a gated window shows for a given phase (also checked by the self-test).79 enum Presentation: Equatable { case content, lock, signedOut }80 static func presentation(for phase: AppModel.Phase, requiresSession: Bool) -> Presentation {81 switch phase {82 case .locked: .lock83 case .ready: .content84 case .launching, .signedOut: requiresSession ? .signedOut : .content85 }86 }8788 var body: some View {89 let presentation = Self.presentation(for: app.phase, requiresSession: requiresSession)90 ZStack {91 if presentation == .content || (keepsContentAlive && presentation == .lock) {92 content()93 .opacity(presentation == .content ? 1 : 0)94 .disabled(presentation != .content)95 .accessibilityHidden(presentation != .content)96 }97 if presentation == .lock { LockView(app: app) }98 if presentation == .signedOut { SPEmptyState(icon: "person.badge.key", title: L("Connectez-vous dans la fenêtre principale.")).background(SPColor.bg) }99 }100 .spTheme(app.settings)101 }102}103104struct SignInView: View {105 @Bindable var model: AppModel106 @ScaledMetric(relativeTo: .largeTitle) private var glyph: CGFloat = 52107 @State private var showEnroll = false108 @State private var link = ""109 @State private var enrollToken: String?110 @State private var purpose = ""111 @State private var knownUser: EnrollCheck.User?112 @State private var handle = ""113 @State private var displayName = ""114115 var body: some View {116 ScrollView {117 VStack(spacing: 20) {118 Image(systemName: "envelope.fill").font(.system(size: glyph)).foregroundStyle(SPColor.accent).padding(.top, 30).accessibilityHidden(true)119 Text("SP Mail").font(SPFont.display).foregroundStyle(SPColor.text)120 Text("Connectez-vous avec votre clé d’accès. Aucun mot de passe.").foregroundStyle(SPColor.text2).multilineTextAlignment(.center)121 Button { Task { await model.signIn() } } label: { Label("Se connecter avec une clé d’accès", systemImage: "person.badge.key.fill").frame(maxWidth: 320) }122 .buttonStyle(.borderedProminent).controlSize(.large).disabled(model.isBusy).keyboardShortcut(.defaultAction)123 if model.isBusy { ProgressView() }124 if let e = model.signInError { Text(e).foregroundStyle(SPColor.danger).multilineTextAlignment(.center).frame(maxWidth: 420).accessibilityLabel(Text("Erreur : \(e)")) }125 Divider().frame(maxWidth: 320)126 DisclosureGroup("J’ai reçu un lien d’inscription", isExpanded: $showEnroll) {127 VStack(alignment: .leading, spacing: 10) {128 Text("Collez le lien https://mail.spboucher.ai/enroll#… pour créer la clé d’accès de cet appareil.").font(.callout).foregroundStyle(.secondary)129 TextField("Lien d’inscription", text: $link).textFieldStyle(.roundedBorder).autocorrectionDisabled()130 #if os(iOS)131 .textInputAutocapitalization(.never).keyboardType(.URL)132 #endif133 if enrollToken == nil {134 Button("Vérifier le lien") { Task { await check() } }.disabled(link.isEmpty || model.isBusy)135 } else {136 if let u = knownUser {137 Text("Nouvelle clé d’accès pour \(u.display_name) (\(u.handle))").font(.callout)138 } else {139 TextField("Identifiant (a-z, 0-9)", text: $handle).textFieldStyle(.roundedBorder).autocorrectionDisabled()140 #if os(iOS)141 .textInputAutocapitalization(.never)142 #endif143 TextField("Nom affiché", text: $displayName).textFieldStyle(.roundedBorder)144 }145 Button("Créer la clé d’accès") { Task { await model.enroll(token: enrollToken ?? "", handle: knownUser == nil ? handle.lowercased() : nil, displayName: knownUser == nil ? displayName : nil) } }146 .buttonStyle(.borderedProminent).disabled(model.isBusy || (knownUser == nil && (handle.count < 2 || displayName.isEmpty)))147 }148 }.padding(.top, 8)149 }.frame(maxWidth: 420)150 Text(AppConfig.baseURL.host ?? "").font(.footnote).foregroundStyle(.tertiary)151 }152 .padding(28).frame(maxWidth: .infinity)153 }154 .onOpenURL { url in155 // a tapped https://mail.spboucher.ai/enroll#token link opened in the app156 if PasskeyCoordinator.enrollmentToken(from: url.absoluteString) != nil { link = url.absoluteString; showEnroll = true; Task { await check() } }157 }158 .onChange(of: link) { _, _ in enrollToken = nil }159 }160161 private func check() async {162 guard let r = await model.checkEnrollment(link) else { return }163 enrollToken = r.token; purpose = r.check.purpose; knownUser = r.check.user164 }165}166