Cotton code
86 lines · 3.8 KB · SwiftRaw
// The shown database's cap, as the input box's chip draws it (Spec 7, Cotton// note 708fe4fa). The database page's own bar — Search, the cap in the middle,// New thought — is gone: the page's foot is the input box (`HomeInput.swift`),// Search is the round button at the page's top right, and the chip in the box// opens the Databases screen, the one way to another database.import CottonKitimport SwiftUI/// The shown database as its bottle cap, printed with its name in the/// database's own colour — the one `DatabasesModel.colours` gives every/// surface, so the bar, the Databases screen and Settings agree.struct DatabaseCap: View {    let name: String    let colour: CapColour    let side: CGFloat    /// The database is syncing: the cap's crimped edge turns (B8).    var turning = false    /// The personal database's name on the cap before any name is read.    static let personal = "You"    /// What the bar draws for a window: the cap's name and colour. With no    /// held list (a window before its first read) it is the personal cap.    @MainActor    /// The shown database's cap. Its colour is the THEME's (`theme.cap`,    /// design note 26203e21 §6): the window's theme is the shown database's,    /// read from its theme row, so the cap the head draws and the theme the    /// faces wear are one row. The held list's colour — the same theme's cap,    /// composed by `databases.list` for every database — stands in until the    /// theme is read, and the id's own enamel when neither names one.    static func shown(scene: DatabaseScene?, in model: DatabasesModel?, name: String?, theme: Theme)        -> (name: String, colour: CapColour)    {        let row = model?.database(scene?.shownId)        return (            name: (name?.isEmpty == false ? name : nil) ?? row?.name ?? personal,            colour: theme.cap.colour ?? row.flatMap { model?.colours[$0.databaseId] } ?? .navy        )    }    var body: some View {        if turning {            TurningCap(name: name, colour: colour, side: side)        } else {            ProjectCap(name: name, colour: colour, side: side)        }    }}/// A cap whose EDGE turns while its database syncs (B8, 7 Oct): about one/// turn in four seconds; with Reduce Motion the edge pulses softly instead./// The same cap art twice — the whole enamel turning underneath, and the/// still face with its printed name over it — so only the crimped edge moves/// and nothing is redrawn. No words: the sentence is the device page's.struct TurningCap: View {    let name: String    let colour: CapColour    let side: CGFloat    @Environment(\.accessibilityReduceMotion) private var reduceMotion    /// Seconds for one turn of the edge.    static let turn: TimeInterval = 4    /// The still face's diameter, as a share of the side: the enamel's face    /// is ~64 % of it (`ProjectCap.face`), so the edge outside it turns.    static let face: CGFloat = 0.66    var body: some View {        TimelineView(.animation) { context in            let phase = context.date.timeIntervalSinceReferenceDate                .truncatingRemainder(dividingBy: Self.turn) / Self.turn            ZStack {                // The turning layer prints no name: only its edge shows.                ProjectCap(name: "", colour: colour, side: side)                    .rotationEffect(.degrees(reduceMotion ? 0 : phase * 360))                    .opacity(reduceMotion ? 0.75 + 0.25 * cos(phase * 2 * .pi) : 1)                ProjectCap(name: name, colour: colour, side: side)                    .mask(Circle().frame(width: side * Self.face, height: side * Self.face))            }            .frame(width: side, height: side)        }        .accessibilityElement(children: .ignore)        .accessibilityLabel(name)    }}