Commit 065a5cf4c5
Verified · cmc
Layout: unified · split
Sources/OrgApp/UserConfig.swift +12
| @@ -49,6 +49,18 @@ public enum UserConfig { | ||
| 49 | 49 | return (merged, problems + more) |
| 50 | 50 | } |
| 51 | 51 | |
| 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 | ||
| 52 | 64 | /// Where earlier versions kept the configuration files. |
| 53 | 65 | public static var legacyDirectory: URL { |
| 54 | 66 | FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("Orgstar") |
Sources/OrgPresentation/Palettes.swift +15
| @@ -146,3 +146,18 @@ extension ThemeSpec { | ||
| 146 | 146 | Palette(id: id, name: name, appearance: appearance, colors: roles.colors(dark: appearance == .dark)) |
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | ||
| 150 | extension 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 { | ||
| 779 | 779 | |
| 780 | 780 | private var appearance: some View { |
| 781 | 781 | Form { |
| 782 | ThemePickerSection() | |
| 782 | 783 | Section { |
| 783 | 784 | Picker("Font", selection: $themeFont) { |
| 784 | 785 | Text("System monospaced").tag("") |
| @@ -950,7 +951,7 @@ struct SettingsView: View { | ||
| 950 | 951 | } |
| 951 | 952 | } |
| 952 | 953 | |
| 953 | private extension Text { | |
| 954 | extension Text { | |
| 954 | 955 | func settingsFooter() -> some View { |
| 955 | 956 | font(.caption) |
| 956 | 957 | .foregroundStyle(.secondary) |
Sources/Orgstar/ThemePickerSection.swift added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | import AppKit | |
| 2 | import OrgApp | |
| 3 | import OrgPresentation | |
| 4 | import SwiftUI | |
| 5 | ||
| 6 | /// Settings ▸ Appearance ▸ Theme: the built-in palette for each appearance. | |
| 7 | struct 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 { | ||
| 280 | 280 | let theme = try #require(UserConfig.theme(config: "", themeFile: nil, lightTheme: nil, darkTheme: nil, in: folder)) |
| 281 | 281 | #expect(theme.theme == ThemeSpec.default && theme.problems.isEmpty) |
| 282 | 282 | } |
| 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 | } | |
| 283 | 297 | } |
Tests/OrgPresentationTests/PaletteTests.swift +11
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import Foundation |
| 2 | import OrgCore | |
| 2 | 3 | import OrgPresentation |
| 3 | 4 | import Testing |
| 4 | 5 | |
| @@ -74,4 +75,14 @@ struct PaletteTests { | ||
| 74 | 75 | #expect(spec == ThemeSpec.default) |
| 75 | 76 | #expect(problems == ["config.toml: unknown light-theme nord", "config.toml: unknown dark-theme unknown"]) |
| 76 | 77 | } |
| 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 | } | |
| 77 | 88 | } |