Repository Public
1// SP Lecture Studio2// File: Apps/LectureStudio/Shared/App/RootView.swift3// Description: Adaptive root layout (three panes on Mac / iPad, navigation stack on iPhone), project container, inspector and presentation window.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import LectureCore9import LectureStorage1011enum ProjectMode: String, CaseIterable, Identifiable {12 case present, edit13 var id: String { rawValue }14 var title: LocalizedStringKey { self == .present ? "Présenter" : "Monter" }15}1617struct RootView: View {18 let model: AppModel19 @Environment(\.horizontalSizeClass) private var hSize20 @State private var sidebarVisible = true21 @State private var path: [UUID] = []2223 var body: some View {24 Group {25 if hSize == .compact {26 NavigationStack(path: $path) {27 LibraryView(model: model, compact: true) { id in if path.last != id { path = [id] } }28 .navigationBarBackButtonHiddenCompat()29 .navigationDestination(for: UUID.self) { id in30 ProjectContainer(model: model, id: id, sidebarVisible: .constant(false), compact: true)31 .navigationBarBackButtonHiddenCompat()32 }33 }34 } else {35 // Plain HStack: NavigationSplitView / HSplitView mis-size their detail column on macOS 27.36 HStack(spacing: 0) {37 if sidebarVisible {38 LibraryView(model: model, compact: false) { _ in }.frame(width: 300)39 Divider()40 }41 if let id = model.selectedID {42 ProjectContainer(model: model, id: id, sidebarVisible: $sidebarVisible, compact: false).id(id)43 } else {44 placeholder45 }46 }47 }48 }49 .alert(item: Binding(get: { model.alert }, set: { model.alert = $0 })) { a in50 Alert(title: Text(verbatim: a.title), message: Text(verbatim: a.message), dismissButton: .default(Text("OK")))51 }52 .onChange(of: model.selectedID) { _, id in if hSize == .compact, id == nil { path = [] } }53 }5455 private var placeholder: some View {56 VStack(spacing: 10) {57 Image(systemName: "rectangle.on.rectangle.angled").font(.system(size: 46)).foregroundStyle(.tertiary)58 Text("Choisissez une séance").font(.title3.weight(.semibold)).foregroundStyle(.secondary)59 Text("ou importez un PDF pour en créer une.").font(.callout).foregroundStyle(.tertiary)60 }61 .frame(maxWidth: .infinity, maxHeight: .infinity).background(DS.stage.opacity(0.06))62 }63}6465extension View {66 /// Sheets use a compact inline title on iOS so that long French titles are not truncated.67 @ViewBuilder func inlineTitle() -> some View {68 #if os(iOS)69 self.navigationBarTitleDisplayMode(.inline)70 #else71 self72 #endif73 }74 @ViewBuilder func navigationBarBackButtonHiddenCompat() -> some View {75 #if os(iOS)76 self.toolbar(.hidden, for: .navigationBar)77 #else78 self79 #endif80 }81}8283struct ProjectContainer: View {84 let model: AppModel85 let id: UUID86 @Binding var sidebarVisible: Bool87 var compact: Bool88 @Environment(\.dismiss) private var dismiss89 @Environment(\.openWindow) private var openWindow90 @Environment(\.verticalSizeClass) private var vSize91 @State private var session: ProjectSession?92 @State private var mode: ProjectMode = .present93 @State private var showInspector = false94 @State private var showExport = false95 @State private var failed = false9697 var body: some View {98 Group {99 if let session {100 // iPhone in landscape: the header is folded into the workspace / editor bars to leave height for the slide.101 let folded = vSize == .compact102 let chrome = folded ? ProjectChrome(mode: $mode, canEdit: session.manifest.hasRecording,103 onBack: { if session.recState == .idle { model.selectedID = nil; dismiss() } },104 onExport: { showExport = true }, onInfo: { showInspector = true }) : nil105 VStack(spacing: 0) {106 if !folded { header(session); Divider() }107 HStack(spacing: 0) {108 Group {109 switch mode {110 case .present: WorkspaceView(session: session, settings: model.settings, chrome: chrome)111 case .edit: EditorView(session: session, settings: model.settings, chrome: chrome)112 }113 }114 .frame(maxWidth: .infinity, maxHeight: .infinity)115 if showInspector && !compact {116 Divider()117 ProjectInspector(model: model, session: session).frame(width: 290)118 }119 }120 }121 .sheet(isPresented: Binding(get: { showInspector && compact }, set: { showInspector = $0 })) {122 NavigationStack {123 ProjectInspector(model: model, session: session).navigationTitle("Informations").inlineTitle()124 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Terminé") { showInspector = false } } }125 }126 }127 .sheet(isPresented: $showExport) { ExportSheet(model: model, session: session) }128 .onChange(of: showExport || (showInspector && compact)) { _, active in session.chromeOverlayActive = active }129 .focusedSceneValue(\.activeSession, session)130 } else if failed {131 VStack(spacing: 10) {132 Image(systemName: "exclamationmark.triangle").font(.largeTitle).foregroundStyle(DS.paused)133 Text("Ce projet n'a pas pu être ouvert.").font(.headline)134 if compact { Button("Retour") { dismiss() } }135 }.frame(maxWidth: .infinity, maxHeight: .infinity)136 } else {137 ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity)138 }139 }140 .task(id: id) {141 session = model.session(for: id)142 failed = session == nil143 }144 }145146 /// Fixed-height header (see the macOS 27 layout note in RootView).147 private func header(_ s: ProjectSession) -> some View {148 HStack(spacing: 8) {149 if compact {150 Button { if s.recState == .idle { model.selectedID = nil; dismiss() } } label: { Image(systemName: "chevron.left") }151 .buttonStyle(IconButtonStyle()).disabled(s.recState != .idle).accessibilityLabel("Retour à la bibliothèque").accessibilityIdentifier("project.back")152 } else {153 Button { withAnimation(.easeOut(duration: 0.15)) { sidebarVisible.toggle() } } label: { Image(systemName: "sidebar.left") }154 .buttonStyle(IconButtonStyle()).help("Bibliothèque").accessibilityLabel("Afficher ou masquer la bibliothèque")155 }156 VStack(alignment: .leading, spacing: 0) {157 Text(verbatim: s.title).font(compact ? .subheadline.weight(.semibold) : .headline).lineLimit(compact ? 2 : 1).minimumScaleFactor(0.8)158 let sub = s.manifest.subtitle(sessionWord: String(localized: "Séance"))159 if !sub.isEmpty && !compact { Text(verbatim: sub).font(.caption).foregroundStyle(.secondary).lineLimit(1) }160 }161 .accessibilityElement(children: .combine).accessibilityIdentifier("project.title")162 Spacer(minLength: 4)163 Picker("Mode", selection: $mode) { ForEach(ProjectMode.allCases) { Text($0.title).tag($0) } }164 .pickerStyle(.segmented).labelsHidden().fixedSize()165 .disabled(s.recState != .idle || !s.manifest.hasRecording && mode == .present)166 .help(s.manifest.hasRecording ? "Présenter ou monter" : "Le montage devient disponible après un premier enregistrement")167 .accessibilityIdentifier("project.mode")168 Button { showExport = true } label: { Image(systemName: "square.and.arrow.up") }169 .buttonStyle(IconButtonStyle()).disabled(s.recState != .idle).help("Exporter").accessibilityLabel("Exporter").accessibilityIdentifier("project.export")170 if !compact {171 Button { openWindow(id: "presentation", value: id) } label: { Image(systemName: "rectangle.on.rectangle") }172 .buttonStyle(IconButtonStyle()).help("Ouvrir la fenêtre de présentation (à placer sur un second écran)").accessibilityLabel("Fenêtre de présentation")173 }174 Button { showInspector.toggle() } label: { Image(systemName: "info.circle") }175 .buttonStyle(IconButtonStyle(active: showInspector && !compact)).help("Informations").accessibilityLabel("Informations sur la séance").accessibilityIdentifier("project.info")176 }177 .padding(.horizontal, 10).frame(height: 50)178 .background(.bar)179 .onChange(of: s.recState) { _, st in if st != .idle { mode = .present } }180 }181}182183struct ActiveSessionKey: FocusedValueKey { typealias Value = ProjectSession }184extension FocusedValues {185 var activeSession: ProjectSession? { get { self[ActiveSessionKey.self] } set { self[ActiveSessionKey.self] = newValue } }186}187188// MARK: Inspector189190struct ProjectInspector: View {191 let model: AppModel192 let session: ProjectSession193 @State private var diagnostics: ProjectPackage.Diagnostics?194 @State private var checking = false195196 private func bind<T>(_ get: @escaping (ProjectManifest) -> T, _ set: @escaping (inout ProjectManifest, T) -> Void) -> Binding<T> {197 Binding(get: { get(session.manifest) }, set: { v in session.updateManifest { set(&$0, v) } })198 }199200 var body: some View {201 Form {202 Section {203 TextField("Titre", text: bind({ $0.title }, { $0.title = $1 }))204 TextField("Sigle du cours", text: bind({ $0.courseCode }, { $0.courseCode = $1 }))205 TextField("Numéro de séance", text: bind({ $0.sessionNumber.map(String.init) ?? "" }, { $0.sessionNumber = Int($1) }))206 DatePicker("Date", selection: bind({ $0.lectureDate }, { $0.lectureDate = $1 }), displayedComponents: .date)207 TextField("Destination de publication", text: bind({ $0.publishingDestination }, { $0.publishingDestination = $1 }), prompt: Text("Moodle, YouTube…"))208 Stepper(value: bind({ $0.coverPage + 1 }, { $0.coverPage = max(0, min(session.pageCount - 1, $1 - 1)) }), in: 1...max(1, session.pageCount)) {209 Text("Page de couverture : \(session.manifest.coverPage + 1)")210 }211 } header: { Text("Séance") }212 .disabled(session.recState != .idle)213214 Section {215 LabeledContent("Diapositives") { Text(verbatim: "\(session.pageCount)") }216 LabeledContent("Durée enregistrée") { Text(verbatim: DS.duration(session.duration)) }217 LabeledContent("Prises") { Text(verbatim: "\(session.manifest.sessions.count)") }218 LabeledContent("Marqueurs") { Text(verbatim: "\(session.markerCount)") }219 LabeledContent("PDF d'origine") { Text(verbatim: session.manifest.pdf?.originalName ?? "—").lineLimit(1) }220 LabeledContent("Espace utilisé") { Text(verbatim: DS.bytes(session.package.diskUsage())) }221 } header: { Text("Contenu") }222223 Section {224 if let d = diagnostics {225 if d.isHealthy && d.skippedEventLines == 0 {226 Label("Tous les fichiers sont présents et intacts.", systemImage: "checkmark.seal.fill").foregroundStyle(DS.ok)227 } else {228 if d.missingPDF { Label("Le PDF est manquant.", systemImage: "xmark.octagon").foregroundStyle(DS.recording) }229 if d.pdfChecksumMismatch { Label("Le PDF a été modifié depuis l'importation.", systemImage: "exclamationmark.triangle").foregroundStyle(DS.paused) }230 if !d.missingSegments.isEmpty { Label("\(d.missingSegments.count) segment(s) audio ou vidéo manquant(s). Ces passages seront muets.", systemImage: "waveform.slash").foregroundStyle(DS.recording) }231 if !d.corruptSegments.isEmpty { Label("\(d.corruptSegments.count) segment(s) dont la somme de contrôle ne correspond plus.", systemImage: "exclamationmark.triangle").foregroundStyle(DS.paused) }232 if d.skippedEventLines > 0 { Label("\(d.skippedEventLines) ligne(s) du journal illisible(s), ignorée(s).", systemImage: "text.badge.xmark").foregroundStyle(DS.paused) }233 }234 }235 Button { verify() } label: { if checking { ProgressView().controlSize(.small) } else { Text("Vérifier l'intégrité du projet") } }.disabled(checking)236 #if os(macOS)237 Button("Afficher le projet dans le Finder") { NSWorkspace.shared.activateFileViewerSelecting([session.package.url]) }238 #endif239 } header: { Text("Diagnostic") } footer: { Text("Les enregistrements d'origine ne sont jamais modifiés ni supprimés par le montage.") }240 }241 .formStyle(.grouped)242 }243244 private func verify() {245 checking = true246 let pkg = session.package, m = session.manifest247 Task.detached {248 let d = pkg.diagnose(m, deep: true)249 await MainActor.run { diagnostics = d; checking = false }250 }251 }252}253254// MARK: Presentation window (clean stage for a second display)255256struct PresentationView: View {257 let model: AppModel258 let id: UUID?259 var body: some View {260 Group {261 if let id, let session = model.session(for: id) {262 ZStack(alignment: .topTrailing) {263 StageCanvas(session: session, interactive: false)264 if session.recState == .recording {265 Circle().fill(DS.recording).frame(width: 12, height: 12).padding(12).accessibilityLabel("Enregistrement en cours")266 }267 }268 .background(DS.stage).ignoresSafeArea()269 .navigationTitle(Text(verbatim: session.title))270 } else {271 Text("Ouvrez une séance dans la fenêtre principale.").foregroundStyle(.secondary).frame(maxWidth: .infinity, maxHeight: .infinity)272 }273 }274 }275}276