Files
uskey/Sources/Config.swift
loveuer 6285cabdd1
Some checks failed
Build and Release / Build DMG for arm64 (push) Has been cancelled
Build and Release / Build DMG for x86_64 (push) Has been cancelled
Build and Release / Create Release (push) Has been cancelled
Major improvements and bug fixes for v1.1.0
Features:
- Add auto-enable on startup based on config
- Add enabled state persistence in config.json
- Add limitations notice in GUI menu
- Simplify logging to single file (uskey.log)
- Change "Open Logs Folder" to "Open Config Folder"

Bug Fixes:
- Fix GUI status display mismatch on startup
- Fix first toggle click not working issue
- Revert password field changes that caused key blocking

UI Changes:
- Add ⚠️ emoji to "About Limitations" menu item
- Remove button action to fix menu refresh

🤖 Generated with [Qoder][https://qoder.com]
2025-12-05 13:48:15 +08:00

103 lines
3.0 KiB
Swift

@preconcurrency import Foundation
enum LogLevel: String, Codable {
case debug
case info
case warning
case error
func shouldLog(_ level: LogLevel) -> Bool {
let levels: [LogLevel] = [.debug, .info, .warning, .error]
guard let currentIndex = levels.firstIndex(of: self),
let targetIndex = levels.firstIndex(of: level) else {
return false
}
return targetIndex >= currentIndex
}
}
struct LogConfig: Codable {
let level: LogLevel
init(level: LogLevel = .info) {
self.level = level
}
}
struct KeyMapping: Codable {
let from: Int64
let to: Int64
}
struct MappingConfig: Codable {
private let mappings: [String: KeyMapping]
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
mappings = try container.decode([String: KeyMapping].self)
}
init(mappings: [String: KeyMapping] = [:]) {
self.mappings = mappings
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(mappings)
}
func getAllMappings() -> [(Int64, Int64)] {
return mappings.values.map { ($0.from, $0.to) }
}
}
struct Config: Codable {
let log: LogConfig
let mapping: MappingConfig
let enabled: Bool
init(log: LogConfig = LogConfig(), mapping: MappingConfig = MappingConfig(), enabled: Bool = true) {
self.log = log
self.mapping = mapping
self.enabled = enabled
}
static func load(from path: String) throws -> Config {
let url = URL(fileURLWithPath: path)
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
return try decoder.decode(Config.self, from: data)
}
static func save(_ config: Config, to path: String) throws {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let data = try encoder.encode(config)
try data.write(to: URL(fileURLWithPath: path))
}
static func createDefault(at path: String) throws {
let defaultConfig = Config(
log: LogConfig(level: .info),
mapping: MappingConfig(mappings: [
"backslash2backspace": KeyMapping(from: 42, to: 51),
"backspace2backslash": KeyMapping(from: 51, to: 42)
]),
enabled: true
)
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let data = try encoder.encode(defaultConfig)
try data.write(to: URL(fileURLWithPath: path))
}
static func getConfigPath() -> String {
let homeDir = FileManager.default.homeDirectoryForCurrentUser
let configDir = homeDir.appendingPathComponent(".config/uskey")
try? FileManager.default.createDirectory(at: configDir, withIntermediateDirectories: true)
return configDir.appendingPathComponent("config.json").path
}
}