// 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.importSwiftUIpublicstructPaperToggleStyle: ToggleStyle {let showsLabel: Boolpublicfunc makeBody(configuration: Configuration) -> someView {PaperSwitchBody(configuration: configuration, showsLabel: showsLabel) }}extensionToggleStylewhereSelf == PaperToggleStyle {/// The paper switch alone — its label is what a screen reader hears.publicstaticvar paper: PaperToggleStyle { PaperToggleStyle(showsLabel: false) }/// The paper switch at the end of a row its label leads.publicstaticvar paperLabelled: PaperToggleStyle { PaperToggleStyle(showsLabel: true) }}/// The drawing, in its own view so it can read the environment a style's/// `makeBody` cannot.structPaperSwitchBody: View {let configuration: ToggleStyleConfigurationlet showsLabel: Bool @Environment(\.theme) privatevar theme @Environment(\.isEnabled) privatevar isEnabled @Environment(\.accessibilityReduceMotion) privatevar reduceMotionvar body: someView {// 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.structPaperSwitch: View { @Environment(\.theme) privatevar themeprivatelet isOn: Boolpublicinit(isOn: Bool) {self.isOn = isOn }publicvar body: someView {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) }}structPaperSwitchMetrics {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`).enumPaperSwitchKnobs {staticlet paperSwitchHeight: CGFloat = 28staticlet paperSwitchHitTarget: CGFloat = 44staticlet paperSwitchKnobInset: CGFloat = 5staticlet paperSwitchKnobSide: CGFloat = 18staticlet paperSwitchLabelGap: CGFloat = 12staticlet paperSwitchOffKnobWidth: CGFloat = 1.2staticlet paperSwitchPressedOpacity: CGFloat = 0.7staticlet paperSwitchSpringDamping: CGFloat = 0.8staticlet paperSwitchSpringResponse: CGFloat = 0.28staticlet paperSwitchTrackWidth: CGFloat = 1.3staticlet paperSwitchWidth: CGFloat = 50}