Sources/Orgstar/ThemePickerSection.swift

065a5cf4c58176da6374247e13bd1b1580a10a86
orgstar/Sources/Orgstar/ThemePickerSection.swift

68 lines · 2946 bytes

 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 ?? "Colors set in config.toml or in the theme file apply on top of these.")
24                .settingsFooter()
25        }
26    }
27
28    private func picker(_ title: LocalizedStringKey, _ appearance: Palette.Appearance, _ selection: Binding<String>) -> some View {
29        Picker(title, selection: selection) {
30            ForEach(ThemeSpec.palettes(appearance), id: \.id) { palette in
31                Label { Text(palette.name) } icon: { Image(nsImage: Self.swatch(palette)) }.tag(palette.id)
32            }
33            if ThemeSpec.palette(selection.wrappedValue, appearance) == nil {
34                Text(selection.wrappedValue).tag(selection.wrappedValue)
35            }
36        }
37    }
38
39    /// Background, text, heading, TODO and accent, side by side.
40    static func swatch(_ palette: Palette) -> NSImage {
41        let keys = ["background", "foreground", "heading-1", "todo", "accent"]
42        return NSImage(size: NSSize(width: 40, height: 12), flipped: false) { rect in
43            for (i, key) in keys.enumerated() {
44                let c = palette.colors[key] ?? palette.colors["cursor"] ?? RGBA(red: 0.5, green: 0.5, blue: 0.5)
45                NSColor(srgbRed: c.red, green: c.green, blue: c.blue, alpha: 1).setFill()
46                NSRect(x: CGFloat(i) * 8, y: 0, width: 8, height: rect.height).fill()
47            }
48            NSColor.separatorColor.setStroke()
49            NSBezierPath(rect: rect.insetBy(dx: 0.5, dy: 0.5)).stroke()
50            return true
51        }
52    }
53
54    private func copy() {
55        let light = ThemeSpec.palette(themeLight, .light) ?? ThemeSpec.palettes(.light)[0]
56        let dark = ThemeSpec.palette(themeDark, .dark) ?? ThemeSpec.palettes(.dark)[0]
57        do {
58            guard try UserConfig.copyTheme(light: light, dark: dark, to: UserConfig.directory) else {
59                message = String(localized: "my-theme.toml is already in the config folder; it was left as it is.")
60                return
61            }
62            themeFile = UserConfig.copiedThemeFile
63            message = String(localized: "Copied to my-theme.toml, now the theme file.")
64        } catch {
65            message = error.localizedDescription
66        }
67    }
68}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form