Cotton code
67 lines · 3.0 KB · SwiftRaw
// Focus on paper is INK, and never the platform's ring.//// The Mac walk photographed the system's blue focus ring sitting on the cream// search capsule (finding 15) and on Settings' one switch (finding 21). It is// the same fault in both: AppKit draws a bordered control's focus for itself,// in the accent colour, at a radius of its own choosing, and nothing in the// app had ever said not to. On a page that is otherwise ink on paper it reads// as a piece of another application showing through.//// The rule the app already kept for a SELECTED object is the one to keep for a// FOCUSED control: `Chrome/ListKeyboard.swift`'s `cottonKeyboardSelected` draws// "a thin ink outline … so it reads as a pencil ring around the paper rather// than a system focus ring laid on it". These two modifiers say the same// thing for the controls a person types into, and they are the ONLY place the// app suppresses a platform focus effect — a screen that wants a focus ring// asks for one of these rather than inventing a second look.//// `focusEffectDisabled()` is macOS 14 / iOS 17, and the app's floors are 14// and 18, so no availability check is needed on either host. On the phone// `textFieldStyle(.plain)` and the focus effect are both no-ops, so the same// call is written once for both hosts.import SwiftUIextension View {    /// A text field on paper: no bordered platform field around it, and no    /// platform focus ring on it. What says the field has the keyboard is the    /// paper's own ink — `cottonInkFocusRing`, drawn by whatever shape the    /// field sits in.    public func cottonPaperField() -> some View {        textFieldStyle(.plain)            .focusEffectDisabled()    }    /// The ring a paper control wears while it holds the keyboard: the app's    /// ink on the control's own shape, over the hairline it wears the rest of    /// the time. One border either way, so nothing shifts when focus arrives.    public func cottonInkFocusRing(_ focused: Bool, in shape: some InsettableShape) -> some View {        modifier(InkFocusRing(focused: focused, shape: shape))    }}/// `cottonInkFocusRing`, reading the theme's ink and hairline.private struct InkFocusRing<Outline: InsettableShape>: ViewModifier {    let focused: Bool    let shape: Outline    @Environment(\.theme) private var theme    func body(content: Content) -> some View {        content.overlay(            shape.strokeBorder(                focused                    ? theme.colors.ink.color.opacity(Double(PaperFocusKnobs.paperFocusRingOpacity))                    : theme.colors.hairline.color,                lineWidth: focused ? PaperFocusKnobs.paperFocusRingWidth : theme.scale.hairline            )        )    }}/// The sizes, counts and timings PaperFocus draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum PaperFocusKnobs {    static let paperFocusRingOpacity: CGFloat = 0.55    static let paperFocusRingWidth: CGFloat = 1.5}