oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3/// The web's buttons (`.btn`, `.btn.ghost`): one control height, 44 pt,
4/// 10 pt corners, semibold. Primary is frost on the dark (ink on the
5/// light); ghost is a faint fill with an edge.
6public struct BaseButtonStyle: ButtonStyle {
7 public enum Kind: Sendable { case primary, ghost }
8 let kind: Kind
9 var expands = false
10 @Environment(\.colorScheme) private var scheme
11 @Environment(\.isEnabled) private var enabled
12 public init(_ kind: Kind, expands: Bool = false) { self.kind = kind; self.expands = expands }
13
14 public func makeBody(configuration: Configuration) -> some View {
15 let p = Palette(scheme)
16 let shape = RoundedRectangle(cornerRadius: 10, style: .continuous)
17 configuration.label
18 .font(BaseFont.body(15, .semibold))
19 .lineLimit(2)
20 .multilineTextAlignment(.center)
21 .padding(.horizontal, 18)
22 .frame(minHeight: 44)
23 .frame(maxWidth: expands ? .infinity : nil)
24 .foregroundStyle(kind == .primary ? p[.btnInk] : p[.frost])
25 .background(kind == .primary ? p[.btn] : p[.ghost], in: shape)
26 .overlay { if kind == .ghost { shape.strokeBorder(p[.ghostEdge], lineWidth: 1) } }
27 .opacity(enabled ? (configuration.isPressed ? 0.8 : 1) : 0.55)
28 .contentShape(shape)
29 }
30}
31
32/// What a card is drawn with: its question, a link per link, a ghost button
33/// per option while it is open, and the line saying how it ended (ask.js
34/// `addCard`, chat.html `ask-card`). A job's decision cards hang under its
35/// pill with a thread line (`hanging`).
36struct CardView: View {
37 let card: AskCard
38 let busy: Bool
39 var hanging = false
40 let answer: (AskCard.Option) -> Void
41 @Environment(\.colorScheme) private var scheme
42 @Environment(\.openURL) private var openURL
43
44 var body: some View {
45 let p = Palette(scheme)
46 VStack(alignment: .leading, spacing: 10) {
47 Text(verbatim: card.question).font(BaseFont.body(15)).fixedSize(horizontal: false, vertical: true)
48 ForEach(card.links, id: \.self) { link in
49 if let url = link.url {
50 Button { openURL(url) } label: {
51 Text(verbatim: link.label.isEmpty ? url.absoluteString : link.label)
52 .font(BaseFont.body(15, .medium)).underline().foregroundStyle(p[.focus])
53 .frame(minHeight: 44, alignment: .leading)
54 }
55 .buttonStyle(.plain)
56 .accessibilityAddTraits(.isLink)
57 }
58 }
59 if card.isOpen, !card.options.isEmpty {
60 FlowLayout(spacing: 8) {
61 ForEach(card.options, id: \.self) { option in
62 Button { answer(option) } label: { Text(verbatim: option.label.isEmpty ? option.value : option.label) }
63 .buttonStyle(BaseButtonStyle(.ghost))
64 .disabled(busy)
65 }
66 }
67 }
68 if !card.result.isEmpty {
69 Text(verbatim: card.result).font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze])
70 }
71 }
72 .foregroundStyle(p[.frost])
73 .padding(.vertical, 10).padding(.horizontal, 14)
74 .frame(maxWidth: hanging ? 340 : .infinity, alignment: .leading)
75 .background(p[.card], in: RoundedRectangle(cornerRadius: 14, style: .continuous))
76 .overlay { RoundedRectangle(cornerRadius: 14, style: .continuous).strokeBorder(p[.cardEdge], lineWidth: 1) }
77 .accessibilityElement(children: .contain)
78 }
79}
80
81/// An open connect card: the Connect Oficina button, coloured glass teal
82/// into violet with white ink in both themes, and under it what connecting
83/// unlocks; connected, a quiet line with a tick (chat.html `ask-offer`).
84/// Connecting is Base's own web page: it opens in the browser.
85struct OfferView: View {
86 let offer: AskModel.Offer
87 @Environment(\.colorScheme) private var scheme
88 @Environment(\.openURL) private var openURL
89
90 var body: some View {
91 let p = Palette(scheme)
92 if offer.connected {
93 HStack(spacing: 6) {
94 Icon(.done, weight: 2.4).foregroundStyle(p[.focus]).frame(width: 16, height: 16)
95 Text(BaseText.text("ask.connected")).font(BaseFont.body(14, .medium)).foregroundStyle(p[.haze])
96 }
97 .padding(.leading, 4)
98 } else {
99 VStack(alignment: .leading, spacing: 6) {
100 Button {
101 if let url = offer.card.connectLink?.url { openURL(url) }
102 } label: {
103 HStack(spacing: 10) {
104 Image(systemName: "circle.hexagongrid.fill")
105 .font(.system(size: 18))
106 .frame(width: 32, height: 32)
107 .accessibilityHidden(true)
108 Text(BaseText.text("ask.connect_oficina")).font(BaseFont.body(15, .semibold))
109 }
110 .foregroundStyle(p[.goInk])
111 .padding(.leading, 8).padding(.trailing, 20)
112 .frame(minHeight: 48)
113 .background(LinearGradient(colors: [p[.go1], p[.go2]], startPoint: .leading, endPoint: .trailing), in: Capsule())
114 .overlay { Capsule().strokeBorder(p[.goEdge], lineWidth: 1) }
115 .shadow(color: p[.edgeGlowB].opacity(0.6), radius: 11, y: 10)
116 }
117 .buttonStyle(.plain)
118 Text(BaseText.text("ask.connect_oficina.line"))
119 .font(BaseFont.body(13.5, relativeTo: .footnote)).foregroundStyle(p[.haze]).padding(.leading, 8)
120 }
121 .padding(.leading, 4)
122 }
123 }
124}
125
126/// Lays its children in rows, wrapping, as `.choices` (flex-wrap) does.
127struct FlowLayout: Layout {
128 var spacing: CGFloat = 8
129
130 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
131 let width = proposal.width ?? .infinity
132 var x: CGFloat = 0, y: CGFloat = 0, row: CGFloat = 0, widest: CGFloat = 0
133 for view in subviews {
134 let size = view.sizeThatFits(ProposedViewSize(width: width, height: nil))
135 if x > 0, x + size.width > width { y += row + spacing; x = 0; row = 0 }
136 x += size.width + spacing
137 row = max(row, size.height)
138 widest = max(widest, x - spacing)
139 }
140 return CGSize(width: min(widest, width), height: y + row)
141 }
142
143 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
144 var x = bounds.minX, y = bounds.minY, row: CGFloat = 0
145 for view in subviews {
146 let size = view.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil))
147 if x > bounds.minX, x + size.width > bounds.maxX { y += row + spacing; x = bounds.minX; row = 0 }
148 view.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(width: min(size.width, bounds.width), height: size.height))
149 x += size.width + spacing
150 row = max(row, size.height)
151 }
152 }
153}
154