KMPreferenceManager.swift 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. //
  2. // KMPreferenceManager.swift
  3. // PDF Master
  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 KMRememberSnapshotKey: KMPreferenceKey = "KMRememberSnapshotKey"
  33. private let KMRevertInitPDFViewSettingTypeKey: KMPreferenceKey = "KMRevertInitPDFViewSettingTypeKey"
  34. private let KMAutoSaveNoteBackupKey: KMPreferenceKey = "KMAutoSaveNoteBackupKey"
  35. private let KMKeepSnapshotWindowToTopKey: KMPreferenceKey = "KMKeepSnapshotWindowToTopKey"
  36. /// display
  37. private let KMViewPageDisplayTypeKey: KMPreferenceKey = "KMViewPageDisplayTypeKey"
  38. private let KMViewZoomScaleTypeKey: KMPreferenceKey = "KMViewZoomScaleTypeKey"
  39. private let KMLeftSideDisplayTypeKey: KMPreferenceKey = "KMLeftSideDisplayTypeKey"
  40. private let KMShowOutlineListKey: KMPreferenceKey = "KMShowOutlineListKey"
  41. private let KMLeftSideExpandTypeKey: KMPreferenceKey = "KMLeftSideExpandTypeKey"
  42. private let KMPropertyPanelExpandTypeKey: KMPreferenceKey = "KMPropertyPanelExpandTypeKey"
  43. private let KMDisplayBackgroundNormalColorKey: KMPreferenceKey = "KMDisplayBackgroundNormalColorKey"
  44. private let KMDisplayBackgroundFullScreenColorKey: KMPreferenceKey = "KMDisplayBackgroundFullScreenColorKey"
  45. private let KMHighlightFormsKey: KMPreferenceKey = "KMHighlightFormsKey"
  46. private let KMDisplayFieldHighlightingColorKey: KMPreferenceKey = "KMDisplayFieldHighlightingColorKey"
  47. private let KMDisplayRequiredFieldHighlightingColorKey: KMPreferenceKey = "KMDisplayRequiredFieldHighlightingColorKey"
  48. private let KMPageIndicatorTypeKey: KMPreferenceKey = "KMPageIndicatorTypeKey"
  49. private let KMHighlightLinksKey: KMPreferenceKey = "KMHighlightLinksKey"
  50. // Tip: 新补充
  51. private let KMThumbPageSizeKey: KMPreferenceKey = "KMThumbPageSizeKey"
  52. private let KMThumbSnapshotSizeKey: KMPreferenceKey = "KMThumbSnapshotSizeKey"
  53. private let KMDiscreteSizeKey: KMPreferenceKey = "KMDiscreteSizeKey"
  54. private let KMOutlineFontSizeKey: KMPreferenceKey = "KMOutlineFontSizeKey"
  55. private let KMGreekThresholdKey: KMPreferenceKey = "KMGreekThresholdKey"
  56. private let KMAntiAliasTextKey: KMPreferenceKey = "KMAntiAliasTextKey"
  57. private let KMReadBarColorKey: KMPreferenceKey = "KMReadBarColorKey"
  58. private let KMInvertBarKey: KMPreferenceKey = "KMInvertBarKey"
  59. /// markup
  60. private let KMMarkupColorHighlightKey: KMPreferenceKey = "KMMarkupColorHighlightKey"
  61. private let KMMarkupColorStrikthroughKey: KMPreferenceKey = "KMMarkupColorStrikthroughKey"
  62. private let KMMarkupColorUnderlineKey: KMPreferenceKey = "KMMarkupColorUnderlineKey"
  63. private let KMMarkupColorPenKey: KMPreferenceKey = "KMMarkupColorPenKey"
  64. private let KMMarkupColorTextKey: KMPreferenceKey = "KMMarkupColorTextKey"
  65. private let KMMarkupColorNoteKey: KMPreferenceKey = "KMMarkupColorNoteKey"
  66. private let KMMarkupColorRectangleFillKey: KMPreferenceKey = "KMMarkupColorRectangleFillKey"
  67. private let KMMarkupColorRectangleBorderKey: KMPreferenceKey = "KMMarkupColorRectangleBorderKey"
  68. private let KMMarkupColorCircleFillKey: KMPreferenceKey = "KMMarkupColorCircleFillKey"
  69. private let KMMarkupColorCircleBorderKey: KMPreferenceKey = "KMMarkupColorCircleBorderKey"
  70. private let KMMarkupColorLineKey: KMPreferenceKey = "KMMarkupColorLineKey"
  71. private let KMMarkupColorArrowKey: KMPreferenceKey = "KMMarkupColorArrowKey"
  72. private let KMMarkupFontTextStringKey: KMPreferenceKey = "KMMarkupFontTextStringKey"
  73. private let KMMarkupFontTextAligmentKey: KMPreferenceKey = "KMMarkupFontTextAligmentKey"
  74. private let KMMarkupFontNoteStringKey: KMPreferenceKey = "KMMarkupFontNoteStringKey"
  75. /// 偏好设置已改变
  76. private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName"
  77. private let KMDefaultFontName = "Helvetica-Oblique"
  78. typealias KMPreference = KMPreferenceManager
  79. @objcMembers class KMPreferenceManager: NSObject {
  80. static let shared = KMPreferenceManager()
  81. private var generalGroupKeys: Array<KMPreferenceKey> = []
  82. private var displayGroupKeys: Array<KMPreferenceKey> = []
  83. private var markupGroupKeys: Array<KMPreferenceKey> = []
  84. public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName)
  85. /// general
  86. /// files
  87. public static let openLastUnclosedDocumentWhenAppStartKey = KMOpenLastUnclosedDocumentWhenAppStartKey
  88. public static let openLastUnlockedDocumentWhenAppStartKey = KMOpenLastUnlockedDocumentWhenAppStartKey
  89. public static let documentMaximunDisplayNumberKey = KMDocumentMaximunDisplayNumberKey
  90. public static let autoSaveKey = KMAutoSaveKey
  91. public static let autoSavePerNumberMinuteKey = KMAutoSavePerNumberMinuteKey
  92. public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey
  93. /// open image file
  94. public static let openImageFileTypeKey = KMOpenImageFileTypeKey
  95. public static let setDefaultPDFReaderKey = KMSetDefaultPDFReaderKey
  96. /// save password
  97. public static let savePasswordTypeKey = KMSavePasswordTypeKey
  98. public static let generalAuthorNameKey = KMGeneralAuthorNameKey
  99. // Tip: 新补充
  100. public static let openDocumentTypeKey = KMOpenDocumentTypeKey
  101. public static let showInMenuBarKey = KMGeneralShowInMenuBarKey
  102. public static let openFileTypeKey = KMOpenFileTypeKey
  103. public static let showLeftSideBarKey = KMShowLeftSideBarKey
  104. public static let rememberSnapshotKey = KMRememberSnapshotKey
  105. public static let revertInitPDFViewSettingTypeKey = KMRevertInitPDFViewSettingTypeKey
  106. public static let autoSaveNoteBackupKey = KMAutoSaveNoteBackupKey
  107. public static let keepSnapshotWindowToTopKey = KMKeepSnapshotWindowToTopKey
  108. /// display
  109. public static let viewPageDisplayTypeKey = KMViewPageDisplayTypeKey
  110. public static let viewZoomScaleTypeKey = KMViewZoomScaleTypeKey
  111. public static let leftSideDisplayTypeKey = KMLeftSideDisplayTypeKey
  112. public static let showOutlineListKey = KMShowOutlineListKey
  113. public static let leftSideExpandTypeKey = KMLeftSideExpandTypeKey
  114. public static let propertyPanelExpandTypeKey = KMPropertyPanelExpandTypeKey
  115. public static let displayBackgroundNormalColorKey = KMDisplayBackgroundNormalColorKey
  116. public static let displayBackgroundFullScreenColorKey = KMDisplayBackgroundFullScreenColorKey
  117. public static let highlightFormsKey = KMHighlightFormsKey
  118. public static let displayFieldHighlightingColorKey = KMDisplayFieldHighlightingColorKey
  119. public static let displayRequiredFieldHighlightingColorKey = KMDisplayRequiredFieldHighlightingColorKey
  120. public static let pageIndicatorTypeKey = KMPageIndicatorTypeKey
  121. public static let highlightLinksKey = KMHighlightLinksKey
  122. // Tip: 新补充
  123. public static let thumbPageSizeKey = KMThumbPageSizeKey
  124. public static let thumbSnapshotSizeKey = KMThumbSnapshotSizeKey
  125. public static let discreteSizeKey = KMDiscreteSizeKey
  126. public static let outlineFontSizeKey = KMOutlineFontSizeKey
  127. public static let greekThresholdKey = KMGreekThresholdKey
  128. public static let antiAliasTextKey = KMAntiAliasTextKey
  129. public static let readBarColorKey = KMReadBarColorKey
  130. public static let invertBarKey = KMInvertBarKey
  131. /// markup
  132. public static let markupColorHighlightKey = KMMarkupColorHighlightKey
  133. public static let markupColorStrikthroughKey = KMMarkupColorStrikthroughKey
  134. public static let markupColorUnderlineKey = KMMarkupColorUnderlineKey
  135. public static let markupColorPenKey = KMMarkupColorPenKey
  136. public static let markupColorTextKey = KMMarkupColorTextKey
  137. public static let markupColorNoteKey = KMMarkupColorNoteKey
  138. public static let markupColorRectangleFillKey = KMMarkupColorRectangleFillKey
  139. public static let markupColorRectangleBorderKey = KMMarkupColorRectangleBorderKey
  140. public static let markupColorCircleFillKey = KMMarkupColorCircleFillKey
  141. public static let markupColorCircleBorderKey = KMMarkupColorCircleBorderKey
  142. public static let markupColorLineKey = KMMarkupColorLineKey
  143. public static let markupColorArrowKey = KMMarkupColorArrowKey
  144. public static let markupFontTextStringKey = KMMarkupFontTextStringKey
  145. public static let markupFontTextAligmentKey = KMMarkupFontTextAligmentKey
  146. public static let markupFontNoteStringKey = KMMarkupFontNoteStringKey
  147. override init() {
  148. super.init()
  149. /// 初始化
  150. generalGroupKeys = self.getDefaultKeys(for: .general)
  151. displayGroupKeys = self.getDefaultKeys(for: .display)
  152. markupGroupKeys = self.getDefaultKeys(for: .markup)
  153. let info = UserDefaults.standard.value(forKey: KMPreferenceInfoKey)
  154. if (info == nil) {
  155. let info = self.getDefaultInfo()
  156. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  157. UserDefaults.standard.synchronize()
  158. }
  159. }
  160. // MARK: 保存/获取数据
  161. public func setData(data: Any,forKey key: KMPreferenceKey) -> Bool {
  162. return self.setData(data: data, forKey: key, in: findGroup(forKey: key))
  163. }
  164. public func setData(data: Any,forKey key: KMPreferenceKey, in group: KMPreferenceGroup) -> Bool {
  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. var groupInfo: [String : Any] = info![group.rawValue] as! [String : Any]
  172. groupInfo.updateValue(data, forKey: key)
  173. info?.updateValue(groupInfo, forKey: group.rawValue)
  174. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  175. UserDefaults.standard.synchronize()
  176. self.postNotification(object: [group], userInfo: [key : data])
  177. return true
  178. }
  179. public func getData(forKey key: KMPreferenceKey) -> Any {
  180. var info: [String : Any]?
  181. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  182. info = self.getDefaultInfo()
  183. } else {
  184. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as! [String : Any])
  185. }
  186. let groupInfo: [String : Any] = info![findGroup(forKey: key).rawValue] as! [String : Any]
  187. return groupInfo[key] as Any
  188. }
  189. public func resetData(_ group: KMPreferenceGroup) {
  190. var info: [String : Any]?
  191. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  192. info = self.getDefaultInfo()
  193. } else {
  194. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as! [String : Any])
  195. }
  196. let groupInfo = self.getDefaultInfo()[group.rawValue]
  197. info?.updateValue(groupInfo as Any, forKey: group.rawValue)
  198. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  199. UserDefaults.standard.synchronize()
  200. self.resetDataToPDFView()
  201. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  202. let info: [String : Any]? = UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]
  203. var groupInfo: [KMPreferenceKey : Any]?
  204. if (info != nil) {
  205. groupInfo = info![group.rawValue] as? [KMPreferenceKey : Any]
  206. }
  207. self.postNotification(object: [group], userInfo: groupInfo != nil ? groupInfo : [:])
  208. }
  209. }
  210. public func resetAllData() {
  211. let info = self.getDefaultInfo()
  212. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  213. UserDefaults.standard.synchronize()
  214. self.resetDataToPDFView()
  215. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  216. var groppInfos: [KMPreferenceKey : Any] = [:]
  217. for groupInfo in info.values {
  218. for (key, value) in groupInfo {
  219. groppInfos.updateValue(value, forKey: key)
  220. }
  221. }
  222. self.postNotification(object: [KMPreferenceGroup.general, KMPreferenceGroup.display, KMPreferenceGroup.markup, KMPreferenceGroup.infomation, KMPreferenceGroup.other], userInfo: groppInfos)
  223. }
  224. }
  225. // MARK: 注册 key
  226. public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool {
  227. if (group == .general) {
  228. if (self.generalGroupKeys.contains(key)) {
  229. return false
  230. }
  231. self.generalGroupKeys.append(key)
  232. }
  233. return true
  234. }
  235. // MARK: -
  236. // MARK: 发布通知
  237. private func postNotification(object: [KMPreferenceGroup]?, userInfo: [KMPreferenceKey : Any]?) {
  238. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  239. NotificationCenter.default.post(name: KMPreferenceManager.didChangeNotification, object: object, userInfo: userInfo)
  240. }
  241. }
  242. // MARK: 获取信息
  243. private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup {
  244. if (self.generalGroupKeys.contains(key)) {
  245. return .general
  246. } else if (self.displayGroupKeys.contains(key)) {
  247. return .display
  248. } else if (self.markupGroupKeys.contains(key)) {
  249. return .markup
  250. }
  251. return .other
  252. }
  253. private func getDefaultKeys(for group: KMPreferenceGroup) -> Array<KMPreferenceKey> {
  254. if (group == .general) {
  255. return [KMOpenLastUnclosedDocumentWhenAppStartKey,
  256. KMOpenLastUnlockedDocumentWhenAppStartKey,
  257. KMDocumentMaximunDisplayNumberKey,
  258. KMAutoSaveKey,
  259. KMAutoSavePerNumberMinuteKey,
  260. KMCloseFilePromptTypeKey,
  261. KMOpenImageFileTypeKey,
  262. KMSetDefaultPDFReaderKey,
  263. KMSavePasswordTypeKey,
  264. KMGeneralAuthorNameKey,
  265. // Tip: 新补充
  266. KMOpenDocumentTypeKey, KMGeneralShowInMenuBarKey, KMOpenFileTypeKey, KMShowLeftSideBarKey,
  267. KMRememberSnapshotKey, KMRevertInitPDFViewSettingTypeKey, KMAutoSaveNoteBackupKey, KMKeepSnapshotWindowToTopKey]
  268. } else if (group == .display) {
  269. return [KMViewPageDisplayTypeKey,
  270. KMViewZoomScaleTypeKey,
  271. KMLeftSideDisplayTypeKey,
  272. KMShowOutlineListKey,
  273. KMLeftSideExpandTypeKey,
  274. KMPropertyPanelExpandTypeKey,
  275. KMDisplayBackgroundNormalColorKey,
  276. KMDisplayBackgroundFullScreenColorKey,
  277. KMHighlightFormsKey,
  278. KMDisplayFieldHighlightingColorKey,
  279. KMDisplayRequiredFieldHighlightingColorKey,
  280. KMPageIndicatorTypeKey,
  281. KMHighlightLinksKey,
  282. // Tip: 新补充
  283. KMThumbPageSizeKey, KMThumbSnapshotSizeKey, KMDiscreteSizeKey, KMOutlineFontSizeKey,
  284. KMGreekThresholdKey, KMAntiAliasTextKey, KMReadBarColorKey, KMInvertBarKey]
  285. } else if (group == .markup) {
  286. return [KMMarkupColorHighlightKey,
  287. KMMarkupColorUnderlineKey,
  288. KMMarkupColorStrikthroughKey,
  289. KMMarkupColorPenKey,
  290. KMMarkupColorLineKey,
  291. KMMarkupColorArrowKey,
  292. KMMarkupColorNoteKey,
  293. KMMarkupColorTextKey,
  294. KMMarkupColorCircleFillKey,
  295. KMMarkupColorCircleBorderKey,
  296. KMMarkupColorRectangleFillKey,
  297. KMMarkupColorRectangleBorderKey,
  298. KMMarkupFontTextStringKey,
  299. KMMarkupFontTextAligmentKey,
  300. KMMarkupFontNoteStringKey]
  301. }
  302. return []
  303. }
  304. private func getDefaultInfo() -> Dictionary<String, Dictionary<String, Any>> {
  305. return [KMPreferenceGroup.general.rawValue : [KMOpenLastUnclosedDocumentWhenAppStartKey : false,
  306. KMOpenLastUnlockedDocumentWhenAppStartKey : true,
  307. KMDocumentMaximunDisplayNumberKey : 10,
  308. KMAutoSaveKey : true,
  309. KMAutoSavePerNumberMinuteKey : 5,
  310. KMCloseFilePromptTypeKey : 0,
  311. KMOpenImageFileTypeKey : 0,
  312. KMSetDefaultPDFReaderKey : true,
  313. KMSavePasswordTypeKey : 2,
  314. KMGeneralAuthorNameKey : NSFullUserName(),
  315. // Tip: 新补充
  316. KMOpenDocumentTypeKey : self.openDocumentTypeDefaultValue.rawValue,
  317. KMGeneralShowInMenuBarKey : self.showInMenuBarDefaultValue,
  318. KMOpenFileTypeKey : self.openFileTypeDefaultValue.rawValue,
  319. KMShowLeftSideBarKey : self.showLeftSideBarDefaultValue,
  320. KMRememberSnapshotKey : self.rememberSnapshotDefaultValue,
  321. KMRevertInitPDFViewSettingTypeKey : self.revertInitPDFViewSettingTypeDefaultValue.rawValue,
  322. KMAutoSaveNoteBackupKey : self.autoSaveNoteBackupDefaultValue,
  323. KMKeepSnapshotWindowToTopKey : self.keepSnapshotWindowToTopDefaultValue()],
  324. KMPreferenceGroup.display.rawValue : [KMViewPageDisplayTypeKey : 1,
  325. KMViewZoomScaleTypeKey : 0,
  326. KMLeftSideDisplayTypeKey : 0,
  327. KMShowOutlineListKey: true,
  328. KMLeftSideExpandTypeKey : 0,
  329. KMPropertyPanelExpandTypeKey : 0,
  330. KMDisplayBackgroundNormalColorKey : self.displayBackgroundNormalColorValues,
  331. KMDisplayBackgroundFullScreenColorKey : self.displayBackgroundFullScreenColorValues,
  332. KMHighlightFormsKey : true,
  333. KMDisplayFieldHighlightingColorKey : self.displayFieldHighlightingColorValues,
  334. KMDisplayRequiredFieldHighlightingColorKey : self.displayRequiredFieldHighlightingColorValues,
  335. KMPageIndicatorTypeKey : 0,
  336. KMHighlightLinksKey : true,
  337. // Tip: 新补充
  338. KMThumbPageSizeKey : self.thumbPageSizeDefaultValue(),
  339. KMThumbSnapshotSizeKey : self.thumbSnapshotSizeDefaultValue(),
  340. KMDiscreteSizeKey : self.discreteSizeDefaultValue(),
  341. KMOutlineFontSizeKey : self.outlineFontSizeDefaultValue(),
  342. KMGreekThresholdKey : self.greekThresholdDefaultValue(),
  343. KMAntiAliasTextKey : self.antiAliasTextDefaultValue(),
  344. KMReadBarColorKey : self.readBarColorValues,
  345. KMInvertBarKey : self.invertBarDefaultValue()],
  346. KMPreferenceGroup.markup.rawValue : [KMMarkupColorHighlightKey : self.markupHighlightColorValues,
  347. KMMarkupColorStrikthroughKey : self.markupStrikthroughColorValues,
  348. KMMarkupColorUnderlineKey : self.markupUnderlineColorValues,
  349. KMMarkupColorPenKey : self.markupPenColorValues,
  350. KMMarkupColorTextKey : self.markupTextColorValues,
  351. KMMarkupColorNoteKey : self.markupNoteColorValues,
  352. KMMarkupColorRectangleFillKey : self.markupRectangleFillColorValues,
  353. KMMarkupColorRectangleBorderKey : self.markupRectangleBorderColorValues,
  354. KMMarkupColorCircleFillKey : self.markupCircleFillColorValues,
  355. KMMarkupColorCircleBorderKey : self.markupCircleBorderColorValues,
  356. KMMarkupColorLineKey : self.markupLineColorValues,
  357. KMMarkupColorArrowKey : self.markupArrowColorValues,
  358. KMMarkupFontTextStringKey : KMDefaultFontName,
  359. KMMarkupFontTextAligmentKey : 0,
  360. KMMarkupFontNoteStringKey : KMDefaultFontName],
  361. KMPreferenceGroup.infomation.rawValue : [:],
  362. KMPreferenceGroup.other.rawValue : [:]]
  363. }
  364. }
  365. // MARK: - 默认值
  366. extension KMPreferenceManager {
  367. // General
  368. var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType {
  369. get {
  370. return .inSameWindow
  371. }
  372. }
  373. var showInMenuBarDefaultValue: Bool {
  374. get {
  375. return false
  376. }
  377. }
  378. var openFileTypeDefaultValue: KMPreferenceOpenFileType {
  379. get {
  380. return .default
  381. }
  382. }
  383. var showLeftSideBarDefaultValue: Bool {
  384. get {
  385. return true
  386. }
  387. }
  388. var rememberSnapshotDefaultValue: Bool {
  389. get {
  390. return true
  391. }
  392. }
  393. var revertInitPDFViewSettingTypeDefaultValue: KMPreferenceRevertInitSettingType {
  394. get {
  395. return .normal
  396. }
  397. }
  398. var autoSaveNoteBackupDefaultValue: Bool {
  399. get {
  400. return false
  401. }
  402. }
  403. // 内联函数
  404. @inline (__always) func keepSnapshotWindowToTopDefaultValue() -> Bool {
  405. return true
  406. }
  407. // Display
  408. @inline (__always) func thumbPageSizeDefaultValue() -> Float {
  409. return 128
  410. }
  411. @inline (__always) func thumbSnapshotSizeDefaultValue() -> Float {
  412. return 128
  413. }
  414. @inline (__always) func discreteSizeDefaultValue() -> Bool {
  415. return false
  416. }
  417. @inline (__always) func outlineFontSizeDefaultValue() -> Float {
  418. return 12
  419. }
  420. @inline (__always) func greekThresholdDefaultValue() -> Float {
  421. return 3
  422. }
  423. @inline (__always) func antiAliasTextDefaultValue() -> Bool {
  424. return true
  425. }
  426. // @inline (__always) func readBarColorDefaultValue() -> Float {
  427. // return 0
  428. // }
  429. @inline (__always) func invertBarDefaultValue() -> Bool {
  430. return false
  431. }
  432. }
  433. //protocol KMPreferenceManagerColorPart: NSObjectProtocol {
  434. // var markupHighlightColorValues: [Double] { get }
  435. //}
  436. // MARK: - 扩展 颜色
  437. extension KMPreferenceManager {
  438. fileprivate var displayBackgroundNormalColorValues: [Double] {
  439. get {
  440. return [206/255.0, 208/255.0, 212/255.0, 1.0]
  441. }
  442. }
  443. fileprivate var displayBackgroundFullScreenColorValues: [Double] {
  444. get {
  445. return [54/255.0, 56/255.0, 59/255.0, 1.0]
  446. }
  447. }
  448. fileprivate var displayFieldHighlightingColorValues: [Double] {
  449. get {
  450. return [189/255.0, 223/255.0, 253/255.0, 1.0]
  451. }
  452. }
  453. fileprivate var displayRequiredFieldHighlightingColorValues: [Double] {
  454. get {
  455. return [23/255.0, 112/255.0, 224/255.0, 1.0]
  456. }
  457. }
  458. fileprivate var markupHighlightColorValues: [Double] {
  459. get {
  460. return [255/255.0, 199/255.0, 0.0, 1.0]
  461. }
  462. }
  463. fileprivate var markupStrikthroughColorValues: [Double] {
  464. get {
  465. return [252/255.0, 31/255.0, 31/255.0, 1.0]
  466. }
  467. }
  468. fileprivate var markupUnderlineColorValues: [Double] {
  469. get {
  470. return [30/255.0, 137/255.0, 86/255.0, 1.0]
  471. }
  472. }
  473. fileprivate var markupPenColorValues: [Double] {
  474. get {
  475. self.markupStrikthroughColorValues
  476. }
  477. }
  478. fileprivate var markupTextColorValues: [Double] {
  479. get {
  480. return [37/255.0, 38/255.0, 41/255.0, 1.0]
  481. }
  482. }
  483. fileprivate var markupNoteColorValues: [Double] {
  484. get {
  485. return [255/255.0, 213/255.0, 115/255.0, 1.0]
  486. }
  487. }
  488. fileprivate var markupRectangleFillColorValues: [Double] {
  489. get {
  490. return [0/255.0, 0/255.0, 0/255.0, 0.0]
  491. }
  492. }
  493. fileprivate var markupRectangleBorderColorValues: [Double] {
  494. get {
  495. self.markupStrikthroughColorValues
  496. }
  497. }
  498. fileprivate var markupCircleFillColorValues: [Double] {
  499. get {
  500. self.markupRectangleFillColorValues
  501. }
  502. }
  503. fileprivate var markupCircleBorderColorValues: [Double] {
  504. get {
  505. return self.markupRectangleBorderColorValues
  506. }
  507. }
  508. fileprivate var markupLineColorValues: [Double] {
  509. get {
  510. self.markupStrikthroughColorValues
  511. }
  512. }
  513. fileprivate var markupArrowColorValues: [Double] {
  514. get {
  515. self.markupStrikthroughColorValues
  516. }
  517. }
  518. fileprivate var readBarColorValues: [Double] {
  519. get {
  520. return [23/255.0, 112/255.0, 224/255.0, 1.0]
  521. }
  522. }
  523. private func setColor(_ color: NSColor, forKey key: KMPreferenceKey) -> Bool {
  524. var red: CGFloat = 0.0
  525. var green: CGFloat = 0.0
  526. var blue: CGFloat = 0.0
  527. var alpha: CGFloat = 0.0
  528. color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  529. return KMPreferenceManager.shared.setData(data: [red, green, blue, alpha], forKey: key)
  530. }
  531. private func getColor(forKey key: KMPreferenceKey) -> NSColor {
  532. var colors: [Double]? = self.getData(forKey: key) as? [Double]
  533. if (colors == nil) {
  534. colors = self.getDefaultColors(forKey: key)
  535. }
  536. return NSColor(red: colors![0], green: colors![1], blue: colors![2], alpha: colors![3])
  537. }
  538. private func dataToColor(colors:[Double]) -> NSColor {
  539. NSColor(red: colors[0], green: colors[1], blue: colors[2], alpha: colors[3])
  540. }
  541. }
  542. // MARK: 扩展 General
  543. extension KMPreferenceManager {
  544. var openLastUnclosedDocumentWhenAppStart: Bool { // SKReopenLastOpenFilesKey
  545. get {
  546. return self.getData(forKey: KMOpenLastUnclosedDocumentWhenAppStartKey) as! Bool
  547. }
  548. set {
  549. let _ = self.setData(data: newValue, forKey: KMOpenLastUnclosedDocumentWhenAppStartKey)
  550. }
  551. }
  552. var openLastUnlockedDocumentWhenAppStart: Bool {
  553. get {
  554. return self.getData(forKey: KMOpenLastUnlockedDocumentWhenAppStartKey) as! Bool
  555. }
  556. set {
  557. let _ = self.setData(data: newValue, forKey: KMOpenLastUnlockedDocumentWhenAppStartKey)
  558. }
  559. }
  560. var documentMaximunDisplayNumber: Int {
  561. get {
  562. return self.getData(forKey: KMDocumentMaximunDisplayNumberKey) as! Int
  563. }
  564. set {
  565. let _ = self.setData(data: newValue, forKey: KMDocumentMaximunDisplayNumberKey)
  566. }
  567. }
  568. var autoSave: Bool {
  569. get {
  570. return self.getData(forKey: KMAutoSaveKey) as! Bool
  571. }
  572. set {
  573. let _ = self.setData(data: newValue, forKey: KMAutoSaveKey)
  574. }
  575. }
  576. var autoSavePerNumberMinute: Int {
  577. get {
  578. return self.getData(forKey: KMAutoSavePerNumberMinuteKey) as! Int
  579. }
  580. set {
  581. let _ = self.setData(data: newValue, forKey: KMAutoSavePerNumberMinuteKey)
  582. }
  583. }
  584. var closeFilePromptType: KMPreferenceCloseFilePromptType {
  585. get {
  586. let type: Int? = self.getData(forKey: KMCloseFilePromptTypeKey) as? Int
  587. if (type != nil && type! == 1) { // 无提示,直接保存
  588. return .noPromp
  589. }
  590. return .promp
  591. }
  592. set {
  593. if (newValue == .promp || newValue == .noPromp) {
  594. let _ = self.setData(data: newValue.rawValue, forKey: KMCloseFilePromptTypeKey)
  595. }
  596. }
  597. }
  598. var openImageFileType: Int {
  599. get {
  600. return self.getData(forKey: KMOpenImageFileTypeKey) as! Int
  601. }
  602. set {
  603. let _ = self.setData(data: newValue, forKey: KMOpenImageFileTypeKey)
  604. }
  605. }
  606. var author: String {
  607. get {
  608. return self.getData(forKey: KMGeneralAuthorNameKey) as! String
  609. }
  610. set {
  611. let _ = self.setData(data: newValue, forKey: KMGeneralAuthorNameKey)
  612. }
  613. }
  614. var setDefaultPDFReader: Bool {
  615. get {
  616. return self.getData(forKey: KMSetDefaultPDFReaderKey) as! Bool
  617. }
  618. set {
  619. let result = KMTools.setDefaultPDFReader(newValue)
  620. if (result) {
  621. let _ = self.setData(data: newValue, forKey: KMSetDefaultPDFReaderKey)
  622. }
  623. }
  624. }
  625. var savePasswordType: KMPreferenceSavePasswordType {
  626. get {
  627. let type: Int? = self.getData(forKey: KMSavePasswordTypeKey) as? Int
  628. if (type == nil || type! == 2) {
  629. return .ask
  630. }
  631. if (type! == 0) {
  632. return .always
  633. } else if (type! == 1) {
  634. return .never
  635. }
  636. return .ask
  637. }
  638. set {
  639. if (newValue == .always || newValue == .never || newValue == .ask) {
  640. let _ = self.setData(data: newValue.rawValue, forKey: KMSavePasswordTypeKey)
  641. }
  642. }
  643. }
  644. var openDocumentType: KMPreferenceOpenDocumentType { // KMOpenDocumentInTab
  645. get {
  646. guard let type = self.getData(forKey: KMOpenDocumentTypeKey) as? Int else {
  647. return self.openDocumentTypeDefaultValue
  648. }
  649. if type == 1 {
  650. return .newWindow
  651. }
  652. return .inSameWindow
  653. }
  654. set {
  655. if (newValue == .inSameWindow || newValue == .newWindow) {
  656. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenDocumentTypeKey)
  657. }
  658. }
  659. }
  660. var showInMenuBar: Bool {
  661. get {
  662. return self.getData(forKey: KMGeneralShowInMenuBarKey) as? Bool ?? self.showInMenuBarDefaultValue
  663. }
  664. set {
  665. _ = self.setData(data: newValue, forKey: KMGeneralShowInMenuBarKey)
  666. }
  667. }
  668. var openFileType: KMPreferenceOpenFileType {
  669. get {
  670. guard let type = self.getData(forKey: KMOpenFileTypeKey) as? Int else {
  671. return .default
  672. }
  673. if type == 1 {
  674. return .maxim
  675. } else if type == 2 {
  676. return .fit
  677. }
  678. return .default
  679. }
  680. set {
  681. if (newValue == .default || newValue == .maxim || newValue == .fit) {
  682. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenFileTypeKey)
  683. }
  684. }
  685. }
  686. var showLeftSideBar: Bool {
  687. get {
  688. return self.getData(forKey: KMShowLeftSideBarKey) as? Bool ?? self.showLeftSideBarDefaultValue
  689. }
  690. set {
  691. _ = self.setData(data: newValue, forKey: KMShowLeftSideBarKey)
  692. }
  693. }
  694. var rememberSnapshot: Bool {
  695. get {
  696. return self.getData(forKey: KMRememberSnapshotKey) as? Bool ?? self.rememberSnapshotDefaultValue
  697. }
  698. set {
  699. _ = self.setData(data: newValue, forKey: KMRememberSnapshotKey)
  700. }
  701. }
  702. var revertInitPDFViewSettingType: KMPreferenceRevertInitSettingType {
  703. get {
  704. guard let type = self.getData(forKey: KMRevertInitPDFViewSettingTypeKey) as? Int else {
  705. return self.revertInitPDFViewSettingTypeDefaultValue
  706. }
  707. if type == 1 {
  708. return .fullScreen
  709. }
  710. return .normal
  711. }
  712. set {
  713. if (newValue == .normal || newValue == .fullScreen) {
  714. let _ = self.setData(data: newValue.rawValue, forKey: KMRevertInitPDFViewSettingTypeKey)
  715. }
  716. }
  717. }
  718. var autoSaveNoteBackup: Bool {
  719. get {
  720. return self.getData(forKey: KMAutoSaveNoteBackupKey) as? Bool ?? self.autoSaveNoteBackupDefaultValue
  721. }
  722. set {
  723. _ = self.setData(data: newValue, forKey: KMAutoSaveNoteBackupKey)
  724. }
  725. }
  726. var keepSnapshotWindowToTop: Bool {
  727. get {
  728. return self.getData(forKey: KMKeepSnapshotWindowToTopKey) as? Bool ?? self.keepSnapshotWindowToTopDefaultValue()
  729. }
  730. set {
  731. _ = self.setData(data: newValue, forKey: KMKeepSnapshotWindowToTopKey)
  732. }
  733. }
  734. }
  735. // MARK: 扩展 Display
  736. extension KMPreferenceManager {
  737. var viewPageDisplayType: KMPDFDisplayType {
  738. get {
  739. let type: Int? = self.getData(forKey: KMViewPageDisplayTypeKey) as? Int
  740. if (type == nil || type == 1) {
  741. return .singlePageContinuous
  742. }
  743. if (type! == 0) {
  744. return .singlePage
  745. } else if (type! == 2) {
  746. return .twoUp
  747. } else if (type! == 3) {
  748. return .twoUpContinuous
  749. } else if (type! == 4) {
  750. return .bookMode
  751. } else if (type! == 5) {
  752. return .bookContinuous
  753. }
  754. return .singlePageContinuous
  755. }
  756. set {
  757. if (newValue == .singlePage || newValue == .singlePageContinuous || newValue == .twoUp ||
  758. newValue == .twoUpContinuous || newValue == .bookMode || newValue == .bookContinuous) {
  759. let _ = self.setData(data: newValue.rawValue, forKey: KMViewPageDisplayTypeKey)
  760. }
  761. }
  762. }
  763. var viewZoomScaleType: KMPDFZoomType {
  764. get {
  765. let type: Int? = self.getData(forKey: KMViewZoomScaleTypeKey) as? Int
  766. if (type == nil || type! == 0) {
  767. return .width
  768. } else if (type! == 1) {
  769. return .fit
  770. } else if (type! == 2) {
  771. return .actualSize
  772. }
  773. return .width
  774. }
  775. set {
  776. if (newValue == .width || newValue == .fit || newValue == .actualSize) {
  777. let _ = self.setData(data: newValue.rawValue, forKey: KMViewZoomScaleTypeKey)
  778. }
  779. }
  780. }
  781. var leftSideDisplayType: KMPreferenceLeftSideDisplayType {
  782. get {
  783. let type: Int? = self.getData(forKey: KMLeftSideDisplayTypeKey) as? Int
  784. if (type == nil || type! == 0) {
  785. return .closeWhenOpenFile
  786. } else if (type! == 1) {
  787. return .openAppSaveLastSelect
  788. } else if (type! == 2) {
  789. return .showOutlineIfHas
  790. }
  791. return .closeWhenOpenFile
  792. }
  793. set {
  794. if (newValue == .closeWhenOpenFile || newValue == .openAppSaveLastSelect || newValue == .showOutlineIfHas) {
  795. let _ = self.setData(data: newValue.rawValue, forKey: KMLeftSideDisplayTypeKey)
  796. }
  797. }
  798. }
  799. // var showOutlineList: Bool {
  800. // get {
  801. // return self.getData(forKey: KMShowOutlineListKey) as! Bool
  802. // }
  803. // set {
  804. // let _ = self.setData(data: newValue, forKey: KMShowOutlineListKey)
  805. // }
  806. // }
  807. // var leftSideExpandType: Int {
  808. // get {
  809. // return self.getData(forKey: KMLeftSideExpandTypeKey) as! Int
  810. // }
  811. // set {
  812. // let _ = self.setData(data: newValue, forKey: KMLeftSideExpandTypeKey)
  813. // }
  814. // }
  815. var propertyPanelExpandType: KMPreferencePropertyPanelExpandType {
  816. get {
  817. let type: Int? = self.getData(forKey: KMPropertyPanelExpandTypeKey) as? Int
  818. if (type == nil || type! == 0) {
  819. return .auto
  820. } else if (type! == 1) {
  821. return .manual
  822. }
  823. return .auto
  824. }
  825. set {
  826. if (newValue == .auto || newValue == .manual) {
  827. let _ = self.setData(data: newValue.rawValue, forKey: KMPropertyPanelExpandTypeKey)
  828. }
  829. }
  830. }
  831. var highlightForms: Bool {
  832. get {
  833. return self.getData(forKey: KMHighlightFormsKey) as! Bool
  834. }
  835. set {
  836. let _ = self.setData(data: newValue, forKey: KMHighlightFormsKey)
  837. }
  838. }
  839. var pageIndicatorType: KMPreferencePageIndicatorDisplayType {
  840. get {
  841. let type: Int? = self.getData(forKey: KMPageIndicatorTypeKey) as? Int
  842. if (type == nil || type! == 2) {
  843. return .never
  844. } else if (type! == 0) {
  845. return .automatic
  846. } else if (type! == 1) {
  847. return .always
  848. }
  849. return .never
  850. }
  851. set {
  852. if (newValue == .automatic || newValue == .always || newValue == .never) {
  853. let _ = self.setData(data: newValue.rawValue, forKey: KMPageIndicatorTypeKey)
  854. }
  855. }
  856. }
  857. var highlightLinks: Bool {
  858. get {
  859. return self.getData(forKey: KMHighlightLinksKey) as! Bool
  860. }
  861. set {
  862. let _ = self.setData(data: newValue, forKey: KMHighlightLinksKey)
  863. }
  864. }
  865. var displayBackgroundNormalColor: NSColor {
  866. get {
  867. return self.getColor(forKey: KMDisplayBackgroundNormalColorKey)
  868. }
  869. set {
  870. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  871. }
  872. }
  873. var displayBackgroundFullScreenColor: NSColor {
  874. get {
  875. return self.getColor(forKey: KMDisplayBackgroundFullScreenColorKey)
  876. }
  877. set {
  878. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  879. }
  880. }
  881. var displayFieldHighlightingColor: NSColor {
  882. get {
  883. return self.getColor(forKey: KMDisplayFieldHighlightingColorKey)
  884. }
  885. set {
  886. let _ = self.setColor(newValue, forKey: KMDisplayFieldHighlightingColorKey)
  887. }
  888. }
  889. var displayRequiredFieldHighlightingColor: NSColor {
  890. get {
  891. return self.getColor(forKey: KMDisplayRequiredFieldHighlightingColorKey)
  892. }
  893. set {
  894. let _ = self.setColor(newValue, forKey: KMDisplayRequiredFieldHighlightingColorKey)
  895. }
  896. }
  897. // Tip: 新补充
  898. var thumbPageSize: Float { // SKThumbnailSize
  899. get {
  900. return self.getData(forKey: KMThumbPageSizeKey) as? Float ?? self.thumbPageSizeDefaultValue()
  901. }
  902. set {
  903. let _ = self.setData(data: newValue, forKey: KMThumbPageSizeKey)
  904. }
  905. }
  906. var thumbSnapshotSize: Float { // SKSnapshotThumbnailSize
  907. get {
  908. return self.getData(forKey: KMThumbSnapshotSizeKey) as? Float ?? self.thumbSnapshotSizeDefaultValue()
  909. }
  910. set {
  911. let _ = self.setData(data: newValue, forKey: KMThumbSnapshotSizeKey)
  912. }
  913. }
  914. var discreteSize: Bool {
  915. get {
  916. return self.getData(forKey: KMDiscreteSizeKey) as? Bool ?? self.discreteSizeDefaultValue()
  917. }
  918. set {
  919. let _ = self.setData(data: newValue, forKey: KMDiscreteSizeKey)
  920. }
  921. }
  922. var outlineFontSize: Float { // SKTableFontSize
  923. get {
  924. return self.getData(forKey: KMOutlineFontSizeKey) as? Float ?? self.outlineFontSizeDefaultValue()
  925. }
  926. set {
  927. let _ = self.setData(data: newValue, forKey: KMOutlineFontSizeKey)
  928. }
  929. }
  930. var greekThreshold: Float { // SKGreekingThreshold
  931. get {
  932. return self.getData(forKey: KMGreekThresholdKey) as? Float ?? self.greekThresholdDefaultValue()
  933. }
  934. set {
  935. let _ = self.setData(data: newValue, forKey: KMGreekThresholdKey)
  936. }
  937. }
  938. var antiAliasText: Bool {
  939. get {
  940. return self.getData(forKey: KMAntiAliasTextKey) as? Bool ?? self.antiAliasTextDefaultValue()
  941. }
  942. set {
  943. let _ = self.setData(data: newValue, forKey: KMAntiAliasTextKey)
  944. }
  945. }
  946. var readBarColor: NSColor {
  947. get {
  948. return self.getColor(forKey: KMReadBarColorKey)
  949. }
  950. set {
  951. let _ = self.setColor(newValue, forKey: KMReadBarColorKey)
  952. }
  953. }
  954. var invertBar: Bool {
  955. get {
  956. return self.getData(forKey: KMInvertBarKey) as? Bool ?? self.invertBarDefaultValue()
  957. }
  958. set {
  959. let _ = self.setData(data: newValue, forKey: KMInvertBarKey)
  960. }
  961. }
  962. }
  963. // MARK: 扩展 Markup
  964. extension KMPreferenceManager {
  965. var markupHighlightColor: NSColor {
  966. get {
  967. return self.getColor(forKey: KMPreference.markupColorHighlightKey)
  968. }
  969. set {
  970. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorHighlightKey)
  971. let _ = self.setColor(newValue, forKey: KMPreference.markupColorHighlightKey)
  972. }
  973. }
  974. var markupStrikthroughColor: NSColor {
  975. get {
  976. return self.getColor(forKey: KMPreference.markupColorStrikthroughKey)
  977. }
  978. set {
  979. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  980. let _ = self.setColor(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  981. }
  982. }
  983. var markupUnderlineColor: NSColor {
  984. get {
  985. return self.getColor(forKey: KMPreference.markupColorUnderlineKey)
  986. }
  987. set {
  988. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorUnderlineKey)
  989. let _ = self.setColor(newValue, forKey: KMPreference.markupColorUnderlineKey)
  990. }
  991. }
  992. var markupPenColor: NSColor {
  993. get {
  994. return self.getColor(forKey: KMPreference.markupColorPenKey)
  995. }
  996. set {
  997. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorPenKey)
  998. let _ = self.setColor(newValue, forKey: KMPreference.markupColorPenKey)
  999. }
  1000. }
  1001. var markupTextColor: NSColor {
  1002. get {
  1003. return self.getColor(forKey: KMPreference.markupColorTextKey)
  1004. }
  1005. set {
  1006. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorTextKey)
  1007. let _ = self.setColor(newValue, forKey: KMPreference.markupColorTextKey)
  1008. }
  1009. }
  1010. var markupNoteColor: NSColor {
  1011. get {
  1012. return self.getColor(forKey: KMPreference.markupColorNoteKey)
  1013. }
  1014. set {
  1015. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorNoteKey)
  1016. let _ = self.setColor(newValue, forKey: KMPreference.markupColorNoteKey)
  1017. }
  1018. }
  1019. var markupRectangleFillColor: NSColor {
  1020. get {
  1021. return self.getColor(forKey: KMPreference.markupColorRectangleFillKey)
  1022. }
  1023. set {
  1024. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  1025. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  1026. }
  1027. }
  1028. var markupRectangleBorderColor: NSColor {
  1029. get {
  1030. return self.getColor(forKey: KMPreference.markupColorRectangleBorderKey)
  1031. }
  1032. set {
  1033. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  1034. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  1035. }
  1036. }
  1037. var markupCircleFillColor: NSColor {
  1038. get {
  1039. return self.getColor(forKey: KMPreference.markupColorCircleFillKey)
  1040. }
  1041. set {
  1042. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleFillKey)
  1043. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleFillKey)
  1044. }
  1045. }
  1046. var markupCircleBorderColor: NSColor {
  1047. get {
  1048. return self.getColor(forKey: KMPreference.markupColorCircleBorderKey)
  1049. }
  1050. set {
  1051. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  1052. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  1053. }
  1054. }
  1055. var markupLineColor: NSColor {
  1056. get {
  1057. return self.getColor(forKey: KMPreference.markupColorLineKey)
  1058. }
  1059. set {
  1060. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorLineKey)
  1061. let _ = self.setColor(newValue, forKey: KMPreference.markupColorLineKey)
  1062. }
  1063. }
  1064. var markupArrowColor: NSColor {
  1065. get {
  1066. return self.getColor(forKey: KMPreference.markupColorArrowKey)
  1067. }
  1068. set {
  1069. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorArrowKey)
  1070. let _ = self.setColor(newValue, forKey: KMPreference.markupColorArrowKey)
  1071. }
  1072. }
  1073. }
  1074. // MARK: -
  1075. // MARK: Additions
  1076. extension KMPreferenceManager {
  1077. var markupFontTextString: String {
  1078. get {
  1079. let string: String? = UserDefaults.standard.object(forKey: CFreeTextNoteFontNameKey) as? String
  1080. if (string == nil) {
  1081. return KMDefaultFontName
  1082. }
  1083. if (KMPreferenceManager.supportFonts.contains(string!)) {
  1084. return string!
  1085. }
  1086. return KMDefaultFontName
  1087. }
  1088. set {
  1089. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  1090. UserDefaults.standard.set(newValue, forKey: CFreeTextNoteFontNameKey)
  1091. UserDefaults.standard.synchronize()
  1092. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextStringKey : newValue])
  1093. }
  1094. }
  1095. }
  1096. var markupFontTextAligment: NSTextAlignment {
  1097. get {
  1098. let type: Int? = UserDefaults.standard.integer(forKey: CFreeTextNoteAlignmentKey)
  1099. if (type == nil) {
  1100. return .left
  1101. }
  1102. if (type! == 0 || type! == 1 || type == 2) {
  1103. return NSTextAlignment(rawValue: type!)!
  1104. }
  1105. return .left
  1106. }
  1107. set {
  1108. if (newValue == .left || newValue == .center || newValue == .right) {
  1109. UserDefaults.standard.set(newValue.rawValue, forKey: CFreeTextNoteAlignmentKey)
  1110. UserDefaults.standard.synchronize()
  1111. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextAligmentKey : newValue.rawValue])
  1112. }
  1113. }
  1114. }
  1115. var markupFontNoteString: String {
  1116. get {
  1117. let fontName: String? = KMPreferenceManager.shared.getData(forKey: KMPreference.markupFontNoteStringKey) as? String
  1118. if (fontName == nil) {
  1119. return KMDefaultFontName
  1120. }
  1121. if (KMPreference.supportFonts.contains(fontName!)) {
  1122. return fontName!
  1123. }
  1124. return KMDefaultFontName
  1125. }
  1126. set {
  1127. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  1128. let _ = KMPreferenceManager.shared.setData(data: newValue, forKey: KMPreference.markupFontNoteStringKey)
  1129. }
  1130. }
  1131. }
  1132. class var supportFonts: [String] {
  1133. get {
  1134. var fontNames: Array<String> = []
  1135. for fontInfo in CPDFAnnotationModel.supportFonts() {
  1136. if ((fontInfo is NSDictionary) == false) {
  1137. continue
  1138. }
  1139. for (familyString, styleStrings) in (fontInfo as! NSDictionary) {
  1140. if ((styleStrings is NSArray) == false) {
  1141. break
  1142. }
  1143. for styleString in (styleStrings as! NSArray) {
  1144. fontNames.append("\(familyString)-\(styleString)")
  1145. }
  1146. }
  1147. }
  1148. return fontNames
  1149. }
  1150. }
  1151. private func syncDataToPDFView(_ data: Any, forKey key: KMPreferenceKey) {
  1152. if (key == KMMarkupColorHighlightKey) {
  1153. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CHighlightNoteColorKey)
  1154. } else if (key == KMMarkupColorUnderlineKey) {
  1155. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CUnderlineNoteColorKey)
  1156. } else if (key == KMMarkupColorStrikthroughKey) {
  1157. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CStrikeOutNoteColorKey)
  1158. } else if (key == KMMarkupColorPenKey) {
  1159. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CInkNoteColorKey)
  1160. } else if (key == KMMarkupColorNoteKey) {
  1161. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CAnchoredNoteColorKey)
  1162. } else if (key == KMMarkupColorRectangleFillKey) {
  1163. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CSquareNoteInteriorColorKey)
  1164. } else if (key == KMMarkupColorRectangleBorderKey) {
  1165. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CSquareNoteColorKey)
  1166. } else if (key == KMMarkupColorCircleFillKey) {
  1167. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CCircleNoteInteriorColorKey)
  1168. } else if (key == KMMarkupColorCircleBorderKey) {
  1169. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CCircleNoteColorKey)
  1170. } else if (key == KMMarkupColorLineKey) {
  1171. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CLineNoteColorKey)
  1172. } else if (key == KMMarkupColorArrowKey) {
  1173. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CArrowNoteColorKey)
  1174. } else if (key == KMMarkupColorTextKey) {
  1175. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CFreeTextNoteColorKey)
  1176. } else if (key == KMMarkupFontTextStringKey) {
  1177. UserDefaults.standard.set(data, forKey: CFreeTextNoteFontNameKey)
  1178. UserDefaults.standard.synchronize()
  1179. } else if (key == KMMarkupFontTextAligmentKey) {
  1180. UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1181. UserDefaults.standard.synchronize()
  1182. } else if (key == KMMarkupFontNoteStringKey) {
  1183. // UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1184. // UserDefaults.standard.synchronize()
  1185. }
  1186. }
  1187. private func getDefaultColors(forKey key: KMPreferenceKey) -> [Double] {
  1188. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue]!
  1189. for key_i in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1190. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1191. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1192. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1193. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1194. if (key == key_i) {
  1195. return markupGroupInfo[key] as! [Double]
  1196. }
  1197. }
  1198. let displayGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.display.rawValue]!
  1199. for key_i in [KMDisplayBackgroundNormalColorKey, KMDisplayBackgroundFullScreenColorKey,
  1200. KMDisplayFieldHighlightingColorKey, KMDisplayRequiredFieldHighlightingColorKey] {
  1201. if (key == key_i) {
  1202. return displayGroupInfo[key] as! [Double]
  1203. }
  1204. }
  1205. return [0.0, 0.0, 0.0, 1.0]
  1206. }
  1207. private func getDefaultColor(forKey key: KMPreferenceKey) -> NSColor {
  1208. return self.dataToColor(colors: self.getDefaultColors(forKey: key))
  1209. }
  1210. private func resetDataToPDFView() {
  1211. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue]!
  1212. for key in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1213. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1214. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1215. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1216. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1217. self.syncDataToPDFView(self.dataToColor(colors: markupGroupInfo[key] as! [Double]), forKey: key)
  1218. }
  1219. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontTextStringKey)
  1220. self.syncDataToPDFView(NSTextAlignment.left.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1221. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontNoteStringKey)
  1222. }
  1223. public func resumeDataToPDFView() {
  1224. self.syncDataToPDFView(KMPreferenceManager.shared.markupHighlightColor, forKey: KMMarkupColorHighlightKey)
  1225. self.syncDataToPDFView(KMPreferenceManager.shared.markupUnderlineColor, forKey: KMMarkupColorUnderlineKey)
  1226. self.syncDataToPDFView(KMPreferenceManager.shared.markupStrikthroughColor, forKey: KMMarkupColorStrikthroughKey)
  1227. self.syncDataToPDFView(KMPreferenceManager.shared.markupPenColor, forKey: KMMarkupColorPenKey)
  1228. self.syncDataToPDFView(KMPreferenceManager.shared.markupNoteColor, forKey: KMMarkupColorNoteKey)
  1229. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleFillColor, forKey: KMMarkupColorRectangleFillKey)
  1230. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleBorderColor, forKey: KMMarkupColorRectangleBorderKey)
  1231. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleFillColor, forKey: KMMarkupColorCircleFillKey)
  1232. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleBorderColor, forKey: KMMarkupColorCircleBorderKey)
  1233. self.syncDataToPDFView(KMPreferenceManager.shared.markupLineColor, forKey: KMMarkupColorLineKey)
  1234. self.syncDataToPDFView(KMPreferenceManager.shared.markupArrowColor, forKey: KMMarkupColorArrowKey)
  1235. self.syncDataToPDFView(KMPreferenceManager.shared.markupTextColor, forKey: KMMarkupColorTextKey)
  1236. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextString, forKey: KMMarkupFontTextStringKey)
  1237. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextAligment.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1238. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontNoteString, forKey: KMMarkupFontNoteStringKey)
  1239. }
  1240. public func initDataForAppLaunch() {
  1241. // KMPreferenceManager.shared.author = NSFullUserName()
  1242. if (KMPreferenceManager.shared.autoSave) {
  1243. Task { @MainActor in
  1244. if await KMLightMemberManager.manager.canPayFunction() == false {
  1245. KMPreferenceManager.shared.autoSave = false
  1246. }
  1247. }
  1248. }
  1249. }
  1250. }
  1251. // MARK: -
  1252. // MARK: UserDefaults
  1253. fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath"
  1254. fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  1255. fileprivate let kKMViewSettingKey = "viewSetting"
  1256. fileprivate let kKMPageNumberKey = "pageNumber"
  1257. fileprivate let kKMPageScaleKey = "pageScale"
  1258. extension KMPreferenceManager {
  1259. var lastOpenFilepath: String? {
  1260. get {
  1261. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  1262. }
  1263. set {
  1264. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  1265. UserDefaults.standard.synchronize()
  1266. }
  1267. }
  1268. var lastOpenFilepaths: [String]? {
  1269. get {
  1270. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  1271. }
  1272. set {
  1273. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  1274. UserDefaults.standard.synchronize()
  1275. }
  1276. }
  1277. var viewSetting: [KMPreferenceViewSetting : Any]? {
  1278. get {
  1279. return UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [KMPreferenceViewSetting : Any]
  1280. }
  1281. set {
  1282. if (newValue == nil) {
  1283. UserDefaults.standard.set(newValue, forKey: kKMViewSettingKey)
  1284. UserDefaults.standard.synchronize()
  1285. return
  1286. }
  1287. let viewSetting: [String : Any]? = UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [String : Any]
  1288. var info: [String : Any] = [:]
  1289. if (viewSetting != nil) {
  1290. for (key, value) in viewSetting! {
  1291. info.updateValue(value, forKey: key)
  1292. }
  1293. }
  1294. for (key, value) in newValue! {
  1295. if (key == .pageNumber) { // 只处理 枚举
  1296. info.updateValue(value, forKey: key.rawValue)
  1297. }
  1298. }
  1299. UserDefaults.standard.set(info, forKey: kKMViewSettingKey)
  1300. UserDefaults.standard.synchronize()
  1301. }
  1302. }
  1303. func setPageNumber(_ number: Int, forKey key: String) {
  1304. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  1305. UserDefaults.standard.synchronize()
  1306. }
  1307. func getPageNumber(forKey key: String) -> Int? {
  1308. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  1309. }
  1310. func setPageScale(_ scale: Float, forKey key: String) {
  1311. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  1312. UserDefaults.standard.synchronize()
  1313. }
  1314. func getPageScale(forKey key: String) -> Float? {
  1315. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  1316. }
  1317. }
  1318. // MARK: -
  1319. // MARK: Qiuck
  1320. extension KMPreferenceManager {
  1321. internal func closeFileIsPrompt() -> Bool {
  1322. return self.closeFilePromptType == .promp
  1323. }
  1324. func leftSideNeedCloseWhenOpenFile() -> Bool {
  1325. return KMPreferenceManager.shared.leftSideDisplayType == .closeWhenOpenFile
  1326. }
  1327. var autoExpandPropertyPanel: Bool {
  1328. get {
  1329. return KMPreferenceManager.shared.propertyPanelExpandType == .auto
  1330. }
  1331. }
  1332. // 单位: 秒
  1333. var autoSaveTimeInterval: TimeInterval {
  1334. get {
  1335. var minute = KMPreferenceManager.shared.autoSavePerNumberMinute
  1336. if (minute < 5) {
  1337. minute = 5
  1338. } else if (minute > 99) {
  1339. minute = 99
  1340. }
  1341. let interval: TimeInterval = Double(minute) * 60.0
  1342. return interval
  1343. }
  1344. }
  1345. }