Sources/Orgstar/ThemePickerSection.swift

76 lines · 3403 bytes

4 symbols in this file
 1import AppKit
 2import OrgApp
 3import OrgPresentation
 4import SwiftUI
 5
 6/// Settings ▸ Appearance ▸ Theme: the built-in palette for each appearance.
 7struct ThemePickerSection: View {
 8    @AppStorage("themeLight") private var themeLight = "default-light"
 9    @AppStorage("themeDark") private var themeDark = "default-dark"
10    @AppStorage("themeFile") private var themeFile = ""
11    @State private var message: String?
12
13    var body: some View {
14        Section {
15            picker("Light", .light, $themeLight)
16            picker("Dark", .dark, $themeDark)
17            LabeledContent("Starting point") {
18                Button("Copy to Config Folder") { copy() }
19            }
20        } header: {
21            Text("Theme")
22        } footer: {
23            Text(message ?? footer)
24                .settingsFooter()
25        }
26    }
27
28    private var footer: String {
29        themeFile.isEmpty ? String(localized: "Colors set in config.toml or in the theme file apply on top of these.")
30            : String(localized: "The theme file \(themeFile) sets colors over these, and config.toml's colors apply on top of both.")
31    }
32
33    private func picker(_ title: LocalizedStringKey, _ appearance: Palette.Appearance, _ selection: Binding<String>) -> some View {
34        Picker(title, selection: selection) {
35            ForEach(ThemeSpec.palettes(appearance), id: \.id) { palette in
36                Label { Text(palette.displayName) } icon: { Image(nsImage: Self.swatch(palette)) }.tag(palette.id)
37            }
38            if ThemeSpec.palette(selection.wrappedValue, appearance) == nil {
39                Text(selection.wrappedValue).tag(selection.wrappedValue)
40            }
41        }
42    }
43
44    /// Background, text, heading, TODO and accent, side by side.
45    static func swatch(_ palette: Palette) -> NSImage {
46        let keys = ["background", "foreground", "heading-1", "todo", "accent"]
47        return NSImage(size: NSSize(width: 40, height: 12), flipped: false) { rect in
48            for (i, key) in keys.enumerated() {
49                let c = palette.colors[key] ?? palette.colors["cursor"] ?? RGBA(red: 0.5, green: 0.5, blue: 0.5)
50                NSColor(srgbRed: c.red, green: c.green, blue: c.blue, alpha: 1).setFill()
51                NSRect(x: CGFloat(i) * 8, y: 0, width: 8, height: rect.height).fill()
52            }
53            NSColor.separatorColor.setStroke()
54            NSBezierPath(rect: rect.insetBy(dx: 0.5, dy: 0.5)).stroke()
55            return true
56        }
57    }
58
59    private func copy() {
60        let light = ThemeSpec.palette(themeLight, .light) ?? ThemeSpec.palettes(.light)[0]
61        let dark = ThemeSpec.palette(themeDark, .dark) ?? ThemeSpec.palettes(.dark)[0]
62        do {
63            guard try UserConfig.copyTheme(light: light, dark: dark, to: UserConfig.directory) else {
64                message = String(localized: "my-theme.toml is already in the config folder; it was left as it is.")
65                return
66            }
67            let previous = themeFile
68            themeFile = UserConfig.copiedThemeFile
69            message = previous.isEmpty || previous == UserConfig.copiedThemeFile
70                ? String(localized: "Copied to my-theme.toml, now the theme file.")
71                : String(localized: "Copied to my-theme.toml, now the theme file in place of \(previous).")
72        } catch {
73            message = error.localizedDescription
74        }
75    }
76}