| 1 | import SwiftUI |
| 2 | |
| 3 | /// Base's mark: the wordmark's rounded square, `--mark-1` into `--mark-2` |
| 4 | /// at 160°, with its edge (`.wordmark::before`, `.mark`). 14 pt in a |
| 5 | /// heading, 16 pt in the sidebar, 18 pt in the Base button; its corners |
| 6 | /// are 5/16 of its side, as the web's 16 px mark has 5 px. |
| 7 | public struct BaseMark: View { |
| 8 | let size: CGFloat |
| 9 | @Environment(\.colorScheme) private var scheme |
| 10 | public init(size: CGFloat = 16) { self.size = size } |
| 11 | public var body: some View { |
| 12 | let p = Palette(scheme) |
| 13 | let shape = RoundedRectangle(cornerRadius: size * 5 / 16, style: .continuous) |
| 14 | let (start, end) = SkyPainter.cssLine(degrees: 160, in: CGRect(x: 0, y: 0, width: 1, height: 1)) |
| 15 | shape.fill(LinearGradient(colors: [p[.mark1], p[.mark2]], startPoint: UnitPoint(x: start.x, y: start.y), endPoint: UnitPoint(x: end.x, y: end.y))) |
| 16 | .overlay { shape.strokeBorder(p[.markEdge], lineWidth: 1) } |
| 17 | .frame(width: size, height: size) |
| 18 | .accessibilityHidden(true) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// A job's state, as the web names them (ask.js `jobStates`). |
| 23 | public enum JobState: String, Sendable, CaseIterable, Codable { |
| 24 | case working, asking, ready, done, paused, stopped |
| 25 | } |
| 26 | |
| 27 | /// The 24-unit drawings Base's web uses (ask.js `marks`, the templates' |
| 28 | /// `ask-mark`, and the job view's and composer's icons), as paths. |
| 29 | public enum BaseIcon: Sendable { |
| 30 | case asking, ready, done, paused, stopped, arrowOut, close, send |
| 31 | |
| 32 | var svg: String { |
| 33 | switch self { |
| 34 | case .asking: "M9.2 9.2a2.9 2.9 0 0 1 5.6 1c0 2-2.8 2.4-2.8 4.3M12 18.2h.01" |
| 35 | case .ready: "M8 16L16 8M9.5 8H16v6.5" |
| 36 | case .done: "M6 12.5l4 4 8-9" |
| 37 | // A clock: it carries on by itself. |
| 38 | case .paused: "M19.2 12a7.2 7.2 0 1 1-14.4 0a7.2 7.2 0 1 1 14.4 0zM12 8.6V12l2.4 1.6" |
| 39 | // A square: stopped until the person acts (never paused's clock). |
| 40 | case .stopped: "M9 7.5h6a1.5 1.5 0 0 1 1.5 1.5v6a1.5 1.5 0 0 1-1.5 1.5H9a1.5 1.5 0 0 1-1.5-1.5V9A1.5 1.5 0 0 1 9 7.5z" |
| 41 | case .arrowOut: "M9 6h9v9M18 6L7 17" |
| 42 | case .close: "M6 6l12 12M18 6L6 18" |
| 43 | case .send: "M12 19V5M6 11l6-6 6 6" |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// The drawing scaled from its 24-unit box into `rect`. |
| 48 | public func path(in rect: CGRect) -> Path { |
| 49 | SVGPath.parse(svg).applying(CGAffineTransform(scaleX: rect.width / 24, y: rect.height / 24) |
| 50 | .concatenating(CGAffineTransform(translationX: rect.minX, y: rect.minY))) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// An icon drawn as the web draws it: a round-capped stroke, `weight` in |
| 55 | /// the drawing's 24 units (2.6 for a state's mark, 2.4 for Send, 1.8 for |
| 56 | /// Close). |
| 57 | public struct IconShape: Shape { |
| 58 | let icon: BaseIcon |
| 59 | public init(_ icon: BaseIcon) { self.icon = icon } |
| 60 | public func path(in rect: CGRect) -> Path { icon.path(in: rect) } |
| 61 | } |
| 62 | |
| 63 | public struct Icon: View { |
| 64 | let icon: BaseIcon |
| 65 | let weight: CGFloat |
| 66 | public init(_ icon: BaseIcon, weight: CGFloat = 1.8) { self.icon = icon; self.weight = weight } |
| 67 | public var body: some View { |
| 68 | GeometryReader { geo in |
| 69 | IconShape(icon).stroke(style: StrokeStyle(lineWidth: weight * geo.size.width / 24, lineCap: .round, lineJoin: .round)) |
| 70 | } |
| 71 | .aspectRatio(1, contentMode: .fit) |
| 72 | .accessibilityHidden(true) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// A job state's mark (`.state-mark`): a disc. Working is a slow arc |
| 77 | /// turning round a breathing dot, on the accent's tint; the rest are |
| 78 | /// still icons on the state's colour (working, ready and done the accent, |
| 79 | /// asking warm, paused a calm indigo with a clock, stopped rose with a |
| 80 | /// square). With Reduce Motion, working holds still: a |
| 81 | /// quarter arc and a steady dot. |
| 82 | public struct StateMark: View { |
| 83 | let state: JobState |
| 84 | let size: CGFloat |
| 85 | @Environment(\.colorScheme) private var scheme |
| 86 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 87 | @Environment(\.skyFrozen) private var frozen |
| 88 | |
| 89 | public init(_ state: JobState, size: CGFloat = 28) { self.state = state; self.size = size } |
| 90 | |
| 91 | public static func color(_ state: JobState, _ p: Palette) -> Color { |
| 92 | switch state { |
| 93 | case .working, .ready, .done: p[.signal] |
| 94 | case .asking: p[.stAsk] |
| 95 | case .paused: p[.stPause] |
| 96 | case .stopped: p[.stStop] |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public var body: some View { |
| 101 | let p = Palette(scheme) |
| 102 | let st = Self.color(state, p) |
| 103 | ZStack { |
| 104 | if state == .working { |
| 105 | Circle().fill(st.opacity(0.14)) |
| 106 | Circle().strokeBorder(st.opacity(0.26), lineWidth: 2) |
| 107 | if reduceMotion || frozen { |
| 108 | WorkingArc(color: st).rotationEffect(.degrees(45)) |
| 109 | Circle().fill(st).frame(width: 6, height: 6) |
| 110 | } else { |
| 111 | TimelineView(.animation) { timeline in |
| 112 | let t = timeline.date.timeIntervalSinceReferenceDate |
| 113 | // turn: 1.6s cubic-bezier(.5,.15,.5,.85) infinite |
| 114 | let turn = SkyPainter.cubicBezier(t.truncatingRemainder(dividingBy: 1.6) / 1.6, 0.5, 0.15, 0.5, 0.85) |
| 115 | // glow: 1.6s ease-in-out infinite alternate, .45 → 1 |
| 116 | let glow = SkyPainter.mix(0.45, 1, SkyPainter.easeInOut(SkyPainter.progress(t, period: 1.6, delay: 0, reverse: false))) |
| 117 | ZStack { |
| 118 | WorkingArc(color: st).rotationEffect(.degrees(360 * turn)) |
| 119 | Circle().fill(st).frame(width: 6, height: 6).opacity(glow) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } else { |
| 124 | Circle().fill(st) |
| 125 | Icon(icon, weight: 2.6) |
| 126 | .foregroundStyle(p[.onSignal]) |
| 127 | .frame(width: size * 15 / 28, height: size * 15 / 28) |
| 128 | } |
| 129 | } |
| 130 | .frame(width: size, height: size) |
| 131 | .accessibilityHidden(true) |
| 132 | } |
| 133 | |
| 134 | var icon: BaseIcon { |
| 135 | switch state { |
| 136 | case .asking: .asking |
| 137 | case .ready: .ready |
| 138 | case .done: .done |
| 139 | case .paused: .paused |
| 140 | case .stopped, .working: .stopped |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// `border: 2px solid transparent; border-top-color: --st`: a quarter of |
| 146 | /// the ring, centred on the top. |
| 147 | struct WorkingArc: View { |
| 148 | let color: Color |
| 149 | var body: some View { |
| 150 | Circle() |
| 151 | .inset(by: 1) |
| 152 | .trim(from: 0, to: 0.25) |
| 153 | .stroke(color, style: StrokeStyle(lineWidth: 2, lineCap: .butt)) |
| 154 | .rotationEffect(.degrees(-135)) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Just enough SVG path syntax for the web's icons: M L H V C A Z, absolute |
| 159 | /// and relative, with implicit repeats. |
| 160 | enum SVGPath { |
| 161 | static func parse(_ d: String) -> Path { |
| 162 | var path = Path() |
| 163 | var tokens = Tokens(d) |
| 164 | var current = CGPoint.zero, start = CGPoint.zero |
| 165 | var command: Character = "M" |
| 166 | while let next = tokens.peekCommand() ?? (tokens.atEnd ? nil : command) { |
| 167 | if tokens.peekCommand() != nil { tokens.skipCommand() } |
| 168 | command = next |
| 169 | let relative = command.isLowercase |
| 170 | let base = relative ? current : .zero |
| 171 | switch command.uppercased() { |
| 172 | case "M": |
| 173 | guard let x = tokens.number(), let y = tokens.number() else { return path } |
| 174 | current = CGPoint(x: base.x + x, y: base.y + y); start = current |
| 175 | path.move(to: current) |
| 176 | command = relative ? "l" : "L" |
| 177 | case "L": |
| 178 | guard let x = tokens.number(), let y = tokens.number() else { return path } |
| 179 | current = CGPoint(x: base.x + x, y: base.y + y); path.addLine(to: current) |
| 180 | case "H": |
| 181 | guard let x = tokens.number() else { return path } |
| 182 | current = CGPoint(x: (relative ? current.x : 0) + x, y: current.y); path.addLine(to: current) |
| 183 | case "V": |
| 184 | guard let y = tokens.number() else { return path } |
| 185 | current = CGPoint(x: current.x, y: (relative ? current.y : 0) + y); path.addLine(to: current) |
| 186 | case "C": |
| 187 | guard let x1 = tokens.number(), let y1 = tokens.number(), let x2 = tokens.number(), let y2 = tokens.number(), |
| 188 | let x = tokens.number(), let y = tokens.number() else { return path } |
| 189 | let end = CGPoint(x: base.x + x, y: base.y + y) |
| 190 | path.addCurve(to: end, control1: CGPoint(x: base.x + x1, y: base.y + y1), control2: CGPoint(x: base.x + x2, y: base.y + y2)) |
| 191 | current = end |
| 192 | case "A": |
| 193 | guard let rx = tokens.number(), let ry = tokens.number(), let rotation = tokens.number(), |
| 194 | let large = tokens.flag(), let sweep = tokens.flag(), let x = tokens.number(), let y = tokens.number() else { return path } |
| 195 | let end = CGPoint(x: base.x + x, y: base.y + y) |
| 196 | addArc(&path, from: current, to: end, rx: rx, ry: ry, rotation: rotation, large: large, sweep: sweep) |
| 197 | current = end |
| 198 | case "Z": |
| 199 | path.closeSubpath(); current = start |
| 200 | default: |
| 201 | return path |
| 202 | } |
| 203 | } |
| 204 | return path |
| 205 | } |
| 206 | |
| 207 | /// SVG's endpoint arc (implementation notes F.6.5), as cubic curves. |
| 208 | static func addArc(_ path: inout Path, from p0: CGPoint, to p1: CGPoint, rx: Double, ry: Double, rotation: Double, large: Bool, sweep: Bool) { |
| 209 | var rx = abs(rx), ry = abs(ry) |
| 210 | guard rx > 0, ry > 0, p0 != p1 else { path.addLine(to: p1); return } |
| 211 | let phi = rotation * .pi / 180, cosPhi = cos(phi), sinPhi = sin(phi) |
| 212 | let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2 |
| 213 | let x1 = cosPhi * dx + sinPhi * dy, y1 = -sinPhi * dx + cosPhi * dy |
| 214 | let lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry) |
| 215 | if lambda > 1 { rx *= sqrt(lambda); ry *= sqrt(lambda) } |
| 216 | let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1 |
| 217 | let den = rx * rx * y1 * y1 + ry * ry * x1 * x1 |
| 218 | var coef = sqrt(max(0, num / den)) |
| 219 | if large == sweep { coef = -coef } |
| 220 | let cx1 = coef * rx * y1 / ry, cy1 = -coef * ry * x1 / rx |
| 221 | let cx = cosPhi * cx1 - sinPhi * cy1 + (p0.x + p1.x) / 2 |
| 222 | let cy = sinPhi * cx1 + cosPhi * cy1 + (p0.y + p1.y) / 2 |
| 223 | func angle(_ ux: Double, _ uy: Double, _ vx: Double, _ vy: Double) -> Double { |
| 224 | let a = atan2(ux * vy - uy * vx, ux * vx + uy * vy) |
| 225 | return a |
| 226 | } |
| 227 | let theta1 = angle(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry) |
| 228 | var delta = angle((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry) |
| 229 | if !sweep && delta > 0 { delta -= 2 * .pi } |
| 230 | if sweep && delta < 0 { delta += 2 * .pi } |
| 231 | let segments = max(1, Int(ceil(abs(delta) / (.pi / 2)))) |
| 232 | let step = delta / Double(segments) |
| 233 | var theta = theta1 |
| 234 | for _ in 0..<segments { |
| 235 | let t = 4.0 / 3.0 * tan(step / 4) |
| 236 | func point(_ a: Double) -> (CGPoint, CGPoint) { |
| 237 | let ex = rx * cos(a), ey = ry * sin(a) |
| 238 | let px = cx + cosPhi * ex - sinPhi * ey, py = cy + sinPhi * ex + cosPhi * ey |
| 239 | let tx = -rx * sin(a), ty = ry * cos(a) |
| 240 | return (CGPoint(x: px, y: py), CGPoint(x: cosPhi * tx - sinPhi * ty, y: sinPhi * tx + cosPhi * ty)) |
| 241 | } |
| 242 | let (a, da) = point(theta), (b, db) = point(theta + step) |
| 243 | path.addCurve(to: b, control1: CGPoint(x: a.x + t * da.x, y: a.y + t * da.y), control2: CGPoint(x: b.x - t * db.x, y: b.y - t * db.y)) |
| 244 | theta += step |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | struct Tokens { |
| 249 | let chars: [Character] |
| 250 | var i = 0 |
| 251 | init(_ s: String) { chars = Array(s) } |
| 252 | var atEnd: Bool { mutating get { skipSeparators(); return i >= chars.count } } |
| 253 | mutating func skipSeparators() { while i < chars.count, chars[i] == " " || chars[i] == "," || chars[i] == "\n" { i += 1 } } |
| 254 | mutating func peekCommand() -> Character? { |
| 255 | skipSeparators() |
| 256 | guard i < chars.count, chars[i].isLetter, chars[i] != "e", chars[i] != "E" else { return nil } |
| 257 | return chars[i] |
| 258 | } |
| 259 | mutating func skipCommand() { i += 1 } |
| 260 | mutating func flag() -> Bool? { |
| 261 | skipSeparators() |
| 262 | guard i < chars.count, chars[i] == "0" || chars[i] == "1" else { return nil } |
| 263 | defer { i += 1 } |
| 264 | return chars[i] == "1" |
| 265 | } |
| 266 | mutating func number() -> Double? { |
| 267 | skipSeparators() |
| 268 | var s = "" |
| 269 | var seenDot = false, seenExp = false |
| 270 | while i < chars.count { |
| 271 | let c = chars[i] |
| 272 | if c == "-" || c == "+" { |
| 273 | if !s.isEmpty && !(s.last == "e" || s.last == "E") { break } |
| 274 | } else if c == "." { |
| 275 | if seenDot || seenExp { break } |
| 276 | seenDot = true |
| 277 | } else if c == "e" || c == "E" { |
| 278 | if seenExp { break } |
| 279 | seenExp = true |
| 280 | } else if !c.isNumber { break } |
| 281 | s.append(c); i += 1 |
| 282 | } |
| 283 | return Double(s) |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |