Commit 065a5cf4c5

065a5cf4c58176da6374247e13bd1b1580a10a86

parent: 42a3851969

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-14 14:01 UTC

Theme pickers in Settings ▸ Appearance; Copy to Config Folder

Layout: unified · split

Sources/OrgApp/UserConfig.swift +12
@@ -49,6 +49,18 @@ public enum UserConfig {
4949 return (merged, problems + more)
5050 }
5151
52 /// The file Copy to Config Folder writes.
53 public static let copiedThemeFile = "my-theme.toml"
54
55 /// Writes the two palettes into `folder` as `copiedThemeFile`; false, writing nothing, when
56 /// the file is already there.
57 public static func copyTheme(light: Palette, dark: Palette, to folder: URL) throws -> Bool {
58 let file = folder.appendingPathComponent(copiedThemeFile)
59 guard !FileManager.default.fileExists(atPath: file.path) else { return false }
60 try ThemeSpec.themeFile(light: light, dark: dark).write(to: file, atomically: true, encoding: .utf8)
61 return true
62 }
63
5264 /// Where earlier versions kept the configuration files.
5365 public static var legacyDirectory: URL {
5466 FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("Orgstar")
Sources/OrgPresentation/Palettes.swift +15
@@ -146,3 +146,18 @@ extension ThemeSpec {
146146 Palette(id: id, name: name, appearance: appearance, colors: roles.colors(dark: appearance == .dark))
147147 }
148148}
149
150extension ThemeSpec {
151 /// Two palettes as a theme file, every color written out, in slot order.
152 public static func themeFile(light: Palette, dark: Palette) -> String {
153 var out = "# \(light.name) and \(dark.name), copied from orgstar's built-in themes. Used with\n"
154 + "# theme-file = \"my-theme.toml\" under [theme] in config.toml; colors there apply on top.\n"
155 for (table, palette) in [("theme.light", light), ("theme.dark", dark)] {
156 out += "\n[\(table)]\n"
157 for slot in slots {
158 if let color = palette.colors[slot.key] { out += "\(slot.key) = \"\(color.hex)\"\n" }
159 }
160 }
161 return out
162 }
163}
Sources/Orgstar/OrgstarApp.swift +2 −1
@@ -779,6 +779,7 @@ struct SettingsView: View {
779779
780780 private var appearance: some View {
781781 Form {
782 ThemePickerSection()
782783 Section {
783784 Picker("Font", selection: $themeFont) {
784785 Text("System monospaced").tag("")
@@ -950,7 +951,7 @@ struct SettingsView: View {
950951 }
951952}
952953
953private extension Text {
954extension Text {
954955 func settingsFooter() -> some View {
955956 font(.caption)
956957 .foregroundStyle(.secondary)
Sources/Orgstar/ThemePickerSection.swift added +68
@@ -0,0 +1,68 @@
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}
Tests/OrgAppTests/UserConfigTests.swift +14
@@ -280,4 +280,18 @@ struct ThemeReadingTests {
280280 let theme = try #require(UserConfig.theme(config: "", themeFile: nil, lightTheme: nil, darkTheme: nil, in: folder))
281281 #expect(theme.theme == ThemeSpec.default && theme.problems.isEmpty)
282282 }
283
284 @Test func copyingAThemeNeverOverwrites() throws {
285 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-copy-\(UUID().uuidString)")
286 defer { try? FileManager.default.removeItem(at: folder) }
287 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
288 let light = try #require(ThemeSpec.palette("paper", .light))
289 let dark = try #require(ThemeSpec.palette("nord", .dark))
290 #expect(try UserConfig.copyTheme(light: light, dark: dark, to: folder))
291 let file = folder.appendingPathComponent(UserConfig.copiedThemeFile)
292 #expect(try String(contentsOf: file, encoding: .utf8) == ThemeSpec.themeFile(light: light, dark: dark))
293 try "mine".write(to: file, atomically: true, encoding: .utf8)
294 #expect(try !UserConfig.copyTheme(light: light, dark: dark, to: folder))
295 #expect(try String(contentsOf: file, encoding: .utf8) == "mine")
296 }
283297}
Tests/OrgPresentationTests/PaletteTests.swift +11
@@ -1,4 +1,5 @@
11import Foundation
2import OrgCore
23import OrgPresentation
34import Testing
45
@@ -74,4 +75,14 @@ struct PaletteTests {
7475 #expect(spec == ThemeSpec.default)
7576 #expect(problems == ["config.toml: unknown light-theme nord", "config.toml: unknown dark-theme unknown"])
7677 }
78
79 @Test func aCopiedThemeReadsBackAsThePalettes() throws {
80 let light = try #require(ThemeSpec.palette("paper", .light))
81 let dark = try #require(ThemeSpec.palette("nord", .dark))
82 let text = ThemeSpec.themeFile(light: light, dark: dark)
83 #expect(text.hasPrefix("# Paper and Nord, copied from orgstar's built-in themes."))
84 let (spec, problems) = ThemeSpec.reading(try TOML.parse(text))
85 #expect(problems.isEmpty, "\(problems)")
86 #expect(spec.light.filter { light.colors[$0.key] != nil } == light.colors && spec.dark.filter { dark.colors[$0.key] != nil } == dark.colors)
87 }
7788}