SPB Git

Repository Public License

LunarFields.swift 5.7 KB 153 lines swift

1import SwiftUI23extension View {4    /// Styles a `TextField` / `SecureField` as a Lunar field: a recessed well with a hairline, and the accent focus5    /// ring. Apply it to the field itself.6    ///7    /// ```swift8    /// TextField("Name", text: $name).lunarField(isFocused: focused)9    /// ```10    public func lunarField(_ shape: LunarShape = .xs, isFocused: Bool = false) -> some View {11        modifier(LunarFieldModifier(shape: shape, isFocused: isFocused))12    }13}1415private struct LunarFieldModifier: ViewModifier {16    @Environment(\.lunarTheme) private var theme17    @Environment(\.isEnabled) private var isEnabled18    let shape: LunarShape19    let isFocused: Bool2021    func body(content: Content) -> some View {22        content23            .textFieldStyle(.plain)24            .lunarFont(.body)25            .foregroundStyle(theme.textPrimary)26            .padding(.horizontal, LunarSpacing.sm)27            .frame(minHeight: LunarMetrics.hitTarget > LunarMetrics.controlHeight ? LunarMetrics.hitTarget28                                                                                   : LunarMetrics.controlHeight)29            .background { shape.fill(theme.sunkenSurface) }30            .overlay { shape.strokeBorder(isFocused ? theme.accent.withAlpha(0.7) : theme.hairline, lineWidth: 1) }31            .lunarFocusRing(shape, isFocused: isFocused)32            .opacity(isEnabled ? 1 : 0.5)33    }34}3536/// A labelled single-line text field in the Lunar style, managing its own focus ring.37public struct LunarTextField: View {38    @Environment(\.lunarTheme) private var theme39    @FocusState private var focused: Bool40    private let prompt: String41    private let icon: LunarIcon?42    @Binding private var text: String4344    public init(_ prompt: String, text: Binding<String>, icon: LunarIcon? = nil) {45        self.prompt = prompt46        self.icon = icon47        self._text = text48    }4950    public var body: some View {51        HStack(spacing: LunarSpacing.xs) {52            if let icon {53                LunarIconView(icon, size: 16).foregroundStyle(theme.textTertiary)54            }55            LunarEditableText(prompt: prompt, text: $text)56                .focused($focused)57        }58        .lunarField(isFocused: focused)59        .accessibilityLabel(prompt)60    }61}6263/// The floating capsule search field: the base of the omnibox.64public struct LunarSearchField<Accessory: View>: View {65    @Environment(\.lunarTheme) private var theme66    @FocusState private var focused: Bool67    private let prompt: String68    private let leading: LunarIcon69    private let accessory: Accessory70    @Binding private var text: String7172    /// - Parameters:73    ///   - leading: the icon at the leading edge (a lock, a lens, the Orbit glyph…).74    ///   - accessory: trailing controls inside the capsule (reload, reader, shield…).75    ///76    /// Attach `.onSubmit { … }` to the field to handle Return.77    public init(text: Binding<String>, prompt: String, leading: LunarIcon = .glyph(.lens),78                @ViewBuilder accessory: () -> Accessory) {79        self._text = text80        self.prompt = prompt81        self.leading = leading82        self.accessory = accessory()83    }8485    public var body: some View {86        HStack(spacing: LunarSpacing.xs) {87            LunarIconView(leading, size: 16)88                .foregroundStyle(focused ? theme.accent : theme.textSecondary)89            LunarEditableText(prompt: prompt, text: $text)90                .focused($focused)91            if !text.isEmpty && focused {92                Button { text = "" } label: {93                    LunarIconView(.symbol("xmark.circle.fill"), size: 16).foregroundStyle(theme.textTertiary)94                }95                .buttonStyle(LunarPressStyle())96                .accessibilityLabel("Clear text")97            }98            accessory99        }100        .padding(.horizontal, LunarSpacing.sm + 2)101        .frame(minHeight: LunarMetrics.controlHeight + LunarSpacing.unit)102        .lunarGlass(.capsule, style: .control, elevation: .flat)103        .lunarFocusRing(.capsule, isFocused: focused)104        .contentShape(Capsule())105        .onTapGesture { focused = true }106        .accessibilityElement(children: .contain)107        .accessibilityLabel(prompt)108    }109}110111extension LunarSearchField where Accessory == EmptyView {112    public init(text: Binding<String>, prompt: String, leading: LunarIcon = .glyph(.lens)) {113        self.init(text: text, prompt: prompt, leading: leading) { EmptyView() }114    }115}116117/// The editable text of a Lunar field. Platform text fields cannot be captured offscreen, so snapshot renders draw118/// the same text statically.119struct LunarEditableText: View {120    @Environment(\.lunarTheme) private var theme121    @Environment(\.lunarRenderMode) private var renderMode122    let prompt: String123    @Binding var text: String124125    var body: some View {126        if renderMode == .snapshot {127            Text(text.isEmpty ? prompt : text)128                .lunarFont(.body)129                .foregroundStyle(text.isEmpty ? theme.textTertiary : theme.textPrimary)130                .lineLimit(1)131                .frame(maxWidth: .infinity, alignment: .leading)132        } else {133            TextField("", text: $text, prompt: Text(prompt).foregroundStyle(theme.textTertiary))134                .textFieldStyle(.plain)135                .lunarFont(.body)136                .foregroundStyle(theme.textPrimary)137                .tint(theme.accent.color)138        }139    }140}141142#Preview("Fields") {143    @Previewable @State var text = ""144    VStack(spacing: LunarSpacing.md) {145        LunarSearchField(text: $text, prompt: "Search or enter an address")146        LunarTextField("Workspace name", text: $text, icon: .glyph(.orbit))147    }148    .padding(LunarSpacing.xl)149    .frame(width: 420)150    .background(LunarWindowBackground())151    .lunarTheme(LunarThemes.lunar.dark)152}153
Files main
Menu

Theme