KMPreferenceManager.swift 47 KB

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