Sources/OrgstarMobile/ThemePickerScreen.swift
67 lines · 3238 bytes
5 symbols in this file
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 HStack(alignment: .firstTextBaseline) {
21 Text(palette.displayName).foregroundStyle(.primary)
22 // Below 4.5:1 for body text or coloured roles in places.
23 if palette.lowContrast { Text("Low contrast").font(.caption).foregroundStyle(.secondary) }
24 }
25 Self.sample(palette)
26 }
27 Spacer()
28 if palette.id == current { Image(systemName: "checkmark").foregroundStyle(.tint) }
29 }
30 }
31 .buttonStyle(.plain)
32 }
33 .navigationTitle(appearance == .light ? "Light Theme" : "Dark Theme")
34 }
35
36 /// The palette `light-theme` or `dark-theme` names, Default when unknown.
37 static func current(_ appearance: Palette.Appearance) -> Palette {
38 let id = UserDefaults.standard.string(forKey: appearance == .light ? "themeLight" : "themeDark") ?? ""
39 return ThemeSpec.palette(id, appearance) ?? ThemeSpec.palettes(appearance)[0]
40 }
41
42 private static func sample(_ palette: Palette) -> some View {
43 func color(_ key: String) -> Color {
44 guard let c = palette.colors[key] else { return .primary }
45 return Color(.sRGB, red: c.red, green: c.green, blue: c.blue, opacity: c.alpha)
46 }
47 let font = Font.system(.footnote, design: .monospaced)
48 func run(_ text: String, _ key: String, bold: Bool = false, underline: Bool = false) -> AttributedString {
49 var run = AttributedString(text)
50 run.foregroundColor = color(key)
51 if bold { run.inlinePresentationIntent = .stronglyEmphasized }
52 if underline { run.underlineStyle = .single }
53 return run
54 }
55 return VStack(alignment: .leading, spacing: 2) {
56 Text(run("* ", "heading-1") + run("TODO ", "todo", bold: true) + run("Plan the trip ", "heading-1", bold: true) + run(":travel:", "tags"))
57 Text(run(" SCHEDULED: ", "metadata") + run("<2026-10-20 Tue>", "timestamp"))
58 Text(run(" Book via ", "foreground") + run("the site", "link", underline: true) + run(", ", "foreground") + run("~code~", "code"))
59 Text(run("** ", "heading-2") + run("DONE ", "done", bold: true) + run("Passport", "heading-2", bold: true))
60 }
61 .font(font)
62 .padding(8)
63 .frame(maxWidth: .infinity, alignment: .leading)
64 .background(color("background"), in: RoundedRectangle(cornerRadius: 8))
65 }
66}
67#endif