Cotton code
72 lines · 2.9 KB · SwiftRaw
// The chrome's two shadows.//// The Bear chrome's geometry (`docs/research/bear/geometry.md`, measured, not// preferred) lives in the theme's scale as its `chrome_*` keys, and a view reads// it from the WORN theme — `ContentMarginKnobs.chromeContentMargin` — never from a// static that reads the shipped one, so a database's theme reaches the chrome.import CoreGraphicsimport SwiftUI/// The two shadows. Shadow is physical darkness, not a theme slot, so the/// colour is black at an opacity rather than a palette role — which is also why/// the colour guard allows `.black` and nothing else. Their weights are the/// WORN theme's (`chrome_shadow_*` in its scale), read from the environment.private struct SoftShadow: ViewModifier {    @Environment(\.theme) private var theme    func body(content: Content) -> some View {        content.shadow(            color: .black.opacity(Double(MetricsKnobs.chromeShadowSoftOpacity)),            radius: MetricsKnobs.chromeShadowSoftRadius,            x: MetricsKnobs.chromeShadowSoftOffsetX,            y: MetricsKnobs.chromeShadowSoftOffsetY        )    }}/// A lifted row in `ReorderableList`.private struct LiftShadow: ViewModifier {    @Environment(\.theme) private var theme    let opacity: Double?    func body(content: Content) -> some View {        content.shadow(            color: .black.opacity(opacity ?? Double(MetricsKnobs.chromeShadowLiftOpacity)),            radius: MetricsKnobs.chromeShadowLiftRadius,            x: MetricsKnobs.chromeShadowLiftOffsetX,            y: MetricsKnobs.chromeShadowLiftOffsetY        )    }}extension View {    /// The nav-chrome shadow. A `radius` in SwiftUI is a blur radius the way    /// `shadowRadius` is on the phone today, so the number carries across.    public func softShadow() -> some View {        modifier(SoftShadow())    }    /// The lifted-row shadow, whose OPACITY is what animates during a drag —    /// the geometry stays fixed, because adding a shadow mid-gesture    /// re-rasterizes the layer. Home's New thought pill wears it at rest, at    /// its full weight (`opacity: nil`, the worn theme's): the one control on    /// the page that floats above it.    public func liftShadow(opacity: Double? = nil) -> some View {        modifier(LiftShadow(opacity: opacity))    }}/// The sizes, counts and timings Metrics draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum MetricsKnobs {    static let chromeShadowLiftOffsetX: CGFloat = 0    static let chromeShadowLiftOffsetY: CGFloat = 6    static let chromeShadowLiftOpacity: CGFloat = 0.16    static let chromeShadowLiftRadius: CGFloat = 14    static let chromeShadowSoftOffsetX: CGFloat = 0    static let chromeShadowSoftOffsetY: CGFloat = 1    static let chromeShadowSoftOpacity: CGFloat = 0.1    static let chromeShadowSoftRadius: CGFloat = 5}