move window definition to theme and make it dynamic
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
struct PHConfig: Decodable {
|
||||
let window: String
|
||||
let env: [String: String]?
|
||||
}
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
window = "top"
|
||||
height = 30
|
||||
|
||||
[window]
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
struct PHTheme: Decodable {
|
||||
let window: PHThemeWindow
|
||||
let windows: [PHThemeWindow]?
|
||||
let texts: [PHThemeText]?
|
||||
let styles: [PHThemeStyle]?
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case window
|
||||
case windows = "window"
|
||||
case texts = "text"
|
||||
case styles = "style"
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
struct PHThemeWindow: Decodable {
|
||||
let hasShadow: Bool
|
||||
let blur: Double
|
||||
let name: String
|
||||
var hasShadow: Bool? = false
|
||||
var blur: Double? = 0
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case name
|
||||
case hasShadow = "shadow"
|
||||
case blur
|
||||
}
|
||||
|
||||
static let `default`: Self = {
|
||||
PHThemeWindow(
|
||||
name: "_default",
|
||||
hasShadow: false,
|
||||
blur: 0.0
|
||||
)
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,13 @@
|
||||
#
|
||||
# Place at ~/.config/phbar/themes/voltage.toml
|
||||
|
||||
[window]
|
||||
[[window]]
|
||||
name = "default"
|
||||
shadow = false
|
||||
blur = 0.0
|
||||
|
||||
[[window]]
|
||||
name = "bottom"
|
||||
shadow = false
|
||||
blur = 0.0
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import AppKit
|
||||
|
||||
final class BackgroundView: NSView {
|
||||
var hasShadow: Bool {
|
||||
var hasShadow: Bool? = false {
|
||||
didSet { configureShadow() }
|
||||
}
|
||||
|
||||
var blur: CGFloat {
|
||||
var blur: CGFloat? = 0 {
|
||||
didSet { configureBlur() }
|
||||
}
|
||||
|
||||
init(controller: BarController) {
|
||||
self.hasShadow = controller.theme.window.hasShadow
|
||||
self.blur = controller.theme.window.blur
|
||||
if let hasShadow = controller.window.hasShadow { self.hasShadow = hasShadow }
|
||||
if let blur = controller.window.blur { self.blur = blur }
|
||||
super.init(frame: .zero)
|
||||
setup()
|
||||
}
|
||||
@@ -36,12 +36,12 @@ final class BackgroundView: NSView {
|
||||
}
|
||||
|
||||
private func configureShadow() {
|
||||
guard let window else { return }
|
||||
guard let window, let hasShadow else { return }
|
||||
window.hasShadow = hasShadow
|
||||
}
|
||||
|
||||
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
|
||||
// to be visible at all, let alone blurred.
|
||||
window.isOpaque = false
|
||||
|
||||
@@ -5,6 +5,7 @@ final class BarController: ObservableObject {
|
||||
let config: PHConfig
|
||||
let screen: NSScreen
|
||||
let theme: PHTheme
|
||||
let window: PHThemeWindow
|
||||
@Published var blocks: [PHBlock]
|
||||
|
||||
var arrangedBlocks: [PHBlock] {
|
||||
@@ -19,6 +20,7 @@ final class BarController: ObservableObject {
|
||||
self.config = config
|
||||
self.screen = screen
|
||||
self.theme = theme
|
||||
self.window = Self.window(for: config, from: theme)
|
||||
self.blocks = blocks
|
||||
|
||||
for block in blocks {
|
||||
@@ -32,6 +34,16 @@ final class BarController: ObservableObject {
|
||||
// Theming
|
||||
|
||||
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 {
|
||||
guard let text = theme.texts?.first(where: { $0.name == block.textName }) else {
|
||||
guard let defaultText = theme.texts?.first(where: { $0.name == "default" }) else {
|
||||
|
||||
Reference in New Issue
Block a user