cleanup Config
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ let package = Package(
|
||||
.target(name: "phbarEvents"),
|
||||
],
|
||||
resources: [
|
||||
.process("config.toml"),
|
||||
.process("Models/Config/config.toml"),
|
||||
.process("Models/Theme/theme.toml"),
|
||||
],
|
||||
),
|
||||
|
||||
+18
-5
@@ -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)
|
||||
+34
-110
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
struct PHConfig: Decodable {
|
||||
let text: Text
|
||||
}
|
||||
Reference in New Issue
Block a user