From 2d047d69b6e5973a6c7adb680c1496ad6a23331b Mon Sep 17 00:00:00 2001 From: Tommaso Negri Date: Sun, 12 Jul 2026 00:25:06 +0200 Subject: [PATCH] cleanup Config --- Package.swift | 2 +- .../PHConfig.swift} | 39 +++-- .../PHConfigText.swift} | 156 +++++------------- .../Config/Text/PHConfigTextStyle.swift | 9 + .../Config/Text/PHConfigTextWeight.swift | 60 +++++++ Sources/phbar/{ => Models/Config}/config.toml | 0 Sources/phbar/Models/PHConfig.swift | 3 - 7 files changed, 136 insertions(+), 133 deletions(-) rename Sources/phbar/Models/{PHConfig/PHConfig+Loading.swift => Config/PHConfig.swift} (57%) rename Sources/phbar/Models/{PHConfig/PHConfig+Text.swift => Config/PHConfigText.swift} (50%) create mode 100644 Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift create mode 100644 Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift rename Sources/phbar/{ => Models/Config}/config.toml (100%) delete mode 100644 Sources/phbar/Models/PHConfig.swift diff --git a/Package.swift b/Package.swift index 7e7ea9c..efc607b 100644 --- a/Package.swift +++ b/Package.swift @@ -32,7 +32,7 @@ let package = Package( .target(name: "phbarEvents"), ], resources: [ - .process("config.toml"), + .process("Models/Config/config.toml"), .process("Models/Theme/theme.toml"), ], ), diff --git a/Sources/phbar/Models/PHConfig/PHConfig+Loading.swift b/Sources/phbar/Models/Config/PHConfig.swift similarity index 57% rename from Sources/phbar/Models/PHConfig/PHConfig+Loading.swift rename to Sources/phbar/Models/Config/PHConfig.swift index c6672bf..73d5715 100644 --- a/Sources/phbar/Models/PHConfig/PHConfig+Loading.swift +++ b/Sources/phbar/Models/Config/PHConfig.swift @@ -1,31 +1,44 @@ import Foundation import TOML +struct PHConfig: Decodable { + let text: PHConfigText +} + +// Loading + extension PHConfig { - /// Load configuration from the default path (~/.config/pmenu/config.toml). + /// Load configuration from the default path (~/.config/phbar/config.toml). /// If the file doesn't exist or fails to parse, defaults are used (non-fatal). static func load() throws -> PHConfig { let url = FileManager.default.homeDirectoryForCurrentUser.appending( - path: ".config/phbar/config.toml") + path: ".config/phbar/config.toml" + ) if FileManager.default.fileExists(atPath: url.relativePath) { return try load(from: url) } else { - guard - let defaultConfig = Bundle.module.url( - forResource: "config", - withExtension: "toml" - )?.resolvingSymlinksInPath() - else { - throw phbar.Error("Failed to load config file") - } - return try load(from: defaultConfig) + return try loadFromBundle() } } + /// Load the bundled configuration. + /// If the file doesn't exist or fails to parse, the execution is interrupted. + static private func loadFromBundle() throws -> PHConfig { + guard + let defaultConfig = Bundle.module.url( + forResource: "config", + withExtension: "toml" + )?.resolvingSymlinksInPath() + else { + throw phbar.Error("Failed to load config file") + } + return try load(from: defaultConfig) + } + /// Decode configuration from a file at URL. /// If the file doesn't exist or fails to parse, the execution is interrupted. - static func load(from url: URL) throws -> PHConfig { + static private func load(from url: URL) throws -> PHConfig { guard let data = try? Data(contentsOf: url), let contents = String(data: data, encoding: .utf8) else { @@ -37,7 +50,7 @@ extension PHConfig { /// Decode configuration from a TOML string. /// If the content fails to parse, the execution is interrupted. - static func load(from contents: String) throws -> PHConfig { + static private func load(from contents: String) throws -> PHConfig { do { let decoder = TOMLDecoder() let configFile = try decoder.decode(PHConfig.self, from: contents) diff --git a/Sources/phbar/Models/PHConfig/PHConfig+Text.swift b/Sources/phbar/Models/Config/PHConfigText.swift similarity index 50% rename from Sources/phbar/Models/PHConfig/PHConfig+Text.swift rename to Sources/phbar/Models/Config/PHConfigText.swift index 354bc16..c7276dd 100644 --- a/Sources/phbar/Models/PHConfig/PHConfig+Text.swift +++ b/Sources/phbar/Models/Config/PHConfigText.swift @@ -1,24 +1,52 @@ import AppKit import SwiftUI -extension PHConfig { - struct Text: Decodable { - let fontFamily: String - let size: Double - let weight: Weight - let style: Style - let offset: Double? +struct PHConfigText: Decodable { + let fontFamily: String + let size: Double + let weight: PHConfigTextWeight + let style: PHConfigTextStyle + let offset: Double? - enum CodingKeys: String, CodingKey { - case fontFamily = "font" - case size, weight, style, offset + enum CodingKeys: String, CodingKey { + case fontFamily = "font" + case size, weight, style, offset + } +} + +// Design + +extension PHConfigText { + var design: SwiftUI.Font.Design? { + switch fontFamily { + case "sans": + return .default + case "monospace": + return .monospaced + case "serif": + return .serif + default: + return nil + } + } + + var systemDesign: NSFontDescriptor.SystemDesign? { + switch fontFamily { + case "sans": + return .default + case "monospace": + return .monospaced + case "serif": + return .serif + default: + return nil } } } // Font -extension PHConfig.Text { +extension PHConfigText { /// Construct an `NSFont` from the typeface settings. /// /// Falls back to the system font if parsing fails. @@ -48,7 +76,7 @@ extension PHConfig.Text { /// Construct a `Font` from the typeface settings. /// /// Falls back to the system font if parsing fails. - var font: Font { + var font: SwiftUI.Font { if let design { return Font.system(size: size, weight: weight.uiWeight, design: design) } else { @@ -56,107 +84,3 @@ extension PHConfig.Text { } } } - -// Design - -extension PHConfig.Text { - var design: SwiftUI.Font.Design? { - switch fontFamily { - case "sans": - return .default - case "monospace": - return .monospaced - case "serif": - return .serif - default: - return nil - } - } - - var systemDesign: NSFontDescriptor.SystemDesign? { - switch fontFamily { - case "sans": - return .default - case "monospace": - return .monospaced - case "serif": - return .serif - default: - return nil - } - } -} - -// Weight - -extension PHConfig.Text { - enum Weight: String, Decodable { - case thin - case ultraLight = "ultralight" - case light - case regular - case medium - case semiBold = "semibold" - case bold - case heavy - case black - - var uiWeight: SwiftUI.Font.Weight { - switch self { - case .thin: - return .thin - case .ultraLight: - return .ultraLight - case .light: - return .light - case .regular: - return .regular - case .medium: - return .medium - case .semiBold: - return .semibold - case .bold: - return .bold - case .heavy: - return .heavy - case .black: - return .black - } - } - - var nsWeight: NSFont.Weight { - switch self { - case .thin: - return .thin - case .ultraLight: - return .ultraLight - case .light: - return .light - case .regular: - return .regular - case .medium: - return .medium - case .semiBold: - return .semibold - case .bold: - return .bold - case .heavy: - return .heavy - case .black: - return .black - } - } - } -} - -// Style - -extension PHConfig.Text { - enum Style: String, Decodable { - case normal, italic - } - - var italic: Bool { - style == .italic - } -} diff --git a/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift b/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift new file mode 100644 index 0000000..b9b075e --- /dev/null +++ b/Sources/phbar/Models/Config/Text/PHConfigTextStyle.swift @@ -0,0 +1,9 @@ +enum PHConfigTextStyle: String, Decodable { + case normal, italic +} + +extension PHConfigText { + var italic: Bool { + style == .italic + } +} diff --git a/Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift b/Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift new file mode 100644 index 0000000..1a7f5be --- /dev/null +++ b/Sources/phbar/Models/Config/Text/PHConfigTextWeight.swift @@ -0,0 +1,60 @@ +import AppKit +import SwiftUI + +enum PHConfigTextWeight: String, Decodable { + case thin + case ultraLight = "ultralight" + case light + case regular + case medium + case semiBold = "semibold" + case bold + case heavy + case black + + var uiWeight: SwiftUI.Font.Weight { + switch self { + case .thin: + return .thin + case .ultraLight: + return .ultraLight + case .light: + return .light + case .regular: + return .regular + case .medium: + return .medium + case .semiBold: + return .semibold + case .bold: + return .bold + case .heavy: + return .heavy + case .black: + return .black + } + } + + var nsWeight: NSFont.Weight { + switch self { + case .thin: + return .thin + case .ultraLight: + return .ultraLight + case .light: + return .light + case .regular: + return .regular + case .medium: + return .medium + case .semiBold: + return .semibold + case .bold: + return .bold + case .heavy: + return .heavy + case .black: + return .black + } + } +} diff --git a/Sources/phbar/config.toml b/Sources/phbar/Models/Config/config.toml similarity index 100% rename from Sources/phbar/config.toml rename to Sources/phbar/Models/Config/config.toml diff --git a/Sources/phbar/Models/PHConfig.swift b/Sources/phbar/Models/PHConfig.swift deleted file mode 100644 index 9abbf91..0000000 --- a/Sources/phbar/Models/PHConfig.swift +++ /dev/null @@ -1,3 +0,0 @@ -struct PHConfig: Decodable { - let text: Text -}