Cotton code
174 lines · 6.9 KB · SwiftRaw
// One search in the system, drawn ONE way.//// THE GRAMMAR (phase 20, 2026-09-14) is Home's footer's, because that is the// one the owner approved (picture 30): a 56pt paper-coloured capsule floating// over the plaster — cream, a hairline, the soft shadow — the magnifier and the// words TYPED, and a separate 56pt close circle beside it. There were two// grammars (this file's 40pt elevated capsule with "Cancel" beside it, and// Home's); `SearchField` and `SearchCloseCircle` are now the only drawing of// either, and Home's footer and every screen's pull-down pill both draw them.//// CLOSE CLEARS, THEN CLOSES. A search that ends never leaves a filter behind// silently — the list you come back to is the list you left.//// The HOST owns the query: it dims its content while the pill is open with an// empty query, and renders the filtered list at full strength once the query// bites.import SwiftUI/// The capsule: magnifier and field. Focus is the caller's, because Home's/// footer owns its field's focus from above and a screen's pill owns its own.struct SearchField: View {    @Binding var text: String    let placeholder: String    var isFocused: FocusState<Bool>.Binding    @Environment(\.theme) private var theme    var body: some View {        HStack(spacing: 12) {            Image(systemName: Glyph.symbol("search") ?? "magnifyingglass")                .font(.system(size: theme.scale.sectionTitleSize, weight: .regular))                .foregroundStyle(CottonColors.inkTertiary)            // The placeholder is typed in tertiary ink — the page's one            // invitation, read at arm's length.            TextField(                "",                text: $text,                prompt: Text(placeholder).foregroundStyle(CottonColors.inkTertiary)            )            .font(CottonFonts.type(theme.scale.titleSize))            .foregroundStyle(CottonColors.ink)            .textInputAutocapitalization(.never)            .autocorrectionDisabled()            .submitLabel(.search)            .focused(isFocused)            .accessibilityLabel(placeholder)            // No bordered platform field inside the capsule, and no blue ring            // on it (`Paper/PaperFocus.swift`).            .cottonPaperField()        }        .padding(.horizontal, 16)        .frame(height: SearchPillKnobs.chromeFooterSearchHeight)        .frame(maxWidth: .infinity)        // Paper-coloured, not white: the input floats over the plaster as the        // quiet cream of the objects around it (paper kit §1.6).        .background(Capsule().fill(SearchGrammar.fill))        // The capsule's own hairline IS the focus ring: it inks while the        // field has the keyboard. The walk photographed the system's blue one        // over the paper instead (finding 15).        .cottonInkFocusRing(isFocused.wrappedValue, in: Capsule())        .softShadow()    }}/// The close beside the capsule — the capsule's own height, so the two read as/// one row. The nav circle's fill, with the shadow doing the work.struct SearchCloseCircle: View {    let action: () -> Void    @Environment(\.theme) private var theme    var body: some View {        Button(action: action) {            ZStack {                Circle()                    .fill(NavChrome.circleFill)                    .overlay(Circle().strokeBorder(CottonColors.hairline, lineWidth: ComposerCardKnobs.chromeHairline))                    .softShadow()                Image(systemName: Glyph.symbol("x") ?? "xmark")                    .font(.system(size: theme.scale.sectionTitleSize, weight: .regular))                    .foregroundStyle(NavChrome.glyphInk)            }            .frame(width: SearchPillKnobs.chromeFooterSearchHeight, height: SearchPillKnobs.chromeFooterSearchHeight)            .contentShape(Circle())        }        .buttonStyle(MotionButtonStyle())        .accessibilityLabel(CottonStrings.cancel)    }}/// The grammar's numbers and paint, named once.enum SearchGrammar {    static let fill: Color = PaperDye.cream.fill    /// What closing does, in order: the query is cleared before the host hears.    static func close(clear: () -> Void, then onClose: () -> Void) {        clear()        onClose()    }}public struct SearchPill: View {    @Binding private var text: String    private let placeholder: String    private let onClose: () -> Void    /// The pill takes the keyboard the moment it opens, because it opens BY a    /// deliberate tap on the search circle and a person who tapped it is    /// already typing. A specimen that is only being LOOKED at passes `false` —    /// otherwise the gallery raises a keyboard nobody asked for.    private let takesFocus: Bool    @FocusState private var isFocused: Bool    public init(        text: Binding<String>,        placeholder: String? = nil,        takesFocus: Bool = true,        onClose: @escaping () -> Void    ) {        _text = text        self.placeholder = placeholder ?? CottonStrings.search        self.takesFocus = takesFocus        self.onClose = onClose    }    public var body: some View {        HStack(spacing: 10) {            SearchField(text: $text, placeholder: placeholder, isFocused: $isFocused)            SearchCloseCircle {                // Clear, THEN close.                SearchGrammar.close(clear: { text = "" }, then: onClose)            }        }        .cottonContentMargin()        .onAppear { if takesFocus { isFocused = true } }    }}/// The sizes, counts and timings SearchPill draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum SearchPillKnobs {    static let chromeFooterSearchHeight: CGFloat = 56}/// A page head's magnifier (fan3 part 6): where a page's search face is/// `icon`, the search is this circle in the head's row, beside the stack of/// cards; a tap turns the row into the field (`SearchPill`) with the/// keyboard up, and its close gives the row back.struct HeadSearchCircle: View {    let action: () -> Void    var body: some View {        NavCircle(symbol: Glyph.symbol("search") ?? "magnifyingglass", label: CottonStrings.search, action: action)    }}/// The board's magnifier, handed to the head it stands in; nil where the/// board offers no search in its head.private struct HeadSearchKey: EnvironmentKey {    static let defaultValue: (@MainActor @Sendable () -> Void)? = nil}extension EnvironmentValues {    var headSearch: (@MainActor @Sendable () -> Void)? {        get { self[HeadSearchKey.self] }        set { self[HeadSearchKey.self] = newValue }    }}/// A page under Home's fan has its search field open: Home puts the fan and/// the record button away while it is, as it does for its own head's field.struct PageSearchingKey: PreferenceKey {    static let defaultValue = false    static func reduce(value: inout Bool, nextValue: () -> Bool) { value = value || nextValue() }}