Repository Public
1// SP Lecture Studio2// File: Apps/LectureStudio/Shared/Editor/TimelineView.swift3// Description: Multi-track timeline (ruler, waveform, slides, ink, markers, camera) with cuts, suggestions, selection and playhead.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import LectureCore910struct TimelineView: View {11 let model: EditorModel12 var compact: Bool13 @State private var offset: CGFloat = 014 @State private var viewport: CGFloat = 60015 @State private var dragAnchor: MediaTime?1617 private var rowHeights: (ruler: CGFloat, wave: CGFloat, slides: CGFloat, ink: CGFloat, marks: CGFloat, camera: CGFloat) {18 (18, compact ? 46 : 68, compact ? 22 : 30, 14, 16, model.hasCamera ? 10 : 0)19 }20 private var totalHeight: CGFloat { let r = rowHeights; return r.ruler + r.wave + r.slides + r.ink + r.marks + r.camera + 6 }21 private var contentWidth: CGFloat { max(viewport, CGFloat(model.duration.seconds * model.pixelsPerSecond) + 40) }2223 private func time(at x: CGFloat) -> MediaTime { MediaTime(seconds: min(model.duration.seconds, max(0, Double(x) / model.pixelsPerSecond))) }24 private func x(_ t: MediaTime) -> CGFloat { CGFloat(t.seconds * model.pixelsPerSecond) }2526 var body: some View {27 ScrollViewReader { proxy in28 ScrollView(.horizontal) {29 ZStack(alignment: .topLeading) {30 Color.clear.frame(width: contentWidth, height: totalHeight)31 // Only the visible window is drawn; the canvas travels with the scroll offset.32 Canvas { ctx, size in draw(&ctx, size: size) }33 .frame(width: viewport, height: totalHeight)34 .offset(x: offset)35 .allowsHitTesting(false)36 Color.clear.frame(width: 1, height: 1).id("playhead").offset(x: x(model.playhead))37 }38 .contentShape(Rectangle())39 .gesture(SpatialTapGesture().onEnded { v in model.seek(to: time(at: v.location.x)); model.selection = nil })40 #if os(macOS)41 .gesture(DragGesture(minimumDistance: 6).onChanged { v in42 if dragAnchor == nil { dragAnchor = time(at: v.startLocation.x) }43 if let a = dragAnchor { model.selection = TimeRange(start: a, end: time(at: v.location.x)) }44 }.onEnded { _ in dragAnchor = nil })45 #endif46 }47 .onScrollGeometryChange(for: [CGFloat].self) { [$0.contentOffset.x, $0.containerSize.width] } action: { _, v in48 offset = max(0, v[0]); viewport = max(100, v[1])49 model.fitTimelineIfNeeded(width: Double(viewport))50 }51 .onChange(of: model.playhead) { _, t in52 // Keep the playhead in view while playing.53 guard model.isPlaying else { return }54 let px = x(t)55 if px < offset + 20 || px > offset + viewport - 40 { proxy.scrollTo("playhead", anchor: UnitPoint(x: 0.15, y: 0)) }56 }57 }58 .frame(height: totalHeight)59 .background(DS.card)60 .accessibilityElement()61 .accessibilityLabel("Ligne de temps")62 .accessibilityValue(Text("Tête de lecture à \(MediaTime.clock(model.playhead)) sur \(MediaTime.clock(model.duration)). \(model.edits.cuts.count) coupe(s), \(model.edits.suggestions.count) suggestion(s)."))63 .accessibilityAdjustableAction { direction in64 let step = MediaTime(seconds: 5)65 model.seek(to: direction == .increment ? model.playhead + step : model.playhead - step)66 }67 .accessibilityIdentifier("editor.timeline")68 }6970 // MARK: Drawing7172 private func draw(_ ctx: inout GraphicsContext, size: CGSize) {73 let pps = model.pixelsPerSecond74 let t0 = Double(offset) / pps, t1 = Double(offset + size.width) / pps75 func lx(_ t: Double) -> CGFloat { CGFloat(t * pps) - offset }76 let r = rowHeights77 let yWave = r.ruler, ySlides = yWave + r.wave + 2, yInk = ySlides + r.slides + 2, yMarks = yInk + r.ink, yCam = yMarks + r.marks78 let dur = model.duration.seconds79 let endX = lx(dur)8081 // Ruler.82 let step = Self.rulerStep(pps: pps)83 var tick = (t0 / step).rounded(.down) * step84 while tick <= min(t1, dur) {85 let x = lx(tick)86 ctx.fill(Path(CGRect(x: x, y: 0, width: 1, height: r.ruler)), with: .color(.secondary.opacity(0.5)))87 ctx.draw(Text(verbatim: MediaTime.clock(MediaTime(seconds: tick))).font(.system(size: 9, design: .monospaced)).foregroundStyle(.secondary), at: CGPoint(x: x + 3, y: 8), anchor: .leading)88 tick += step89 }9091 // Waveform.92 let waveRect = CGRect(x: max(0, lx(0)), y: yWave, width: max(0, min(size.width, endX) - max(0, lx(0))), height: r.wave)93 ctx.fill(Path(roundedRect: waveRect, cornerRadius: 3), with: .color(.primary.opacity(0.05)))94 if let env = model.envelope, !env.peak.isEmpty {95 let hop = env.hop.seconds96 var path = Path()97 let mid = yWave + r.wave / 298 var px = max(0, lx(0)).rounded(.down)99 while px < min(size.width, endX) {100 let a = Int((Double(px + offset) / pps) / hop), b = max(a + 1, Int((Double(px + 1 + offset) / pps) / hop))101 if a < env.peak.count {102 var pk: Float = 0103 var i = a104 let stride = max(1, (b - a) / 24)105 while i < min(b, env.peak.count) { pk = max(pk, env.peak[i]); i += stride }106 let h = max(0.5, CGFloat(min(1, Double(pk).squareRoot())) * r.wave / 2)107 path.addRect(CGRect(x: px, y: mid - h, width: 1, height: h * 2))108 }109 px += 1110 }111 ctx.fill(path, with: .color(DS.accent.opacity(0.85)))112 } else if model.envelopeProgress != nil {113 ctx.draw(Text("Analyse de la forme d'onde…").font(.caption).foregroundStyle(.secondary), at: CGPoint(x: size.width / 2, y: yWave + r.wave / 2))114 } else if model.manifest.segments(.audio).isEmpty {115 ctx.draw(Text("Séance enregistrée sans le son").font(.caption).foregroundStyle(.secondary), at: CGPoint(x: size.width / 2, y: yWave + r.wave / 2))116 }117118 // Slides.119 var changes = model.index.pageChanges120 if changes.first?.t != .zero { changes.insert(PageChange(t: .zero, page: model.index.initialPage), at: 0) }121 for (i, c) in changes.enumerated() {122 let s = c.t.seconds, e = i + 1 < changes.count ? changes[i + 1].t.seconds : dur123 guard e >= t0, s <= t1 else { continue }124 let rect = CGRect(x: lx(s) + 0.5, y: ySlides, width: max(1, lx(e) - lx(s) - 1), height: r.slides)125 ctx.fill(Path(roundedRect: rect, cornerRadius: 3), with: .color(Color.primary.opacity(c.page % 2 == 0 ? 0.13 : 0.08)))126 if rect.width > 22 {127 ctx.draw(Text(verbatim: "\(c.page + 1)").font(.system(size: 10, weight: .semibold)).foregroundStyle(.secondary),128 at: CGPoint(x: max(rect.minX, 0) + 5, y: rect.midY), anchor: .leading)129 }130 }131132 // Ink: one tick per stroke, as long as the stroke took to draw.133 for life in model.index.allStrokes {134 let s = life.stroke.begin.seconds, e = life.stroke.end.seconds135 guard e >= t0, s <= t1 else { continue }136 ctx.fill(Path(CGRect(x: lx(s), y: yInk + 3, width: max(1.5, lx(e) - lx(s)), height: r.ink - 6)), with: .color(Color(life.stroke.color).opacity(0.9)))137 }138139 // Markers, chapters, pauses.140 for m in model.index.markers where m.t.seconds >= t0 && m.t.seconds <= t1 {141 let color: Color = m.marker.kind == .retake ? DS.paused : m.marker.kind == .chapter ? DS.accent : .secondary142 let x = lx(m.t.seconds)143 var p = Path(); p.move(to: CGPoint(x: x, y: yMarks + 2)); p.addLine(to: CGPoint(x: x + 5, y: yMarks + 7)); p.addLine(to: CGPoint(x: x, y: yMarks + 12)); p.addLine(to: CGPoint(x: x - 5, y: yMarks + 7)); p.closeSubpath()144 ctx.fill(p, with: .color(color))145 }146 for c in model.edits.chapters where c.time.seconds >= t0 && c.time.seconds <= t1 {147 let x = lx(c.time.seconds)148 ctx.fill(Path(CGRect(x: x, y: yWave, width: 1, height: yMarks - yWave + 12)), with: .color(DS.accent.opacity(0.55)))149 }150 for p in model.index.pauses where p.t.seconds >= t0 && p.t.seconds <= t1 {151 ctx.stroke(Path { $0.move(to: CGPoint(x: lx(p.t.seconds), y: yWave)); $0.addLine(to: CGPoint(x: lx(p.t.seconds), y: yWave + r.wave)) },152 with: .color(.secondary), style: StrokeStyle(lineWidth: 1, dash: [2, 3]))153 }154155 // Camera segments.156 if r.camera > 0 {157 for seg in model.manifest.segments(.camera) where seg.range.end.seconds >= t0 && seg.start.seconds <= t1 {158 ctx.fill(Path(roundedRect: CGRect(x: lx(seg.start.seconds), y: yCam + 2, width: max(2, lx(seg.range.end.seconds) - lx(seg.start.seconds)), height: r.camera - 3), cornerRadius: 2), with: .color(.purple.opacity(0.6)))159 }160 }161162 let tracks = CGRect(x: 0, y: yWave, width: size.width, height: yCam + r.camera - yWave)163 // Muted ranges.164 for m in model.edits.muted where m.end.seconds >= t0 && m.start.seconds <= t1 {165 ctx.fill(Path(CGRect(x: lx(m.start.seconds), y: yWave, width: lx(m.end.seconds) - lx(m.start.seconds), height: r.wave)), with: .color(Color.gray.opacity(0.55)))166 }167 // Approved cuts: hatched red.168 for c in model.edits.cuts where c.range.end.seconds >= t0 && c.range.start.seconds <= t1 {169 let rect = CGRect(x: lx(c.range.start.seconds), y: tracks.minY, width: max(1, lx(c.range.end.seconds) - lx(c.range.start.seconds)), height: tracks.height)170 ctx.fill(Path(rect), with: .color(DS.recording.opacity(c.id == model.selectedCut ? 0.42 : 0.26)))171 var hatch = Path()172 var hx = rect.minX - rect.height173 while hx < rect.maxX { hatch.move(to: CGPoint(x: hx, y: rect.maxY)); hatch.addLine(to: CGPoint(x: hx + rect.height, y: rect.minY)); hx += 7 }174 var clipped = ctx; clipped.clip(to: Path(rect))175 clipped.stroke(hatch, with: .color(DS.recording.opacity(0.5)), lineWidth: 1)176 }177 // Suggestions: dashed outline, never applied until approved.178 for s in model.edits.suggestions where s.range.end.seconds >= t0 && s.range.start.seconds <= t1 {179 let rect = CGRect(x: lx(s.range.start.seconds), y: tracks.minY + 1, width: max(2, lx(s.range.end.seconds) - lx(s.range.start.seconds)), height: tracks.height - 2)180 ctx.fill(Path(rect), with: .color(DS.paused.opacity(s.id == model.selectedCut ? 0.3 : 0.12)))181 ctx.stroke(Path(rect), with: .color(DS.paused), style: StrokeStyle(lineWidth: 1.5, dash: [5, 3]))182 }183 // Splits.184 for s in model.edits.splits where s.seconds >= t0 && s.seconds <= t1 {185 ctx.fill(Path(CGRect(x: lx(s.seconds) - 0.5, y: tracks.minY, width: 1, height: tracks.height)), with: .color(.primary.opacity(0.6)))186 }187 // Selection.188 if let sel = model.selection, !sel.isEmpty {189 let rect = CGRect(x: lx(sel.start.seconds), y: 0, width: lx(sel.end.seconds) - lx(sel.start.seconds), height: size.height)190 ctx.fill(Path(rect), with: .color(DS.accent.opacity(0.22)))191 ctx.stroke(Path(rect), with: .color(DS.accent), lineWidth: 1)192 }193 // Playhead.194 let ph = lx(model.playhead.seconds)195 if ph >= -2, ph <= size.width + 2 {196 ctx.fill(Path(CGRect(x: ph - 0.75, y: 0, width: 1.5, height: size.height)), with: .color(DS.recording))197 var head = Path(); head.move(to: CGPoint(x: ph - 5, y: 0)); head.addLine(to: CGPoint(x: ph + 5, y: 0)); head.addLine(to: CGPoint(x: ph, y: 7)); head.closeSubpath()198 ctx.fill(head, with: .color(DS.recording))199 }200 }201202 static func rulerStep(pps: Double) -> Double {203 // Aim for a label every ~90 points.204 let target = 90 / pps205 for s in [1.0, 2, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600] where s >= target { return s }206 return 3600207 }208}209