Repository Public License
1import SwiftUI23/// How a glass surface is being used; drives tint strength and the hairline.4public enum LunarGlassStyle: String, Sendable, CaseIterable {5 /// Bars, sidebars, docks, panels.6 case chrome7 /// Buttons, chips and other controls sitting on chrome or on the window.8 case control9 /// A surface that must stay readable over arbitrary web content (popovers, toasts): more tint, less see-through.10 case dense11}1213extension View {14 /// Puts the view on a Lunar Glass surface.15 ///16 /// - On OS 26 and later the surface is Liquid Glass (`glassEffect`), tinted with the theme.17 /// - On earlier systems it is a system material with a theme tint, a top highlight and a luminous hairline.18 /// - Under Reduce Transparency (and in snapshot renders) it is an opaque theme surface with the same hairline.19 ///20 /// Glass is for chrome and controls. Never put it behind the body text of a page.21 ///22 /// - Parameters:23 /// - shape: the silhouette (`.capsule`, `.rounded(radius)`, `.circle`, or a radius token such as `.md`).24 /// - style: `.chrome`, `.control` or `.dense`.25 /// - elevation: shadow depth; `.flat` draws none.26 /// - interactive: on OS 26+, lets Liquid Glass react to touch and pointer; use for tappable surfaces only.27 /// - tint: optional workspace tint washed into the glass.28 public func lunarGlass(_ shape: LunarShape = .md, style: LunarGlassStyle = .chrome,29 elevation: LunarElevation = .raised, interactive: Bool = false,30 tint: LunarTint? = nil) -> some View {31 modifier(LunarGlassModifier(shape: shape, style: style, elevation: elevation, interactive: interactive,32 tint: tint))33 }3435 /// A discreet luminous border following `shape`: brighter at the top edge, fading downwards.36 public func lunarHairline(_ shape: LunarShape = .md) -> some View {37 modifier(LunarHairlineModifier(shape: shape))38 }3940 /// The soft Lunar shadow for an elevation.41 public func lunarShadow(_ elevation: LunarElevation) -> some View {42 modifier(LunarShadowModifier(elevation: elevation))43 }44}4546struct LunarGlassModifier: ViewModifier {47 @Environment(\.lunarTheme) private var theme48 @Environment(\.lunarRenderMode) private var renderMode49 @Environment(\.accessibilityReduceTransparency) private var reduceTransparency50 let shape: LunarShape51 let style: LunarGlassStyle52 let elevation: LunarElevation53 let interactive: Bool54 let tint: LunarTint?5556 private var opaque: Bool { reduceTransparency || renderMode == .snapshot }5758 private var tintColor: LunarRGBA {59 let base: LunarRGBA = switch style {60 case .chrome: theme.glassTint61 case .control: theme.elevatedSurface.withAlpha(theme.isDark ? 0.46 : 0.66)62 case .dense: theme.elevatedSurface.withAlpha(0.86)63 }64 guard let tint else { return base }65 return base.mixed(with: theme.color(for: tint).withAlpha(base.alpha), 0.22)66 }6768 private var opaqueFill: LunarRGBA {69 let base: LunarRGBA = switch style {70 case .chrome: theme.chromeSurface71 case .control, .dense: theme.elevatedSurface72 }73 guard let tint else { return base }74 return base.mixed(with: theme.color(for: tint), 0.14)75 }7677 func body(content: Content) -> some View {78 surface(content)79 .lunarHairline(shape)80 .lunarShadow(elevation)81 }8283 @ViewBuilder private func surface(_ content: Content) -> some View {84 if opaque {85 content.background {86 shape.fill(opaqueFill)87 shape.fill(LunarGlassHighlight.gradient(theme: theme))88 }89 } else if #available(macOS 26.0, iOS 26.0, *) {90 content.glassEffect(Glass.regular.tint(tintColor.color).interactive(interactive), in: shape)91 } else {92 content.background {93 // `.thinMaterial` rather than an explicit blur: the system composites it once and caches it.94 shape.fill(style == .dense ? AnyShapeStyle(.regularMaterial) : AnyShapeStyle(.thinMaterial))95 shape.fill(tintColor)96 shape.fill(LunarGlassHighlight.gradient(theme: theme))97 }98 }99 }100}101102enum LunarGlassHighlight {103 /// A faint light falling from the top edge; this is what makes an opaque fallback still read as glass.104 static func gradient(theme: LunarTheme) -> LinearGradient {105 LinearGradient(106 stops: [107 .init(color: Color.white.opacity(theme.isDark ? 0.055 : 0.5), location: 0),108 .init(color: Color.white.opacity(0), location: 0.55),109 ],110 startPoint: .top, endPoint: .bottom)111 }112}113114struct LunarHairlineModifier: ViewModifier {115 @Environment(\.lunarTheme) private var theme116 @Environment(\.colorSchemeContrast) private var contrast117 let shape: LunarShape118119 func body(content: Content) -> some View {120 content.overlay {121 shape.strokeBorder(122 LinearGradient(colors: [theme.hairlineStrong.color, theme.hairline.color], startPoint: .top,123 endPoint: .bottom),124 lineWidth: contrast == .increased ? LunarStroke.hairlineStrong : LunarStroke.hairline)125 .allowsHitTesting(false)126 }127 }128}129130struct LunarShadowModifier: ViewModifier {131 @Environment(\.lunarTheme) private var theme132 let elevation: LunarElevation133134 func body(content: Content) -> some View {135 if elevation == .flat {136 content137 } else {138 content.shadow(color: theme.shadow.withAlpha(theme.shadow.alpha * elevation.shadowStrength).color,139 radius: elevation.shadowRadius, x: 0, y: elevation.shadowYOffset)140 }141 }142}143144/// The window field: the theme's vertical gradient, edge to edge.145public struct LunarWindowBackground: View {146 @Environment(\.lunarTheme) private var theme147148 public init() {}149150 public var body: some View {151 Rectangle().fill(theme.windowGradient).ignoresSafeArea()152 }153}154155/// A one-point separator in the theme's hairline colour.156public struct LunarHairline: View {157 @Environment(\.lunarTheme) private var theme158 @Environment(\.colorSchemeContrast) private var contrast159 private let axis: Axis160161 public init(_ axis: Axis = .horizontal) { self.axis = axis }162163 public var body: some View {164 let width = contrast == .increased ? LunarStroke.hairlineStrong : LunarStroke.hairline165 Rectangle()166 .fill(theme.hairline)167 .frame(width: axis == .vertical ? width : nil, height: axis == .horizontal ? width : nil)168 .accessibilityHidden(true)169 }170}171