SPB Git

Repository Public License

LunarSupport.swift 2.7 KB 80 lines swift

1import SwiftUI23/// An icon source: an SF Symbol or a proprietary Lunar glyph.4public enum LunarIcon: Hashable, Sendable {5    case symbol(String)6    case glyph(LunarGlyph)7}89/// Renders a `LunarIcon` at a point size, in the current foreground style. Decorative by default.10public struct LunarIconView: View {11    private let icon: LunarIcon12    private let size: CGFloat1314    public init(_ icon: LunarIcon, size: CGFloat = LunarMetrics.icon) {15        self.icon = icon16        self.size = size17    }1819    public var body: some View {20        switch icon {21        case .symbol(let name):22            Image(systemName: name)23                .font(.system(size: size * 0.82, weight: .medium))24                .frame(width: size, height: size)25                .accessibilityHidden(true)26        case .glyph(let glyph):27            LunarGlyphView(glyph, size: size)28        }29    }30}3132extension View {33    /// Guarantees the platform minimum hit target (44 pt on iOS, 28 pt on macOS) without changing the visual size.34    public func lunarHitTarget(_ shape: LunarShape = .rounded(LunarRadius.xs)) -> some View {35        frame(minWidth: LunarMetrics.hitTarget, minHeight: LunarMetrics.hitTarget)36            .contentShape(shape)37    }3839    /// Draws the Lunar keyboard focus ring around `shape` when `isFocused` is true.40    public func lunarFocusRing(_ shape: LunarShape, isFocused: Bool) -> some View {41        modifier(LunarFocusRingModifier(shape: shape, explicit: isFocused))42    }4344    /// Draws the Lunar focus ring when the enclosing focusable control (a `Button` label, for instance) has45    /// keyboard focus, and hides the system ring.46    public func lunarFocusRing(_ shape: LunarShape) -> some View {47        modifier(LunarFocusRingModifier(shape: shape, explicit: nil))48    }49}5051struct LunarFocusRingModifier: ViewModifier {52    @Environment(\.lunarTheme) private var theme53    @Environment(\.isFocused) private var environmentFocus54    let shape: LunarShape55    let explicit: Bool?5657    func body(content: Content) -> some View {58        let focused = explicit ?? environmentFocus59        content60            .overlay {61                // Outset so the ring never covers the control's own hairline.62                shape.inset(by: -3)63                    .stroke(theme.focusRing, lineWidth: LunarStroke.focus)64                    .opacity(focused ? 1 : 0)65                    .allowsHitTesting(false)66            }67            .focusEffectDisabled()68    }69}7071/// Tracks pointer hover (macOS, iPadOS with a pointer). Always `false` in snapshot renders.72struct LunarHoverReader<Content: View>: View {73    @State private var hovering = false74    @ViewBuilder var content: (Bool) -> Content7576    var body: some View {77        content(hovering).onHover { hovering = $0 }78    }79}80
Files main
Menu

Theme