Compare commits

...

2 Commits

Author SHA1 Message Date
tommaso 2d047d69b6 cleanup Config 2026-07-12 00:25:06 +02:00
tommaso a872ca1d6b improve code style 2026-07-12 00:24:34 +02:00
9 changed files with 142 additions and 139 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ let package = Package(
.target(name: "phbarEvents"),
],
resources: [
.process("config.toml"),
.process("Models/Config/config.toml"),
.process("Models/Theme/theme.toml"),
],
),
@@ -1,16 +1,30 @@
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 {
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",
@@ -21,11 +35,10 @@ extension PHConfig {
}
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)
@@ -1,12 +1,11 @@
import AppKit
import SwiftUI
extension PHConfig {
struct Text: Decodable {
struct PHConfigText: Decodable {
let fontFamily: String
let size: Double
let weight: Weight
let style: Style
let weight: PHConfigTextWeight
let style: PHConfigTextStyle
let offset: Double?
enum CodingKeys: String, CodingKey {
@@ -14,11 +13,40 @@ extension PHConfig {
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
}
}
@@ -0,0 +1,9 @@
enum PHConfigTextStyle: String, Decodable {
case normal, italic
}
extension PHConfigText {
var italic: Bool {
style == .italic
}
}
@@ -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
}
}
}
-3
View File
@@ -1,3 +0,0 @@
struct PHConfig: Decodable {
let text: Text
}
+1 -1
View File
@@ -31,7 +31,7 @@ extension PHTheme {
}
/// Load the bundled theme.
/// If the theme doesn't exist or fails to parse, the execution is interrupted.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func loadFromBundle() throws -> PHTheme {
guard
let defaultConfig = Bundle.module.url(