// // KMPreferenceManager.swift // PDF Reader Pro // // Created by tangchao on 2023/2/3. // import Cocoa enum KMPreferenceGroup: String { case general, display, markup, infomation, other } private let KMPreferenceInfoKey: String = "KMPreferenceInfoKey" typealias KMPreferenceKey = String /// general /// files private let KMCloseFilePromptTypeKey: KMPreferenceKey = "KMCloseFilePromptTypeKey" /// open image file private let KMOpenImageFileTypeKey: KMPreferenceKey = "KMOpenImageFileTypeKey" /// save password private let KMGeneralAuthorNameKey: KMPreferenceKey = "KMGeneralAuthorNameKey" /// 偏好设置已改变 private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName" private let KMDefaultFontName = "Helvetica-Oblique" var TeXEditors: [[String : Any]] = [] let NAME_KEY = "name" var INITIALUSERDEFAULTS_KEY = "InitialUserDefaults" var TEXEDITORS_KEY = "TeXEditors" var COMMAND_KEY = "command" var ARGUMENTS_KEY = "arguments" typealias KMPreference = KMPreferenceManager @objcMembers class KMPreferenceManager: NSObject { static let shared = KMPreferenceManager() private var generalGroupKeys: Array = [] private var displayGroupKeys: Array = [] private var markupGroupKeys: Array = [] public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName) /// general /// files public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey /// open image file public static let openImageFileTypeKey = KMOpenImageFileTypeKey /// save password public static let generalAuthorNameKey = KMGeneralAuthorNameKey override init() { super.init() } public func resetData(_ group: KMPreferenceGroup) { } // MARK: 注册 key public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool { if (group == .general) { if (self.generalGroupKeys.contains(key)) { return false } self.generalGroupKeys.append(key) } return true } // MARK: 获取信息 private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup { if (self.generalGroupKeys.contains(key)) { return .general } else if (self.displayGroupKeys.contains(key)) { return .display } else if (self.markupGroupKeys.contains(key)) { return .markup } return .other } } // MARK: - 默认值 extension KMPreferenceManager { // General var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType { get { return .inSameWindow } } } // MARK: 扩展 General extension KMPreferenceManager { var closeFilePromptType: KMPreferenceCloseFilePromptType { get { return .noPromp } set { if (newValue == .promp || newValue == .noPromp) { } } } var openImageFileType: Int { get { return 0 } set { } } var author: String { get { return KMDataManager.ud_string(forKey: KMUserNameKey) ?? NSFullUserName() } set { } } var openDocumentType: KMPreferenceOpenDocumentType { // KMOpenDocumentInTab get { guard let num = KMDataManager.ud_object(forKey: KMOpenDocumentInTabKey) as? NSNumber else { return self.openDocumentTypeDefaultValue } if num.intValue == 1 { return .newWindow } return .inSameWindow } set { if (newValue == .inSameWindow || newValue == .newWindow) { } } } var keepSnapshotWindowToTop: Bool { get { return KMDataManager.ud_bool(forKey: KMSnapshotsOnTopKey) } set { // 数据兼容 } } } // MARK: UserDefaults fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath" fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths" fileprivate let kKMViewSettingKey = "viewSetting" fileprivate let kKMPageNumberKey = "pageNumber" fileprivate let kKMPageScaleKey = "pageScale" extension KMPreferenceManager { var lastOpenFilepath: String? { get { return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String } set { UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey) UserDefaults.standard.synchronize() } } var lastOpenFilepaths: [String]? { get { return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String] } set { UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys) UserDefaults.standard.synchronize() } } func setPageNumber(_ number: Int, forKey key: String) { UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)") UserDefaults.standard.synchronize() } func getPageNumber(forKey key: String) -> Int? { return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int } func setPageScale(_ scale: Float, forKey key: String) { UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)") UserDefaults.standard.synchronize() } func getPageScale(forKey key: String) -> Float? { return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float } } // MARK: - Grouping extension KMPreferenceManager { var grouping: Bool { get { return false } } private static var _groupingInfosKey = "KMGroupingInfosKey" private var _grouping_infos: [KMPreferenceKey : Any] { get { return (objc_getAssociatedObject(self, &Self._groupingInfosKey) as? [KMPreferenceKey : Any]) ?? [:] } set { objc_setAssociatedObject(self, &Self._groupingInfosKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } private static var _groupingObjectsKey = "KMGroupingObjectsKey" private var _grouping_objects: Set { get { return (objc_getAssociatedObject(self, &Self._groupingObjectsKey) as? Set) ?? [] } set { objc_setAssociatedObject(self, &Self._groupingObjectsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } private static var _groupingKey = "KMGroupingKey" private var _grouping: Bool { get { return (objc_getAssociatedObject(self, &Self._groupingKey) as? Bool) ?? false } set { objc_setAssociatedObject(self, &Self._groupingKey, newValue, .OBJC_ASSOCIATION_ASSIGN) } } func beginGrouping() { self._grouping = true self._grouping_objects = [] self._grouping_infos = [:] } func endGrouping() { self._grouping = false if self._grouping_objects.isEmpty == false { // 发送通知 var objects: [KMPreferenceGroup] = [] for data in self._grouping_objects { objects.append(data) } } self._grouping_objects = [] self._grouping_infos = [:] } }