147 lines
5.2 KiB
Swift
147 lines
5.2 KiB
Swift
import SwiftUI
|
|
|
|
/// The frame every overlay sits in: a flat ink scrim (no blur) and a `panel` card with a hairline border,
|
|
/// radius 8 and the overlay shadow. `.inset` fills the window minus 34pt; `.sized` centres a fixed-width card
|
|
/// at the top. A click on the scrim calls `onDismiss`.
|
|
public struct OverlayShell<Content: View>: View {
|
|
public enum Placement {
|
|
/// Search, Diff: the whole window minus the 34pt inset.
|
|
case inset
|
|
/// A card of this width, top-aligned inside the inset.
|
|
case sized(width: CGFloat)
|
|
}
|
|
|
|
var placement: Placement
|
|
var onDismiss: () -> Void
|
|
var content: Content
|
|
|
|
public init(_ placement: Placement = .inset, onDismiss: @escaping () -> Void, @ViewBuilder content: () -> Content) {
|
|
self.placement = placement
|
|
self.onDismiss = onDismiss
|
|
self.content = content()
|
|
}
|
|
|
|
public var body: some View {
|
|
ZStack(alignment: .top) {
|
|
Palette.scrim
|
|
.contentShape(Rectangle())
|
|
.onTapGesture(perform: onDismiss)
|
|
card
|
|
.padding(Metrics.overlayInset)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var card: some View {
|
|
switch placement {
|
|
case .inset:
|
|
OverlayCard { content }
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
case let .sized(width):
|
|
OverlayCard { content }
|
|
.frame(width: width)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The overlay card alone, for surfaces that place themselves (the launcher, the pass-on prompt).
|
|
public struct OverlayCard<Content: View>: View {
|
|
var content: Content
|
|
|
|
public init(@ViewBuilder content: () -> Content) {
|
|
self.content = content()
|
|
}
|
|
|
|
public var body: some View {
|
|
content
|
|
.background(Palette.panel)
|
|
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusOverlay))
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusOverlay).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
|
.shadow(color: Palette.overlayShadow, radius: Metrics.overlayShadowRadius / 2, y: Metrics.overlayShadowY)
|
|
}
|
|
}
|
|
|
|
/// An overlay's header row: glyph (amber by default), mono 700 14 title, an optional mono detail right after it,
|
|
/// optional trailing content, then the `esc` chip unless `showsEscChip` is false.
|
|
public struct OverlayHeader<Trailing: View>: View {
|
|
var systemImage: String
|
|
var title: String
|
|
var detail: String?
|
|
var iconColor: Color
|
|
var showsEscChip: Bool
|
|
var height: CGFloat
|
|
var trailing: Trailing
|
|
|
|
public init(systemImage: String, title: String, detail: String? = nil, iconColor: Color = Palette.amber,
|
|
showsEscChip: Bool = true, height: CGFloat = 50, @ViewBuilder trailing: () -> Trailing) {
|
|
self.systemImage = systemImage
|
|
self.title = title
|
|
self.detail = detail
|
|
self.iconColor = iconColor
|
|
self.showsEscChip = showsEscChip
|
|
self.height = height
|
|
self.trailing = trailing()
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack(spacing: 12) {
|
|
Image(systemName: systemImage).font(.system(size: 14, weight: .light)).foregroundStyle(iconColor)
|
|
Text(title).typeRole(Typography.dialogTitle).foregroundStyle(Palette.text1)
|
|
if let detail {
|
|
Text(detail).typeRole(Typography.path).foregroundStyle(Palette.text4).lineLimit(1)
|
|
}
|
|
Spacer(minLength: 12)
|
|
trailing
|
|
if showsEscChip { EscChip() }
|
|
}
|
|
.padding(.horizontal, 18)
|
|
.frame(height: height)
|
|
.hairline(.bottom)
|
|
}
|
|
}
|
|
|
|
extension OverlayHeader where Trailing == EmptyView {
|
|
public init(systemImage: String, title: String, detail: String? = nil, iconColor: Color = Palette.amber,
|
|
showsEscChip: Bool = true, height: CGFloat = 50) {
|
|
self.init(systemImage: systemImage, title: title, detail: detail, iconColor: iconColor,
|
|
showsEscChip: showsEscChip, height: height) { EmptyView() }
|
|
}
|
|
}
|
|
|
|
/// An overlay footer (38pt): mono 11 key hints with amber glyphs. Build the hints with `OverlayFooter.hint`.
|
|
public struct OverlayFooter<Trailing: View>: View {
|
|
var hints: Text
|
|
var trailing: Trailing
|
|
|
|
public init(_ hints: Text, @ViewBuilder trailing: () -> Trailing) {
|
|
self.hints = hints
|
|
self.trailing = trailing()
|
|
}
|
|
|
|
public var body: some View {
|
|
HStack(spacing: 14) {
|
|
hints.typeRole(Typography.chip)
|
|
Spacer(minLength: 0)
|
|
trailing
|
|
}
|
|
.padding(.horizontal, 18)
|
|
.frame(height: 38)
|
|
.hairline(.top)
|
|
}
|
|
}
|
|
|
|
extension OverlayFooter where Trailing == EmptyView {
|
|
public init(_ hints: Text) {
|
|
self.init(hints) { EmptyView() }
|
|
}
|
|
|
|
/// "⌘↑↓ navigate · ↵ open": pass pairs of (keys, action).
|
|
public static func hint(_ pairs: [(keys: String, action: String)]) -> Text {
|
|
pairs.enumerated().reduce(Text("")) { text, item in
|
|
let (index, pair) = item
|
|
let separator = index == 0 ? Text("") : Text(" · ").foregroundColor(Palette.text4)
|
|
return text + separator + Text(pair.keys).foregroundColor(Palette.amber) + Text(" " + pair.action).foregroundColor(Palette.text4)
|
|
}
|
|
}
|
|
}
|