Cotton code
129 lines · 5.2 KB · SwiftRaw
// The pieces a track is played from, on its face and on the player's// cards: the round play and pause disc, the clock a length is said in, and// the scrubber that moves the playhead.import CottonPaperimport CottonValuesimport SwiftUI/// The round dark disc a track plays and pauses from: the paper's triangle/// while it is still, two bars while it plays.public struct TrackDisc: View {    let playing: Bool    let size: CGFloat    @Environment(\.theme) private var theme    public init(playing: Bool, size: CGFloat) {        self.playing = playing        self.size = size    }    public var body: some View {        ZStack {            Circle().fill(theme.print(.ink).opacity(VideoFaceKnobs.playOpacity))            if playing {                HStack(spacing: size * 0.1) {                    Capsule().frame(width: size * 0.1, height: size * 0.34)                    Capsule().frame(width: size * 0.1, height: size * 0.34)                }                .foregroundStyle(theme.colors.paper.color)            } else {                PlayTriangle()                    .fill(theme.colors.paper.color)                    .frame(width: size * 0.32, height: size * 0.36)                    .offset(x: size * 0.04)            }        }        .frame(width: size, height: size)        .accessibilityHidden(true)    }}/// A length said as a clock: "3:41", "1:02:05".public enum TrackClock {    public static func said(_ seconds: TimeInterval) -> String {        let whole = Int(max(0, seconds.isFinite ? seconds : 0).rounded(.down))        let pattern: Duration.TimeFormatStyle.Pattern = whole >= 3600 ? .hourMinuteSecond : .minuteSecond        return Duration.seconds(whole).formatted(.time(pattern: pattern))    }    /// What is left of the track, said as a clock running down: "−2:05".    public static func left(_ elapsed: TimeInterval, of duration: TimeInterval) -> String {        "\u{2212}" + said(max(0, duration - elapsed))    }}/// The playhead on a line: the part played in ink, the rest in pencil, a/// knob where it stands. Dragged, the knob follows the finger and the/// player moves there as it lifts; the clocks under it say the knob's place/// and the whole length.public struct TrackScrubber: View {    let elapsed: TimeInterval    let duration: TimeInterval    /// The track's title: what VoiceOver says the scrubber moves through.    let title: String?    let seek: (TimeInterval) -> Void    @State private var dragged: TimeInterval?    @Environment(\.theme) private var theme    public init(elapsed: TimeInterval, duration: TimeInterval, title: String?, seek: @escaping (TimeInterval) -> Void) {        self.elapsed = elapsed        self.duration = duration        self.title = title        self.seek = seek    }    private var shown: TimeInterval { dragged ?? elapsed }    public var body: some View {        VStack(spacing: 4) {            GeometryReader { line in                let width = line.size.width                let played = duration > 0 ? CGFloat(min(1, max(0, shown / duration))) * width : 0                let knob = dragged == nil ? MusicPartsKnobs.knob : MusicPartsKnobs.knobDragging                ZStack(alignment: .leading) {                    Capsule().fill(theme.print(.pencil).opacity(0.35)).frame(height: MusicPartsKnobs.line)                    Capsule().fill(theme.print(.ink)).frame(width: played, height: MusicPartsKnobs.line)                    Circle()                        .fill(theme.print(.ink))                        .frame(width: knob, height: knob)                        .offset(x: played - knob / 2)                }                .frame(height: MusicPartsKnobs.height)                .contentShape(Rectangle())                .gesture(                    DragGesture(minimumDistance: 0)                        .onChanged { drag in                            guard duration > 0, width > 0 else { return }                            dragged = TimeInterval(min(1, max(0, drag.location.x / width))) * duration                        }                        .onEnded { _ in                            if let dragged { seek(dragged) }                            dragged = nil                        })                .animation(.easeOut(duration: 0.12), value: dragged == nil)            }            .frame(height: MusicPartsKnobs.height)            HStack {                Text(TrackClock.said(shown))                Spacer(minLength: 8)                Text(TrackClock.said(duration))            }            .font(CottonFonts.type(MusicPartsKnobs.clockSize))            .monospacedDigit()            .faceInk(.pencil)            .accessibilityHidden(true)        }        .accessibilityElement(children: .ignore)        .accessibilityLabel(title ?? ShellLines.line("faces", "recording.position"))        .accessibilityValue(TrackClock.said(shown))        .accessibilityAdjustableAction { direction in            switch direction {            case .increment: seek(min(duration, elapsed + MusicPartsKnobs.step))            case .decrement: seek(max(0, elapsed - MusicPartsKnobs.step))            @unknown default: break            }        }        .accessibilityIdentifier("track-scrubber")    }}