| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | #if os(macOS) |
| 4 | import AppKit |
| 5 | #else |
| 6 | import UIKit |
| 7 | #endif |
| 8 | |
| 9 | /// Loads Oficina apps' icons (`icon_url`, content-hashed SVGs on Base's |
| 10 | /// own origin, so cached for good) through the session's transport. |
| 11 | @MainActor @Observable public final class IconStore { |
| 12 | public static let shared = IconStore() |
| 13 | private var images: [String: Image] = [:] |
| 14 | private var failed: Set<String> = [] |
| 15 | private var loading: Set<String> = [] |
| 16 | |
| 17 | public func image(_ path: String) -> Image? { images[path] } |
| 18 | |
| 19 | public func load(_ path: String, from transport: NativeTransport?) { |
| 20 | guard let transport, images[path] == nil, !failed.contains(path), !loading.contains(path) else { return } |
| 21 | loading.insert(path) |
| 22 | Task { |
| 23 | defer { loading.remove(path) } |
| 24 | guard let response = try? await transport.responseDetails(path, method: "GET", body: nil, maxBytes: 1 << 20), |
| 25 | response.status == 200, let image = Self.decode(response.data) else { failed.insert(path); return } |
| 26 | images[path] = image |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | static func decode(_ data: Data) -> Image? { |
| 31 | #if os(macOS) |
| 32 | NSImage(data: data).map(Image.init(nsImage:)) |
| 33 | #else |
| 34 | UIImage(data: data).map(Image.init(uiImage:)) |
| 35 | #endif |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// An app's icon, as Home draws it: an Oficina app's own artwork; an |
| 40 | /// installed app's or Base's own page's line art in white on its tint, in |
| 41 | /// the web's squircle. Until an Oficina app's artwork has loaded (or when it |
| 42 | /// can't be read), its tile with a symbol on Calma's accent. |
| 43 | public struct AppIcon: View { |
| 44 | let app: HomeData.App |
| 45 | let size: CGFloat |
| 46 | let transport: NativeTransport? |
| 47 | @Environment(\.colorScheme) private var scheme |
| 48 | |
| 49 | public init(_ app: HomeData.App, size: CGFloat, transport: NativeTransport?) { |
| 50 | self.app = app; self.size = size; self.transport = transport |
| 51 | } |
| 52 | |
| 53 | public var body: some View { |
| 54 | Group { |
| 55 | if app.kind == .oficina, let path = app.iconURL, let image = IconStore.shared.image(path) { |
| 56 | image.resizable().interpolation(.high).aspectRatio(contentMode: .fit) |
| 57 | } else if app.kind == .oficina { |
| 58 | tile(fill: AnyShapeStyle(OficinaPalette.accent(OficinaApp(rawValue: app.id) ?? .oficina))) { |
| 59 | Image(systemName: OficinaApp(rawValue: app.id)?.symbol ?? Self.symbol(app.id)) |
| 60 | .font(.system(size: size * 0.42, weight: .semibold)).foregroundStyle(.white) |
| 61 | } |
| 62 | } else { |
| 63 | tile(fill: AnyShapeStyle(Self.tint(app.tint, Palette(scheme)))) { |
| 64 | SVGGlyph(svg: app.iconSVG ?? "") |
| 65 | .stroke(Palette(scheme)[.tileInk], style: StrokeStyle(lineWidth: max(1.5, size * 2.2 / 64), lineCap: .round, lineJoin: .round)) |
| 66 | .frame(width: size * 0.62, height: size * 0.62) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | .frame(width: size, height: size) |
| 71 | .onAppear { if let path = app.iconURL, app.kind == .oficina { IconStore.shared.load(path, from: transport) } } |
| 72 | .accessibilityHidden(true) |
| 73 | } |
| 74 | |
| 75 | private func tile<G: View>(fill: AnyShapeStyle, @ViewBuilder glyph: () -> G) -> some View { |
| 76 | let p = Palette(scheme) |
| 77 | let shape = RoundedRectangle(cornerRadius: size * 0.22 / 0.93, style: .continuous) |
| 78 | return ZStack { |
| 79 | shape.fill(fill) |
| 80 | shape.strokeBorder(p[.glassEdge], lineWidth: 1) |
| 81 | glyph() |
| 82 | } |
| 83 | .padding(size * 0.035) |
| 84 | } |
| 85 | |
| 86 | /// `.tile.t-<tint>`: 155°, its two tokens. |
| 87 | static func tint(_ name: String?, _ p: Palette) -> LinearGradient { |
| 88 | let pair: (Palette.Token, Palette.Token) = switch name { |
| 89 | case "amber": (.amber1, .amber2) |
| 90 | case "violet": (.violet1, .violet2) |
| 91 | case "mist": (.mist1, .mist2) |
| 92 | case "rose": (.rose1, .rose2) |
| 93 | default: (.teal1, .teal2) |
| 94 | } |
| 95 | let (s, e) = SkyPainter.cssLine(degrees: 155, in: CGRect(x: 0, y: 0, width: 1, height: 1)) |
| 96 | return LinearGradient(colors: [p[pair.0], p[pair.1]], startPoint: UnitPoint(x: s.x, y: s.y), endPoint: UnitPoint(x: e.x, y: e.y)) |
| 97 | } |
| 98 | |
| 99 | static func symbol(_ id: String) -> String { |
| 100 | switch id { |
| 101 | case "equipo": "person.3.fill" |
| 102 | case "slides": "rectangle.on.rectangle" |
| 103 | case "fichas": "rectangle.stack.fill" |
| 104 | default: "app.fill" |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Line art from a small SVG (the web's tile icons: path, circle, rect, |
| 110 | /// line, polyline in a 24-unit box), as one path to stroke. |
| 111 | public struct SVGGlyph: Shape { |
| 112 | let svg: String |
| 113 | public init(svg: String) { self.svg = svg } |
| 114 | |
| 115 | public func path(in rect: CGRect) -> Path { |
| 116 | var path = Path() |
| 117 | func attr(_ tag: String, _ name: String) -> Double? { |
| 118 | guard let r = tag.range(of: #"\b\#(name)="([^"]*)""#, options: .regularExpression) else { return nil } |
| 119 | return Double(tag[r].split(separator: "\"")[1]) |
| 120 | } |
| 121 | func string(_ tag: String, _ name: String) -> String? { |
| 122 | guard let r = tag.range(of: #"\b\#(name)="([^"]*)""#, options: .regularExpression) else { return nil } |
| 123 | let parts = tag[r].split(separator: "\"", omittingEmptySubsequences: false) |
| 124 | return parts.count > 1 ? String(parts[1]) : nil |
| 125 | } |
| 126 | let regex = try! NSRegularExpression(pattern: #"<(path|circle|rect|line|polyline|polygon)\b[^>]*>"#) |
| 127 | for match in regex.matches(in: svg, range: NSRange(svg.startIndex..., in: svg)) { |
| 128 | let tag = String(svg[Range(match.range, in: svg)!]) |
| 129 | switch String(svg[Range(match.range(at: 1), in: svg)!]) { |
| 130 | case "path": if let d = string(tag, "d") { path.addPath(SVGPath.parse(d)) } |
| 131 | case "circle": |
| 132 | if let cx = attr(tag, "cx"), let cy = attr(tag, "cy"), let r = attr(tag, "r") { |
| 133 | path.addEllipse(in: CGRect(x: cx - r, y: cy - r, width: 2 * r, height: 2 * r)) |
| 134 | } |
| 135 | case "rect": |
| 136 | if let w = attr(tag, "width"), let h = attr(tag, "height") { |
| 137 | let r = attr(tag, "rx") ?? 0 |
| 138 | path.addRoundedRect(in: CGRect(x: attr(tag, "x") ?? 0, y: attr(tag, "y") ?? 0, width: w, height: h), cornerSize: CGSize(width: r, height: r)) |
| 139 | } |
| 140 | case "line": |
| 141 | if let x1 = attr(tag, "x1"), let y1 = attr(tag, "y1"), let x2 = attr(tag, "x2"), let y2 = attr(tag, "y2") { |
| 142 | path.move(to: CGPoint(x: x1, y: y1)); path.addLine(to: CGPoint(x: x2, y: y2)) |
| 143 | } |
| 144 | case let kind: |
| 145 | let numbers = (string(tag, "points") ?? "").split(whereSeparator: { $0 == " " || $0 == "," }).compactMap { Double($0) } |
| 146 | let points = stride(from: 0, to: numbers.count - 1, by: 2).map { CGPoint(x: numbers[$0], y: numbers[$0 + 1]) } |
| 147 | if let first = points.first { |
| 148 | path.move(to: first) |
| 149 | for p in points.dropFirst() { path.addLine(to: p) } |
| 150 | if kind == "polygon" { path.closeSubpath() } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | return path.applying(CGAffineTransform(scaleX: rect.width / 24, y: rect.height / 24).concatenating(CGAffineTransform(translationX: rect.minX, y: rect.minY))) |
| 155 | } |
| 156 | } |
| 157 | |