move text definition to theme and make it dynamic
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,3 @@
|
||||
[text]
|
||||
font = "Comic Code"
|
||||
size = 14
|
||||
weight = "regular"
|
||||
style = "normal"
|
||||
offset = -0.5
|
||||
height = 30
|
||||
|
||||
[window]
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
+18
-5
@@ -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.
|
||||
@@ -0,0 +1,9 @@
|
||||
enum PHThemeTextStyle: String, Decodable {
|
||||
case normal, italic
|
||||
}
|
||||
|
||||
extension PHThemeText {
|
||||
var italic: Bool {
|
||||
style == .italic
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
enum PHConfigTextWeight: String, Decodable {
|
||||
enum PHThemeTextWeight: String, Decodable {
|
||||
case thin
|
||||
case ultraLight = "ultralight"
|
||||
case light
|
||||
@@ -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" }
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -31,8 +31,6 @@ struct BarView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.font(bar.config.text.font)
|
||||
.italic(bar.config.text.italic)
|
||||
.frame(height: 30)
|
||||
.ignoresSafeArea()
|
||||
.environmentObject(bar)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user