SPB Git

Repository Public License

LunarContainers.swift 12.2 KB 326 lines swift

1import SwiftUI23/// The floating capsule that holds the omnibox and toolbar controls: the signature piece of Lunar chrome.4public struct LunarCapsuleBar<Content: View>: View {5    private let spacing: CGFloat6    private let elevation: LunarElevation7    private let content: Content89    public init(spacing: CGFloat = LunarSpacing.unit, elevation: LunarElevation = .floating,10                @ViewBuilder content: () -> Content) {11        self.spacing = spacing12        self.elevation = elevation13        self.content = content()14    }1516    public var body: some View {17        HStack(spacing: spacing) { content }18            .padding(.horizontal, LunarSpacing.unit + 2)19            .padding(.vertical, LunarSpacing.unit)20            .frame(minHeight: LunarMetrics.barHeight + LunarSpacing.unit)21            .lunarGlass(.capsule, style: .chrome, elevation: elevation)22            .accessibilityElement(children: .contain)23    }24}2526/// An opaque elevated container. Unlike glass, a card may hold body text.27public struct LunarCard<Content: View>: View {28    @Environment(\.lunarTheme) private var theme29    private let padding: CGFloat30    private let shape: LunarShape31    private let elevation: LunarElevation32    private let content: Content3334    public init(padding: CGFloat = LunarSpacing.md, shape: LunarShape = .md, elevation: LunarElevation = .raised,35                @ViewBuilder content: () -> Content) {36        self.padding = padding37        self.shape = shape38        self.elevation = elevation39        self.content = content()40    }4142    public var body: some View {43        content44            .padding(padding)45            .background {46                shape.fill(theme.elevatedSurface)47                shape.fill(LunarGlassHighlight.gradient(theme: theme)).opacity(theme.isDark ? 1 : 0)48            }49            .lunarHairline(shape)50            .lunarShadow(elevation)51    }52}5354/// An overline title for a group of rows, with an optional trailing action.55public struct LunarSectionHeader: View {56    @Environment(\.lunarTheme) private var theme57    private let title: String58    private let actionTitle: String?59    private let action: (() -> Void)?6061    public init(_ title: String, actionTitle: String? = nil, action: (() -> Void)? = nil) {62        self.title = title63        self.actionTitle = actionTitle64        self.action = action65    }6667    public var body: some View {68        HStack(alignment: .firstTextBaseline) {69            Text(title.uppercased())70                .lunarFont(.overline)71                .foregroundStyle(theme.textSecondary)72                .accessibilityLabel(title)73                .accessibilityAddTraits(.isHeader)74            Spacer(minLength: LunarSpacing.xs)75            if let actionTitle, let action {76                Button(actionTitle, action: action)77                    .buttonStyle(LunarPressStyle())78                    .lunarFont(.caption)79                    .foregroundStyle(theme.accent)80            }81        }82        .padding(.horizontal, LunarSpacing.xs)83    }84}8586/// A row for sidebars, menus, history and settings lists.87public struct LunarListRow<Leading: View, Trailing: View>: View {88    @Environment(\.lunarTheme) private var theme89    @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor90    private let title: String91    private let subtitle: String?92    private let isSelected: Bool93    private let action: (() -> Void)?94    private let leading: Leading95    private let trailing: Trailing9697    public init(_ title: String, subtitle: String? = nil, isSelected: Bool = false, action: (() -> Void)? = nil,98                @ViewBuilder leading: () -> Leading, @ViewBuilder trailing: () -> Trailing) {99        self.title = title100        self.subtitle = subtitle101        self.isSelected = isSelected102        self.action = action103        self.leading = leading()104        self.trailing = trailing()105    }106107    public var body: some View {108        if let action {109            Button(action: action) { row }110                .buttonStyle(LunarPressStyle())111                .accessibilityAddTraits(isSelected ? .isSelected : [])112        } else {113            row.accessibilityElement(children: .combine)114        }115    }116117    private var row: some View {118        LunarHoverReader { hovering in119            HStack(spacing: LunarSpacing.sm) {120                leading.foregroundStyle(isSelected ? theme.accent : theme.textSecondary)121                VStack(alignment: .leading, spacing: 2) {122                    Text(title)123                        .lunarFont(isSelected ? .bodyEmphasized : .body)124                        .foregroundStyle(theme.textPrimary)125                        .lineLimit(1)126                    if let subtitle {127                        Text(subtitle).lunarFont(.footnote).foregroundStyle(theme.textSecondary).lineLimit(1)128                    }129                }130                Spacer(minLength: LunarSpacing.xs)131                trailing.foregroundStyle(theme.textSecondary)132            }133            .padding(.horizontal, LunarSpacing.sm)134            .padding(.vertical, LunarSpacing.unit + 2)135            .frame(minHeight: LunarMetrics.hitTarget > 36 ? LunarMetrics.hitTarget : 36)136            .background {137                LunarShape.xs.fill(isSelected ? theme.selection138                                              : theme.textPrimary.withAlpha(hovering && action != nil ? 0.06 : 0))139            }140            .overlay(alignment: .leading) {141                // Selection is also told by a leading bar, not by colour alone.142                if isSelected && differentiateWithoutColor {143                    Capsule().fill(theme.accent).frame(width: 3).padding(.vertical, LunarSpacing.xs)144                }145            }146            .lunarFocusRing(.xs)147            .contentShape(LunarShape.xs)148        }149    }150}151152extension LunarListRow where Leading == LunarIconView, Trailing == EmptyView {153    public init(_ title: String, subtitle: String? = nil, icon: LunarIcon, isSelected: Bool = false,154                action: (() -> Void)? = nil) {155        self.init(title, subtitle: subtitle, isSelected: isSelected, action: action) {156            LunarIconView(icon, size: LunarMetrics.icon)157        } trailing: { EmptyView() }158    }159}160161extension LunarListRow where Leading == LunarIconView {162    public init(_ title: String, subtitle: String? = nil, icon: LunarIcon, isSelected: Bool = false,163                action: (() -> Void)? = nil, @ViewBuilder trailing: () -> Trailing) {164        self.init(title, subtitle: subtitle, isSelected: isSelected, action: action,165                  leading: { LunarIconView(icon, size: LunarMetrics.icon) }, trailing: trailing)166    }167}168169/// The header of a sheet or popover: title, optional subtitle, and a close button.170public struct LunarSheetHeader: View {171    @Environment(\.lunarTheme) private var theme172    private let title: String173    private let subtitle: String?174    private let icon: LunarIcon?175    private let closeLabel: String176    private let onClose: (() -> Void)?177178    public init(_ title: String, subtitle: String? = nil, icon: LunarIcon? = nil, closeLabel: String = "Close",179                onClose: (() -> Void)? = nil) {180        self.title = title181        self.subtitle = subtitle182        self.icon = icon183        self.closeLabel = closeLabel184        self.onClose = onClose185    }186187    public var body: some View {188        HStack(spacing: LunarSpacing.sm) {189            if let icon {190                LunarIconView(icon, size: LunarMetrics.icon)191                    .foregroundStyle(theme.accent)192                    .frame(width: 36, height: 36)193                    .background { Circle().fill(theme.selection) }194            }195            VStack(alignment: .leading, spacing: 2) {196                Text(title).lunarFont(.headline).foregroundStyle(theme.textPrimary)197                    .accessibilityAddTraits(.isHeader)198                if let subtitle {199                    Text(subtitle).lunarFont(.footnote).foregroundStyle(theme.textSecondary)200                }201            }202            Spacer(minLength: LunarSpacing.xs)203            if let onClose {204                LunarIconButton(systemName: "xmark", label: closeLabel, diameter: LunarMetrics.controlHeightCompact,205                                action: onClose)206            }207        }208        .padding(.horizontal, LunarSpacing.md)209        .padding(.vertical, LunarSpacing.sm)210    }211}212213/// Icon + text whose tooltip (`help`) and accessibility hint are set from one string.214public struct LunarLabel: View {215    @Environment(\.lunarTheme) private var theme216    private let title: String217    private let icon: LunarIcon218    private let help: String?219    private let style: LunarTextStyle220221    public init(_ title: String, icon: LunarIcon, help: String? = nil, style: LunarTextStyle = .subheadline) {222        self.title = title223        self.icon = icon224        self.help = help225        self.style = style226    }227228    public var body: some View {229        HStack(spacing: LunarSpacing.unit + 2) {230            LunarIconView(icon, size: 16)231            Text(title).lunarFont(style).lineLimit(1)232        }233        .help(help ?? title)234        .accessibilityElement(children: .combine)235        .accessibilityHint(help ?? "")236    }237}238239/// A keyboard key, for shortcut hints.240public struct LunarKeyCap: View {241    @Environment(\.lunarTheme) private var theme242    private let key: String243244    public init(_ key: String) { self.key = key }245246    public var body: some View {247        Text(key)248            .font(.system(.caption, design: .rounded, weight: .semibold))249            .foregroundStyle(theme.textSecondary)250            .padding(.horizontal, 6)251            .frame(minWidth: 22, minHeight: 22)252            .background {253                RoundedRectangle(cornerRadius: 6, style: .continuous).fill(theme.textPrimary.withAlpha(0.07))254            }255            .overlay {256                RoundedRectangle(cornerRadius: 6, style: .continuous).strokeBorder(theme.hairline, lineWidth: 1)257            }258            .overlay(alignment: .bottom) {259                // The slightly heavier bottom edge that makes it read as a key.260                RoundedRectangle(cornerRadius: 6, style: .continuous)261                    .trim(from: 0.08, to: 0.42)262                    .stroke(theme.hairlineStrong, lineWidth: 1)263            }264            .accessibilityLabel(Self.spokenName(key))265    }266267    /// Spoken names for the modifier and navigation symbols.268    nonisolated static func spokenName(_ key: String) -> String {269        switch key {270        case "⌘": "Command"271        case "⇧": "Shift"272        case "⌥": "Option"273        case "⌃": "Control"274        case "⏎", "↩": "Return"275        case "⌫": "Delete"276        case "⎋": "Escape"277        case "⇥": "Tab"278        case "␣": "Space"279        case "↑": "Up Arrow"280        case "↓": "Down Arrow"281        case "←": "Left Arrow"282        case "→": "Right Arrow"283        default: key284        }285    }286}287288/// A key combination: a row of key caps read as one phrase ("Command Shift K").289public struct LunarShortcut: View {290    private let keys: [String]291292    public init(_ keys: [String]) { self.keys = keys }293294    /// Splits a compact string such as `"⌘⇧K"` into one cap per character.295    public init(_ compact: String) { self.keys = compact.map(String.init) }296297    public var body: some View {298        HStack(spacing: 3) {299            ForEach(Array(keys.enumerated()), id: \.offset) { _, key in LunarKeyCap(key) }300        }301        .accessibilityElement(children: .ignore)302        .accessibilityLabel(keys.map(LunarKeyCap.spokenName).joined(separator: " "))303    }304}305306#Preview("Containers") {307    VStack(alignment: .leading, spacing: LunarSpacing.md) {308        LunarCapsuleBar {309            LunarIconButton(systemName: "chevron.left", label: "Back") {}310            Text("orixlab.dev").lunarFont(.body).padding(.horizontal, LunarSpacing.lg)311            LunarIconButton(glyph: .shield, label: "Shield", isActive: true) {}312        }313        LunarSectionHeader("Pinned", actionTitle: "Edit") {}314        LunarCard {315            VStack(spacing: 0) {316                LunarListRow("Reading list", subtitle: "12 pages", icon: .glyph(.reader), isSelected: true) {}317                LunarListRow("Archive", icon: .glyph(.archive)) {} trailing: { LunarShortcut("⌘⇧A") }318            }319        }320    }321    .padding(LunarSpacing.xl)322    .frame(width: 420)323    .background(LunarWindowBackground())324    .lunarTheme(LunarThemes.lunar.dark)325}326
Files main
Menu

Theme