From 70a170f54476b9f4e43ccaeec035c17291f6c7e7 Mon Sep 17 00:00:00 2001 From: Tommaso Negri Date: Sun, 12 Jul 2026 11:34:52 +0200 Subject: [PATCH] move text definition to theme and make it dynamic --- Sources/phbar/Models/Block/PHBlock.swift | 3 +++ Sources/phbar/Models/Config/PHConfig.swift | 2 +- .../Config/Text/PHConfigTextStyle.swift | 9 -------- Sources/phbar/Models/Config/config.toml | 9 +++----- Sources/phbar/Models/Theme/PHTheme.swift | 2 ++ .../Text/PHThemeText.swift} | 23 +++++++++++++++---- .../Models/Theme/Text/PHThemeTextStyle.swift | 9 ++++++++ .../Text/PHThemeTextWeight.swift} | 2 +- Sources/phbar/Models/Theme/theme.toml | 16 +++++++++++++ Sources/phbar/Views/BarController.swift | 13 ++++++++++- Sources/phbar/Views/BarView.swift | 2 -- Sources/phbar/Views/TextBlockView.swift | 4 +++- 12 files changed, 68 insertions(+), 26 deletions(-) delete mode 100644 Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift rename Sources/phbar/Models/{Config/PHConfigText.swift => Theme/Text/PHThemeText.swift} (83%) create mode 100644 Sources/phbar/Models/Theme/Text/PHThemeTextStyle.swift rename Sources/phbar/Models/{Config/Text/PHConfigTextWeight.swift => Theme/Text/PHThemeTextWeight.swift} (95%) diff --git a/Sources/phbar/Models/Block/PHBlock.swift b/Sources/phbar/Models/Block/PHBlock.swift index 85b3a67..e147c1f 100644 --- a/Sources/phbar/Models/Block/PHBlock.swift +++ b/Sources/phbar/Models/Block/PHBlock.swift @@ -6,6 +6,8 @@ final class PHBlock: ObservableObject, Decodable, Identifiable { let command: String let name: String? + let textName: String? + @Published var text: PHThemeText = .default let styleName: String? @Published var style: PHThemeStyle = .default let refresh: Double? @@ -38,6 +40,7 @@ final class PHBlock: ObservableObject, Decodable, Identifiable { private enum CodingKeys: String, CodingKey { case command, name + case textName = "text" case styleName = "style" case refresh, events, centered } diff --git a/Sources/phbar/Models/Config/PHConfig.swift b/Sources/phbar/Models/Config/PHConfig.swift index ac24f2f..a11b0ce 100644 --- a/Sources/phbar/Models/Config/PHConfig.swift +++ b/Sources/phbar/Models/Config/PHConfig.swift @@ -1,3 +1,3 @@ struct PHConfig: Decodable { - let text: PHConfigText + let env: [String: String]? } diff --git a/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift b/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift deleted file mode 100644 index b9b075e..0000000 --- a/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift +++ /dev/null @@ -1,9 +0,0 @@ -enum PHConfigTextStyle: String, Decodable { - case normal, italic -} - -extension PHConfigText { - var italic: Bool { - style == .italic - } -} diff --git a/Sources/phbar/Models/Config/config.toml b/Sources/phbar/Models/Config/config.toml index 3d0ba84..29de895 100644 --- a/Sources/phbar/Models/Config/config.toml +++ b/Sources/phbar/Models/Config/config.toml @@ -1,6 +1,3 @@ -[text] -font = "Comic Code" -size = 14 -weight = "regular" -style = "normal" -offset = -0.5 +height = 30 + +[window] diff --git a/Sources/phbar/Models/Theme/PHTheme.swift b/Sources/phbar/Models/Theme/PHTheme.swift index f7f939f..2fdf7a8 100644 --- a/Sources/phbar/Models/Theme/PHTheme.swift +++ b/Sources/phbar/Models/Theme/PHTheme.swift @@ -1,9 +1,11 @@ struct PHTheme: Decodable { let window: PHThemeWindow + let texts: [PHThemeText]? let styles: [PHThemeStyle] private enum CodingKeys: String, CodingKey { case window + case texts = "text" case styles = "style" } } diff --git a/Sources/phbar/Models/Config/PHConfigText.swift b/Sources/phbar/Models/Theme/Text/PHThemeText.swift similarity index 83% rename from Sources/phbar/Models/Config/PHConfigText.swift rename to Sources/phbar/Models/Theme/Text/PHThemeText.swift index 9a0669b..fc3912b 100644 --- a/Sources/phbar/Models/Config/PHConfigText.swift +++ b/Sources/phbar/Models/Theme/Text/PHThemeText.swift @@ -1,22 +1,35 @@ import AppKit import SwiftUI -struct PHConfigText: Decodable { +struct PHThemeText: Decodable { + let name: String let fontFamily: String let size: Double - let weight: PHConfigTextWeight - let style: PHConfigTextStyle + let weight: PHThemeTextWeight + let style: PHThemeTextStyle let offset: Double? private enum CodingKeys: String, CodingKey { + case name case fontFamily = "font" case size, weight, style, offset } + + static let `default`: Self = { + PHThemeText( + name: "_default", + fontFamily: "monospace", + size: 14.0, + weight: .regular, + style: .normal, + offset: nil + ) + }() } // Design -extension PHConfigText { +extension PHThemeText { var design: SwiftUI.Font.Design? { switch fontFamily { case "sans": @@ -46,7 +59,7 @@ extension PHConfigText { // Font -extension PHConfigText { +extension PHThemeText { /// Construct an `NSFont` from the typeface settings. /// /// Falls back to the system font if parsing fails. diff --git a/Sources/phbar/Models/Theme/Text/PHThemeTextStyle.swift b/Sources/phbar/Models/Theme/Text/PHThemeTextStyle.swift new file mode 100644 index 0000000..94f05e2 --- /dev/null +++ b/Sources/phbar/Models/Theme/Text/PHThemeTextStyle.swift @@ -0,0 +1,9 @@ +enum PHThemeTextStyle: String, Decodable { + case normal, italic +} + +extension PHThemeText { + var italic: Bool { + style == .italic + } +} diff --git a/Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift b/Sources/phbar/Models/Theme/Text/PHThemeTextWeight.swift similarity index 95% rename from Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift rename to Sources/phbar/Models/Theme/Text/PHThemeTextWeight.swift index 1a7f5be..3e2a63c 100644 --- a/Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift +++ b/Sources/phbar/Models/Theme/Text/PHThemeTextWeight.swift @@ -1,7 +1,7 @@ import AppKit import SwiftUI -enum PHConfigTextWeight: String, Decodable { +enum PHThemeTextWeight: String, Decodable { case thin case ultraLight = "ultralight" case light diff --git a/Sources/phbar/Models/Theme/theme.toml b/Sources/phbar/Models/Theme/theme.toml index 01c526f..7430c03 100644 --- a/Sources/phbar/Models/Theme/theme.toml +++ b/Sources/phbar/Models/Theme/theme.toml @@ -6,6 +6,22 @@ shadow = false blur = 0.0 +[[text]] +name = "default" +font = "Comic Code" +size = 14 +weight = "regular" +style = "normal" +offset = -0.5 + +[[text]] +name = "italic" +font = "Comic Code" +size = 14 +weight = "regular" +style = "italic" +offset = -0.5 + [[style]] name = "default" foreground = { color = "#aed3f3" } diff --git a/Sources/phbar/Views/BarController.swift b/Sources/phbar/Views/BarController.swift index 28f44ec..e9addc5 100644 --- a/Sources/phbar/Views/BarController.swift +++ b/Sources/phbar/Views/BarController.swift @@ -29,14 +29,25 @@ final class BarController: ObservableObject { self.blocks = blocks for block in blocks { + block.text = text(for: block) block.style = style(for: block) } } } -// Style +// Theming extension BarController { + func text(for block: PHBlock) -> PHThemeText { + guard let text = theme.texts?.first(where: { $0.name == block.textName }) else { + guard let defaultText = theme.texts?.first(where: { $0.name == "default" }) else { + return PHThemeText.default + } + return defaultText + } + return text + } + func style(for block: PHBlock) -> PHThemeStyle { guard let style = theme.styles.first(where: { $0.name == block.styleName }) else { guard let defaultStyle = theme.styles.first(where: { $0.name == "default" }) else { diff --git a/Sources/phbar/Views/BarView.swift b/Sources/phbar/Views/BarView.swift index 70c004c..4631d67 100644 --- a/Sources/phbar/Views/BarView.swift +++ b/Sources/phbar/Views/BarView.swift @@ -31,8 +31,6 @@ struct BarView: View { } } } - .font(bar.config.text.font) - .italic(bar.config.text.italic) .frame(height: 30) .ignoresSafeArea() .environmentObject(bar) diff --git a/Sources/phbar/Views/TextBlockView.swift b/Sources/phbar/Views/TextBlockView.swift index 4ef076c..945695d 100644 --- a/Sources/phbar/Views/TextBlockView.swift +++ b/Sources/phbar/Views/TextBlockView.swift @@ -7,7 +7,7 @@ struct TextBlockView: View { @State var hovering = false private var adjustOffset: CGAffineTransform { - guard let offset = bar.config.text.offset else { return .init() } + guard let offset = block.text.offset else { return .init() } return .init(translationX: 0, y: offset) } @@ -19,6 +19,8 @@ struct TextBlockView: View { Group { Text(label) + .font(block.text.font) + .italic(block.text.italic) .foregroundStyle(block.style.foreground?.uiColor ?? .clear) .transformEffect(adjustOffset) .border(bar.debug ? .blue : .clear)