SPB Git

Repository Public License

TimelineState.swift 3.6 KB 87 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Features/Visuals/TimelineState.swift4//  Description: Shared time axis for the file views: visible window, zoom, pan, selection and time ↔ x mapping.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import Foundation10import ORIXAudioCore11import SwiftUI1213struct TimelineState: Equatable {14    var duration: Double15    var visibleStart: Double = 016    var visibleDuration: Double17    var selection: ClosedRange<Double>?1819    init(duration: Double) { self.duration = max(duration, 0.001); visibleDuration = max(duration, 0.001) }2021    var visibleEnd: Double { visibleStart + visibleDuration }22    var visible: ClosedRange<Double> { visibleStart...visibleEnd }23    var isZoomed: Bool { visibleDuration < duration * 0.999 }24    static let minimumVisible = 0.052526    func x(for time: Double, width: CGFloat) -> CGFloat { CGFloat((time - visibleStart) / visibleDuration) * width }27    func time(forX x: CGFloat, width: CGFloat) -> Double { max(0, min(duration, visibleStart + Double(x / max(1, width)) * visibleDuration)) }2829    mutating func zoom(by factor: Double, anchorFraction: Double) {30        let anchorTime = visibleStart + anchorFraction * visibleDuration31        let newDuration = max(Self.minimumVisible, min(duration, visibleDuration / factor))32        visibleStart = anchorTime - anchorFraction * newDuration33        visibleDuration = newDuration34        clamp()35    }3637    mutating func pan(bySeconds delta: Double) { visibleStart += delta; clamp() }3839    mutating func reset() { visibleStart = 0; visibleDuration = duration }4041    /// Scrolls so `time` is visible (used to follow the playhead).42    mutating func reveal(_ time: Double) {43        guard isZoomed, time < visibleStart || time > visibleEnd else { return }44        visibleStart = time - visibleDuration * 0.145        clamp()46    }4748    mutating func setDuration(_ d: Double) {49        let wasFull = !isZoomed50        duration = max(d, 0.001)51        if wasFull { reset() } else { clamp() }52        if let s = selection { selection = s.lowerBound < duration ? s.lowerBound...min(s.upperBound, duration) : nil }53    }5455    private mutating func clamp() {56        visibleDuration = min(visibleDuration, duration)57        visibleStart = max(0, min(duration - visibleDuration, visibleStart))58    }5960    /// Tick spacing in seconds giving roughly one label per 80 points.61    func tickInterval(width: CGFloat) -> Double {62        let target = visibleDuration / Double(max(1, width / 80))63        let steps: [Double] = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600]64        return steps.first { $0 >= target } ?? 360065    }66}6768/// Time ruler drawn above the file plots.69struct TimeRuler: View {70    let timeline: TimelineState71    var body: some View {72        Canvas { context, size in73            let step = timeline.tickInterval(width: size.width)74            var t = (timeline.visibleStart / step).rounded(.up) * step75            while t <= timeline.visibleEnd {76                let x = timeline.x(for: t, width: size.width)77                context.stroke(Path { $0.move(to: CGPoint(x: x, y: size.height - 5)); $0.addLine(to: CGPoint(x: x, y: size.height)) }, with: .color(ORIX.Palette.plotText), lineWidth: 1)78                let label = step < 1 ? Timecode.precise(t) : Timecode.clock(t)79                context.draw(Text(label).font(.system(size: 9, design: .monospaced)).foregroundStyle(ORIX.Palette.plotText), at: CGPoint(x: x + 3, y: size.height - 11), anchor: .leading)80                t += step81            }82        }83        .frame(height: 20)84        .accessibilityHidden(true)85    }86}87
Files main
Menu

Theme