import SwiftUI /// Base's mark: the wordmark's rounded square, `--mark-1` into `--mark-2` /// at 160°, with its edge (`.wordmark::before`, `.mark`). 14 pt in a /// heading, 16 pt in the sidebar, 18 pt in the Base button; its corners /// are 5/16 of its side, as the web's 16 px mark has 5 px. public struct BaseMark: View { let size: CGFloat @Environment(\.colorScheme) private var scheme public init(size: CGFloat = 16) { self.size = size } public var body: some View { let p = Palette(scheme) let shape = RoundedRectangle(cornerRadius: size * 5 / 16, style: .continuous) let (start, end) = SkyPainter.cssLine(degrees: 160, in: CGRect(x: 0, y: 0, width: 1, height: 1)) shape.fill(LinearGradient(colors: [p[.mark1], p[.mark2]], startPoint: UnitPoint(x: start.x, y: start.y), endPoint: UnitPoint(x: end.x, y: end.y))) .overlay { shape.strokeBorder(p[.markEdge], lineWidth: 1) } .frame(width: size, height: size) .accessibilityHidden(true) } } /// A job's state, as the web names them (ask.js `jobStates`). public enum JobState: String, Sendable, CaseIterable, Codable { case working, asking, ready, done, paused, stopped } /// The 24-unit drawings Base's web uses (ask.js `marks`, the templates' /// `ask-mark`, and the job view's and composer's icons), as paths. public enum BaseIcon: Sendable { case asking, ready, done, paused, stopped, arrowOut, close, send var svg: String { switch self { 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" case .ready: "M8 16L16 8M9.5 8H16v6.5" case .done: "M6 12.5l4 4 8-9" // A clock: it carries on by itself. 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" // A square: stopped until the person acts (never paused's clock). 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" case .arrowOut: "M9 6h9v9M18 6L7 17" case .close: "M6 6l12 12M18 6L6 18" case .send: "M12 19V5M6 11l6-6 6 6" } } /// The drawing scaled from its 24-unit box into `rect`. public func path(in rect: CGRect) -> Path { SVGPath.parse(svg).applying(CGAffineTransform(scaleX: rect.width / 24, y: rect.height / 24) .concatenating(CGAffineTransform(translationX: rect.minX, y: rect.minY))) } } /// An icon drawn as the web draws it: a round-capped stroke, `weight` in /// the drawing's 24 units (2.6 for a state's mark, 2.4 for Send, 1.8 for /// Close). public struct IconShape: Shape { let icon: BaseIcon public init(_ icon: BaseIcon) { self.icon = icon } public func path(in rect: CGRect) -> Path { icon.path(in: rect) } } public struct Icon: View { let icon: BaseIcon let weight: CGFloat public init(_ icon: BaseIcon, weight: CGFloat = 1.8) { self.icon = icon; self.weight = weight } public var body: some View { GeometryReader { geo in IconShape(icon).stroke(style: StrokeStyle(lineWidth: weight * geo.size.width / 24, lineCap: .round, lineJoin: .round)) } .aspectRatio(1, contentMode: .fit) .accessibilityHidden(true) } } /// A job state's mark (`.state-mark`): a disc. Working is a slow arc /// turning round a breathing dot, on the accent's tint; the rest are /// still icons on the state's colour (working, ready and done the accent, /// asking warm, paused a calm indigo with a clock, stopped rose with a /// square). With Reduce Motion, working holds still: a /// quarter arc and a steady dot. public struct StateMark: View { let state: JobState let size: CGFloat @Environment(\.colorScheme) private var scheme @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.skyFrozen) private var frozen public init(_ state: JobState, size: CGFloat = 28) { self.state = state; self.size = size } public static func color(_ state: JobState, _ p: Palette) -> Color { switch state { case .working, .ready, .done: p[.signal] case .asking: p[.stAsk] case .paused: p[.stPause] case .stopped: p[.stStop] } } public var body: some View { let p = Palette(scheme) let st = Self.color(state, p) ZStack { if state == .working { Circle().fill(st.opacity(0.14)) Circle().strokeBorder(st.opacity(0.26), lineWidth: 2) if reduceMotion || frozen { WorkingArc(color: st).rotationEffect(.degrees(45)) Circle().fill(st).frame(width: 6, height: 6) } else { TimelineView(.animation) { timeline in let t = timeline.date.timeIntervalSinceReferenceDate // turn: 1.6s cubic-bezier(.5,.15,.5,.85) infinite let turn = SkyPainter.cubicBezier(t.truncatingRemainder(dividingBy: 1.6) / 1.6, 0.5, 0.15, 0.5, 0.85) // glow: 1.6s ease-in-out infinite alternate, .45 → 1 let glow = SkyPainter.mix(0.45, 1, SkyPainter.easeInOut(SkyPainter.progress(t, period: 1.6, delay: 0, reverse: false))) ZStack { WorkingArc(color: st).rotationEffect(.degrees(360 * turn)) Circle().fill(st).frame(width: 6, height: 6).opacity(glow) } } } } else { Circle().fill(st) Icon(icon, weight: 2.6) .foregroundStyle(p[.onSignal]) .frame(width: size * 15 / 28, height: size * 15 / 28) } } .frame(width: size, height: size) .accessibilityHidden(true) } var icon: BaseIcon { switch state { case .asking: .asking case .ready: .ready case .done: .done case .paused: .paused case .stopped, .working: .stopped } } } /// `border: 2px solid transparent; border-top-color: --st`: a quarter of /// the ring, centred on the top. struct WorkingArc: View { let color: Color var body: some View { Circle() .inset(by: 1) .trim(from: 0, to: 0.25) .stroke(color, style: StrokeStyle(lineWidth: 2, lineCap: .butt)) .rotationEffect(.degrees(-135)) } } /// Just enough SVG path syntax for the web's icons: M L H V C A Z, absolute /// and relative, with implicit repeats. enum SVGPath { static func parse(_ d: String) -> Path { var path = Path() var tokens = Tokens(d) var current = CGPoint.zero, start = CGPoint.zero var command: Character = "M" while let next = tokens.peekCommand() ?? (tokens.atEnd ? nil : command) { if tokens.peekCommand() != nil { tokens.skipCommand() } command = next let relative = command.isLowercase let base = relative ? current : .zero switch command.uppercased() { case "M": guard let x = tokens.number(), let y = tokens.number() else { return path } current = CGPoint(x: base.x + x, y: base.y + y); start = current path.move(to: current) command = relative ? "l" : "L" case "L": guard let x = tokens.number(), let y = tokens.number() else { return path } current = CGPoint(x: base.x + x, y: base.y + y); path.addLine(to: current) case "H": guard let x = tokens.number() else { return path } current = CGPoint(x: (relative ? current.x : 0) + x, y: current.y); path.addLine(to: current) case "V": guard let y = tokens.number() else { return path } current = CGPoint(x: current.x, y: (relative ? current.y : 0) + y); path.addLine(to: current) case "C": guard let x1 = tokens.number(), let y1 = tokens.number(), let x2 = tokens.number(), let y2 = tokens.number(), let x = tokens.number(), let y = tokens.number() else { return path } let end = CGPoint(x: base.x + x, y: base.y + y) path.addCurve(to: end, control1: CGPoint(x: base.x + x1, y: base.y + y1), control2: CGPoint(x: base.x + x2, y: base.y + y2)) current = end case "A": guard let rx = tokens.number(), let ry = tokens.number(), let rotation = tokens.number(), let large = tokens.flag(), let sweep = tokens.flag(), let x = tokens.number(), let y = tokens.number() else { return path } let end = CGPoint(x: base.x + x, y: base.y + y) addArc(&path, from: current, to: end, rx: rx, ry: ry, rotation: rotation, large: large, sweep: sweep) current = end case "Z": path.closeSubpath(); current = start default: return path } } return path } /// SVG's endpoint arc (implementation notes F.6.5), as cubic curves. static func addArc(_ path: inout Path, from p0: CGPoint, to p1: CGPoint, rx: Double, ry: Double, rotation: Double, large: Bool, sweep: Bool) { var rx = abs(rx), ry = abs(ry) guard rx > 0, ry > 0, p0 != p1 else { path.addLine(to: p1); return } let phi = rotation * .pi / 180, cosPhi = cos(phi), sinPhi = sin(phi) let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2 let x1 = cosPhi * dx + sinPhi * dy, y1 = -sinPhi * dx + cosPhi * dy let lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry) if lambda > 1 { rx *= sqrt(lambda); ry *= sqrt(lambda) } let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1 let den = rx * rx * y1 * y1 + ry * ry * x1 * x1 var coef = sqrt(max(0, num / den)) if large == sweep { coef = -coef } let cx1 = coef * rx * y1 / ry, cy1 = -coef * ry * x1 / rx let cx = cosPhi * cx1 - sinPhi * cy1 + (p0.x + p1.x) / 2 let cy = sinPhi * cx1 + cosPhi * cy1 + (p0.y + p1.y) / 2 func angle(_ ux: Double, _ uy: Double, _ vx: Double, _ vy: Double) -> Double { let a = atan2(ux * vy - uy * vx, ux * vx + uy * vy) return a } let theta1 = angle(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry) var delta = angle((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry) if !sweep && delta > 0 { delta -= 2 * .pi } if sweep && delta < 0 { delta += 2 * .pi } let segments = max(1, Int(ceil(abs(delta) / (.pi / 2)))) let step = delta / Double(segments) var theta = theta1 for _ in 0.. (CGPoint, CGPoint) { let ex = rx * cos(a), ey = ry * sin(a) let px = cx + cosPhi * ex - sinPhi * ey, py = cy + sinPhi * ex + cosPhi * ey let tx = -rx * sin(a), ty = ry * cos(a) return (CGPoint(x: px, y: py), CGPoint(x: cosPhi * tx - sinPhi * ty, y: sinPhi * tx + cosPhi * ty)) } let (a, da) = point(theta), (b, db) = point(theta + step) 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)) theta += step } } struct Tokens { let chars: [Character] var i = 0 init(_ s: String) { chars = Array(s) } var atEnd: Bool { mutating get { skipSeparators(); return i >= chars.count } } mutating func skipSeparators() { while i < chars.count, chars[i] == " " || chars[i] == "," || chars[i] == "\n" { i += 1 } } mutating func peekCommand() -> Character? { skipSeparators() guard i < chars.count, chars[i].isLetter, chars[i] != "e", chars[i] != "E" else { return nil } return chars[i] } mutating func skipCommand() { i += 1 } mutating func flag() -> Bool? { skipSeparators() guard i < chars.count, chars[i] == "0" || chars[i] == "1" else { return nil } defer { i += 1 } return chars[i] == "1" } mutating func number() -> Double? { skipSeparators() var s = "" var seenDot = false, seenExp = false while i < chars.count { let c = chars[i] if c == "-" || c == "+" { if !s.isEmpty && !(s.last == "e" || s.last == "E") { break } } else if c == "." { if seenDot || seenExp { break } seenDot = true } else if c == "e" || c == "E" { if seenExp { break } seenExp = true } else if !c.isNumber { break } s.append(c); i += 1 } return Double(s) } } }