| 1 | import SwiftUI |
| 2 | |
| 3 | /// Base's surfaces (P10), each a rounded panel over the sky: |
| 4 | /// |
| 5 | /// - **glass**: the web's `.glass`, tinted dark in the dark and white frost |
| 6 | /// in the light: `linear-gradient(--glass-1, --glass-2)` over |
| 7 | /// `--glass-base`, over the system's thin material for the blur. |
| 8 | /// - **sheet**: the panel that carries the conversation (the mockups' |
| 9 | /// `.sheet`): the same, with `--sheet-*`, denser, so text keeps WCAG AA |
| 10 | /// whatever the fog behind it does. |
| 11 | /// - **solid**: menus and the job view (`.solid`, `.jobview`): near-solid |
| 12 | /// `--menu-base`, as they open over other glass. |
| 13 | /// |
| 14 | /// Each has a 1 px bright top edge (`inset 0 1px 0 --glass-shine`), a |
| 15 | /// hairline border (`--glass-edge`) and the web's soft lift. Text sits only |
| 16 | /// on these, never straight on the sky (spec 6). |
| 17 | public enum Surface: Sendable { case glass, sheet, solid } |
| 18 | |
| 19 | public extension View { |
| 20 | func glass(cornerRadius: CGFloat = 18) -> some View { modifier(SurfaceModifier(kind: .glass, radius: cornerRadius)) } |
| 21 | /// `.sheet` on the web; named so it can't be mistaken for SwiftUI's |
| 22 | /// `.sheet(isPresented:)`. |
| 23 | func glassSheet(cornerRadius: CGFloat = 18) -> some View { modifier(SurfaceModifier(kind: .sheet, radius: cornerRadius)) } |
| 24 | func solid(cornerRadius: CGFloat = 16) -> some View { modifier(SurfaceModifier(kind: .solid, radius: cornerRadius)) } |
| 25 | } |
| 26 | |
| 27 | struct SurfaceModifier: ViewModifier { |
| 28 | let kind: Surface |
| 29 | let radius: CGFloat |
| 30 | @Environment(\.colorScheme) private var scheme |
| 31 | |
| 32 | func body(content: Content) -> some View { |
| 33 | let p = Palette(scheme) |
| 34 | let shape = RoundedRectangle(cornerRadius: radius, style: .continuous) |
| 35 | content |
| 36 | .background { |
| 37 | switch kind { |
| 38 | case .glass: |
| 39 | ZStack { |
| 40 | shape.fill(.ultraThinMaterial) |
| 41 | shape.fill(p[.glassBase]) |
| 42 | shape.fill(LinearGradient(colors: [p[.glass1], p[.glass2]], startPoint: .top, endPoint: .bottom)) |
| 43 | } |
| 44 | case .sheet: |
| 45 | ZStack { |
| 46 | shape.fill(.ultraThinMaterial) |
| 47 | shape.fill(p[.sheetBase]) |
| 48 | shape.fill(LinearGradient(colors: [p[.sheet1], p[.sheet2]], startPoint: .top, endPoint: .bottom)) |
| 49 | } |
| 50 | case .solid: |
| 51 | shape.fill(p[.menuBase]) |
| 52 | } |
| 53 | } |
| 54 | .overlay { |
| 55 | ZStack { |
| 56 | if kind != .solid { shape.strokeBorder(p[.glassEdge], lineWidth: 1) } |
| 57 | TopShine(radius: radius, color: kind == .solid ? p[.menuShine] : p[.glassShine]) |
| 58 | } |
| 59 | .allowsHitTesting(false) |
| 60 | } |
| 61 | .clipShape(shape) |
| 62 | // 0 18px 40px var(--glass-lift) var(--glass-shadow): a spread as |
| 63 | // negative as half the blur leaves a soft, low shadow. SwiftUI has |
| 64 | // no spread, so the radius and alpha are smaller; and the shadow |
| 65 | // is drawn outside the panel only, as a box-shadow is, so the |
| 66 | // glass doesn't darken itself. |
| 67 | .background { |
| 68 | OuterShadow(radius: radius, color: kind == .solid ? p[.menuShadow] : p[.glassShadow], |
| 69 | blur: kind == .solid ? 24 : 14, y: kind == .solid ? 16 : 12) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// A shadow cast outside a rounded panel, and nowhere under it. |
| 75 | struct OuterShadow: View { |
| 76 | let radius: CGFloat |
| 77 | let color: Color |
| 78 | let blur: CGFloat |
| 79 | let y: CGFloat |
| 80 | var body: some View { |
| 81 | let shape = RoundedRectangle(cornerRadius: radius, style: .continuous) |
| 82 | shape.fill(Color.black) |
| 83 | .shadow(color: color.opacity(0.55), radius: blur, y: y) |
| 84 | .mask { |
| 85 | Rectangle().padding(-(blur * 3 + abs(y))) |
| 86 | .overlay { shape.blendMode(.destinationOut) } |
| 87 | .compositingGroup() |
| 88 | } |
| 89 | .allowsHitTesting(false) |
| 90 | .accessibilityHidden(true) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// `inset 0 1px 0 <colour>`: a 1 px line inside the top edge that follows |
| 95 | /// its corners and fades down them. |
| 96 | struct TopShine: View { |
| 97 | let radius: CGFloat |
| 98 | let color: Color |
| 99 | var body: some View { |
| 100 | RoundedRectangle(cornerRadius: radius, style: .continuous) |
| 101 | .stroke(color, lineWidth: 2) |
| 102 | .offset(y: 1) |
| 103 | .mask(LinearGradient(stops: [.init(color: .white, location: 0), .init(color: .clear, location: 0.5)], |
| 104 | startPoint: .top, endPoint: UnitPoint(x: 0.5, y: min(1, radius / 40)))) |
| 105 | .clipShape(RoundedRectangle(cornerRadius: radius, style: .continuous)) |
| 106 | } |
| 107 | } |
| 108 | |