Cotton code
200 lines · 9.8 KB · SwiftRaw
// CINEMAS' LOOK AND MOTION: always dark (CONTRACT-2 "The places"), the streaming genre's conventions in Cinemas' own// colours (the splash's deep red under the ground, never another service's marks or typeface), and every number from// assets/design/cinemas/MOTION.md so the iPhone and Android move the same.import CoreTextimport SwiftUIenum CinemasInk {    private static func hex(_ value: UInt32, _ alpha: Double = 1) -> Color {        Color(red: Double((value >> 16) & 0xFF) / 255, green: Double((value >> 8) & 0xFF) / 255, blue: Double(value & 0xFF) / 255, opacity: alpha)    }    /// cinemas-3.html's tokens: a warm near-black ground, the marquee's amber for the one act that leads.    static let ground = hex(0x0D0A09)    static let raised = hex(0x17110F)    static let raisedHigh = hex(0x1F1815)    static let sheet = hex(0x1C1613)    static let field = hex(0x221B18)    static let rule = hex(0x2E2521)    static let ink = hex(0xF5EFE9)    static let quiet = hex(0xB5A9A0)    static let faint = hex(0x7C716A)    static let body = hex(0xD9CFC7)    static let accent = hex(0xF5B544)    static let onAccent = hex(0x1B1206)    static let disabled = hex(0x2A2320)    static let ok = hex(0x8FD4A4)    /// The splash's velvet, for warmth only (the remote's ground).    static let velvet = hex(0x4B0205)    static let remoteTop = hex(0x1C1310)    static let skeleton = hex(0x1D1715)    static let skeletonLight = hex(0x2C2420)    /// The side margin every row and the featured title line up to.    static let side: CGFloat = 16    /// The gap between posters along a row.    static let gap: CGFloat = 8    /// A poster's corner.    static let corner: CGFloat = 7    static let buttonCorner: CGFloat = 10    static let buttonHeight: CGFloat = 46}/// Big Shoulders Display for titles (always upper case, like the letters on a cinema's sign) and DM Sans for every/// word read or tapped, SIL OFL, bundled in the folder `font` (the files Android ships).enum CinemasFonts {    private static let registered: [String: String] = {        var names: [String: String] = [:]        for file in ["big_shoulders_black", "big_shoulders_extrabold", "dm_sans_regular", "dm_sans_medium", "dm_sans_semibold", "dm_sans_bold"] {            guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "font") else { continue }            CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)            let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? []            if let name = descriptors.first.flatMap({ CTFontDescriptorCopyAttribute($0, kCTFontNameAttribute) as? String }) { names[file] = name }        }        return names    }()    static func register() { _ = registered }    /// A title face at `size`, scaled with the phone's text: Black (900) or ExtraBold (800).    static func display(_ size: CGFloat, black: Bool = true, relativeTo style: Font.TextStyle = .largeTitle) -> Font {        if let name = registered[black ? "big_shoulders_black" : "big_shoulders_extrabold"] {            return .custom(name, size: size, relativeTo: style)        }        return .system(size: size, weight: black ? .black : .heavy).width(.compressed)    }    /// DM Sans at `size` and weight (400, 500, 600, 700), scaled with the phone's text.    static func sans(_ size: CGFloat, _ weight: Int = 400, relativeTo style: Font.TextStyle = .body) -> Font {        let file = weight >= 700 ? "dm_sans_bold" : weight >= 600 ? "dm_sans_semibold" : weight >= 500 ? "dm_sans_medium" : "dm_sans_regular"        if let name = registered[file] { return .custom(name, size: size, relativeTo: style) }        let system: Font.Weight = weight >= 700 ? .bold : weight >= 600 ? .semibold : weight >= 500 ? .medium : .regular        return .system(size: size, weight: system)    }}enum CinemasType {    static func title(_ size: CGFloat, black: Bool = true) -> Font { CinemasFonts.display(size, black: black) }    static let featured = CinemasFonts.display(64)    static let page = CinemasFonts.display(46)    static let heading = CinemasFonts.display(34, black: false)    static let rowTitle = CinemasFonts.sans(16, 700, relativeTo: .headline)    static let rowCount = CinemasFonts.sans(12, 500, relativeTo: .caption)    static let body = CinemasFonts.sans(14, 400, relativeTo: .body)    static let list = CinemasFonts.sans(15, 400, relativeTo: .body)    static let meta = CinemasFonts.sans(13, 400, relativeTo: .subheadline)    static let small = CinemasFonts.sans(12, 400, relativeTo: .footnote)    static let smallBold = CinemasFonts.sans(12, 700, relativeTo: .footnote)    static let label = CinemasFonts.sans(12, 700, relativeTo: .caption)    static let tiny = CinemasFonts.sans(11, 600, relativeTo: .caption2)    static let button = CinemasFonts.sans(15, 700, relativeTo: .callout)    static let chip = CinemasFonts.sans(13, 600, relativeTo: .subheadline)}/// MOTION.md's table, as SwiftUI animations. Each has its Reduce Motion form beside it: every move becomes a 150 ms/// cross-fade, parallax is off, the shared-element grow becomes a fade, the press-in stays at 0.98 with no spring.enum CinemasMotion {    /// Standard: cubic-bezier(0.2, 0.0, 0.0, 1.0), fast out, soft land.    static func standard(_ seconds: Double) -> Animation { .timingCurve(0.2, 0.0, 0.0, 1.0, duration: seconds) }    /// Exit: cubic-bezier(0.3, 0.0, 0.8, 0.15).    static func exit(_ seconds: Double) -> Animation { .timingCurve(0.3, 0.0, 0.8, 0.15, duration: seconds) }    static let reduced = Animation.easeInOut(duration: 0.15)    static let appOpens = standard(0.22)    static let featuredBackdrop = standard(0.70)    static let featuredTitle = standard(0.42).delay(0.12)    static let featuredButtons = standard(0.42).delay(0.20)    static let featuredChanges = standard(0.45)    static let featuredRise: CGFloat = 16    static let featuredScale: CGFloat = 1.06    static func rowAppears(_ index: Int) -> Animation { standard(0.32).delay(0.04 * Double(min(index, 5))) }    static let rowRise: CGFloat = 12    static let shimmerPass: Double = 1.2    static let skeletonToPoster = standard(0.18)    static let pressDown = standard(0.09)    static let pressUp = Animation.spring(response: 0.3, dampingFraction: 0.6)    static let pressScale: CGFloat = 0.96    static let pressScaleReduced: CGFloat = 0.98    static let liftAfter: Double = 0.35    static let lift = standard(0.18)    static let liftScale: CGFloat = 1.04    static let sheet = Animation.spring(response: 0.4, dampingFraction: 0.86)    static let headerBar = standard(0.20)    static let seasonSwitch = standard(0.26)    static let seasonSlide: CGFloat = 24    static let tabChange = standard(0.20)    static let tabIcon = Animation.spring(response: 0.3, dampingFraction: 0.7)    static let tvBar = standard(0.30)    static let keeping = Animation.linear(duration: 0.4)    static let preparingPulse = Animation.easeInOut(duration: 1.2).repeatForever(autoreverses: true)    static let toastIn = standard(0.22)    static let toastOut = exit(0.18)    static let removeOut = exit(0.22)    static let closeGap = Animation.spring(response: 0.35, dampingFraction: 0.9)    /// The parallax factor on a title page's backdrop, and the blur it reaches as the page scrolls up.    static let parallax: CGFloat = 0.5    static let backdropBlur: CGFloat = 12    /// An animation, or its Reduce Motion cross-fade.    static func or(_ animation: Animation, reduced reduce: Bool) -> Animation { reduce ? reduced : animation }}/// The press-in every poster and button takes: down in 90 ms to 0.96, back on a spring (0.98 and no spring under/// Reduce Motion).struct CinemasPress: ButtonStyle {    @Environment(\.accessibilityReduceMotion) private var reduceMotion    func makeBody(configuration: Configuration) -> some View {        configuration.label            .scaleEffect(configuration.isPressed ? (reduceMotion ? CinemasMotion.pressScaleReduced : CinemasMotion.pressScale) : 1)            .animation(configuration.isPressed ? CinemasMotion.pressDown : (reduceMotion ? nil : CinemasMotion.pressUp),                       value: configuration.isPressed)    }}/// The skeleton's shimmer: a band 40% of the width passing left to right every 1.2 s, opacity 0.06 → 0.12. Still under/// Reduce Motion.struct CinemasShimmer: ViewModifier {    @Environment(\.accessibilityReduceMotion) private var reduceMotion    @State private var phase: CGFloat = -0.4    func body(content: Content) -> some View {        content            .overlay {                if !reduceMotion {                    GeometryReader { box in                        // A shape asked for an unbounded width has none to sweep: no band, never a NaN frame.                        let wide = box.size.width.drawableOrZero                        LinearGradient(colors: [.white.opacity(0), .white.opacity(0.06), .white.opacity(0)],                                       startPoint: .leading, endPoint: .trailing)                            .frame(width: wide * 0.4)                            .offset(x: phase * wide)                    }                    .clipped()                    .allowsHitTesting(false)                }            }            .onAppear {                guard !reduceMotion else { return }                withAnimation(.linear(duration: CinemasMotion.shimmerPass).repeatForever(autoreverses: false)) { phase = 1.0 }            }    }}extension View {    func cinemasShimmer() -> some View { modifier(CinemasShimmer()) }}extension CGFloat {    /// A length a frame can take: finite, not negative and no wider than any screen could be, or nil.    var drawable: CGFloat? { isFinite && self >= 0 && self < 100_000 ? self : nil }    /// The same, with nothing drawn (0) for a length no frame can take.    var drawableOrZero: CGFloat { drawable ?? 0 }}