Cotton code
84 lines · 3.3 KB · SwiftRaw
// What a face asks the app for beyond its card, as a picture is asked// (`FacePicture`): a document's first page off its file, and where the player// stands with a track. The app installs each (CottonUI's `DeckFirstPage`,// `DeckTrackPlaying`); unset — the Loom app, the face shots — a document// draws its blank page and a track stands still.import CottonValuesimport SwiftUI/// A record's file's first page, by its ref; it tells its card the page's/// shape (width over height) once it is drawn.public typealias FaceFilePage = @MainActor @Sendable (_ ref: String, _ aspect: Binding<CGFloat>) -> AnyView/// Where the player stands with a track's card, and what its disc does: plays/// or pauses it, moves the playhead in what it holds, and — on a card of/// several songs (a playlist's tape) — plays from one of them. An object with/// a volume knob of its own (a podcast's radio) reads and turns the sound's/// volume through it too.public struct FaceTrackPlay {    public var play: TrackPlay    public var toggle: (() -> Void)?    public var seek: (TimeInterval) -> Void    /// Plays the card's song at this place in its songs (from 0), from its    /// start; nil on a card of one track, or where nothing plays.    public var start: ((Int) -> Void)?    /// The sound's volume, nought to one (on the phone the system's); nil    /// where none is known.    public var volume: Double?    /// Turns the volume to a share, nought to one; nil where it cannot be turned.    public var turnVolume: ((Double) -> Void)?    public init(        play: TrackPlay, toggle: (() -> Void)?, seek: @escaping (TimeInterval) -> Void, start: ((Int) -> Void)? = nil,        volume: Double? = nil, turnVolume: ((Double) -> Void)? = nil    ) {        self.play = play        self.toggle = toggle        self.seek = seek        self.start = start        self.volume = volume        self.turnVolume = turnVolume    }}/// The player's word on a track's card, or a card of several songs, handed to/// what draws it.public typealias FaceTrackPlayer = @MainActor @Sendable (_ card: DeckCard, _ drawn: @escaping (FaceTrackPlay) -> AnyView) -> AnyViewextension EnvironmentValues {    @Entry public var faceFilePage: FaceFilePage? = nil    @Entry public var faceTrackPlayer: FaceTrackPlayer? = nil}/// A track's card under the installed player, or still where none is/// installed.struct TrackPlaying<Drawn: View>: View {    let card: DeckCard    @ViewBuilder let drawn: (FaceTrackPlay) -> Drawn    @Environment(\.faceTrackPlayer) private var player    var body: some View {        if let player {            player(card) { AnyView(drawn($0)) }        } else {            drawn(FaceTrackPlay(play: .still, toggle: nil, seek: { _ in }))        }    }}/// A document's card with its first page off its file, where the app draws/// one (`faceFilePage`) and the card names a file; else the drawn blank page.struct DocumentFileCard: View {    let card: DeckCard    @Environment(\.faceFilePage) private var filePage    @State private var aspect = DocumentFaceKnobs.blankAspect    var body: some View {        if let filePage, let file = card.file {            DocumentFace(card: card, page: filePage(file, $aspect), pageAspect: aspect)        } else {            DocumentFace<EmptyView>(card: card, page: nil, pageAspect: aspect)        }    }}