move text definition to theme and make it dynamic

This commit is contained in:
2026-07-12 11:34:52 +02:00
parent 7c0551c662
commit 70a170f544
12 changed files with 68 additions and 26 deletions
+3
View File
@@ -6,6 +6,8 @@ final class PHBlock: ObservableObject, Decodable, Identifiable {
let command: String let command: String
let name: String? let name: String?
let textName: String?
@Published var text: PHThemeText = .default
let styleName: String? let styleName: String?
@Published var style: PHThemeStyle = .default @Published var style: PHThemeStyle = .default
let refresh: Double? let refresh: Double?
@@ -38,6 +40,7 @@ final class PHBlock: ObservableObject, Decodable, Identifiable {
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case command, name case command, name
case textName = "text"
case styleName = "style" case styleName = "style"
case refresh, events, centered case refresh, events, centered
} }
+1 -1
View File
@@ -1,3 +1,3 @@
struct PHConfig: Decodable { struct PHConfig: Decodable {
let text: PHConfigText let env: [String: String]?
} }
@@ -1,9 +0,0 @@
enum PHConfigTextStyle: String, Decodable {
case normal, italic
}
extension PHConfigText {
var italic: Bool {
style == .italic
}
}
+3 -6
View File
@@ -1,6 +1,3 @@
[text] height = 30
font = "Comic Code"
size = 14 [window]
weight = "regular"
style = "normal"
offset = -0.5
+2
View File
@@ -1,9 +1,11 @@
struct PHTheme: Decodable { struct PHTheme: Decodable {
let window: PHThemeWindow let window: PHThemeWindow
let texts: [PHThemeText]?
let styles: [PHThemeStyle] let styles: [PHThemeStyle]
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case window case window
case texts = "text"
case styles = "style" case styles = "style"
} }
} }
@@ -1,22 +1,35 @@
import AppKit import AppKit
import SwiftUI import SwiftUI
struct PHConfigText: Decodable { struct PHThemeText: Decodable {
let name: String
let fontFamily: String let fontFamily: String
let size: Double let size: Double
let weight: PHConfigTextWeight let weight: PHThemeTextWeight
let style: PHConfigTextStyle let style: PHThemeTextStyle
let offset: Double? let offset: Double?
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case name
case fontFamily = "font" case fontFamily = "font"
case size, weight, style, offset case size, weight, style, offset
} }
static let `default`: Self = {
PHThemeText(
name: "_default",
fontFamily: "monospace",
size: 14.0,
weight: .regular,
style: .normal,
offset: nil
)
}()
} }
// Design // Design
extension PHConfigText { extension PHThemeText {
var design: SwiftUI.Font.Design? { var design: SwiftUI.Font.Design? {
switch fontFamily { switch fontFamily {
case "sans": case "sans":
@@ -46,7 +59,7 @@ extension PHConfigText {
// Font // Font
extension PHConfigText { extension PHThemeText {
/// Construct an `NSFont` from the typeface settings. /// Construct an `NSFont` from the typeface settings.
/// ///
/// Falls back to the system font if parsing fails. /// Falls back to the system font if parsing fails.
@@ -0,0 +1,9 @@
enum PHThemeTextStyle: String, Decodable {
case normal, italic
}
extension PHThemeText {
var italic: Bool {
style == .italic
}
}
@@ -1,7 +1,7 @@
import AppKit import AppKit
import SwiftUI import SwiftUI
enum PHConfigTextWeight: String, Decodable { enum PHThemeTextWeight: String, Decodable {
case thin case thin
case ultraLight = "ultralight" case ultraLight = "ultralight"
case light case light
+16
View File
@@ -6,6 +6,22 @@
shadow = false shadow = false
blur = 0.0 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]] [[style]]
name = "default" name = "default"
foreground = { color = "#aed3f3" } foreground = { color = "#aed3f3" }
+12 -1
View File
@@ -29,14 +29,25 @@ final class BarController: ObservableObject {
self.blocks = blocks self.blocks = blocks
for block in blocks { for block in blocks {
block.text = text(for: block)
block.style = style(for: block) block.style = style(for: block)
} }
} }
} }
// Style // Theming
extension BarController { 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 { func style(for block: PHBlock) -> PHThemeStyle {
guard let style = theme.styles.first(where: { $0.name == block.styleName }) else { guard let style = theme.styles.first(where: { $0.name == block.styleName }) else {
guard let defaultStyle = theme.styles.first(where: { $0.name == "default" }) else { guard let defaultStyle = theme.styles.first(where: { $0.name == "default" }) else {
-2
View File
@@ -31,8 +31,6 @@ struct BarView: View {
} }
} }
} }
.font(bar.config.text.font)
.italic(bar.config.text.italic)
.frame(height: 30) .frame(height: 30)
.ignoresSafeArea() .ignoresSafeArea()
.environmentObject(bar) .environmentObject(bar)
+3 -1
View File
@@ -7,7 +7,7 @@ struct TextBlockView: View {
@State var hovering = false @State var hovering = false
private var adjustOffset: CGAffineTransform { 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) return .init(translationX: 0, y: offset)
} }
@@ -19,6 +19,8 @@ struct TextBlockView: View {
Group { Group {
Text(label) Text(label)
.font(block.text.font)
.italic(block.text.italic)
.foregroundStyle(block.style.foreground?.uiColor ?? .clear) .foregroundStyle(block.style.foreground?.uiColor ?? .clear)
.transformEffect(adjustOffset) .transformEffect(adjustOffset)
.border(bar.debug ? .blue : .clear) .border(bar.debug ? .blue : .clear)