123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // 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<KMPreferenceKey> = []
- private var displayGroupKeys: Array<KMPreferenceKey> = []
- private var markupGroupKeys: Array<KMPreferenceKey> = []
-
- 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<KMPreferenceGroup> {
- 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 = [:]
- }
- }
|