KMPreferenceManager.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // KMPreferenceManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/2/3.
  6. //
  7. import Cocoa
  8. enum KMPreferenceGroup: String {
  9. case general, display, markup, infomation, other
  10. }
  11. private let KMPreferenceInfoKey: String = "KMPreferenceInfoKey"
  12. typealias KMPreferenceKey = String
  13. /// general
  14. /// files
  15. private let KMCloseFilePromptTypeKey: KMPreferenceKey = "KMCloseFilePromptTypeKey"
  16. /// open image file
  17. private let KMOpenImageFileTypeKey: KMPreferenceKey = "KMOpenImageFileTypeKey"
  18. /// save password
  19. private let KMGeneralAuthorNameKey: KMPreferenceKey = "KMGeneralAuthorNameKey"
  20. /// 偏好设置已改变
  21. private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName"
  22. private let KMDefaultFontName = "Helvetica-Oblique"
  23. var TeXEditors: [[String : Any]] = []
  24. let NAME_KEY = "name"
  25. var INITIALUSERDEFAULTS_KEY = "InitialUserDefaults"
  26. var TEXEDITORS_KEY = "TeXEditors"
  27. var COMMAND_KEY = "command"
  28. var ARGUMENTS_KEY = "arguments"
  29. typealias KMPreference = KMPreferenceManager
  30. @objcMembers class KMPreferenceManager: NSObject {
  31. static let shared = KMPreferenceManager()
  32. private var generalGroupKeys: Array<KMPreferenceKey> = []
  33. private var displayGroupKeys: Array<KMPreferenceKey> = []
  34. private var markupGroupKeys: Array<KMPreferenceKey> = []
  35. public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName)
  36. /// general
  37. /// files
  38. public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey
  39. /// open image file
  40. public static let openImageFileTypeKey = KMOpenImageFileTypeKey
  41. /// save password
  42. public static let generalAuthorNameKey = KMGeneralAuthorNameKey
  43. override init() {
  44. super.init()
  45. }
  46. public func resetData(_ group: KMPreferenceGroup) {
  47. }
  48. // MARK: 注册 key
  49. public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool {
  50. if (group == .general) {
  51. if (self.generalGroupKeys.contains(key)) {
  52. return false
  53. }
  54. self.generalGroupKeys.append(key)
  55. }
  56. return true
  57. }
  58. // MARK: 获取信息
  59. private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup {
  60. if (self.generalGroupKeys.contains(key)) {
  61. return .general
  62. } else if (self.displayGroupKeys.contains(key)) {
  63. return .display
  64. } else if (self.markupGroupKeys.contains(key)) {
  65. return .markup
  66. }
  67. return .other
  68. }
  69. }
  70. // MARK: - 默认值
  71. extension KMPreferenceManager {
  72. // General
  73. var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType {
  74. get {
  75. return .inSameWindow
  76. }
  77. }
  78. }
  79. // MARK: 扩展 General
  80. extension KMPreferenceManager {
  81. var closeFilePromptType: KMPreferenceCloseFilePromptType {
  82. get {
  83. return .noPromp
  84. }
  85. set {
  86. if (newValue == .promp || newValue == .noPromp) {
  87. }
  88. }
  89. }
  90. var openImageFileType: Int {
  91. get {
  92. return 0
  93. }
  94. set {
  95. }
  96. }
  97. var author: String {
  98. get {
  99. return KMDataManager.ud_string(forKey: KMUserNameKey) ?? NSFullUserName()
  100. }
  101. set {
  102. }
  103. }
  104. var openDocumentType: KMPreferenceOpenDocumentType { // KMOpenDocumentInTab
  105. get {
  106. guard let num = KMDataManager.ud_object(forKey: KMOpenDocumentInTabKey) as? NSNumber else {
  107. return self.openDocumentTypeDefaultValue
  108. }
  109. if num.intValue == 1 {
  110. return .newWindow
  111. }
  112. return .inSameWindow
  113. }
  114. set {
  115. if (newValue == .inSameWindow || newValue == .newWindow) {
  116. }
  117. }
  118. }
  119. var keepSnapshotWindowToTop: Bool {
  120. get {
  121. return KMDataManager.ud_bool(forKey: KMSnapshotsOnTopKey)
  122. }
  123. set {
  124. // 数据兼容
  125. }
  126. }
  127. }
  128. // MARK: UserDefaults
  129. fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath"
  130. fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  131. fileprivate let kKMViewSettingKey = "viewSetting"
  132. fileprivate let kKMPageNumberKey = "pageNumber"
  133. fileprivate let kKMPageScaleKey = "pageScale"
  134. extension KMPreferenceManager {
  135. var lastOpenFilepath: String? {
  136. get {
  137. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  138. }
  139. set {
  140. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  141. UserDefaults.standard.synchronize()
  142. }
  143. }
  144. var lastOpenFilepaths: [String]? {
  145. get {
  146. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  147. }
  148. set {
  149. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  150. UserDefaults.standard.synchronize()
  151. }
  152. }
  153. func setPageNumber(_ number: Int, forKey key: String) {
  154. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  155. UserDefaults.standard.synchronize()
  156. }
  157. func getPageNumber(forKey key: String) -> Int? {
  158. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  159. }
  160. func setPageScale(_ scale: Float, forKey key: String) {
  161. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  162. UserDefaults.standard.synchronize()
  163. }
  164. func getPageScale(forKey key: String) -> Float? {
  165. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  166. }
  167. }
  168. // MARK: - Grouping
  169. extension KMPreferenceManager {
  170. var grouping: Bool {
  171. get {
  172. return false
  173. }
  174. }
  175. private static var _groupingInfosKey = "KMGroupingInfosKey"
  176. private var _grouping_infos: [KMPreferenceKey : Any] {
  177. get {
  178. return (objc_getAssociatedObject(self, &Self._groupingInfosKey) as? [KMPreferenceKey : Any]) ?? [:]
  179. }
  180. set {
  181. objc_setAssociatedObject(self, &Self._groupingInfosKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  182. }
  183. }
  184. private static var _groupingObjectsKey = "KMGroupingObjectsKey"
  185. private var _grouping_objects: Set<KMPreferenceGroup> {
  186. get {
  187. return (objc_getAssociatedObject(self, &Self._groupingObjectsKey) as? Set) ?? []
  188. }
  189. set {
  190. objc_setAssociatedObject(self, &Self._groupingObjectsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  191. }
  192. }
  193. private static var _groupingKey = "KMGroupingKey"
  194. private var _grouping: Bool {
  195. get {
  196. return (objc_getAssociatedObject(self, &Self._groupingKey) as? Bool) ?? false
  197. }
  198. set {
  199. objc_setAssociatedObject(self, &Self._groupingKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
  200. }
  201. }
  202. func beginGrouping() {
  203. self._grouping = true
  204. self._grouping_objects = []
  205. self._grouping_infos = [:]
  206. }
  207. func endGrouping() {
  208. self._grouping = false
  209. if self._grouping_objects.isEmpty == false { // 发送通知
  210. var objects: [KMPreferenceGroup] = []
  211. for data in self._grouping_objects {
  212. objects.append(data)
  213. }
  214. }
  215. self._grouping_objects = []
  216. self._grouping_infos = [:]
  217. }
  218. }