Sources/Orgstar/ThemeColors.swift
37 lines · 1417 bytes
1import AppKit
2import OrgPresentation
3import SwiftUI
4
5extension EnvironmentValues {
6 /// The theme's colors, for the views around the editor.
7 @Entry var orgTheme: ThemeSpec = .default
8}
9
10extension ThemeSpec {
11 /// A color that follows the appearance, or nil when the theme leaves `key` to the system.
12 func color(_ key: String) -> Color? {
13 guard let light = self.light[key] ?? self.dark[key], let dark = self.dark[key] ?? self.light[key] else { return nil }
14 return Color(nsColor: NSColor(name: nil) { appearance in
15 let c = appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? dark : light
16 return NSColor(srgbRed: c.red, green: c.green, blue: c.blue, alpha: c.alpha)
17 })
18 }
19
20 /// A TODO keyword's color: its own from `[theme.todo]`, else the theme's TODO or DONE.
21 func keywordColor(_ keyword: String, done: Bool) -> Color {
22 if let c = todo[keyword] { return Color(.sRGB, red: c.red, green: c.green, blue: c.blue, opacity: c.alpha) }
23 return color(done ? "done" : "todo") ?? (done ? .green : .red)
24 }
25}
26
27extension View {
28 /// A list's background from the theme, or the system's when the theme has none.
29 @ViewBuilder
30 func themedListBackground(_ color: Color?) -> some View {
31 if let color {
32 scrollContentBackground(.hidden).background(color)
33 } else {
34 self
35 }
36 }
37}