Cotton code
144 lines · 6.1 KB · SwiftRaw
// The switch on paper.//// A system switch is a green pill of glass; set on a sheet of cream it reads// as a sticker somebody pressed onto the page (phone walk row 155, the news// topics). This is the same control drawn the way the rest of the paper is:// a pencil capsule for the track, and a dot of ink that sits at the right when// the thing is ON and an empty pencil ring at the left when it is off. No// colour carries the state — the ink and its place do, which is also what// keeps it readable in the one-colour print the paper imitates.//// It is a `ToggleStyle`, not a new control: every call site keeps `Toggle`,// its binding and `.disabled`, and a screen reader still hears a switch with// its label (`accessibilityRepresentation` hands it the system one).//// TWO FACES, because a custom style is handed its label whatever// `.labelsHidden()` said (the modifier hides the labels of the SYSTEM styles// only, and the environment value that reports it is macOS 15's): `.paper`// draws the switch alone and does not stretch, for a row that sets its own// words beside it; `.paperLabelled` draws the label leading across the row and// the switch at its end, as the system switch does.// `SwitchSweepTests` holds every `Toggle` under `Screens/` to this style.import SwiftUIpublic struct PaperToggleStyle: ToggleStyle {    let showsLabel: Bool    public func makeBody(configuration: Configuration) -> some View {        PaperSwitchBody(configuration: configuration, showsLabel: showsLabel)    }}extension ToggleStyle where Self == PaperToggleStyle {    /// The paper switch alone — its label is what a screen reader hears.    public static var paper: PaperToggleStyle { PaperToggleStyle(showsLabel: false) }    /// The paper switch at the end of a row its label leads.    public static var paperLabelled: PaperToggleStyle { PaperToggleStyle(showsLabel: true) }}/// The drawing, in its own view so it can read the environment a style's/// `makeBody` cannot.struct PaperSwitchBody: View {    let configuration: ToggleStyleConfiguration    let showsLabel: Bool    @Environment(\.theme) private var theme    @Environment(\.isEnabled) private var isEnabled    @Environment(\.accessibilityReduceMotion) private var reduceMotion    var body: some View {        // The whole row is the switch (S1): its words and the air beside them        // flip it as the capsule does, never the capsule alone.        Button {            withAnimation(reduceMotion ? nil : .spring(                response: Double(PaperSwitchKnobs.paperSwitchSpringResponse),                dampingFraction: Double(PaperSwitchKnobs.paperSwitchSpringDamping))) {                configuration.isOn.toggle()            }        } label: {            HStack(alignment: .center, spacing: PaperSwitchKnobs.paperSwitchLabelGap) {                if showsLabel {                    configuration.label                        .frame(maxWidth: .infinity, alignment: .leading)                }                PaperSwitch(isOn: configuration.isOn)                    // The drawing is small; the finger is not.                    .frame(minWidth: PaperSwitchKnobs.paperSwitchHitTarget, minHeight: PaperSwitchKnobs.paperSwitchHitTarget)            }            .contentShape(Rectangle())        }        .buttonStyle(OpacityButtonStyle(pressed: Double(PaperSwitchKnobs.paperSwitchPressedOpacity)))        .opacity(isEnabled ? 1 : DisabledOpacity.actionPill)        .accessibilityRepresentation {            Toggle(isOn: configuration.$isOn) { configuration.label }        }    }}/// The capsule and the dot, with no behaviour — what the toggle style draws.struct PaperSwitch: View {    @Environment(\.theme) private var theme    private let isOn: Bool    public init(isOn: Bool) {        self.isOn = isOn    }    public var body: some View {        let metrics = PaperSwitchMetrics(scale: theme.scale)        ZStack(alignment: isOn ? .trailing : .leading) {            Capsule()                .stroke(                    theme.print(isOn ? .ink : .pencil),                    style: StrokeStyle(lineWidth: metrics.trackWidth, lineCap: .round)                )                .frame(width: metrics.width, height: metrics.height)            Group {                if isOn {                    Circle().fill(theme.print(.ink))                } else {                    Circle()                        .stroke(theme.print(.pencil), style: StrokeStyle(lineWidth: metrics.offKnobWidth))                }            }            .frame(width: metrics.knob, height: metrics.knob)            .padding(.horizontal, metrics.knobInset)        }        .frame(width: metrics.width, height: metrics.height)        .compositingGroup()        .blendMode(theme.printBlend)        .accessibilityHidden(true)    }}struct PaperSwitchMetrics {    let scale: ThemeScale    /// The capsule's width and height — a system switch's footprint, so a row    /// laid out for one does not move.    var width: CGFloat { PaperSwitchKnobs.paperSwitchWidth }    var height: CGFloat { PaperSwitchKnobs.paperSwitchHeight }    /// The ink dot, and its air inside the capsule.    var knob: CGFloat { PaperSwitchKnobs.paperSwitchKnobSide }    var knobInset: CGFloat { PaperSwitchKnobs.paperSwitchKnobInset }    /// The capsule's pencil, and the empty ring's.    var trackWidth: CGFloat { PaperSwitchKnobs.paperSwitchTrackWidth }    var offKnobWidth: CGFloat { PaperSwitchKnobs.paperSwitchOffKnobWidth }}/// The sizes, counts and timings PaperSwitch draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum PaperSwitchKnobs {    static let paperSwitchHeight: CGFloat = 28    static let paperSwitchHitTarget: CGFloat = 44    static let paperSwitchKnobInset: CGFloat = 5    static let paperSwitchKnobSide: CGFloat = 18    static let paperSwitchLabelGap: CGFloat = 12    static let paperSwitchOffKnobWidth: CGFloat = 1.2    static let paperSwitchPressedOpacity: CGFloat = 0.7    static let paperSwitchSpringDamping: CGFloat = 0.8    static let paperSwitchSpringResponse: CGFloat = 0.28    static let paperSwitchTrackWidth: CGFloat = 1.3    static let paperSwitchWidth: CGFloat = 50}