Sources/OrgstarMobile/ThemePickerScreen.swift

63 lines · 2939 bytes

 1#if os(iOS)
 2import OrgCore
 3import OrgPresentation
 4import SwiftUI
 5
 6/// The built-in palettes of one appearance, each as a few lines of Org in its colors.
 7struct ThemePickerScreen: View {
 8    let model: MobileModel
 9    let appearance: Palette.Appearance
10
11    var body: some View {
12        let _ = model.settingsVersion
13        let current = Self.current(appearance).id
14        List(ThemeSpec.palettes(appearance), id: \.id) { palette in
15            Button {
16                model.set(appearance == .light ? "themeLight" : "themeDark", to: .string(palette.id))
17            } label: {
18                HStack {
19                    VStack(alignment: .leading, spacing: 6) {
20                        Text(palette.displayName).foregroundStyle(.primary)
21                        Self.sample(palette)
22                    }
23                    Spacer()
24                    if palette.id == current { Image(systemName: "checkmark").foregroundStyle(.tint) }
25                }
26            }
27            .buttonStyle(.plain)
28        }
29        .navigationTitle(appearance == .light ? "Light Theme" : "Dark Theme")
30    }
31
32    /// The palette `light-theme` or `dark-theme` names, Default when unknown.
33    static func current(_ appearance: Palette.Appearance) -> Palette {
34        let id = UserDefaults.standard.string(forKey: appearance == .light ? "themeLight" : "themeDark") ?? ""
35        return ThemeSpec.palette(id, appearance) ?? ThemeSpec.palettes(appearance)[0]
36    }
37
38    private static func sample(_ palette: Palette) -> some View {
39        func color(_ key: String) -> Color {
40            guard let c = palette.colors[key] else { return .primary }
41            return Color(.sRGB, red: c.red, green: c.green, blue: c.blue, opacity: c.alpha)
42        }
43        let font = Font.system(.footnote, design: .monospaced)
44        func run(_ text: String, _ key: String, bold: Bool = false, underline: Bool = false) -> AttributedString {
45            var run = AttributedString(text)
46            run.foregroundColor = color(key)
47            if bold { run.inlinePresentationIntent = .stronglyEmphasized }
48            if underline { run.underlineStyle = .single }
49            return run
50        }
51        return VStack(alignment: .leading, spacing: 2) {
52            Text(run("* ", "heading-1") + run("TODO ", "todo", bold: true) + run("Plan the trip ", "heading-1", bold: true) + run(":travel:", "tags"))
53            Text(run("  SCHEDULED: ", "metadata") + run("<2026-10-20 Tue>", "timestamp"))
54            Text(run("  Book via ", "foreground") + run("the site", "link", underline: true) + run(", ", "foreground") + run("~code~", "code"))
55            Text(run("** ", "heading-2") + run("DONE ", "done", bold: true) + run("Passport", "heading-2", bold: true))
56        }
57        .font(font)
58        .padding(8)
59        .frame(maxWidth: .infinity, alignment: .leading)
60        .background(color("background"), in: RoundedRectangle(cornerRadius: 8))
61    }
62}
63#endif