Commit c78d052e44
Verified · cmc
Layout: unified · split
Sources/OrgPresentation/Palettes.swift added +39
| @@ -0,0 +1,39 @@ | |||
| 1 | /// A built-in theme for one appearance, chosen with `light-theme` or `dark-theme`. | ||
| 2 | public struct Palette: Sendable, Equatable { | ||
| 3 | public enum Appearance: String, Sendable { case light, dark } | ||
| 4 | |||
| 5 | public let id: String | ||
| 6 | public let name: String | ||
| 7 | public let appearance: Appearance | ||
| 8 | public let colors: [String: RGBA] | ||
| 9 | |||
| 10 | public init(id: String, name: String, appearance: Appearance, colors: [String: RGBA]) { | ||
| 11 | self.id = id | ||
| 12 | self.name = name | ||
| 13 | self.appearance = appearance | ||
| 14 | self.colors = colors | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | extension ThemeSpec { | ||
| 19 | /// The built-in palettes of `appearance`, Default first. | ||
| 20 | public static func palettes(_ appearance: Palette.Appearance) -> [Palette] { | ||
| 21 | appearance == .light ? lightPalettes : darkPalettes | ||
| 22 | } | ||
| 23 | |||
| 24 | public static func palette(_ id: String, _ appearance: Palette.Appearance) -> Palette? { | ||
| 25 | palettes(appearance).first { $0.id == id } | ||
| 26 | } | ||
| 27 | |||
| 28 | public static func defaultPaletteID(_ appearance: Palette.Appearance) -> String { | ||
| 29 | "default-" + appearance.rawValue | ||
| 30 | } | ||
| 31 | |||
| 32 | static let lightPalettes: [Palette] = [ | ||
| 33 | Palette(id: "default-light", name: "Default", appearance: .light, colors: ThemeSpec.default.light), | ||
| 34 | ] | ||
| 35 | |||
| 36 | static let darkPalettes: [Palette] = [ | ||
| 37 | Palette(id: "default-dark", name: "Default", appearance: .dark, colors: ThemeSpec.default.dark), | ||
| 38 | ] | ||
| 39 | } | ||
Sources/OrgPresentation/ThemeSpec.swift +3 −1
| @@ -73,6 +73,7 @@ public struct ThemeSpec: Equatable, Sendable { | |||
| 73 | ("foreground", "body text"), | 73 | ("foreground", "body text"), |
| 74 | ("cursor", "the caret"), | 74 | ("cursor", "the caret"), |
| 75 | ("selection", "selected text's background"), | 75 | ("selection", "selected text's background"), |
| 76 | ("accent", "buttons, toggles and selection in lists; unset: the system's"), | ||
| 76 | ("heading-1", "level 1 headings"), ("heading-2", "level 2 headings"), ("heading-3", "level 3 headings"), | 77 | ("heading-1", "level 1 headings"), ("heading-2", "level 2 headings"), ("heading-3", "level 3 headings"), |
| 77 | ("heading-4", "level 4 headings"), ("heading-5", "level 5 headings"), ("heading-6", "level 6 headings"), | 78 | ("heading-4", "level 4 headings"), ("heading-5", "level 5 headings"), ("heading-6", "level 6 headings"), |
| 78 | ("heading-7", "level 7 headings"), ("heading-8", "level 8 and deeper headings"), | 79 | ("heading-7", "level 7 headings"), ("heading-8", "level 8 and deeper headings"), |
| @@ -135,6 +136,7 @@ public struct ThemeSpec: Equatable, Sendable { | |||
| 135 | /// Keys the default theme leaves to the system, so windows keep the macOS look. | 136 | /// Keys the default theme leaves to the system, so windows keep the macOS look. |
| 136 | public static let systemDefaults: Set<String> = [ | 137 | public static let systemDefaults: Set<String> = [ |
| 137 | "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background", | 138 | "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background", |
| 139 | "accent", | ||
| 138 | ] | 140 | ] |
| 139 | 141 | ||
| 140 | // MARK: - The default theme | 142 | // MARK: - The default theme |
| @@ -188,7 +190,7 @@ public struct ThemeSpec: Equatable, Sendable { | |||
| 188 | // MARK: - Reading | 190 | // MARK: - Reading |
| 189 | 191 | ||
| 190 | /// Keys of `[theme]` that aren't colors; config.toml's settings handle them. | 192 | /// Keys of `[theme]` that aren't colors; config.toml's settings handle them. |
| 191 | public static let typeKeys: Set<String> = ["font", "body-font", "font-size", "line-spacing", "text-width", "heading-size-step", "theme-file"] | 193 | public static let typeKeys: Set<String> = ["font", "body-font", "font-size", "line-spacing", "text-width", "heading-size-step", "theme-file", "light-theme", "dark-theme"] |
| 192 | 194 | ||
| 193 | /// Whether a table holds theme colors. | 195 | /// Whether a table holds theme colors. |
| 194 | public static func isThemeTable(_ name: String) -> Bool { | 196 | public static func isThemeTable(_ name: String) -> Bool { |
Sources/Orgstar/ThemeColors.swift +1
| @@ -37,6 +37,7 @@ extension ThemeSpec { | |||
| 37 | case "sidebar-background", "modeline-background": .windowBackgroundColor | 37 | case "sidebar-background", "modeline-background": .windowBackgroundColor |
| 38 | case "agenda-background", "background": .textBackgroundColor | 38 | case "agenda-background", "background": .textBackgroundColor |
| 39 | case "sidebar-header", "modeline-foreground", "agenda-time", "agenda-category", "tags": .secondaryLabelColor | 39 | case "sidebar-header", "modeline-foreground", "agenda-time", "agenda-category", "tags": .secondaryLabelColor |
| 40 | case "accent": .controlAccentColor | ||
| 40 | default: .labelColor | 41 | default: .labelColor |
| 41 | } | 42 | } |
| 42 | } | 43 | } |
Tests/OrgPresentationTests/PaletteTests.swift added +31
| @@ -0,0 +1,31 @@ | |||
| 1 | import OrgPresentation | ||
| 2 | import Testing | ||
| 3 | |||
| 4 | struct PaletteTests { | ||
| 5 | @Test func defaultPalettesAreTheDefaultTheme() throws { | ||
| 6 | let light = try #require(ThemeSpec.palette("default-light", .light)) | ||
| 7 | let dark = try #require(ThemeSpec.palette("default-dark", .dark)) | ||
| 8 | #expect(light.colors == ThemeSpec.default.light && dark.colors == ThemeSpec.default.dark) | ||
| 9 | #expect(light.name == "Default" && ThemeSpec.palettes(.light).first == light && ThemeSpec.palettes(.dark).first == dark) | ||
| 10 | #expect(ThemeSpec.defaultPaletteID(.light) == "default-light" && ThemeSpec.defaultPaletteID(.dark) == "default-dark") | ||
| 11 | } | ||
| 12 | |||
| 13 | @Test func aPaletteIsFoundOnlyInItsAppearance() { | ||
| 14 | #expect(ThemeSpec.palette("default-light", .dark) == nil) | ||
| 15 | #expect(ThemeSpec.palette("nope", .light) == nil) | ||
| 16 | } | ||
| 17 | |||
| 18 | @Test func accentIsASlotTheDefaultLeavesToTheSystem() { | ||
| 19 | #expect(ThemeSpec.slots.contains { $0.key == "accent" }) | ||
| 20 | #expect(ThemeSpec.systemDefaults.contains("accent")) | ||
| 21 | #expect(ThemeSpec.default.light["accent"] == nil && ThemeSpec.default.dark["accent"] == nil) | ||
| 22 | } | ||
| 23 | |||
| 24 | @Test func everyPaletteHasExactlyTheSlotKeys() { | ||
| 25 | let all = Set(ThemeSpec.slots.map(\.key)) | ||
| 26 | for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) { | ||
| 27 | let expected = palette.id.hasPrefix("default-") ? all.subtracting(ThemeSpec.systemDefaults) : all | ||
| 28 | #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))") | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||