Sources/OrgstarMobile/ThemePickerScreen.swift

59 lines · 3017 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        return VStack(alignment: .leading, spacing: 2) {
45            (Text(verbatim: "* ").foregroundColor(color("heading-1")) + Text(verbatim: "TODO ").foregroundColor(color("todo")).bold()
46                + Text(verbatim: "Plan the trip ").foregroundColor(color("heading-1")).bold() + Text(verbatim: ":travel:").foregroundColor(color("tags")))
47            (Text(verbatim: "  SCHEDULED: ").foregroundColor(color("metadata")) + Text(verbatim: "<2026-10-20 Tue>").foregroundColor(color("timestamp")))
48            (Text(verbatim: "  Book via ").foregroundColor(color("foreground")) + Text(verbatim: "the site").foregroundColor(color("link")).underline()
49                + Text(verbatim: ", ").foregroundColor(color("foreground")) + Text(verbatim: "~code~").foregroundColor(color("code")))
50            (Text(verbatim: "** ").foregroundColor(color("heading-2")) + Text(verbatim: "DONE ").foregroundColor(color("done")).bold()
51                + Text(verbatim: "Passport").foregroundColor(color("heading-2")).bold())
52        }
53        .font(font)
54        .padding(8)
55        .frame(maxWidth: .infinity, alignment: .leading)
56        .background(color("background"), in: RoundedRectangle(cornerRadius: 8))
57    }
58}
59#endif