121 lines
3.7 KiB
Swift
121 lines
3.7 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
enum TriggerMode: String, CaseIterable, Identifiable {
|
|
case toggle
|
|
case hold
|
|
|
|
var id: String { rawValue }
|
|
var title: String {
|
|
switch self {
|
|
case .toggle: "按一次开始,再按一次结束"
|
|
case .hold: "按住说话,松开结束"
|
|
}
|
|
}
|
|
}
|
|
|
|
enum InsertMethod: String, CaseIterable, Identifiable {
|
|
case auto
|
|
case axOnly
|
|
case paste
|
|
|
|
var id: String { rawValue }
|
|
var title: String {
|
|
switch self {
|
|
case .auto: "直接输入(不使用剪贴板)"
|
|
case .axOnly: "仅 AX 直接写入"
|
|
case .paste: "始终使用粘贴"
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
final class Prefs: ObservableObject {
|
|
static let shared = Prefs()
|
|
|
|
private let defaults = UserDefaults.standard
|
|
|
|
@Published var hotkey: HotkeySpec {
|
|
didSet {
|
|
if let data = try? JSONEncoder().encode(hotkey) {
|
|
defaults.set(data, forKey: Keys.hotkey)
|
|
}
|
|
NotificationCenter.default.post(name: .tinysttHotkeyChanged, object: nil)
|
|
}
|
|
}
|
|
|
|
@Published var triggerMode: TriggerMode {
|
|
didSet { defaults.set(triggerMode.rawValue, forKey: Keys.triggerMode) }
|
|
}
|
|
|
|
@Published var insertMethod: InsertMethod {
|
|
didSet { defaults.set(insertMethod.rawValue, forKey: Keys.insertMethod) }
|
|
}
|
|
|
|
@Published var autoPeriod: Bool {
|
|
didSet { defaults.set(autoPeriod, forKey: Keys.autoPeriod) }
|
|
}
|
|
|
|
@Published var showHUD: Bool {
|
|
didSet { defaults.set(showHUD, forKey: Keys.showHUD) }
|
|
}
|
|
|
|
@Published var playSound: Bool {
|
|
didSet { defaults.set(playSound, forKey: Keys.playSound) }
|
|
}
|
|
|
|
@Published var languageCode: String {
|
|
didSet { defaults.set(languageCode, forKey: Keys.languageCode) }
|
|
}
|
|
|
|
@Published var silenceTimeout: Bool {
|
|
didSet { defaults.set(silenceTimeout, forKey: "silenceTimeout") }
|
|
}
|
|
|
|
@Published var compactIcon: Bool {
|
|
didSet { defaults.set(compactIcon, forKey: "compactIcon") }
|
|
}
|
|
|
|
@Published var hudAtBottom: Bool {
|
|
didSet { defaults.set(hudAtBottom, forKey: "hudAtBottom") }
|
|
}
|
|
|
|
@Published var engineChoice: String {
|
|
didSet { defaults.set(engineChoice, forKey: "engineChoice") }
|
|
}
|
|
|
|
private enum Keys {
|
|
static let hotkey = "hotkey"
|
|
static let triggerMode = "triggerMode"
|
|
static let insertMethod = "insertMethod"
|
|
static let autoPeriod = "autoPeriod"
|
|
static let showHUD = "showHUD"
|
|
static let playSound = "playSound"
|
|
static let languageCode = "languageCode"
|
|
}
|
|
|
|
private init() {
|
|
if let data = defaults.data(forKey: Keys.hotkey),
|
|
let spec = try? JSONDecoder().decode(HotkeySpec.self, from: data) {
|
|
hotkey = spec
|
|
} else {
|
|
hotkey = .rightOption
|
|
}
|
|
triggerMode = TriggerMode(rawValue: defaults.string(forKey: Keys.triggerMode) ?? "") ?? .toggle
|
|
insertMethod = InsertMethod(rawValue: defaults.string(forKey: Keys.insertMethod) ?? "") ?? .auto
|
|
autoPeriod = defaults.object(forKey: Keys.autoPeriod) as? Bool ?? true
|
|
showHUD = defaults.object(forKey: Keys.showHUD) as? Bool ?? true
|
|
playSound = defaults.object(forKey: Keys.playSound) as? Bool ?? true
|
|
engineChoice = defaults.string(forKey: "engineChoice") ?? "auto"
|
|
silenceTimeout = defaults.object(forKey: "silenceTimeout") as? Bool ?? true
|
|
compactIcon = defaults.object(forKey: "compactIcon") as? Bool ?? true
|
|
hudAtBottom = defaults.bool(forKey: "hudAtBottom")
|
|
languageCode = "zh-CN"
|
|
}
|
|
}
|
|
|
|
extension Notification.Name {
|
|
static let tinysttHotkeyChanged = Notification.Name("tinystt.hotkeyChanged")
|
|
static let tinysttOpenSettings = Notification.Name("tinystt.openSettings")
|
|
}
|