Sources/Orgstar/ThemePickerSection.swift
79 lines · 3627 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 {
37 // Below 4.5:1 for body text or coloured roles in places.
38 palette.lowContrast ? Text("\(Text(palette.displayName))\(Text(verbatim: " – "))\(Text("Low contrast"))") : Text(palette.displayName)
39 } icon: { Image(nsImage: Self.swatch(palette)) }.tag(palette.id)
40 }
41 if ThemeSpec.palette(selection.wrappedValue, appearance) == nil {
42 Text(selection.wrappedValue).tag(selection.wrappedValue)
43 }
44 }
45 }
46
47 /// Background, text, heading, TODO and accent, side by side.
48 static func swatch(_ palette: Palette) -> NSImage {
49 let keys = ["background", "foreground", "heading-1", "todo", "accent"]
50 return NSImage(size: NSSize(width: 40, height: 12), flipped: false) { rect in
51 for (i, key) in keys.enumerated() {
52 let c = palette.colors[key] ?? palette.colors["cursor"] ?? RGBA(red: 0.5, green: 0.5, blue: 0.5)
53 NSColor(srgbRed: c.red, green: c.green, blue: c.blue, alpha: 1).setFill()
54 NSRect(x: CGFloat(i) * 8, y: 0, width: 8, height: rect.height).fill()
55 }
56 NSColor.separatorColor.setStroke()
57 NSBezierPath(rect: rect.insetBy(dx: 0.5, dy: 0.5)).stroke()
58 return true
59 }
60 }
61
62 private func copy() {
63 let light = ThemeSpec.palette(themeLight, .light) ?? ThemeSpec.palettes(.light)[0]
64 let dark = ThemeSpec.palette(themeDark, .dark) ?? ThemeSpec.palettes(.dark)[0]
65 do {
66 guard try UserConfig.copyTheme(light: light, dark: dark, to: UserConfig.directory) else {
67 message = String(localized: "my-theme.toml is already in the config folder; it was left as it is.")
68 return
69 }
70 let previous = themeFile
71 themeFile = UserConfig.copiedThemeFile
72 message = previous.isEmpty || previous == UserConfig.copiedThemeFile
73 ? String(localized: "Copied to my-theme.toml, now the theme file.")
74 : String(localized: "Copied to my-theme.toml, now the theme file in place of \(previous).")
75 } catch {
76 message = error.localizedDescription
77 }
78 }
79}