move window definition to theme and make it dynamic

This commit is contained in:
2026-07-12 11:58:01 +02:00
parent 6e258fdffa
commit 34921ca377
7 changed files with 41 additions and 13 deletions
@@ -1,3 +1,4 @@
struct PHConfig: Decodable { struct PHConfig: Decodable {
let window: String
let env: [String: String]? let env: [String: String]?
} }
+1 -2
View File
@@ -1,3 +1,2 @@
window = "top"
height = 30 height = 30
[window]
+2 -2
View File
@@ -1,10 +1,10 @@
struct PHTheme: Decodable { struct PHTheme: Decodable {
let window: PHThemeWindow let windows: [PHThemeWindow]?
let texts: [PHThemeText]? let texts: [PHThemeText]?
let styles: [PHThemeStyle]? let styles: [PHThemeStyle]?
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case window case windows = "window"
case texts = "text" case texts = "text"
case styles = "style" case styles = "style"
} }
+12 -2
View File
@@ -1,9 +1,19 @@
struct PHThemeWindow: Decodable { struct PHThemeWindow: Decodable {
let hasShadow: Bool let name: String
let blur: Double var hasShadow: Bool? = false
var blur: Double? = 0
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case name
case hasShadow = "shadow" case hasShadow = "shadow"
case blur case blur
} }
static let `default`: Self = {
PHThemeWindow(
name: "_default",
hasShadow: false,
blur: 0.0
)
}()
} }
+7 -1
View File
@@ -2,7 +2,13 @@
# #
# Place at ~/.config/phbar/themes/voltage.toml # Place at ~/.config/phbar/themes/voltage.toml
[window] [[window]]
name = "default"
shadow = false
blur = 0.0
[[window]]
name = "bottom"
shadow = false shadow = false
blur = 0.0 blur = 0.0
+6 -6
View File
@@ -1,17 +1,17 @@
import AppKit import AppKit
final class BackgroundView: NSView { final class BackgroundView: NSView {
var hasShadow: Bool { var hasShadow: Bool? = false {
didSet { configureShadow() } didSet { configureShadow() }
} }
var blur: CGFloat { var blur: CGFloat? = 0 {
didSet { configureBlur() } didSet { configureBlur() }
} }
init(controller: BarController) { init(controller: BarController) {
self.hasShadow = controller.theme.window.hasShadow if let hasShadow = controller.window.hasShadow { self.hasShadow = hasShadow }
self.blur = controller.theme.window.blur if let blur = controller.window.blur { self.blur = blur }
super.init(frame: .zero) super.init(frame: .zero)
setup() setup()
} }
@@ -36,12 +36,12 @@ final class BackgroundView: NSView {
} }
private func configureShadow() { private func configureShadow() {
guard let window else { return } guard let window, let hasShadow else { return }
window.hasShadow = hasShadow window.hasShadow = hasShadow
} }
private func configureBlur() { private func configureBlur() {
guard let window else { return } guard let window, let blur else { return }
// Window must be non-opaque for the desktop/other windows behind it // Window must be non-opaque for the desktop/other windows behind it
// to be visible at all, let alone blurred. // to be visible at all, let alone blurred.
window.isOpaque = false window.isOpaque = false
+12
View File
@@ -5,6 +5,7 @@ final class BarController: ObservableObject {
let config: PHConfig let config: PHConfig
let screen: NSScreen let screen: NSScreen
let theme: PHTheme let theme: PHTheme
let window: PHThemeWindow
@Published var blocks: [PHBlock] @Published var blocks: [PHBlock]
var arrangedBlocks: [PHBlock] { var arrangedBlocks: [PHBlock] {
@@ -19,6 +20,7 @@ final class BarController: ObservableObject {
self.config = config self.config = config
self.screen = screen self.screen = screen
self.theme = theme self.theme = theme
self.window = Self.window(for: config, from: theme)
self.blocks = blocks self.blocks = blocks
for block in blocks { for block in blocks {
@@ -32,6 +34,16 @@ final class BarController: ObservableObject {
// Theming // Theming
extension BarController { extension BarController {
static func window(for config: PHConfig, from theme: PHTheme) -> PHThemeWindow {
guard let window = theme.windows?.first(where: { $0.name == config.window }) else {
guard let defaultWindow = theme.windows?.first(where: { $0.name == "default" }) else {
return PHThemeWindow.default
}
return defaultWindow
}
return window
}
func text(for block: PHBlock) -> PHThemeText { func text(for block: PHBlock) -> PHThemeText {
guard let text = theme.texts?.first(where: { $0.name == block.textName }) else { guard let text = theme.texts?.first(where: { $0.name == block.textName }) else {
guard let defaultText = theme.texts?.first(where: { $0.name == "default" }) else { guard let defaultText = theme.texts?.first(where: { $0.name == "default" }) else {