import RastrilloApple import SwiftUI #if os(macOS) import AppKit #else import UIKit #endif /// Loads Oficina apps' icons (`icon_url`, content-hashed SVGs on Base's /// own origin, so cached for good) through the session's transport. @MainActor @Observable public final class IconStore { public static let shared = IconStore() private var images: [String: Image] = [:] private var failed: Set = [] private var loading: Set = [] public func image(_ path: String) -> Image? { images[path] } public func load(_ path: String, from transport: NativeTransport?) { guard let transport, images[path] == nil, !failed.contains(path), !loading.contains(path) else { return } loading.insert(path) Task { defer { loading.remove(path) } guard let response = try? await transport.responseDetails(path, method: "GET", body: nil, maxBytes: 1 << 20), response.status == 200, let image = Self.decode(response.data) else { failed.insert(path); return } images[path] = image } } static func decode(_ data: Data) -> Image? { #if os(macOS) NSImage(data: data).map(Image.init(nsImage:)) #else UIImage(data: data).map(Image.init(uiImage:)) #endif } } /// An app's icon, as Home draws it: an Oficina app's own artwork; an /// installed app's or Base's own page's line art in white on its tint, in /// the web's squircle. Until an Oficina app's artwork has loaded (or when it /// can't be read), its tile with a symbol on Calma's accent. public struct AppIcon: View { let app: HomeData.App let size: CGFloat let transport: NativeTransport? @Environment(\.colorScheme) private var scheme public init(_ app: HomeData.App, size: CGFloat, transport: NativeTransport?) { self.app = app; self.size = size; self.transport = transport } public var body: some View { Group { if app.kind == .oficina, let path = app.iconURL, let image = IconStore.shared.image(path) { image.resizable().interpolation(.high).aspectRatio(contentMode: .fit) } else if app.kind == .oficina { tile(fill: AnyShapeStyle(OficinaPalette.accent(OficinaApp(rawValue: app.id) ?? .oficina))) { Image(systemName: OficinaApp(rawValue: app.id)?.symbol ?? Self.symbol(app.id)) .font(.system(size: size * 0.42, weight: .semibold)).foregroundStyle(.white) } } else { tile(fill: AnyShapeStyle(Self.tint(app.tint, Palette(scheme)))) { SVGGlyph(svg: app.iconSVG ?? "") .stroke(Palette(scheme)[.tileInk], style: StrokeStyle(lineWidth: max(1.5, size * 2.2 / 64), lineCap: .round, lineJoin: .round)) .frame(width: size * 0.62, height: size * 0.62) } } } .frame(width: size, height: size) .onAppear { if let path = app.iconURL, app.kind == .oficina { IconStore.shared.load(path, from: transport) } } .accessibilityHidden(true) } private func tile(fill: AnyShapeStyle, @ViewBuilder glyph: () -> G) -> some View { let p = Palette(scheme) let shape = RoundedRectangle(cornerRadius: size * 0.22 / 0.93, style: .continuous) return ZStack { shape.fill(fill) shape.strokeBorder(p[.glassEdge], lineWidth: 1) glyph() } .padding(size * 0.035) } /// `.tile.t-`: 155°, its two tokens. static func tint(_ name: String?, _ p: Palette) -> LinearGradient { let pair: (Palette.Token, Palette.Token) = switch name { case "amber": (.amber1, .amber2) case "violet": (.violet1, .violet2) case "mist": (.mist1, .mist2) case "rose": (.rose1, .rose2) default: (.teal1, .teal2) } let (s, e) = SkyPainter.cssLine(degrees: 155, in: CGRect(x: 0, y: 0, width: 1, height: 1)) 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)) } static func symbol(_ id: String) -> String { switch id { case "equipo": "person.3.fill" case "slides": "rectangle.on.rectangle" case "fichas": "rectangle.stack.fill" default: "app.fill" } } } /// Line art from a small SVG (the web's tile icons: path, circle, rect, /// line, polyline in a 24-unit box), as one path to stroke. public struct SVGGlyph: Shape { let svg: String public init(svg: String) { self.svg = svg } public func path(in rect: CGRect) -> Path { var path = Path() func attr(_ tag: String, _ name: String) -> Double? { guard let r = tag.range(of: #"\b\#(name)="([^"]*)""#, options: .regularExpression) else { return nil } return Double(tag[r].split(separator: "\"")[1]) } func string(_ tag: String, _ name: String) -> String? { guard let r = tag.range(of: #"\b\#(name)="([^"]*)""#, options: .regularExpression) else { return nil } let parts = tag[r].split(separator: "\"", omittingEmptySubsequences: false) return parts.count > 1 ? String(parts[1]) : nil } let regex = try! NSRegularExpression(pattern: #"<(path|circle|rect|line|polyline|polygon)\b[^>]*>"#) for match in regex.matches(in: svg, range: NSRange(svg.startIndex..., in: svg)) { let tag = String(svg[Range(match.range, in: svg)!]) switch String(svg[Range(match.range(at: 1), in: svg)!]) { case "path": if let d = string(tag, "d") { path.addPath(SVGPath.parse(d)) } case "circle": if let cx = attr(tag, "cx"), let cy = attr(tag, "cy"), let r = attr(tag, "r") { path.addEllipse(in: CGRect(x: cx - r, y: cy - r, width: 2 * r, height: 2 * r)) } case "rect": if let w = attr(tag, "width"), let h = attr(tag, "height") { let r = attr(tag, "rx") ?? 0 path.addRoundedRect(in: CGRect(x: attr(tag, "x") ?? 0, y: attr(tag, "y") ?? 0, width: w, height: h), cornerSize: CGSize(width: r, height: r)) } case "line": if let x1 = attr(tag, "x1"), let y1 = attr(tag, "y1"), let x2 = attr(tag, "x2"), let y2 = attr(tag, "y2") { path.move(to: CGPoint(x: x1, y: y1)); path.addLine(to: CGPoint(x: x2, y: y2)) } case let kind: let numbers = (string(tag, "points") ?? "").split(whereSeparator: { $0 == " " || $0 == "," }).compactMap { Double($0) } let points = stride(from: 0, to: numbers.count - 1, by: 2).map { CGPoint(x: numbers[$0], y: numbers[$0 + 1]) } if let first = points.first { path.move(to: first) for p in points.dropFirst() { path.addLine(to: p) } if kind == "polygon" { path.closeSubpath() } } } } return path.applying(CGAffineTransform(scaleX: rect.width / 24, y: rect.height / 24).concatenating(CGAffineTransform(translationX: rect.minX, y: rect.minY))) } }