Sources/OrgPresentation/AppIcons.swift

47 lines · 2020 bytes

 1import Foundation
 2
 3/// The app icons `scripts/make-icon.swift` draws, by id, in its order.
 4public enum AppIcons {
 5    public static let ids = [
 6        "default", "midnight", "paper", "org", "sunset", "forest", "mono", "rainbow", "outline", "terminal",
 7        "unicorn", "unicorn-night", "unicorn-pastel",
 8    ]
 9
10    private static func words(_ id: String) -> [String] { id.split(separator: "-").map { $0.prefix(1).uppercased() + $0.dropFirst() } }
11
12    /// The icon's name in the user's language.
13    public static func name(_ id: String) -> String {
14        switch id {
15        case "default": String(localized: "Default")
16        case "midnight": String(localized: "Midnight")
17        case "paper": String(localized: "Paper")
18        case "sunset": String(localized: "Sunset")
19        case "forest": String(localized: "Forest")
20        case "rainbow": String(localized: "Rainbow")
21        case "outline": String(localized: "Outline")
22        // Ids from when these were unicorns, kept so an icon chosen then stays chosen.
23        case "unicorn": String(localized: "Org Star")
24        case "unicorn-night": String(localized: "Night Star")
25        case "unicorn-pastel": String(localized: "Pastel Star")
26        default: words(id).joined(separator: " ")
27        }
28    }
29
30    /// The iOS alternate icon's name; nil for the primary icon.
31    public static func alternateName(_ id: String) -> String? {
32        id == "default" ? nil : "AppIcon-" + words(id).joined()
33    }
34
35    public static func id(alternateName: String?) -> String {
36        ids.first { self.alternateName($0) == alternateName && alternateName != nil } ?? "default"
37    }
38}
39
40extension AppIcons {
41    /// The Mac's Dock icon file for `id` in an appearance, in `Contents/Resources/IconPreviews`;
42    /// nil for the default, which is the bundle's own icon.
43    public static func dockFile(_ id: String, dark: Bool) -> String? {
44        guard id != "default", ids.contains(id) else { return nil }
45        return "\(id)-\(dark ? "dark" : "light")"
46    }
47}