Glimmer / app · glimmertoday.ai / native / Glimmer / Glyph.swift
| 1 | import SwiftUI |
| 2 | #if os(macOS) |
| 3 | import AppKit |
| 4 | #else |
| 5 | import UIKit |
| 6 | #endif |
| 7 | |
| 8 | /// Glimmer's one icon system: every row gets a solid, single-colour SF Symbol tinted from the theme. |
| 9 | /// Built-in content may still carry emoji (older catalogs, remote editions, saved tasks); they are |
| 10 | /// translated to symbols here and stripped from names for display, so nothing is shown in colour. |
| 11 | enum Glyph { |
| 12 | // MARK: Emoji handling |
| 13 | |
| 14 | /// Whether a character draws as a colour emoji. |
| 15 | static func isEmoji(_ character: Character) -> Bool { |
| 16 | guard let first = character.unicodeScalars.first else { return false } |
| 17 | if first.properties.isEmojiPresentation { return true } |
| 18 | // Text-default emoji such as ☀ or ✍ followed by the emoji variation selector. |
| 19 | return first.properties.isEmoji && first.value > 0x238C |
| 20 | } |
| 21 | |
| 22 | /// The name without leading or trailing emoji, such as "Deep work" for "💻 Deep work". |
| 23 | static func plainName(_ text: String) -> String { |
| 24 | var characters = Substring(text) |
| 25 | while let first = characters.first, isEmoji(first) || first.isWhitespace { characters = characters.dropFirst() } |
| 26 | while let last = characters.last, isEmoji(last) || last.isWhitespace { characters = characters.dropLast() } |
| 27 | let plain = String(characters) |
| 28 | return plain.isEmpty ? text : plain |
| 29 | } |
| 30 | |
| 31 | /// The first emoji at the start of a name, if any. |
| 32 | static func leadingEmoji(_ text: String) -> String? { |
| 33 | guard let first = text.first, isEmoji(first) else { return nil } |
| 34 | return String(first) |
| 35 | } |
| 36 | |
| 37 | // MARK: Resolving a symbol |
| 38 | |
| 39 | /// The symbol for a row. `icon` is a stored icon (an SF Symbol name or a legacy emoji), |
| 40 | /// `name` is the row's title and `category` a catalog category or kind such as "focus" or "Activity". |
| 41 | static func symbol(icon: String? = nil, name: String, category: String? = nil) -> String { |
| 42 | if let icon, !icon.isEmpty { |
| 43 | if isSymbolName(icon), exists(icon) { return icon } |
| 44 | if let mapped = GlyphMap.emoji[normalized(icon)] { return mapped } |
| 45 | } |
| 46 | let plain = plainName(name) |
| 47 | let key = plain.lowercased() |
| 48 | if let known = GlyphMap.names[key] { return known } |
| 49 | if let emoji = leadingEmoji(name), let mapped = GlyphMap.emoji[normalized(emoji)] { return mapped } |
| 50 | if let match = GlyphMap.keywords.first(where: { key.contains($0.0) }) { return match.1 } |
| 51 | if let category, let fallback = GlyphMap.categoryFallback[category.lowercased()] { return fallback } |
| 52 | return GlyphMap.fallback |
| 53 | } |
| 54 | |
| 55 | /// The theme colour for a category or kind. |
| 56 | static func tint(for category: String?) -> Color { |
| 57 | guard let category else { return GlimmerColor.accent } |
| 58 | return GlyphMap.categoryTint[category.lowercased()] ?? GlimmerColor.accent |
| 59 | } |
| 60 | |
| 61 | static func isSymbolName(_ text: String) -> Bool { |
| 62 | !text.isEmpty && text.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == ".") } |
| 63 | } |
| 64 | |
| 65 | /// Whether this system has the symbol, so a newer name never draws as an empty square. |
| 66 | static func exists(_ symbol: String) -> Bool { |
| 67 | #if os(macOS) |
| 68 | NSImage(systemSymbolName: symbol, accessibilityDescription: nil) != nil |
| 69 | #else |
| 70 | UIImage(systemName: symbol) != nil |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | /// Drops variation selectors so "☀️" and "☀" find the same entry. |
| 75 | private static func normalized(_ emoji: String) -> String { |
| 76 | String(String.UnicodeScalarView(emoji.unicodeScalars.filter { $0.value != 0xFE0F && $0.value != 0xFE0E })) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// A row's leading icon: a solid glyph on a softly tinted rounded square. |
| 81 | struct GlyphTile: View { |
| 82 | @Environment(\.readingTheme) private var theme |
| 83 | let symbol: String |
| 84 | var tint: Color = GlimmerColor.accent |
| 85 | @ScaledMetric(relativeTo: .body) private var size = GlyphTile.defaultSize |
| 86 | |
| 87 | static var defaultSize: CGFloat { |
| 88 | #if os(macOS) |
| 89 | 24 |
| 90 | #else |
| 91 | 30 |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | init(_ symbol: String, tint: Color = GlimmerColor.accent, size: CGFloat? = nil) { |
| 96 | self.symbol = Glyph.exists(symbol) ? symbol : GlyphMap.fallback |
| 97 | self.tint = tint |
| 98 | if let size { _size = ScaledMetric(wrappedValue: size, relativeTo: .body) } |
| 99 | } |
| 100 | |
| 101 | var body: some View { |
| 102 | glyph |
| 103 | .foregroundStyle(tint) |
| 104 | .frame(width: size, height: size) |
| 105 | .background(tint.opacity(theme == .glimmer ? 0.2 : 0.13), in: .rect(cornerRadius: size * 0.28, style: .continuous)) |
| 106 | .accessibilityHidden(true) |
| 107 | } |
| 108 | |
| 109 | /// The glyph at 54% of the tile, stepping down to 44% for very wide symbols such as person.3.fill. |
| 110 | private var glyph: some View { |
| 111 | ViewThatFits(in: .horizontal) { |
| 112 | symbolImage(size * 0.54).frame(maxWidth: size * 0.86) |
| 113 | symbolImage(size * 0.44) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private func symbolImage(_ points: CGFloat) -> some View { |
| 118 | Image(systemName: symbol).font(.system(size: points, weight: .semibold)).symbolRenderingMode(.monochrome).fixedSize() |
| 119 | } |
| 120 | } |