import SwiftUI /// The web's buttons (`.btn`, `.btn.ghost`): one control height, 44 pt, /// 10 pt corners, semibold. Primary is frost on the dark (ink on the /// light); ghost is a faint fill with an edge. public struct BaseButtonStyle: ButtonStyle { public enum Kind: Sendable { case primary, ghost } let kind: Kind var expands = false @Environment(\.colorScheme) private var scheme @Environment(\.isEnabled) private var enabled public init(_ kind: Kind, expands: Bool = false) { self.kind = kind; self.expands = expands } public func makeBody(configuration: Configuration) -> some View { let p = Palette(scheme) let shape = RoundedRectangle(cornerRadius: 10, style: .continuous) configuration.label .font(BaseFont.body(15, .semibold)) .lineLimit(2) .multilineTextAlignment(.center) .padding(.horizontal, 18) .frame(minHeight: 44) .frame(maxWidth: expands ? .infinity : nil) .foregroundStyle(kind == .primary ? p[.btnInk] : p[.frost]) .background(kind == .primary ? p[.btn] : p[.ghost], in: shape) .overlay { if kind == .ghost { shape.strokeBorder(p[.ghostEdge], lineWidth: 1) } } .opacity(enabled ? (configuration.isPressed ? 0.8 : 1) : 0.55) .contentShape(shape) } } /// What a card is drawn with: its question, a link per link, a ghost button /// per option while it is open, and the line saying how it ended (ask.js /// `addCard`, chat.html `ask-card`). A job's decision cards hang under its /// pill with a thread line (`hanging`). struct CardView: View { let card: AskCard let busy: Bool var hanging = false let answer: (AskCard.Option) -> Void @Environment(\.colorScheme) private var scheme @Environment(\.openURL) private var openURL var body: some View { let p = Palette(scheme) VStack(alignment: .leading, spacing: 10) { Text(verbatim: card.question).font(BaseFont.body(15)).fixedSize(horizontal: false, vertical: true) ForEach(card.links, id: \.self) { link in if let url = link.url { Button { openURL(url) } label: { Text(verbatim: link.label.isEmpty ? url.absoluteString : link.label) .font(BaseFont.body(15, .medium)).underline().foregroundStyle(p[.focus]) .frame(minHeight: 44, alignment: .leading) } .buttonStyle(.plain) .accessibilityAddTraits(.isLink) } } if card.isOpen, !card.options.isEmpty { FlowLayout(spacing: 8) { ForEach(card.options, id: \.self) { option in Button { answer(option) } label: { Text(verbatim: option.label.isEmpty ? option.value : option.label) } .buttonStyle(BaseButtonStyle(.ghost)) .disabled(busy) } } } if !card.result.isEmpty { Text(verbatim: card.result).font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze]) } } .foregroundStyle(p[.frost]) .padding(.vertical, 10).padding(.horizontal, 14) .frame(maxWidth: hanging ? 340 : .infinity, alignment: .leading) .background(p[.card], in: RoundedRectangle(cornerRadius: 14, style: .continuous)) .overlay { RoundedRectangle(cornerRadius: 14, style: .continuous).strokeBorder(p[.cardEdge], lineWidth: 1) } .accessibilityElement(children: .contain) } } /// An open connect card: the Connect Oficina button, coloured glass teal /// into violet with white ink in both themes, and under it what connecting /// unlocks; connected, a quiet line with a tick (chat.html `ask-offer`). /// Connecting is Base's own web page: it opens in the browser. struct OfferView: View { let offer: AskModel.Offer @Environment(\.colorScheme) private var scheme @Environment(\.openURL) private var openURL var body: some View { let p = Palette(scheme) if offer.connected { HStack(spacing: 6) { Icon(.done, weight: 2.4).foregroundStyle(p[.focus]).frame(width: 16, height: 16) Text(BaseText.text("ask.connected")).font(BaseFont.body(14, .medium)).foregroundStyle(p[.haze]) } .padding(.leading, 4) } else { VStack(alignment: .leading, spacing: 6) { Button { if let url = offer.card.connectLink?.url { openURL(url) } } label: { HStack(spacing: 10) { Image(systemName: "circle.hexagongrid.fill") .font(.system(size: 18)) .frame(width: 32, height: 32) .accessibilityHidden(true) Text(BaseText.text("ask.connect_oficina")).font(BaseFont.body(15, .semibold)) } .foregroundStyle(p[.goInk]) .padding(.leading, 8).padding(.trailing, 20) .frame(minHeight: 48) .background(LinearGradient(colors: [p[.go1], p[.go2]], startPoint: .leading, endPoint: .trailing), in: Capsule()) .overlay { Capsule().strokeBorder(p[.goEdge], lineWidth: 1) } .shadow(color: p[.edgeGlowB].opacity(0.6), radius: 11, y: 10) } .buttonStyle(.plain) Text(BaseText.text("ask.connect_oficina.line")) .font(BaseFont.body(13.5, relativeTo: .footnote)).foregroundStyle(p[.haze]).padding(.leading, 8) } .padding(.leading, 4) } } } /// Lays its children in rows, wrapping, as `.choices` (flex-wrap) does. struct FlowLayout: Layout { var spacing: CGFloat = 8 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { let width = proposal.width ?? .infinity var x: CGFloat = 0, y: CGFloat = 0, row: CGFloat = 0, widest: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(ProposedViewSize(width: width, height: nil)) if x > 0, x + size.width > width { y += row + spacing; x = 0; row = 0 } x += size.width + spacing row = max(row, size.height) widest = max(widest, x - spacing) } return CGSize(width: min(widest, width), height: y + row) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { var x = bounds.minX, y = bounds.minY, row: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil)) if x > bounds.minX, x + size.width > bounds.maxX { y += row + spacing; x = bounds.minX; row = 0 } view.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(width: min(size.width, bounds.width), height: size.height)) x += size.width + spacing row = max(row, size.height) } } }