SPB Git

Repository Public

VQSurface.swift 5.1 KB 114 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantUI/Components/VQSurface.swift3// Description: Surface, Panel and SectionHeader — depth by layering, hairlines, a rule to open a section.4import SwiftUI5import VQuantKit67/// A layer with an elevation level: 0 canvas, 1 panel, 2 raised (menus, composer), 3 overlays. Glass is for chrome8/// only (toolbars, tab bars, palette) — never around content.9public struct VQSurface<Content: View>: View {10    private let level: Int11    private let glass: Bool12    private let radius: CGFloat13    private let content: Content14    @Environment(\.colorScheme) private var scheme1516    public init(level: Int = 1, glass: Bool = false, radius: CGFloat = VQTokens.Radius.lg, @ViewBuilder content: () -> Content) {17        self.level = min(3, max(0, level)); self.glass = glass; self.radius = radius; self.content = content()18    }1920    private var fill: Color {21        switch level {22        case 0: return VQP.ink023        case 1: return VQP.ink124        default: return VQP.ink225        }26    }27    private var shadow: VQTokens.Elevation.Shadow? {28        switch level {29        case 1: return VQTokens.Elevation.level130        case 2: return VQTokens.Elevation.level231        case 3: return VQTokens.Elevation.level332        default: return nil33        }34    }3536    public var body: some View {37        let shape = RoundedRectangle(cornerRadius: radius, style: .continuous)38        content39            .background {40                if glass { shape.fill(.regularMaterial) } else { shape.fill(fill) }41            }42            .overlay { if level > 0 { shape.strokeBorder(VQP.line, lineWidth: VQTokens.hairline) } }43            .clipShape(shape)44            .shadow(color: .black.opacity(shadow?.opacity(for: scheme) ?? 0), radius: (shadow?.blur ?? 0) / 2, x: shadow?.x ?? 0, y: shadow?.y ?? 0)45    }46}4748/// The workspace building block: a title bar (title · meta · actions), a body, an optional footer.49public struct VQPanel<Meta: View, Actions: View, Content: View, Footer: View>: View {50    private let title: String51    private let flush: Bool52    private let meta: Meta53    private let actions: Actions54    private let content: Content55    private let footer: Footer5657    /// - Parameter flush: the body goes edge to edge (tables, charts); otherwise it is padded.58    public init(_ title: String, flush: Bool = false, @ViewBuilder meta: () -> Meta = { EmptyView() }, @ViewBuilder actions: () -> Actions = { EmptyView() },59                @ViewBuilder content: () -> Content, @ViewBuilder footer: () -> Footer = { EmptyView() }) {60        self.title = title; self.flush = flush; self.meta = meta(); self.actions = actions(); self.content = content(); self.footer = footer()61    }6263    public var body: some View {64        VQSurface(level: 1) {65            VStack(alignment: .leading, spacing: 0) {66                HStack(alignment: .firstTextBaseline, spacing: VQS.s2) {67                    Text(title).vqFont(VQTokens.Typography.heading).foregroundStyle(VQP.text1).lineLimit(1)68                        .accessibilityAddTraits(.isHeader)69                    meta70                    Spacer(minLength: VQS.s2)71                    actions72                }73                .padding(.horizontal, VQS.s4).padding(.vertical, VQS.s3)74                Rectangle().fill(VQP.line).frame(height: VQTokens.hairline)75                content.padding(flush ? 0 : VQS.s4).frame(maxWidth: .infinity, alignment: .leading)76                if Footer.self != EmptyView.self {77                    Rectangle().fill(VQP.line).frame(height: VQTokens.hairline)78                    footer.padding(.horizontal, VQS.s4).padding(.vertical, VQS.s2).frame(maxWidth: .infinity, alignment: .leading)79                }80            }81        }82        .accessibilityElement(children: .contain)83        .accessibilityLabel(title)84    }85}8687/// The editorial opening of a section: a 1-pt rule in full ink, an optional overline, a serif title, actions.88public struct VQSectionHeader<Actions: View>: View {89    private let title: String90    private let overline: String?91    private let rule: Bool92    private let actions: Actions9394    public init(_ title: String, overline: String? = nil, rule: Bool = true, @ViewBuilder actions: () -> Actions = { EmptyView() }) {95        self.title = title; self.overline = overline; self.rule = rule; self.actions = actions()96    }9798    public var body: some View {99        VStack(alignment: .leading, spacing: VQS.s2) {100            if rule { Rectangle().fill(VQP.text1).frame(height: VQTokens.hairline).accessibilityHidden(true) }101            HStack(alignment: .lastTextBaseline, spacing: VQS.s3) {102                VStack(alignment: .leading, spacing: 2) {103                    if let overline { Text(overline).vqFont(VQTokens.Typography.overline).foregroundStyle(VQP.text3) }104                    Text(title).vqFont(VQTokens.Typography.title).foregroundStyle(VQP.text1).fixedSize(horizontal: false, vertical: true)105                }106                Spacer(minLength: VQS.s2)107                actions108            }109        }110        .accessibilityElement(children: .contain)111        .accessibilityAddTraits(.isHeader)112    }113}114
Files main
Menu

Theme