KMPreferenceManager.swift 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 KMOpenLastUnclosedDocumentWhenAppStartKey: KMPreferenceKey = "KMOpenLastUnclosedDocumentWhenAppStartKey"
  16. private let KMOpenLastUnlockedDocumentWhenAppStartKey: KMPreferenceKey = "KMOpenLastUnlockedDocumentWhenAppStartKey"
  17. private let KMDocumentMaximunDisplayNumberKey: KMPreferenceKey = "KMDocumentMaximunDisplayNumberKey"
  18. private let KMAutoSaveKey: KMPreferenceKey = "KMAutoSaveKey"
  19. private let KMAutoSavePerNumberMinuteKey: KMPreferenceKey = "KMAutoSavePerNumberMinuteKey"
  20. private let KMCloseFilePromptTypeKey: KMPreferenceKey = "KMCloseFilePromptTypeKey"
  21. /// open image file
  22. private let KMOpenImageFileTypeKey: KMPreferenceKey = "KMOpenImageFileTypeKey"
  23. private let KMSetDefaultPDFReaderKey: KMPreferenceKey = "KMSetDefaultPDFReaderKey"
  24. /// save password
  25. private let KMSavePasswordTypeKey: KMPreferenceKey = "KMSavePasswordTypeKey"
  26. private let KMGeneralAuthorNameKey: KMPreferenceKey = "KMGeneralAuthorNameKey"
  27. // Tip: 新补充
  28. private let KMOpenDocumentTypeKey: KMPreferenceKey = "KMOpenDocumentTypeKey"
  29. private let KMGeneralShowInMenuBarKey: KMPreferenceKey = "KMGeneralShowInMenuBarKey"
  30. private let KMOpenFileTypeKey: KMPreferenceKey = "KMOpenFileTypeKey"
  31. private let KMShowLeftSideBarKey: KMPreferenceKey = "KMShowLeftSideBarKey"
  32. private let KMEditPDFPopWindowShowKey: KMPreferenceKey = "KMEditPDFPopWindowShowKey"
  33. private let KMRememberSnapshotKey: KMPreferenceKey = "KMRememberSnapshotKey"
  34. private let KMRevertInitPDFViewSettingTypeKey: KMPreferenceKey = "KMRevertInitPDFViewSettingTypeKey"
  35. private let KMAutoSaveNoteBackupKey: KMPreferenceKey = "KMAutoSaveNoteBackupKey"
  36. private let KMKeepSnapshotWindowToTopKey: KMPreferenceKey = "KMKeepSnapshotWindowToTopKey"
  37. /// display
  38. private let KMViewPageDisplayTypeKey: KMPreferenceKey = "KMViewPageDisplayTypeKey"
  39. private let KMViewZoomScaleTypeKey: KMPreferenceKey = "KMViewZoomScaleTypeKey"
  40. private let KMLeftSideDisplayTypeKey: KMPreferenceKey = "KMLeftSideDisplayTypeKey"
  41. private let KMShowOutlineListKey: KMPreferenceKey = "KMShowOutlineListKey"
  42. private let KMLeftSideExpandTypeKey: KMPreferenceKey = "KMLeftSideExpandTypeKey"
  43. private let KMPropertyPanelExpandTypeKey: KMPreferenceKey = "KMPropertyPanelExpandTypeKey"
  44. private let KMDisplayBackgroundNormalColorKey: KMPreferenceKey = "KMDisplayBackgroundNormalColorKey"
  45. private let KMDisplayBackgroundFullScreenColorKey: KMPreferenceKey = "KMDisplayBackgroundFullScreenColorKey"
  46. private let KMHighlightFormsKey: KMPreferenceKey = "KMHighlightFormsKey"
  47. private let KMDisplayFieldHighlightingColorKey: KMPreferenceKey = "KMDisplayFieldHighlightingColorKey"
  48. private let KMDisplayRequiredFieldHighlightingColorKey: KMPreferenceKey = "KMDisplayRequiredFieldHighlightingColorKey"
  49. private let KMPageIndicatorTypeKey: KMPreferenceKey = "KMPageIndicatorTypeKey"
  50. private let KMHighlightLinksKey: KMPreferenceKey = "KMHighlightLinksKey"
  51. // Tip: 新补充
  52. private let KMThumbPageSizeKey: KMPreferenceKey = "KMThumbPageSizeKey"
  53. private let KMThumbSnapshotSizeKey: KMPreferenceKey = "KMThumbSnapshotSizeKey"
  54. private let KMDiscreteSizeKey: KMPreferenceKey = "KMDiscreteSizeKey"
  55. private let KMOutlineFontSizeKey: KMPreferenceKey = "KMOutlineFontSizeKey"
  56. private let KMGreekThresholdKey: KMPreferenceKey = "KMGreekThresholdKey"
  57. private let KMAntiAliasTextKey: KMPreferenceKey = "KMAntiAliasTextKey"
  58. private let KMReadBarColorKey: KMPreferenceKey = "KMReadBarColorKey"
  59. private let KMInvertBarKey: KMPreferenceKey = "KMInvertBarKey"
  60. /// 偏好设置已改变
  61. private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName"
  62. private let KMDefaultFontName = "Helvetica-Oblique"
  63. var TeXEditors: [[String : Any]] = []
  64. let NAME_KEY = "name"
  65. var INITIALUSERDEFAULTS_KEY = "InitialUserDefaults"
  66. var TEXEDITORS_KEY = "TeXEditors"
  67. var COMMAND_KEY = "command"
  68. var ARGUMENTS_KEY = "arguments"
  69. typealias KMPreference = KMPreferenceManager
  70. @objcMembers class KMPreferenceManager: NSObject {
  71. static let shared = KMPreferenceManager()
  72. private var generalGroupKeys: Array<KMPreferenceKey> = []
  73. private var displayGroupKeys: Array<KMPreferenceKey> = []
  74. private var markupGroupKeys: Array<KMPreferenceKey> = []
  75. public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName)
  76. /// general
  77. /// files
  78. public static let openLastUnclosedDocumentWhenAppStartKey = KMOpenLastUnclosedDocumentWhenAppStartKey
  79. public static let openLastUnlockedDocumentWhenAppStartKey = KMOpenLastUnlockedDocumentWhenAppStartKey
  80. public static let documentMaximunDisplayNumberKey = KMDocumentMaximunDisplayNumberKey
  81. public static let autoSaveKey = KMAutoSaveKey
  82. public static let autoSavePerNumberMinuteKey = KMAutoSavePerNumberMinuteKey
  83. public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey
  84. /// open image file
  85. public static let openImageFileTypeKey = KMOpenImageFileTypeKey
  86. public static let setDefaultPDFReaderKey = KMSetDefaultPDFReaderKey
  87. /// save password
  88. public static let savePasswordTypeKey = KMSavePasswordTypeKey
  89. public static let generalAuthorNameKey = KMGeneralAuthorNameKey
  90. // Tip: 新补充
  91. public static let openDocumentTypeKey = KMOpenDocumentTypeKey
  92. public static let showInMenuBarKey = KMGeneralShowInMenuBarKey
  93. public static let openFileTypeKey = KMOpenFileTypeKey
  94. public static let showLeftSideBarKey = KMShowLeftSideBarKey
  95. public static let editPDFPopWindowShowKey = KMEditPDFPopWindowShowKey
  96. public static let rememberSnapshotKey = KMRememberSnapshotKey
  97. public static let revertInitPDFViewSettingTypeKey = KMRevertInitPDFViewSettingTypeKey
  98. public static let autoSaveNoteBackupKey = KMAutoSaveNoteBackupKey
  99. public static let keepSnapshotWindowToTopKey = KMKeepSnapshotWindowToTopKey
  100. /// display
  101. public static let viewPageDisplayTypeKey = KMViewPageDisplayTypeKey
  102. public static let viewZoomScaleTypeKey = KMViewZoomScaleTypeKey
  103. public static let leftSideDisplayTypeKey = KMLeftSideDisplayTypeKey
  104. public static let showOutlineListKey = KMShowOutlineListKey
  105. public static let leftSideExpandTypeKey = KMLeftSideExpandTypeKey
  106. public static let propertyPanelExpandTypeKey = KMPropertyPanelExpandTypeKey
  107. public static let displayBackgroundNormalColorKey = KMDisplayBackgroundNormalColorKey
  108. public static let displayBackgroundFullScreenColorKey = KMDisplayBackgroundFullScreenColorKey
  109. public static let highlightFormsKey = KMHighlightFormsKey
  110. public static let displayFieldHighlightingColorKey = KMDisplayFieldHighlightingColorKey
  111. public static let displayRequiredFieldHighlightingColorKey = KMDisplayRequiredFieldHighlightingColorKey
  112. public static let pageIndicatorTypeKey = KMPageIndicatorTypeKey
  113. public static let highlightLinksKey = KMHighlightLinksKey
  114. // Tip: 新补充
  115. public static let thumbPageSizeKey = KMThumbPageSizeKey
  116. public static let thumbSnapshotSizeKey = KMThumbSnapshotSizeKey
  117. public static let discreteSizeKey = KMDiscreteSizeKey
  118. public static let outlineFontSizeKey = KMOutlineFontSizeKey
  119. public static let greekThresholdKey = KMGreekThresholdKey
  120. public static let antiAliasTextKey = KMAntiAliasTextKey
  121. public static let readBarColorKey = KMReadBarColorKey
  122. public static let invertBarKey = KMInvertBarKey
  123. override init() {
  124. super.init()
  125. /// 初始化
  126. generalGroupKeys = self.getDefaultKeys(for: .general)
  127. displayGroupKeys = self.getDefaultKeys(for: .display)
  128. markupGroupKeys = self.getDefaultKeys(for: .markup)
  129. let info = UserDefaults.standard.value(forKey: KMPreferenceInfoKey)
  130. if (info == nil) {
  131. let info = self.getDefaultInfo()
  132. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  133. UserDefaults.standard.synchronize()
  134. }
  135. }
  136. // MARK: 保存/获取数据
  137. public func setData(data: Any,forKey key: KMPreferenceKey) -> Bool {
  138. return self.setData(data: data, forKey: key, in: findGroup(forKey: key))
  139. }
  140. public func setData(data: Any,forKey key: KMPreferenceKey, in group: KMPreferenceGroup) -> Bool {
  141. var info: [String : Any]?
  142. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  143. info = self.getDefaultInfo()
  144. } else {
  145. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  146. }
  147. var groupInfo: [String : Any] = info?[group.rawValue] as? [String : Any] ?? [:]
  148. groupInfo.updateValue(data, forKey: key)
  149. info?.updateValue(groupInfo, forKey: group.rawValue)
  150. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  151. UserDefaults.standard.synchronize()
  152. if self._grouping {
  153. self._grouping_objects.insert(group)
  154. self._grouping_infos[key] = data
  155. } else {
  156. self.postNotification(object: [group], userInfo: [key : data])
  157. }
  158. return true
  159. }
  160. public func getData(forKey key: KMPreferenceKey) -> Any {
  161. var info: [String : Any]?
  162. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  163. info = self.getDefaultInfo()
  164. } else {
  165. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  166. }
  167. let groupInfo: [String : Any] = info?[findGroup(forKey: key).rawValue] as? [String : Any] ?? [:]
  168. return groupInfo[key] as Any
  169. }
  170. public func resetData(_ group: KMPreferenceGroup) {
  171. var info: [String : Any]?
  172. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  173. info = self.getDefaultInfo()
  174. } else {
  175. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  176. }
  177. let groupInfo = self.getDefaultInfo()[group.rawValue]
  178. info?.updateValue(groupInfo as Any, forKey: group.rawValue)
  179. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  180. UserDefaults.standard.synchronize()
  181. self._resetDataToUserDefault(group: group)
  182. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  183. let info: [String : Any]? = UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]
  184. var groupInfo: [KMPreferenceKey : Any]?
  185. if (info != nil) {
  186. groupInfo = info![group.rawValue] as? [KMPreferenceKey : Any]
  187. }
  188. self.postNotification(object: [group], userInfo: groupInfo != nil ? groupInfo : [:])
  189. }
  190. }
  191. public func resetAllData() {
  192. let info = self.getDefaultInfo()
  193. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  194. UserDefaults.standard.synchronize()
  195. self._resetDataToUserDefault(group: .general)
  196. self._resetDataToUserDefault(group: .display)
  197. self._resetDataToUserDefault(group: .markup)
  198. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  199. var groppInfos: [KMPreferenceKey : Any] = [:]
  200. for groupInfo in info.values {
  201. for (key, value) in groupInfo {
  202. groppInfos.updateValue(value, forKey: key)
  203. }
  204. }
  205. self.postNotification(object: [KMPreferenceGroup.general, KMPreferenceGroup.display, KMPreferenceGroup.markup, KMPreferenceGroup.infomation, KMPreferenceGroup.other], userInfo: groppInfos)
  206. }
  207. }
  208. // MARK: 注册 key
  209. public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool {
  210. if (group == .general) {
  211. if (self.generalGroupKeys.contains(key)) {
  212. return false
  213. }
  214. self.generalGroupKeys.append(key)
  215. }
  216. return true
  217. }
  218. // MARK: -
  219. // MARK: 发布通知
  220. private func postNotification(object: [KMPreferenceGroup]?, userInfo: [KMPreferenceKey : Any]?) {
  221. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  222. NotificationCenter.default.post(name: KMPreferenceManager.didChangeNotification, object: object, userInfo: userInfo)
  223. }
  224. }
  225. // MARK: 获取信息
  226. private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup {
  227. if (self.generalGroupKeys.contains(key)) {
  228. return .general
  229. } else if (self.displayGroupKeys.contains(key)) {
  230. return .display
  231. } else if (self.markupGroupKeys.contains(key)) {
  232. return .markup
  233. }
  234. return .other
  235. }
  236. private func getDefaultKeys(for group: KMPreferenceGroup) -> Array<KMPreferenceKey> {
  237. if (group == .general) {
  238. return [KMOpenLastUnclosedDocumentWhenAppStartKey,
  239. KMOpenLastUnlockedDocumentWhenAppStartKey,
  240. KMDocumentMaximunDisplayNumberKey,
  241. KMAutoSaveKey,
  242. KMAutoSavePerNumberMinuteKey,
  243. KMCloseFilePromptTypeKey,
  244. KMOpenImageFileTypeKey,
  245. KMSetDefaultPDFReaderKey,
  246. KMSavePasswordTypeKey,
  247. KMGeneralAuthorNameKey,
  248. // Tip: 新补充
  249. KMOpenDocumentTypeKey, KMGeneralShowInMenuBarKey, KMOpenFileTypeKey, KMShowLeftSideBarKey, KMEditPDFPopWindowShowKey,
  250. KMRememberSnapshotKey, KMRevertInitPDFViewSettingTypeKey, KMAutoSaveNoteBackupKey, KMKeepSnapshotWindowToTopKey]
  251. } else if (group == .display) {
  252. return [KMViewPageDisplayTypeKey,
  253. KMViewZoomScaleTypeKey,
  254. KMLeftSideDisplayTypeKey,
  255. KMShowOutlineListKey,
  256. KMLeftSideExpandTypeKey,
  257. KMPropertyPanelExpandTypeKey,
  258. KMDisplayBackgroundNormalColorKey,
  259. KMDisplayBackgroundFullScreenColorKey,
  260. KMHighlightFormsKey,
  261. KMDisplayFieldHighlightingColorKey,
  262. KMDisplayRequiredFieldHighlightingColorKey,
  263. KMPageIndicatorTypeKey,
  264. KMHighlightLinksKey,
  265. // Tip: 新补充
  266. KMThumbPageSizeKey, KMThumbSnapshotSizeKey, KMDiscreteSizeKey, KMOutlineFontSizeKey,
  267. KMGreekThresholdKey, KMAntiAliasTextKey, KMReadBarColorKey, KMInvertBarKey]
  268. } else if (group == .markup) {
  269. return []
  270. }
  271. return []
  272. }
  273. private func getDefaultInfo() -> Dictionary<String, Dictionary<String, Any>> {
  274. return [KMPreferenceGroup.general.rawValue : [KMOpenLastUnclosedDocumentWhenAppStartKey : false,
  275. KMOpenLastUnlockedDocumentWhenAppStartKey : true,
  276. KMDocumentMaximunDisplayNumberKey : 10,
  277. KMAutoSaveKey : true,
  278. KMAutoSavePerNumberMinuteKey : 5,
  279. KMCloseFilePromptTypeKey : 0,
  280. KMOpenImageFileTypeKey : 0,
  281. KMSetDefaultPDFReaderKey : true,
  282. KMSavePasswordTypeKey : 2,
  283. KMGeneralAuthorNameKey : NSFullUserName(),
  284. // Tip: 新补充
  285. KMOpenDocumentTypeKey : self.openDocumentTypeDefaultValue.rawValue,
  286. KMGeneralShowInMenuBarKey : self.showInMenuBarDefaultValue,
  287. KMOpenFileTypeKey : self.openFileTypeDefaultValue.rawValue,
  288. KMShowLeftSideBarKey : self.showLeftSideBarDefaultValue,
  289. KMEditPDFPopWindowShowKey : self.editPDFPopWindowIsShowDefaultValue,
  290. KMRememberSnapshotKey : self.rememberSnapshotDefaultValue,
  291. KMRevertInitPDFViewSettingTypeKey : self.revertInitPDFViewSettingTypeDefaultValue.rawValue,
  292. KMAutoSaveNoteBackupKey : self.autoSaveNoteBackupDefaultValue,
  293. KMKeepSnapshotWindowToTopKey : self.keepSnapshotWindowToTopDefaultValue()],
  294. KMPreferenceGroup.display.rawValue : [KMViewPageDisplayTypeKey : 1,
  295. KMViewZoomScaleTypeKey : 0,
  296. KMLeftSideDisplayTypeKey : 0,
  297. KMShowOutlineListKey: true,
  298. KMLeftSideExpandTypeKey : 0,
  299. KMPropertyPanelExpandTypeKey : 0,
  300. KMDisplayBackgroundNormalColorKey : self.displayBackgroundNormalColorValues,
  301. KMDisplayBackgroundFullScreenColorKey : self.displayBackgroundFullScreenColorValues,
  302. KMHighlightFormsKey : true,
  303. KMPageIndicatorTypeKey : 0,
  304. KMHighlightLinksKey : true,
  305. // Tip: 新补充
  306. KMThumbPageSizeKey : self.thumbPageSizeDefaultValue(),
  307. KMThumbSnapshotSizeKey : self.thumbSnapshotSizeDefaultValue(),
  308. KMDiscreteSizeKey : self.discreteSizeDefaultValue(),
  309. KMOutlineFontSizeKey : self.outlineFontSizeDefaultValue(),
  310. KMGreekThresholdKey : self.greekThresholdDefaultValue(),
  311. KMAntiAliasTextKey : self.antiAliasTextDefaultValue(),
  312. KMInvertBarKey : self.invertBarDefaultValue()],
  313. KMPreferenceGroup.infomation.rawValue : [:],
  314. KMPreferenceGroup.other.rawValue : [:]]
  315. }
  316. }
  317. // MARK: - 默认值
  318. extension KMPreferenceManager {
  319. // General
  320. var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType {
  321. get {
  322. return .inSameWindow
  323. }
  324. }
  325. var showInMenuBarDefaultValue: Bool {
  326. get {
  327. return false
  328. }
  329. }
  330. var openFileTypeDefaultValue: KMPreferenceOpenFileType {
  331. get {
  332. return .default
  333. }
  334. }
  335. var showLeftSideBarDefaultValue: Bool {
  336. get {
  337. return true
  338. }
  339. }
  340. var editPDFPopWindowIsShowDefaultValue: Bool {
  341. get {
  342. return true
  343. }
  344. }
  345. var rememberSnapshotDefaultValue: Bool {
  346. get {
  347. return true
  348. }
  349. }
  350. var revertInitPDFViewSettingTypeDefaultValue: KMPreferenceRevertInitSettingType {
  351. get {
  352. return .normal
  353. }
  354. }
  355. var autoSaveNoteBackupDefaultValue: Bool {
  356. get {
  357. return false
  358. }
  359. }
  360. // 内联函数
  361. @inline (__always) func keepSnapshotWindowToTopDefaultValue() -> Bool {
  362. return true
  363. }
  364. // Display
  365. @inline (__always) func thumbPageSizeDefaultValue() -> Float {
  366. return 128
  367. }
  368. @inline (__always) func thumbSnapshotSizeDefaultValue() -> Float {
  369. return 128
  370. }
  371. @inline (__always) func discreteSizeDefaultValue() -> Bool {
  372. return false
  373. }
  374. @inline (__always) func outlineFontSizeDefaultValue() -> Float {
  375. return 12
  376. }
  377. @inline (__always) func greekThresholdDefaultValue() -> Float {
  378. return 3
  379. }
  380. @inline (__always) func antiAliasTextDefaultValue() -> Bool {
  381. return true
  382. }
  383. @inline (__always) func invertBarDefaultValue() -> Bool {
  384. return false
  385. }
  386. }
  387. // MARK: - 扩展 颜色
  388. extension KMPreferenceManager {
  389. fileprivate var displayBackgroundNormalColorValues: [Double] {
  390. get {
  391. return [206/255.0, 208/255.0, 212/255.0, 1.0]
  392. }
  393. }
  394. fileprivate var displayBackgroundFullScreenColorValues: [Double] {
  395. get {
  396. return [54/255.0, 56/255.0, 59/255.0, 1.0]
  397. }
  398. }
  399. private func setColor(_ color: NSColor, forKey key: KMPreferenceKey) -> Bool {
  400. var red: CGFloat = 0.0
  401. var green: CGFloat = 0.0
  402. var blue: CGFloat = 0.0
  403. var alpha: CGFloat = 0.0
  404. color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  405. return KMPreferenceManager.shared.setData(data: [red, green, blue, alpha], forKey: key)
  406. }
  407. private func getColor(forKey key: KMPreferenceKey) -> NSColor {
  408. var colors: [Double]? = self.getData(forKey: key) as? [Double]
  409. if (colors == nil) {
  410. colors = self.getDefaultColors(forKey: key)
  411. }
  412. return NSColor(red: colors![0], green: colors![1], blue: colors![2], alpha: colors![3])
  413. }
  414. private func dataToColor(colors:[Double]) -> NSColor {
  415. NSColor(red: colors[0], green: colors[1], blue: colors[2], alpha: colors[3])
  416. }
  417. }
  418. // MARK: 扩展 General
  419. extension KMPreferenceManager {
  420. var openLastUnclosedDocumentWhenAppStart: Bool {
  421. get {
  422. return KMDataManager.ud_bool(forKey: KMReopenLastOpenFilesKey)
  423. }
  424. set {
  425. let pKey = KMPreference.openLastUnclosedDocumentWhenAppStartKey
  426. self._syncDataToUserDefault(newValue, forKey: pKey)
  427. _ = self.setData(data: newValue, forKey: pKey)
  428. }
  429. }
  430. var openLastUnlockedDocumentWhenAppStart: Bool {
  431. get {
  432. return self.getData(forKey: KMOpenLastUnlockedDocumentWhenAppStartKey) as? Bool ?? true
  433. }
  434. set {
  435. let _ = self.setData(data: newValue, forKey: KMOpenLastUnlockedDocumentWhenAppStartKey)
  436. }
  437. }
  438. var documentMaximunDisplayNumber: Int {
  439. get {
  440. return self.getData(forKey: KMDocumentMaximunDisplayNumberKey) as? Int ?? 10
  441. }
  442. set {
  443. let _ = self.setData(data: newValue, forKey: KMDocumentMaximunDisplayNumberKey)
  444. }
  445. }
  446. var autoSave: Bool {
  447. get {
  448. return self.getData(forKey: KMAutoSaveKey) as? Bool ?? true
  449. }
  450. set {
  451. let _ = self.setData(data: newValue, forKey: KMAutoSaveKey)
  452. }
  453. }
  454. var autoSavePerNumberMinute: Int {
  455. get {
  456. return self.getData(forKey: KMAutoSavePerNumberMinuteKey) as? Int ?? 5
  457. }
  458. set {
  459. let _ = self.setData(data: newValue, forKey: KMAutoSavePerNumberMinuteKey)
  460. }
  461. }
  462. var closeFilePromptType: KMPreferenceCloseFilePromptType {
  463. get {
  464. if let type = self.getData(forKey: KMCloseFilePromptTypeKey) as? Int {
  465. if type == 1 { // 无提示,直接保存
  466. return .noPromp
  467. }
  468. }
  469. return .promp
  470. }
  471. set {
  472. if (newValue == .promp || newValue == .noPromp) {
  473. let _ = self.setData(data: newValue.rawValue, forKey: KMCloseFilePromptTypeKey)
  474. }
  475. }
  476. }
  477. var openImageFileType: Int {
  478. get {
  479. return self.getData(forKey: KMOpenImageFileTypeKey) as? Int ?? 0
  480. }
  481. set {
  482. let _ = self.setData(data: newValue, forKey: KMOpenImageFileTypeKey)
  483. }
  484. }
  485. var author: String {
  486. get {
  487. return KMDataManager.ud_string(forKey: KMUserNameKey) ?? NSFullUserName()
  488. }
  489. set {
  490. self._syncDataToUserDefault(newValue, forKey: KMGeneralAuthorNameKey)
  491. let _ = self.setData(data: newValue, forKey: KMGeneralAuthorNameKey)
  492. }
  493. }
  494. var setDefaultPDFReader: Bool {
  495. get {
  496. return self.getData(forKey: KMSetDefaultPDFReaderKey) as? Bool ?? true
  497. }
  498. set {
  499. let result = KMTools.setDefaultPDFReader(newValue)
  500. if (result) {
  501. let _ = self.setData(data: newValue, forKey: KMSetDefaultPDFReaderKey)
  502. }
  503. }
  504. }
  505. var savePasswordType: KMPreferenceSavePasswordType {
  506. get {
  507. let type = KMDataManager.ud_integer(forKey: KMSavePasswordOptionKey)
  508. if type == -1 {
  509. return .ask
  510. } else if type == 1 {
  511. return .always
  512. }
  513. return .never
  514. }
  515. set {
  516. if (newValue == .always || newValue == .never || newValue == .ask) {
  517. var data: Int = 0
  518. if newValue == .always {
  519. data = 1
  520. } else if newValue == .ask {
  521. data = -1
  522. }
  523. self._syncDataToUserDefault(data, forKey: KMSavePasswordTypeKey)
  524. let _ = self.setData(data: newValue.rawValue, forKey: KMSavePasswordTypeKey)
  525. }
  526. }
  527. }
  528. var openDocumentType: KMPreferenceOpenDocumentType { // KMOpenDocumentInTab
  529. get {
  530. guard let num = KMDataManager.ud_object(forKey: KMOpenDocumentInTabKey) as? NSNumber else {
  531. return self.openDocumentTypeDefaultValue
  532. }
  533. if num.intValue == 1 {
  534. return .newWindow
  535. }
  536. return .inSameWindow
  537. }
  538. set {
  539. if (newValue == .inSameWindow || newValue == .newWindow) {
  540. let pKey = KMOpenDocumentTypeKey
  541. self._syncDataToUserDefault(NSNumber(value: newValue.rawValue), forKey: pKey)
  542. _ = self.setData(data: newValue.rawValue, forKey: pKey)
  543. }
  544. }
  545. }
  546. var showInMenuBar: Bool {
  547. get {
  548. return self.getData(forKey: KMGeneralShowInMenuBarKey) as? Bool ?? self.showInMenuBarDefaultValue
  549. }
  550. set {
  551. _ = self.setData(data: newValue, forKey: KMGeneralShowInMenuBarKey)
  552. }
  553. }
  554. var openFileType: KMPreferenceOpenFileType {
  555. get {
  556. // 数据兼容
  557. let value = KMDataManager.ud_integer(forKey: KMInitialWindowSizeOptionKey)
  558. if let type = KMPreferenceOpenFileType(rawValue: value) {
  559. return type
  560. }
  561. return .default
  562. }
  563. set {
  564. if (newValue == .default || newValue == .maxim || newValue == .fit) {
  565. // 数据兼容
  566. self._syncDataToUserDefault(newValue.rawValue, forKey: KMOpenFileTypeKey)
  567. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenFileTypeKey)
  568. }
  569. }
  570. }
  571. var showLeftSideBar: Bool {
  572. get {
  573. return KMDataManager.ud_bool(forKey: KMOpenContentsPaneOnlyForTOCKey)
  574. }
  575. set {
  576. self._syncDataToUserDefault(newValue, forKey: KMShowLeftSideBarKey)
  577. _ = self.setData(data: newValue, forKey: KMShowLeftSideBarKey)
  578. }
  579. }
  580. var editPDFPopWindowIsShow: Bool {
  581. get {
  582. return self.getData(forKey: Self.editPDFPopWindowShowKey) as? Bool ?? self.editPDFPopWindowIsShowDefaultValue
  583. }
  584. set {
  585. _ = self.setData(data: newValue, forKey: Self.editPDFPopWindowShowKey)
  586. }
  587. }
  588. var rememberSnapshot: Bool {
  589. get {
  590. return KMDataManager.ud_bool(forKey: KMRememberSnapshotsKey)
  591. }
  592. set {
  593. // 数据兼容
  594. self._syncDataToUserDefault(newValue, forKey: KMRememberSnapshotKey)
  595. _ = self.setData(data: newValue, forKey: KMRememberSnapshotKey)
  596. }
  597. }
  598. var revertInitPDFViewSettingType: KMPreferenceRevertInitSettingType {
  599. get {
  600. guard let type = self.getData(forKey: KMRevertInitPDFViewSettingTypeKey) as? Int else {
  601. return self.revertInitPDFViewSettingTypeDefaultValue
  602. }
  603. if type == 1 {
  604. return .fullScreen
  605. }
  606. return .normal
  607. }
  608. set {
  609. if (newValue == .normal || newValue == .fullScreen) {
  610. let _ = self.setData(data: newValue.rawValue, forKey: KMRevertInitPDFViewSettingTypeKey)
  611. }
  612. }
  613. }
  614. var autoSaveNoteBackup: Bool {
  615. get {
  616. return KMDataManager.ud_bool(forKey: KMAutoSaveSkimNotesKey)
  617. }
  618. set {
  619. self._syncDataToUserDefault(newValue, forKey: KMAutoSaveNoteBackupKey)
  620. _ = self.setData(data: newValue, forKey: KMAutoSaveNoteBackupKey)
  621. }
  622. }
  623. var keepSnapshotWindowToTop: Bool {
  624. get {
  625. return KMDataManager.ud_bool(forKey: KMSnapshotsOnTopKey)
  626. }
  627. set {
  628. // 数据兼容
  629. self._syncDataToUserDefault(newValue, forKey: KMKeepSnapshotWindowToTopKey)
  630. _ = self.setData(data: newValue, forKey: KMKeepSnapshotWindowToTopKey)
  631. }
  632. }
  633. }
  634. // MARK: 扩展 Display
  635. extension KMPreferenceManager {
  636. var viewZoomScaleType: KMPDFZoomType {
  637. get {
  638. if let type = self.getData(forKey: KMViewZoomScaleTypeKey) as? Int {
  639. if (type == 0) {
  640. return .width
  641. } else if (type == 1) {
  642. return .fit
  643. } else if (type == 2) {
  644. return .actualSize
  645. }
  646. }
  647. return .width
  648. }
  649. set {
  650. if (newValue == .width || newValue == .fit || newValue == .actualSize) {
  651. let _ = self.setData(data: newValue.rawValue, forKey: KMViewZoomScaleTypeKey)
  652. }
  653. }
  654. }
  655. var leftSideDisplayType: KMPreferenceLeftSideDisplayType {
  656. get {
  657. if let type = self.getData(forKey: KMLeftSideDisplayTypeKey) as? Int {
  658. if type == 0 {
  659. return .closeWhenOpenFile
  660. } else if (type == 1) {
  661. return .openAppSaveLastSelect
  662. } else if (type == 2) {
  663. return .showOutlineIfHas
  664. }
  665. }
  666. return .closeWhenOpenFile
  667. }
  668. set {
  669. if (newValue == .closeWhenOpenFile || newValue == .openAppSaveLastSelect || newValue == .showOutlineIfHas) {
  670. let _ = self.setData(data: newValue.rawValue, forKey: KMLeftSideDisplayTypeKey)
  671. }
  672. }
  673. }
  674. var propertyPanelExpandType: KMPreferencePropertyPanelExpandType {
  675. get {
  676. if let type = self.getData(forKey: KMPropertyPanelExpandTypeKey) as? Int {
  677. if (type == 0) {
  678. return .auto
  679. } else if (type == 1) {
  680. return .manual
  681. }
  682. }
  683. return .auto
  684. }
  685. set {
  686. if (newValue == .auto || newValue == .manual) {
  687. let _ = self.setData(data: newValue.rawValue, forKey: KMPropertyPanelExpandTypeKey)
  688. }
  689. }
  690. }
  691. var highlightForms: Bool {
  692. get {
  693. return self.getData(forKey: KMHighlightFormsKey) as? Bool ?? true
  694. }
  695. set {
  696. let _ = self.setData(data: newValue, forKey: KMHighlightFormsKey)
  697. }
  698. }
  699. var pageIndicatorType: KMPreferencePageIndicatorDisplayType {
  700. get {
  701. if let type = self.getData(forKey: KMPageIndicatorTypeKey) as? Int {
  702. if (type == 2) {
  703. return .never
  704. } else if (type == 0) {
  705. return .automatic
  706. } else if (type == 1) {
  707. return .always
  708. }
  709. }
  710. return .never
  711. }
  712. set {
  713. if (newValue == .automatic || newValue == .always || newValue == .never) {
  714. let _ = self.setData(data: newValue.rawValue, forKey: KMPageIndicatorTypeKey)
  715. }
  716. }
  717. }
  718. var highlightLinks: Bool {
  719. get {
  720. return self.getData(forKey: KMHighlightLinksKey) as? Bool ?? true
  721. }
  722. set {
  723. let _ = self.setData(data: newValue, forKey: KMHighlightLinksKey)
  724. }
  725. }
  726. var displayBackgroundNormalColor: NSColor {
  727. get {
  728. if let color = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  729. return color
  730. }
  731. return self.getColor(forKey: KMDisplayBackgroundNormalColorKey)
  732. }
  733. set {
  734. self._syncDataToUserDefault(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  735. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  736. }
  737. }
  738. var displayBackgroundFullScreenColor: NSColor {
  739. get {
  740. if let color = UserDefaults.standard.color(forKey: KMFullScreenBackgroundColorKey) {
  741. return color
  742. }
  743. return self.getColor(forKey: KMDisplayBackgroundFullScreenColorKey)
  744. }
  745. set {
  746. self._syncDataToUserDefault(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  747. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  748. }
  749. }
  750. var displayFieldHighlightingColor: NSColor {
  751. get {
  752. return self.getColor(forKey: KMDisplayFieldHighlightingColorKey)
  753. }
  754. set {
  755. let _ = self.setColor(newValue, forKey: KMDisplayFieldHighlightingColorKey)
  756. }
  757. }
  758. var displayRequiredFieldHighlightingColor: NSColor {
  759. get {
  760. return self.getColor(forKey: KMDisplayRequiredFieldHighlightingColorKey)
  761. }
  762. set {
  763. let _ = self.setColor(newValue, forKey: KMDisplayRequiredFieldHighlightingColorKey)
  764. }
  765. }
  766. // Tip: 新补充
  767. var thumbPageSize: Float {
  768. get {
  769. let num = KMDataManager.ud_object(forKey: KMThumbnailSizeKey) as? NSNumber
  770. return num?.floatValue ?? self.thumbPageSizeDefaultValue()
  771. }
  772. set {
  773. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMThumbPageSizeKey)
  774. let _ = self.setData(data: NSNumber(value: newValue), forKey: KMThumbPageSizeKey)
  775. }
  776. }
  777. var thumbSnapshotSize: Float {
  778. get {
  779. let num = KMDataManager.ud_object(forKey: KMSnapshotThumbnailSizeKey) as? NSNumber
  780. return num?.floatValue ?? self.thumbSnapshotSizeDefaultValue()
  781. }
  782. set {
  783. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMThumbSnapshotSizeKey)
  784. let _ = self.setData(data: newValue, forKey: KMThumbSnapshotSizeKey)
  785. }
  786. }
  787. var discreteSize: Bool {
  788. get {
  789. return self.getData(forKey: KMDiscreteSizeKey) as? Bool ?? self.discreteSizeDefaultValue()
  790. }
  791. set {
  792. let _ = self.setData(data: newValue, forKey: KMDiscreteSizeKey)
  793. }
  794. }
  795. var outlineFontSize: Float {
  796. get {
  797. let num = KMDataManager.ud_object(forKey: KMTableFontSizeKey) as? NSNumber
  798. return num?.floatValue ?? self.outlineFontSizeDefaultValue()
  799. }
  800. set {
  801. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMOutlineFontSizeKey)
  802. let _ = self.setData(data: newValue, forKey: KMOutlineFontSizeKey)
  803. }
  804. }
  805. var greekThreshold: Float {
  806. get {
  807. let num = KMDataManager.ud_object(forKey: KMGreekingThresholdKey) as? NSNumber
  808. return num?.floatValue ?? self.greekThresholdDefaultValue()
  809. }
  810. set {
  811. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMGreekThresholdKey)
  812. let _ = self.setData(data: newValue, forKey: KMGreekThresholdKey)
  813. }
  814. }
  815. var antiAliasText: Bool {
  816. get {
  817. return KMDataManager.ud_bool(forKey: KMShouldAntiAliasKey)
  818. }
  819. set {
  820. self._syncDataToUserDefault(newValue, forKey: KMAntiAliasTextKey)
  821. let _ = self.setData(data: newValue, forKey: KMAntiAliasTextKey)
  822. }
  823. }
  824. var readBarColor: NSColor {
  825. get {
  826. if let color = UserDefaults.standard.color(forKey: KMReadingBarColorKey) {
  827. return color
  828. }
  829. return self.getColor(forKey: KMReadBarColorKey)
  830. }
  831. set {
  832. self._syncDataToUserDefault(newValue, forKey: KMReadBarColorKey)
  833. let _ = self.setColor(newValue, forKey: KMReadBarColorKey)
  834. }
  835. }
  836. var invertBar: Bool {
  837. get {
  838. return KMDataManager.ud_bool(forKey: KMReadingBarInvertKey)
  839. }
  840. set {
  841. self._syncDataToUserDefault(newValue, forKey: KMInvertBarKey)
  842. let _ = self.setData(data: newValue, forKey: KMInvertBarKey)
  843. }
  844. }
  845. }
  846. // MARK: 扩展 Markup
  847. extension KMPreferenceManager {
  848. }
  849. // MARK: -
  850. // MARK: Additions
  851. extension KMPreferenceManager {
  852. class var supportFonts: [String] {
  853. get {
  854. var fontNames: Array<String> = []
  855. for fontInfo in CPDFAnnotationModel.supportFonts() {
  856. if ((fontInfo is NSDictionary) == false) {
  857. continue
  858. }
  859. for (familyString, styleStrings) in (fontInfo as! NSDictionary) {
  860. if ((styleStrings is NSArray) == false) {
  861. break
  862. }
  863. for styleString in (styleStrings as! NSArray) {
  864. fontNames.append("\(familyString)-\(styleString)")
  865. }
  866. }
  867. }
  868. return fontNames
  869. }
  870. }
  871. private func syncDataToPDFView(_ data: Any, forKey key: KMPreferenceKey) {
  872. }
  873. private func _syncDataToUserDefault(_ data: Any, forKey key: KMPreferenceKey) {
  874. // general
  875. if let udKey = KMPreferenceKeyToUDKey(pKey: key) {
  876. if let color = data as? NSColor {
  877. UserDefaults.standard.setColor(color, forKey: udKey)
  878. } else {
  879. KMDataManager.ud_set(data, forKey: udKey)
  880. }
  881. }
  882. }
  883. private func getDefaultColors(forKey key: KMPreferenceKey) -> [Double] {
  884. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue] ?? [:]
  885. let displayGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.display.rawValue] ?? [:]
  886. for key_i in [KMDisplayBackgroundNormalColorKey, KMDisplayBackgroundFullScreenColorKey,
  887. KMDisplayFieldHighlightingColorKey, KMDisplayRequiredFieldHighlightingColorKey] {
  888. if (key == key_i) {
  889. return displayGroupInfo[key] as? [Double] ?? []
  890. }
  891. }
  892. return [0.0, 0.0, 0.0, 1.0]
  893. }
  894. private func getDefaultColor(forKey key: KMPreferenceKey) -> NSColor {
  895. return self.dataToColor(colors: self.getDefaultColors(forKey: key))
  896. }
  897. private func _resetDataToUserDefault(group: KMPreferenceGroup) {
  898. if group == .general {
  899. KMDataManager.ud_set(nil, forKey: KMUserNameKey)
  900. KMDataManager.ud_set(NSNumber(value: 0), forKey: KMOpenDocumentInTabKey)
  901. KMDataManager.ud_set(false, forKey: KMReopenLastOpenFilesKey)
  902. KMDataManager.ud_set(0, forKey: KMInitialWindowSizeOptionKey)
  903. KMDataManager.ud_set(true, forKey: KMOpenContentsPaneOnlyForTOCKey)
  904. KMDataManager.ud_set(false, forKey: KMRememberSnapshotsKey)
  905. KMDataManager.ud_set(false, forKey: KMAutoSaveSkimNotesKey)
  906. KMDataManager.ud_set(false, forKey: KMSnapshotsOnTopKey)
  907. KMDataManager.ud_set(-1, forKey: KMSavePasswordOptionKey)
  908. } else if group == .display {
  909. KMDataManager.ud_set(NSNumber(value: self.thumbPageSizeDefaultValue()), forKey: KMThumbnailSizeKey)
  910. KMDataManager.ud_set(NSNumber(value: self.thumbSnapshotSizeDefaultValue()), forKey: KMSnapshotThumbnailSizeKey)
  911. KMDataManager.ud_set(NSNumber(value: self.outlineFontSizeDefaultValue()), forKey: KMTableFontSizeKey)
  912. KMDataManager.ud_set(NSNumber(value: self.greekThresholdDefaultValue()), forKey: KMGreekingThresholdKey)
  913. KMDataManager.ud_set(false, forKey: KMShouldAntiAliasKey)
  914. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.displayBackgroundNormalColorKey), forKey: KMBackgroundColorKey)
  915. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.displayBackgroundFullScreenColorKey), forKey: KMFullScreenBackgroundColorKey)
  916. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.readBarColorKey), forKey: KMReadingBarColorKey)
  917. KMDataManager.ud_set(false, forKey: KMReadingBarInvertKey)
  918. }
  919. }
  920. public func initDataForAppLaunch() {
  921. if (KMPreferenceManager.shared.autoSave) {
  922. KMPreferenceManager.shared.autoSave = false
  923. }
  924. }
  925. }
  926. // MARK: UserDefaults
  927. fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath"
  928. fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  929. fileprivate let kKMViewSettingKey = "viewSetting"
  930. fileprivate let kKMPageNumberKey = "pageNumber"
  931. fileprivate let kKMPageScaleKey = "pageScale"
  932. extension KMPreferenceManager {
  933. var lastOpenFilepath: String? {
  934. get {
  935. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  936. }
  937. set {
  938. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  939. UserDefaults.standard.synchronize()
  940. }
  941. }
  942. var lastOpenFilepaths: [String]? {
  943. get {
  944. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  945. }
  946. set {
  947. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  948. UserDefaults.standard.synchronize()
  949. }
  950. }
  951. var viewSetting: [KMPreferenceViewSetting : Any]? {
  952. get {
  953. return UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [KMPreferenceViewSetting : Any]
  954. }
  955. set {
  956. if (newValue == nil) {
  957. UserDefaults.standard.set(newValue, forKey: kKMViewSettingKey)
  958. UserDefaults.standard.synchronize()
  959. return
  960. }
  961. let viewSetting: [String : Any] = UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [String : Any] ?? [:]
  962. var info: [String : Any] = [:]
  963. for (key, value) in viewSetting {
  964. info.updateValue(value, forKey: key)
  965. }
  966. for (key, value) in newValue ?? [:] {
  967. if (key == .pageNumber) { // 只处理 枚举
  968. info.updateValue(value, forKey: key.rawValue)
  969. }
  970. }
  971. UserDefaults.standard.set(info, forKey: kKMViewSettingKey)
  972. UserDefaults.standard.synchronize()
  973. }
  974. }
  975. func setPageNumber(_ number: Int, forKey key: String) {
  976. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  977. UserDefaults.standard.synchronize()
  978. }
  979. func getPageNumber(forKey key: String) -> Int? {
  980. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  981. }
  982. func setPageScale(_ scale: Float, forKey key: String) {
  983. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  984. UserDefaults.standard.synchronize()
  985. }
  986. func getPageScale(forKey key: String) -> Float? {
  987. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  988. }
  989. }
  990. // MARK: Qiuck
  991. extension KMPreferenceManager {
  992. internal func closeFileIsPrompt() -> Bool {
  993. return self.closeFilePromptType == .promp
  994. }
  995. func leftSideNeedCloseWhenOpenFile() -> Bool {
  996. return KMPreferenceManager.shared.leftSideDisplayType == .closeWhenOpenFile
  997. }
  998. var autoExpandPropertyPanel: Bool {
  999. get {
  1000. return KMPreferenceManager.shared.propertyPanelExpandType == .auto
  1001. }
  1002. }
  1003. // 单位: 秒
  1004. var autoSaveTimeInterval: TimeInterval {
  1005. get {
  1006. var minute = KMPreferenceManager.shared.autoSavePerNumberMinute
  1007. if (minute < 5) {
  1008. minute = 5
  1009. } else if (minute > 99) {
  1010. minute = 99
  1011. }
  1012. let interval: TimeInterval = Double(minute) * 60.0
  1013. return interval
  1014. }
  1015. }
  1016. }
  1017. // MARK: - Grouping
  1018. extension KMPreferenceManager {
  1019. var grouping: Bool {
  1020. get {
  1021. return false
  1022. }
  1023. }
  1024. private static var _groupingInfosKey = "KMGroupingInfosKey"
  1025. private var _grouping_infos: [KMPreferenceKey : Any] {
  1026. get {
  1027. return (objc_getAssociatedObject(self, &Self._groupingInfosKey) as? [KMPreferenceKey : Any]) ?? [:]
  1028. }
  1029. set {
  1030. objc_setAssociatedObject(self, &Self._groupingInfosKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  1031. }
  1032. }
  1033. private static var _groupingObjectsKey = "KMGroupingObjectsKey"
  1034. private var _grouping_objects: Set<KMPreferenceGroup> {
  1035. get {
  1036. return (objc_getAssociatedObject(self, &Self._groupingObjectsKey) as? Set) ?? []
  1037. }
  1038. set {
  1039. objc_setAssociatedObject(self, &Self._groupingObjectsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  1040. }
  1041. }
  1042. private static var _groupingKey = "KMGroupingKey"
  1043. private var _grouping: Bool {
  1044. get {
  1045. return (objc_getAssociatedObject(self, &Self._groupingKey) as? Bool) ?? false
  1046. }
  1047. set {
  1048. objc_setAssociatedObject(self, &Self._groupingKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
  1049. }
  1050. }
  1051. func beginGrouping() {
  1052. self._grouping = true
  1053. self._grouping_objects = []
  1054. self._grouping_infos = [:]
  1055. }
  1056. func endGrouping() {
  1057. self._grouping = false
  1058. if self._grouping_objects.isEmpty == false { // 发送通知
  1059. var objects: [KMPreferenceGroup] = []
  1060. for data in self._grouping_objects {
  1061. objects.append(data)
  1062. }
  1063. self.postNotification(object: objects, userInfo: self._grouping_infos)
  1064. }
  1065. self._grouping_objects = []
  1066. self._grouping_infos = [:]
  1067. }
  1068. }