cleanup Config
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ let package = Package(
|
|||||||
.target(name: "phbarEvents"),
|
.target(name: "phbarEvents"),
|
||||||
],
|
],
|
||||||
resources: [
|
resources: [
|
||||||
.process("config.toml"),
|
.process("Models/Config/config.toml"),
|
||||||
.process("Models/Theme/theme.toml"),
|
.process("Models/Theme/theme.toml"),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
+18
-5
@@ -1,16 +1,30 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import TOML
|
import TOML
|
||||||
|
|
||||||
|
struct PHConfig: Decodable {
|
||||||
|
let text: PHConfigText
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loading
|
||||||
|
|
||||||
extension PHConfig {
|
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).
|
/// If the file doesn't exist or fails to parse, defaults are used (non-fatal).
|
||||||
static func load() throws -> PHConfig {
|
static func load() throws -> PHConfig {
|
||||||
let url = FileManager.default.homeDirectoryForCurrentUser.appending(
|
let url = FileManager.default.homeDirectoryForCurrentUser.appending(
|
||||||
path: ".config/phbar/config.toml")
|
path: ".config/phbar/config.toml"
|
||||||
|
)
|
||||||
|
|
||||||
if FileManager.default.fileExists(atPath: url.relativePath) {
|
if FileManager.default.fileExists(atPath: url.relativePath) {
|
||||||
return try load(from: url)
|
return try load(from: url)
|
||||||
} else {
|
} 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
|
guard
|
||||||
let defaultConfig = Bundle.module.url(
|
let defaultConfig = Bundle.module.url(
|
||||||
forResource: "config",
|
forResource: "config",
|
||||||
@@ -21,11 +35,10 @@ extension PHConfig {
|
|||||||
}
|
}
|
||||||
return try load(from: defaultConfig)
|
return try load(from: defaultConfig)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Decode configuration from a file at URL.
|
/// Decode configuration from a file at URL.
|
||||||
/// If the file 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 func load(from url: URL) throws -> PHConfig {
|
static private func load(from url: URL) throws -> PHConfig {
|
||||||
guard let data = try? Data(contentsOf: url),
|
guard let data = try? Data(contentsOf: url),
|
||||||
let contents = String(data: data, encoding: .utf8)
|
let contents = String(data: data, encoding: .utf8)
|
||||||
else {
|
else {
|
||||||
@@ -37,7 +50,7 @@ extension PHConfig {
|
|||||||
|
|
||||||
/// Decode configuration from a TOML string.
|
/// Decode configuration from a TOML string.
|
||||||
/// If the content fails to parse, the execution is interrupted.
|
/// 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 {
|
do {
|
||||||
let decoder = TOMLDecoder()
|
let decoder = TOMLDecoder()
|
||||||
let configFile = try decoder.decode(PHConfig.self, from: contents)
|
let configFile = try decoder.decode(PHConfig.self, from: contents)
|
||||||
+34
-110
@@ -1,24 +1,52 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
extension PHConfig {
|
struct PHConfigText: Decodable {
|
||||||
struct Text: Decodable {
|
|
||||||
let fontFamily: String
|
let fontFamily: String
|
||||||
let size: Double
|
let size: Double
|
||||||
let weight: Weight
|
let weight: PHConfigTextWeight
|
||||||
let style: Style
|
let style: PHConfigTextStyle
|
||||||
let offset: Double?
|
let offset: Double?
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case fontFamily = "font"
|
case fontFamily = "font"
|
||||||
case size, weight, style, offset
|
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
|
// Font
|
||||||
|
|
||||||
extension PHConfig.Text {
|
extension PHConfigText {
|
||||||
/// Construct an `NSFont` from the typeface settings.
|
/// Construct an `NSFont` from the typeface settings.
|
||||||
///
|
///
|
||||||
/// Falls back to the system font if parsing fails.
|
/// Falls back to the system font if parsing fails.
|
||||||
@@ -48,7 +76,7 @@ extension PHConfig.Text {
|
|||||||
/// Construct a `Font` from the typeface settings.
|
/// Construct a `Font` from the typeface settings.
|
||||||
///
|
///
|
||||||
/// Falls back to the system font if parsing fails.
|
/// Falls back to the system font if parsing fails.
|
||||||
var font: Font {
|
var font: SwiftUI.Font {
|
||||||
if let design {
|
if let design {
|
||||||
return Font.system(size: size, weight: weight.uiWeight, design: design)
|
return Font.system(size: size, weight: weight.uiWeight, design: design)
|
||||||
} else {
|
} 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