import SwiftUI /// Base's surfaces (P10), each a rounded panel over the sky: /// /// - **glass**: the web's `.glass`, tinted dark in the dark and white frost /// in the light: `linear-gradient(--glass-1, --glass-2)` over /// `--glass-base`, over the system's thin material for the blur. /// - **sheet**: the panel that carries the conversation (the mockups' /// `.sheet`): the same, with `--sheet-*`, denser, so text keeps WCAG AA /// whatever the fog behind it does. /// - **solid**: menus and the job view (`.solid`, `.jobview`): near-solid /// `--menu-base`, as they open over other glass. /// /// Each has a 1 px bright top edge (`inset 0 1px 0 --glass-shine`), a /// hairline border (`--glass-edge`) and the web's soft lift. Text sits only /// on these, never straight on the sky (spec 6). public enum Surface: Sendable { case glass, sheet, solid } public extension View { func glass(cornerRadius: CGFloat = 18) -> some View { modifier(SurfaceModifier(kind: .glass, radius: cornerRadius)) } /// `.sheet` on the web; named so it can't be mistaken for SwiftUI's /// `.sheet(isPresented:)`. func glassSheet(cornerRadius: CGFloat = 18) -> some View { modifier(SurfaceModifier(kind: .sheet, radius: cornerRadius)) } func solid(cornerRadius: CGFloat = 16) -> some View { modifier(SurfaceModifier(kind: .solid, radius: cornerRadius)) } } struct SurfaceModifier: ViewModifier { let kind: Surface let radius: CGFloat @Environment(\.colorScheme) private var scheme func body(content: Content) -> some View { let p = Palette(scheme) let shape = RoundedRectangle(cornerRadius: radius, style: .continuous) content .background { switch kind { case .glass: ZStack { shape.fill(.ultraThinMaterial) shape.fill(p[.glassBase]) shape.fill(LinearGradient(colors: [p[.glass1], p[.glass2]], startPoint: .top, endPoint: .bottom)) } case .sheet: ZStack { shape.fill(.ultraThinMaterial) shape.fill(p[.sheetBase]) shape.fill(LinearGradient(colors: [p[.sheet1], p[.sheet2]], startPoint: .top, endPoint: .bottom)) } case .solid: shape.fill(p[.menuBase]) } } .overlay { ZStack { if kind != .solid { shape.strokeBorder(p[.glassEdge], lineWidth: 1) } TopShine(radius: radius, color: kind == .solid ? p[.menuShine] : p[.glassShine]) } .allowsHitTesting(false) } .clipShape(shape) // 0 18px 40px var(--glass-lift) var(--glass-shadow): a spread as // negative as half the blur leaves a soft, low shadow. SwiftUI has // no spread, so the radius and alpha are smaller; and the shadow // is drawn outside the panel only, as a box-shadow is, so the // glass doesn't darken itself. .background { OuterShadow(radius: radius, color: kind == .solid ? p[.menuShadow] : p[.glassShadow], blur: kind == .solid ? 24 : 14, y: kind == .solid ? 16 : 12) } } } /// A shadow cast outside a rounded panel, and nowhere under it. struct OuterShadow: View { let radius: CGFloat let color: Color let blur: CGFloat let y: CGFloat var body: some View { let shape = RoundedRectangle(cornerRadius: radius, style: .continuous) shape.fill(Color.black) .shadow(color: color.opacity(0.55), radius: blur, y: y) .mask { Rectangle().padding(-(blur * 3 + abs(y))) .overlay { shape.blendMode(.destinationOut) } .compositingGroup() } .allowsHitTesting(false) .accessibilityHidden(true) } } /// `inset 0 1px 0 `: a 1 px line inside the top edge that follows /// its corners and fades down them. struct TopShine: View { let radius: CGFloat let color: Color var body: some View { RoundedRectangle(cornerRadius: radius, style: .continuous) .stroke(color, lineWidth: 2) .offset(y: 1) .mask(LinearGradient(stops: [.init(color: .white, location: 0), .init(color: .clear, location: 0.5)], startPoint: .top, endPoint: UnitPoint(x: 0.5, y: min(1, radius / 40)))) .clipShape(RoundedRectangle(cornerRadius: radius, style: .continuous)) } }