KMPreferenceManager.swift 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  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 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. // Tip: 新补充
  76. private let KMMarkupTextFontSizeKey: KMPreferenceKey = "KMMarkupTextFontSizeKey"
  77. private let KMMarkupNoteFontSizeKey: KMPreferenceKey = "KMMarkupNoteFontSizeKey"
  78. //private let KMFreeTextNoteLineWidthKey: KMPreferenceKey = "KMFreeTextNoteLineWidthKey"
  79. //private let KMFreeTextNoteLineStyleKey: KMPreferenceKey = "KMFreeTextNoteLineStyleKey"
  80. //private let KMFreeTextNoteDashPatternKey: KMPreferenceKey = "KMFreeTextNoteDashPatternKey"
  81. //private let KMCircleNoteLineWidthKey: KMPreferenceKey = "KMCircleNoteLineWidthKey"
  82. //private let KMCircleNoteLineStyleKey: KMPreferenceKey = "KMCircleNoteLineStyleKey"
  83. private let KMCircleNoteDashPatternKey: KMPreferenceKey = "KMCircleNoteDashPatternKey"
  84. //
  85. //private let KMSquareNoteLineWidthKey: KMPreferenceKey = "KMSquareNoteLineWidthKey"
  86. //private let KMSquareNoteLineStyleKey: KMPreferenceKey = "KMSquareNoteLineStyleKey"
  87. //private let KMSquareNoteDashPatternKey: KMPreferenceKey = "KMSquareNoteDashPatternKey"
  88. //private let KMLineNoteLineWidthKey: KMPreferenceKey = "KMLineNoteLineWidthKey"
  89. //private let KMLineNoteLineStyleKey: KMPreferenceKey = "KMLineNoteLineStyleKey"
  90. //private let KMLineNoteDashPatternKey: KMPreferenceKey = "KMLineNoteDashPatternKey"
  91. //private let KMLineNoteStartLineStyleKey: KMPreferenceKey = "KMLineNoteStartLineStyleKey"
  92. //private let KMLineNoteEndLineStyleKey: KMPreferenceKey = "KMLineNoteEndLineStyleKey"
  93. //
  94. //private let KMInkNoteLineWidthKey: KMPreferenceKey = "KMInkNoteLineWidthKey"
  95. //private let KMInkNoteLineStyleKey: KMPreferenceKey = "KMInkNoteLineStyleKey"
  96. //private let KMInkNoteDashPatternKey: KMPreferenceKey = "KMInkNoteDashPatternKey"
  97. /// 偏好设置已改变
  98. private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName"
  99. private let KMDefaultFontName = "Helvetica-Oblique"
  100. typealias KMPreference = KMPreferenceManager
  101. @objcMembers class KMPreferenceManager: NSObject {
  102. static let shared = KMPreferenceManager()
  103. private var generalGroupKeys: Array<KMPreferenceKey> = []
  104. private var displayGroupKeys: Array<KMPreferenceKey> = []
  105. private var markupGroupKeys: Array<KMPreferenceKey> = []
  106. public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName)
  107. /// general
  108. /// files
  109. public static let openLastUnclosedDocumentWhenAppStartKey = KMOpenLastUnclosedDocumentWhenAppStartKey
  110. public static let openLastUnlockedDocumentWhenAppStartKey = KMOpenLastUnlockedDocumentWhenAppStartKey
  111. public static let documentMaximunDisplayNumberKey = KMDocumentMaximunDisplayNumberKey
  112. public static let autoSaveKey = KMAutoSaveKey
  113. public static let autoSavePerNumberMinuteKey = KMAutoSavePerNumberMinuteKey
  114. public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey
  115. /// open image file
  116. public static let openImageFileTypeKey = KMOpenImageFileTypeKey
  117. public static let setDefaultPDFReaderKey = KMSetDefaultPDFReaderKey
  118. /// save password
  119. public static let savePasswordTypeKey = KMSavePasswordTypeKey
  120. public static let generalAuthorNameKey = KMGeneralAuthorNameKey
  121. // Tip: 新补充
  122. public static let openDocumentTypeKey = KMOpenDocumentTypeKey
  123. public static let showInMenuBarKey = KMGeneralShowInMenuBarKey
  124. public static let openFileTypeKey = KMOpenFileTypeKey
  125. public static let showLeftSideBarKey = KMShowLeftSideBarKey
  126. public static let rememberSnapshotKey = KMRememberSnapshotKey
  127. public static let revertInitPDFViewSettingTypeKey = KMRevertInitPDFViewSettingTypeKey
  128. public static let autoSaveNoteBackupKey = KMAutoSaveNoteBackupKey
  129. public static let keepSnapshotWindowToTopKey = KMKeepSnapshotWindowToTopKey
  130. /// display
  131. public static let viewPageDisplayTypeKey = KMViewPageDisplayTypeKey
  132. public static let viewZoomScaleTypeKey = KMViewZoomScaleTypeKey
  133. public static let leftSideDisplayTypeKey = KMLeftSideDisplayTypeKey
  134. public static let showOutlineListKey = KMShowOutlineListKey
  135. public static let leftSideExpandTypeKey = KMLeftSideExpandTypeKey
  136. public static let propertyPanelExpandTypeKey = KMPropertyPanelExpandTypeKey
  137. public static let displayBackgroundNormalColorKey = KMDisplayBackgroundNormalColorKey
  138. public static let displayBackgroundFullScreenColorKey = KMDisplayBackgroundFullScreenColorKey
  139. public static let highlightFormsKey = KMHighlightFormsKey
  140. public static let displayFieldHighlightingColorKey = KMDisplayFieldHighlightingColorKey
  141. public static let displayRequiredFieldHighlightingColorKey = KMDisplayRequiredFieldHighlightingColorKey
  142. public static let pageIndicatorTypeKey = KMPageIndicatorTypeKey
  143. public static let highlightLinksKey = KMHighlightLinksKey
  144. // Tip: 新补充
  145. public static let thumbPageSizeKey = KMThumbPageSizeKey
  146. public static let thumbSnapshotSizeKey = KMThumbSnapshotSizeKey
  147. public static let discreteSizeKey = KMDiscreteSizeKey
  148. public static let outlineFontSizeKey = KMOutlineFontSizeKey
  149. public static let greekThresholdKey = KMGreekThresholdKey
  150. public static let antiAliasTextKey = KMAntiAliasTextKey
  151. public static let readBarColorKey = KMReadBarColorKey
  152. public static let invertBarKey = KMInvertBarKey
  153. /// markup
  154. public static let markupColorHighlightKey = KMMarkupColorHighlightKey
  155. public static let markupColorStrikthroughKey = KMMarkupColorStrikthroughKey
  156. public static let markupColorUnderlineKey = KMMarkupColorUnderlineKey
  157. public static let markupColorPenKey = KMMarkupColorPenKey
  158. public static let markupColorTextKey = KMMarkupColorTextKey
  159. public static let markupColorNoteKey = KMMarkupColorNoteKey
  160. public static let markupColorRectangleFillKey = KMMarkupColorRectangleFillKey
  161. public static let markupColorRectangleBorderKey = KMMarkupColorRectangleBorderKey
  162. public static let markupColorCircleFillKey = KMMarkupColorCircleFillKey
  163. public static let markupColorCircleBorderKey = KMMarkupColorCircleBorderKey
  164. public static let markupColorLineKey = KMMarkupColorLineKey
  165. public static let markupColorArrowKey = KMMarkupColorArrowKey
  166. public static let markupFontTextStringKey = KMMarkupFontTextStringKey
  167. public static let markupFontTextAligmentKey = KMMarkupFontTextAligmentKey
  168. public static let markupFontNoteStringKey = KMMarkupFontNoteStringKey
  169. // Tip: - 新补充
  170. public static let markupTextFontSizeKey = KMMarkupTextFontSizeKey
  171. public static let markupNoteFontSizeKey = KMMarkupNoteFontSizeKey
  172. public static let freeTextNoteLineWidthKey = KMFreeTextNoteLineWidthKey
  173. public static let freeTextNoteLineStyleKey = KMFreeTextNoteLineStyleKey
  174. public static let freeTextNoteDashPatternKey = KMFreeTextNoteDashPatternKey
  175. public static let circleNoteLineWidthKey = KMCircleNoteLineWidthKey
  176. public static let circleNoteLineStyleKey = KMCircleNoteLineStyleKey
  177. public static let circleNoteDashPatternKey = KMCircleNoteDashPatternKey
  178. public static let squareNoteLineWidthKey = KMSquareNoteLineWidthKey
  179. public static let squareNoteLineStyleKey = KMSquareNoteLineStyleKey
  180. public static let squareNoteDashPatternKey = KMSquareNoteDashPatternKey
  181. public static let lineNoteLineWidthKey = KMLineNoteLineWidthKey
  182. public static let lineNoteLineStyleKey = KMLineNoteLineStyleKey
  183. public static let lineNoteDashPatternKey = KMLineNoteDashPatternKey
  184. public static let lineNoteStartLineStyleKey = KMLineNoteStartLineStyleKey
  185. public static let lineNoteEndLineStyleKey = KMLineNoteEndLineStyleKey
  186. public static let inkNoteLineWidthKey = KMInkNoteLineWidthKey
  187. public static let inkNoteLineStyleKey = KMInkNoteLineStyleKey
  188. public static let inkNoteDashPatternKey = KMInkNoteDashPatternKey
  189. override init() {
  190. super.init()
  191. /// 初始化
  192. generalGroupKeys = self.getDefaultKeys(for: .general)
  193. displayGroupKeys = self.getDefaultKeys(for: .display)
  194. markupGroupKeys = self.getDefaultKeys(for: .markup)
  195. let info = UserDefaults.standard.value(forKey: KMPreferenceInfoKey)
  196. if (info == nil) {
  197. let info = self.getDefaultInfo()
  198. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  199. UserDefaults.standard.synchronize()
  200. }
  201. }
  202. // MARK: 保存/获取数据
  203. public func setData(data: Any,forKey key: KMPreferenceKey) -> Bool {
  204. return self.setData(data: data, forKey: key, in: findGroup(forKey: key))
  205. }
  206. public func setData(data: Any,forKey key: KMPreferenceKey, in group: KMPreferenceGroup) -> Bool {
  207. var info: [String : Any]?
  208. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  209. info = self.getDefaultInfo()
  210. } else {
  211. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  212. }
  213. var groupInfo: [String : Any] = info?[group.rawValue] as? [String : Any] ?? [:]
  214. groupInfo.updateValue(data, forKey: key)
  215. info?.updateValue(groupInfo, forKey: group.rawValue)
  216. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  217. UserDefaults.standard.synchronize()
  218. if self._grouping {
  219. self._grouping_objects.insert(group)
  220. self._grouping_infos[key] = data
  221. } else {
  222. self.postNotification(object: [group], userInfo: [key : data])
  223. }
  224. return true
  225. }
  226. public func getData(forKey key: KMPreferenceKey) -> Any {
  227. var info: [String : Any]?
  228. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  229. info = self.getDefaultInfo()
  230. } else {
  231. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  232. }
  233. let groupInfo: [String : Any] = info?[findGroup(forKey: key).rawValue] as? [String : Any] ?? [:]
  234. return groupInfo[key] as Any
  235. }
  236. public func resetData(_ group: KMPreferenceGroup) {
  237. var info: [String : Any]?
  238. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  239. info = self.getDefaultInfo()
  240. } else {
  241. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]) ?? [:]
  242. }
  243. let groupInfo = self.getDefaultInfo()[group.rawValue]
  244. info?.updateValue(groupInfo as Any, forKey: group.rawValue)
  245. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  246. UserDefaults.standard.synchronize()
  247. self.resetDataToPDFView()
  248. self._resetDataToUserDefault(group: group)
  249. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  250. let info: [String : Any]? = UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]
  251. var groupInfo: [KMPreferenceKey : Any]?
  252. if (info != nil) {
  253. groupInfo = info![group.rawValue] as? [KMPreferenceKey : Any]
  254. }
  255. self.postNotification(object: [group], userInfo: groupInfo != nil ? groupInfo : [:])
  256. }
  257. }
  258. public func resetAllData() {
  259. let info = self.getDefaultInfo()
  260. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  261. UserDefaults.standard.synchronize()
  262. self.resetDataToPDFView()
  263. self._resetDataToUserDefault(group: .general)
  264. self._resetDataToUserDefault(group: .display)
  265. self._resetDataToUserDefault(group: .markup)
  266. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  267. var groppInfos: [KMPreferenceKey : Any] = [:]
  268. for groupInfo in info.values {
  269. for (key, value) in groupInfo {
  270. groppInfos.updateValue(value, forKey: key)
  271. }
  272. }
  273. self.postNotification(object: [KMPreferenceGroup.general, KMPreferenceGroup.display, KMPreferenceGroup.markup, KMPreferenceGroup.infomation, KMPreferenceGroup.other], userInfo: groppInfos)
  274. }
  275. }
  276. // MARK: 注册 key
  277. public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool {
  278. if (group == .general) {
  279. if (self.generalGroupKeys.contains(key)) {
  280. return false
  281. }
  282. self.generalGroupKeys.append(key)
  283. }
  284. return true
  285. }
  286. // MARK: -
  287. // MARK: 发布通知
  288. private func postNotification(object: [KMPreferenceGroup]?, userInfo: [KMPreferenceKey : Any]?) {
  289. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  290. NotificationCenter.default.post(name: KMPreferenceManager.didChangeNotification, object: object, userInfo: userInfo)
  291. }
  292. }
  293. // MARK: 获取信息
  294. private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup {
  295. if (self.generalGroupKeys.contains(key)) {
  296. return .general
  297. } else if (self.displayGroupKeys.contains(key)) {
  298. return .display
  299. } else if (self.markupGroupKeys.contains(key)) {
  300. return .markup
  301. }
  302. return .other
  303. }
  304. private func getDefaultKeys(for group: KMPreferenceGroup) -> Array<KMPreferenceKey> {
  305. if (group == .general) {
  306. return [KMOpenLastUnclosedDocumentWhenAppStartKey,
  307. KMOpenLastUnlockedDocumentWhenAppStartKey,
  308. KMDocumentMaximunDisplayNumberKey,
  309. KMAutoSaveKey,
  310. KMAutoSavePerNumberMinuteKey,
  311. KMCloseFilePromptTypeKey,
  312. KMOpenImageFileTypeKey,
  313. KMSetDefaultPDFReaderKey,
  314. KMSavePasswordTypeKey,
  315. KMGeneralAuthorNameKey,
  316. // Tip: 新补充
  317. KMOpenDocumentTypeKey, KMGeneralShowInMenuBarKey, KMOpenFileTypeKey, KMShowLeftSideBarKey,
  318. KMRememberSnapshotKey, KMRevertInitPDFViewSettingTypeKey, KMAutoSaveNoteBackupKey, KMKeepSnapshotWindowToTopKey]
  319. } else if (group == .display) {
  320. return [KMViewPageDisplayTypeKey,
  321. KMViewZoomScaleTypeKey,
  322. KMLeftSideDisplayTypeKey,
  323. KMShowOutlineListKey,
  324. KMLeftSideExpandTypeKey,
  325. KMPropertyPanelExpandTypeKey,
  326. KMDisplayBackgroundNormalColorKey,
  327. KMDisplayBackgroundFullScreenColorKey,
  328. KMHighlightFormsKey,
  329. KMDisplayFieldHighlightingColorKey,
  330. KMDisplayRequiredFieldHighlightingColorKey,
  331. KMPageIndicatorTypeKey,
  332. KMHighlightLinksKey,
  333. // Tip: 新补充
  334. KMThumbPageSizeKey, KMThumbSnapshotSizeKey, KMDiscreteSizeKey, KMOutlineFontSizeKey,
  335. KMGreekThresholdKey, KMAntiAliasTextKey, KMReadBarColorKey, KMInvertBarKey]
  336. } else if (group == .markup) {
  337. return [KMMarkupColorHighlightKey,
  338. KMMarkupColorUnderlineKey,
  339. KMMarkupColorStrikthroughKey,
  340. KMMarkupColorPenKey,
  341. KMMarkupColorLineKey,
  342. KMMarkupColorArrowKey,
  343. KMMarkupColorNoteKey,
  344. KMMarkupColorTextKey,
  345. KMMarkupColorCircleFillKey,
  346. KMMarkupColorCircleBorderKey,
  347. KMMarkupColorRectangleFillKey,
  348. KMMarkupColorRectangleBorderKey,
  349. KMMarkupFontTextStringKey,
  350. KMMarkupFontTextAligmentKey,
  351. KMMarkupFontNoteStringKey,
  352. // 新补充
  353. KMMarkupTextFontSizeKey, KMMarkupNoteFontSizeKey,
  354. KMFreeTextNoteLineWidthKey, KMFreeTextNoteLineStyleKey, KMFreeTextNoteDashPatternKey,
  355. KMCircleNoteLineWidthKey, KMCircleNoteLineStyleKey, KMCircleNoteDashPatternKey,
  356. KMSquareNoteLineWidthKey, KMSquareNoteLineStyleKey, KMSquareNoteDashPatternKey,
  357. KMLineNoteLineWidthKey, KMLineNoteLineStyleKey, KMLineNoteDashPatternKey, KMLineNoteStartLineStyleKey, KMLineNoteEndLineStyleKey,
  358. KMInkNoteLineWidthKey, KMInkNoteLineStyleKey, KMInkNoteDashPatternKey]
  359. }
  360. return []
  361. }
  362. private func getDefaultInfo() -> Dictionary<String, Dictionary<String, Any>> {
  363. return [KMPreferenceGroup.general.rawValue : [KMOpenLastUnclosedDocumentWhenAppStartKey : false,
  364. KMOpenLastUnlockedDocumentWhenAppStartKey : true,
  365. KMDocumentMaximunDisplayNumberKey : 10,
  366. KMAutoSaveKey : true,
  367. KMAutoSavePerNumberMinuteKey : 5,
  368. KMCloseFilePromptTypeKey : 0,
  369. KMOpenImageFileTypeKey : 0,
  370. KMSetDefaultPDFReaderKey : true,
  371. KMSavePasswordTypeKey : 2,
  372. KMGeneralAuthorNameKey : NSFullUserName(),
  373. // Tip: 新补充
  374. KMOpenDocumentTypeKey : self.openDocumentTypeDefaultValue.rawValue,
  375. KMGeneralShowInMenuBarKey : self.showInMenuBarDefaultValue,
  376. KMOpenFileTypeKey : self.openFileTypeDefaultValue.rawValue,
  377. KMShowLeftSideBarKey : self.showLeftSideBarDefaultValue,
  378. KMRememberSnapshotKey : self.rememberSnapshotDefaultValue,
  379. KMRevertInitPDFViewSettingTypeKey : self.revertInitPDFViewSettingTypeDefaultValue.rawValue,
  380. KMAutoSaveNoteBackupKey : self.autoSaveNoteBackupDefaultValue,
  381. KMKeepSnapshotWindowToTopKey : self.keepSnapshotWindowToTopDefaultValue()],
  382. KMPreferenceGroup.display.rawValue : [KMViewPageDisplayTypeKey : 1,
  383. KMViewZoomScaleTypeKey : 0,
  384. KMLeftSideDisplayTypeKey : 0,
  385. KMShowOutlineListKey: true,
  386. KMLeftSideExpandTypeKey : 0,
  387. KMPropertyPanelExpandTypeKey : 0,
  388. KMDisplayBackgroundNormalColorKey : self.displayBackgroundNormalColorValues,
  389. KMDisplayBackgroundFullScreenColorKey : self.displayBackgroundFullScreenColorValues,
  390. KMHighlightFormsKey : true,
  391. KMDisplayFieldHighlightingColorKey : self.displayFieldHighlightingColorValues,
  392. KMDisplayRequiredFieldHighlightingColorKey : self.displayRequiredFieldHighlightingColorValues,
  393. KMPageIndicatorTypeKey : 0,
  394. KMHighlightLinksKey : true,
  395. // Tip: 新补充
  396. KMThumbPageSizeKey : self.thumbPageSizeDefaultValue(),
  397. KMThumbSnapshotSizeKey : self.thumbSnapshotSizeDefaultValue(),
  398. KMDiscreteSizeKey : self.discreteSizeDefaultValue(),
  399. KMOutlineFontSizeKey : self.outlineFontSizeDefaultValue(),
  400. KMGreekThresholdKey : self.greekThresholdDefaultValue(),
  401. KMAntiAliasTextKey : self.antiAliasTextDefaultValue(),
  402. KMReadBarColorKey : self.readBarColorValues,
  403. KMInvertBarKey : self.invertBarDefaultValue()],
  404. KMPreferenceGroup.markup.rawValue : [KMMarkupColorHighlightKey : self.markupHighlightColorValues,
  405. KMMarkupColorStrikthroughKey : self.markupStrikthroughColorValues,
  406. KMMarkupColorUnderlineKey : self.markupUnderlineColorValues,
  407. KMMarkupColorPenKey : self.markupPenColorValues,
  408. KMMarkupColorTextKey : self.markupTextColorValues,
  409. KMMarkupColorNoteKey : self.markupNoteColorValues,
  410. KMMarkupColorRectangleFillKey : self.markupRectangleFillColorValues,
  411. KMMarkupColorRectangleBorderKey : self.markupRectangleBorderColorValues,
  412. KMMarkupColorCircleFillKey : self.markupCircleFillColorValues,
  413. KMMarkupColorCircleBorderKey : self.markupCircleBorderColorValues,
  414. KMMarkupColorLineKey : self.markupLineColorValues,
  415. KMMarkupColorArrowKey : self.markupArrowColorValues,
  416. KMMarkupFontTextStringKey : KMDefaultFontName,
  417. KMMarkupFontTextAligmentKey : 0,
  418. KMMarkupFontNoteStringKey : KMDefaultFontName,
  419. // 新补充
  420. KMMarkupTextFontSizeKey : self.textFontSizeDefaultValue(),
  421. KMMarkupNoteFontSizeKey : self.noteFontSizeDefaultValue(),
  422. KMFreeTextNoteLineWidthKey : self.freeTextNoteLineWidthDefaultValue(),
  423. KMFreeTextNoteLineStyleKey : self.freeTextNoteLineStyleDefaultValue(),
  424. KMFreeTextNoteDashPatternKey : self.freeTextNoteDashPatternDefaultValue(),
  425. KMCircleNoteLineWidthKey : self.circleNoteLineWidthDefaultValue(),
  426. KMCircleNoteLineStyleKey : self.circleNoteLineStyleDefaultValue(),
  427. KMCircleNoteDashPatternKey : self.circleNoteDashPatternDefaultValue(),
  428. KMSquareNoteLineWidthKey : self.squareNoteLineWidthDefaultValue(),
  429. KMSquareNoteLineStyleKey : self.squareNoteLineStyleDefaultValue(),
  430. KMSquareNoteDashPatternKey : self.squareNoteDashPatternDefaultValue(),
  431. KMLineNoteLineWidthKey : self.lineNoteLineWidthDefaultValue(),
  432. KMLineNoteLineStyleKey : self.lineNoteLineStyleDefaultValue(),
  433. KMLineNoteDashPatternKey : self.lineNoteDashPatternDefaultValue(),
  434. KMLineNoteStartLineStyleKey : self.lineNoteStartLineStyleDefaultValue(),
  435. KMLineNoteEndLineStyleKey : self.lineNoteEndLineStyleDefaultValue(),
  436. KMInkNoteLineWidthKey : self.inkNoteLineWidthDefaultValue(),
  437. KMInkNoteLineStyleKey : self.inkNoteLineStyleDefaultValue(),
  438. KMInkNoteDashPatternKey : self.inkNoteDashPatternDefaultValue()],
  439. KMPreferenceGroup.infomation.rawValue : [:],
  440. KMPreferenceGroup.other.rawValue : [:]]
  441. }
  442. }
  443. // MARK: - 默认值
  444. extension KMPreferenceManager {
  445. // General
  446. var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType {
  447. get {
  448. return .inSameWindow
  449. }
  450. }
  451. var showInMenuBarDefaultValue: Bool {
  452. get {
  453. return false
  454. }
  455. }
  456. var openFileTypeDefaultValue: KMPreferenceOpenFileType {
  457. get {
  458. return .default
  459. }
  460. }
  461. var showLeftSideBarDefaultValue: Bool {
  462. get {
  463. return true
  464. }
  465. }
  466. var rememberSnapshotDefaultValue: Bool {
  467. get {
  468. return true
  469. }
  470. }
  471. var revertInitPDFViewSettingTypeDefaultValue: KMPreferenceRevertInitSettingType {
  472. get {
  473. return .normal
  474. }
  475. }
  476. var autoSaveNoteBackupDefaultValue: Bool {
  477. get {
  478. return false
  479. }
  480. }
  481. // 内联函数
  482. @inline (__always) func keepSnapshotWindowToTopDefaultValue() -> Bool {
  483. return true
  484. }
  485. // Display
  486. @inline (__always) func thumbPageSizeDefaultValue() -> Float {
  487. return 128
  488. }
  489. @inline (__always) func thumbSnapshotSizeDefaultValue() -> Float {
  490. return 128
  491. }
  492. @inline (__always) func discreteSizeDefaultValue() -> Bool {
  493. return false
  494. }
  495. @inline (__always) func outlineFontSizeDefaultValue() -> Float {
  496. return 12
  497. }
  498. @inline (__always) func greekThresholdDefaultValue() -> Float {
  499. return 3
  500. }
  501. @inline (__always) func antiAliasTextDefaultValue() -> Bool {
  502. return true
  503. }
  504. // @inline (__always) func readBarColorDefaultValue() -> Float {
  505. // return 0
  506. // }
  507. @inline (__always) func invertBarDefaultValue() -> Bool {
  508. return false
  509. }
  510. @inline (__always) func textFontSizeDefaultValue() -> Float {
  511. return 11.0
  512. }
  513. @inline (__always) func noteFontSizeDefaultValue() -> Float {
  514. return 12.0
  515. }
  516. @inline (__always) func freeTextNoteLineWidthDefaultValue() -> Float {
  517. return 0.0
  518. }
  519. @inline (__always) func freeTextNoteLineStyleDefaultValue() -> Int {
  520. return 0
  521. }
  522. @inline (__always) func freeTextNoteDashPatternDefaultValue() -> [CGFloat] {
  523. return []
  524. }
  525. @inline (__always) func circleNoteLineWidthDefaultValue() -> Float {
  526. return 2.0
  527. }
  528. @inline (__always) func circleNoteLineStyleDefaultValue() -> Int {
  529. return 0
  530. }
  531. @inline (__always) func circleNoteDashPatternDefaultValue() -> [CGFloat] {
  532. return []
  533. }
  534. @inline (__always) func squareNoteLineWidthDefaultValue() -> Float {
  535. return 2.0
  536. }
  537. @inline (__always) func squareNoteLineStyleDefaultValue() -> Int {
  538. return 0
  539. }
  540. @inline (__always) func squareNoteDashPatternDefaultValue() -> [CGFloat] {
  541. return []
  542. }
  543. @inline (__always) func lineNoteLineWidthDefaultValue() -> Float {
  544. return 2.0
  545. }
  546. @inline (__always) func lineNoteLineStyleDefaultValue() -> Int {
  547. return 0
  548. }
  549. @inline (__always) func lineNoteDashPatternDefaultValue() -> [CGFloat] {
  550. return []
  551. }
  552. @inline (__always) func lineNoteStartLineStyleDefaultValue() -> Int {
  553. return 0
  554. }
  555. @inline (__always) func lineNoteEndLineStyleDefaultValue() -> Int {
  556. return 0
  557. }
  558. @inline (__always) func inkNoteLineWidthDefaultValue() -> Float {
  559. return 2.0
  560. }
  561. @inline (__always) func inkNoteLineStyleDefaultValue() -> Int {
  562. return 0
  563. }
  564. @inline (__always) func inkNoteDashPatternDefaultValue() -> [CGFloat] {
  565. return []
  566. }
  567. }
  568. //protocol KMPreferenceManagerColorPart: NSObjectProtocol {
  569. // var markupHighlightColorValues: [Double] { get }
  570. //}
  571. // MARK: - 扩展 颜色
  572. extension KMPreferenceManager {
  573. fileprivate var displayBackgroundNormalColorValues: [Double] {
  574. get {
  575. return [206/255.0, 208/255.0, 212/255.0, 1.0]
  576. }
  577. }
  578. fileprivate var displayBackgroundFullScreenColorValues: [Double] {
  579. get {
  580. return [54/255.0, 56/255.0, 59/255.0, 1.0]
  581. }
  582. }
  583. fileprivate var displayFieldHighlightingColorValues: [Double] {
  584. get {
  585. return [189/255.0, 223/255.0, 253/255.0, 1.0]
  586. }
  587. }
  588. fileprivate var displayRequiredFieldHighlightingColorValues: [Double] {
  589. get {
  590. return [23/255.0, 112/255.0, 224/255.0, 1.0]
  591. }
  592. }
  593. fileprivate var markupHighlightColorValues: [Double] {
  594. get {
  595. return [255/255.0, 199/255.0, 0.0, 1.0]
  596. }
  597. }
  598. fileprivate var markupStrikthroughColorValues: [Double] {
  599. get {
  600. return [252/255.0, 31/255.0, 31/255.0, 1.0]
  601. }
  602. }
  603. fileprivate var markupUnderlineColorValues: [Double] {
  604. get {
  605. return [30/255.0, 137/255.0, 86/255.0, 1.0]
  606. }
  607. }
  608. fileprivate var markupPenColorValues: [Double] {
  609. get {
  610. self.markupStrikthroughColorValues
  611. }
  612. }
  613. fileprivate var markupTextColorValues: [Double] {
  614. get {
  615. return [37/255.0, 38/255.0, 41/255.0, 1.0]
  616. }
  617. }
  618. fileprivate var markupNoteColorValues: [Double] {
  619. get {
  620. return [255/255.0, 213/255.0, 115/255.0, 1.0]
  621. }
  622. }
  623. fileprivate var markupRectangleFillColorValues: [Double] {
  624. get {
  625. return [0/255.0, 0/255.0, 0/255.0, 0.0]
  626. }
  627. }
  628. fileprivate var markupRectangleBorderColorValues: [Double] {
  629. get {
  630. self.markupStrikthroughColorValues
  631. }
  632. }
  633. fileprivate var markupCircleFillColorValues: [Double] {
  634. get {
  635. self.markupRectangleFillColorValues
  636. }
  637. }
  638. fileprivate var markupCircleBorderColorValues: [Double] {
  639. get {
  640. return self.markupRectangleBorderColorValues
  641. }
  642. }
  643. fileprivate var markupLineColorValues: [Double] {
  644. get {
  645. self.markupStrikthroughColorValues
  646. }
  647. }
  648. fileprivate var markupArrowColorValues: [Double] {
  649. get {
  650. self.markupStrikthroughColorValues
  651. }
  652. }
  653. fileprivate var readBarColorValues: [Double] {
  654. get {
  655. return [23/255.0, 112/255.0, 224/255.0, 1.0]
  656. }
  657. }
  658. private func setColor(_ color: NSColor, forKey key: KMPreferenceKey) -> Bool {
  659. var red: CGFloat = 0.0
  660. var green: CGFloat = 0.0
  661. var blue: CGFloat = 0.0
  662. var alpha: CGFloat = 0.0
  663. color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  664. return KMPreferenceManager.shared.setData(data: [red, green, blue, alpha], forKey: key)
  665. }
  666. private func getColor(forKey key: KMPreferenceKey) -> NSColor {
  667. var colors: [Double]? = self.getData(forKey: key) as? [Double]
  668. if (colors == nil) {
  669. colors = self.getDefaultColors(forKey: key)
  670. }
  671. return NSColor(red: colors![0], green: colors![1], blue: colors![2], alpha: colors![3])
  672. }
  673. private func dataToColor(colors:[Double]) -> NSColor {
  674. NSColor(red: colors[0], green: colors[1], blue: colors[2], alpha: colors[3])
  675. }
  676. }
  677. // MARK: 扩展 General
  678. extension KMPreferenceManager {
  679. var openLastUnclosedDocumentWhenAppStart: Bool {
  680. get {
  681. return KMDataManager.ud_bool(forKey: KMReopenLastOpenFilesKey)
  682. }
  683. set {
  684. let pKey = KMPreference.openLastUnclosedDocumentWhenAppStartKey
  685. self._syncDataToUserDefault(newValue, forKey: pKey)
  686. _ = self.setData(data: newValue, forKey: pKey)
  687. }
  688. }
  689. var openLastUnlockedDocumentWhenAppStart: Bool {
  690. get {
  691. return self.getData(forKey: KMOpenLastUnlockedDocumentWhenAppStartKey) as? Bool ?? true
  692. }
  693. set {
  694. let _ = self.setData(data: newValue, forKey: KMOpenLastUnlockedDocumentWhenAppStartKey)
  695. }
  696. }
  697. var documentMaximunDisplayNumber: Int {
  698. get {
  699. return self.getData(forKey: KMDocumentMaximunDisplayNumberKey) as? Int ?? 10
  700. }
  701. set {
  702. let _ = self.setData(data: newValue, forKey: KMDocumentMaximunDisplayNumberKey)
  703. }
  704. }
  705. var autoSave: Bool {
  706. get {
  707. return self.getData(forKey: KMAutoSaveKey) as? Bool ?? true
  708. }
  709. set {
  710. let _ = self.setData(data: newValue, forKey: KMAutoSaveKey)
  711. }
  712. }
  713. var autoSavePerNumberMinute: Int {
  714. get {
  715. return self.getData(forKey: KMAutoSavePerNumberMinuteKey) as? Int ?? 5
  716. }
  717. set {
  718. let _ = self.setData(data: newValue, forKey: KMAutoSavePerNumberMinuteKey)
  719. }
  720. }
  721. var closeFilePromptType: KMPreferenceCloseFilePromptType {
  722. get {
  723. if let type = self.getData(forKey: KMCloseFilePromptTypeKey) as? Int {
  724. if type == 1 { // 无提示,直接保存
  725. return .noPromp
  726. }
  727. }
  728. return .promp
  729. }
  730. set {
  731. if (newValue == .promp || newValue == .noPromp) {
  732. let _ = self.setData(data: newValue.rawValue, forKey: KMCloseFilePromptTypeKey)
  733. }
  734. }
  735. }
  736. var openImageFileType: Int {
  737. get {
  738. return self.getData(forKey: KMOpenImageFileTypeKey) as? Int ?? 0
  739. }
  740. set {
  741. let _ = self.setData(data: newValue, forKey: KMOpenImageFileTypeKey)
  742. }
  743. }
  744. var author: String {
  745. get {
  746. return KMDataManager.ud_string(forKey: KMUserNameKey) ?? NSFullUserName()
  747. }
  748. set {
  749. self._syncDataToUserDefault(newValue, forKey: KMGeneralAuthorNameKey)
  750. let _ = self.setData(data: newValue, forKey: KMGeneralAuthorNameKey)
  751. }
  752. }
  753. var setDefaultPDFReader: Bool {
  754. get {
  755. return self.getData(forKey: KMSetDefaultPDFReaderKey) as? Bool ?? true
  756. }
  757. set {
  758. let result = KMTools.setDefaultPDFReader(newValue)
  759. if (result) {
  760. let _ = self.setData(data: newValue, forKey: KMSetDefaultPDFReaderKey)
  761. }
  762. }
  763. }
  764. var savePasswordType: KMPreferenceSavePasswordType {
  765. get {
  766. let type = KMDataManager.ud_integer(forKey: KMSavePasswordOptionKey)
  767. if type == -1 {
  768. return .ask
  769. } else if type == 1 {
  770. return .always
  771. }
  772. return .never
  773. }
  774. set {
  775. if (newValue == .always || newValue == .never || newValue == .ask) {
  776. var data: Int = 0
  777. if newValue == .always {
  778. data = 1
  779. } else if newValue == .ask {
  780. data = -1
  781. }
  782. self._syncDataToUserDefault(data, forKey: KMSavePasswordTypeKey)
  783. let _ = self.setData(data: newValue.rawValue, forKey: KMSavePasswordTypeKey)
  784. }
  785. }
  786. }
  787. var openDocumentType: KMPreferenceOpenDocumentType { // KMOpenDocumentInTab
  788. get {
  789. guard let num = KMDataManager.ud_object(forKey: KMOpenDocumentInTabKey) as? NSNumber else {
  790. return self.openDocumentTypeDefaultValue
  791. }
  792. if num.intValue == 1 {
  793. return .newWindow
  794. }
  795. return .inSameWindow
  796. }
  797. set {
  798. if (newValue == .inSameWindow || newValue == .newWindow) {
  799. let pKey = KMOpenDocumentTypeKey
  800. self._syncDataToUserDefault(NSNumber(value: newValue.rawValue), forKey: pKey)
  801. _ = self.setData(data: newValue.rawValue, forKey: pKey)
  802. }
  803. }
  804. }
  805. var showInMenuBar: Bool {
  806. get {
  807. return self.getData(forKey: KMGeneralShowInMenuBarKey) as? Bool ?? self.showInMenuBarDefaultValue
  808. }
  809. set {
  810. _ = self.setData(data: newValue, forKey: KMGeneralShowInMenuBarKey)
  811. }
  812. }
  813. var openFileType: KMPreferenceOpenFileType {
  814. get {
  815. // 数据兼容
  816. let value = KMDataManager.ud_integer(forKey: KMInitialWindowSizeOptionKey)
  817. if let type = KMPreferenceOpenFileType(rawValue: value) {
  818. return type
  819. }
  820. // guard let type = self.getData(forKey: KMOpenFileTypeKey) as? Int else {
  821. // return .default
  822. // }
  823. //
  824. // if type == 1 {
  825. // return .maxim
  826. // } else if type == 2 {
  827. // return .fit
  828. // }
  829. return .default
  830. }
  831. set {
  832. if (newValue == .default || newValue == .maxim || newValue == .fit) {
  833. // 数据兼容
  834. self._syncDataToUserDefault(newValue.rawValue, forKey: KMOpenFileTypeKey)
  835. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenFileTypeKey)
  836. }
  837. }
  838. }
  839. var showLeftSideBar: Bool {
  840. get {
  841. return KMDataManager.ud_bool(forKey: KMOpenContentsPaneOnlyForTOCKey)
  842. // return self.getData(forKey: KMShowLeftSideBarKey) as? Bool ?? self.showLeftSideBarDefaultValue
  843. }
  844. set {
  845. self._syncDataToUserDefault(newValue, forKey: KMShowLeftSideBarKey)
  846. _ = self.setData(data: newValue, forKey: KMShowLeftSideBarKey)
  847. }
  848. }
  849. var rememberSnapshot: Bool {
  850. get {
  851. return KMDataManager.ud_bool(forKey: KMRememberSnapshotsKey)
  852. // return self.getData(forKey: KMRememberSnapshotKey) as? Bool ?? self.rememberSnapshotDefaultValue
  853. }
  854. set {
  855. // 数据兼容
  856. self._syncDataToUserDefault(newValue, forKey: KMRememberSnapshotKey)
  857. _ = self.setData(data: newValue, forKey: KMRememberSnapshotKey)
  858. }
  859. }
  860. var revertInitPDFViewSettingType: KMPreferenceRevertInitSettingType {
  861. get {
  862. guard let type = self.getData(forKey: KMRevertInitPDFViewSettingTypeKey) as? Int else {
  863. return self.revertInitPDFViewSettingTypeDefaultValue
  864. }
  865. if type == 1 {
  866. return .fullScreen
  867. }
  868. return .normal
  869. }
  870. set {
  871. if (newValue == .normal || newValue == .fullScreen) {
  872. let _ = self.setData(data: newValue.rawValue, forKey: KMRevertInitPDFViewSettingTypeKey)
  873. }
  874. }
  875. }
  876. var autoSaveNoteBackup: Bool {
  877. get {
  878. return KMDataManager.ud_bool(forKey: KMAutoSaveSkimNotesKey)
  879. // return self.getData(forKey: KMAutoSaveNoteBackupKey) as? Bool ?? self.autoSaveNoteBackupDefaultValue
  880. }
  881. set {
  882. self._syncDataToUserDefault(newValue, forKey: KMAutoSaveNoteBackupKey)
  883. _ = self.setData(data: newValue, forKey: KMAutoSaveNoteBackupKey)
  884. }
  885. }
  886. var keepSnapshotWindowToTop: Bool {
  887. get {
  888. return KMDataManager.ud_bool(forKey: KMSnapshotsOnTopKey)
  889. // return self.getData(forKey: KMKeepSnapshotWindowToTopKey) as? Bool ?? self.keepSnapshotWindowToTopDefaultValue()
  890. }
  891. set {
  892. // 数据兼容
  893. self._syncDataToUserDefault(newValue, forKey: KMKeepSnapshotWindowToTopKey)
  894. _ = self.setData(data: newValue, forKey: KMKeepSnapshotWindowToTopKey)
  895. }
  896. }
  897. }
  898. // MARK: 扩展 Display
  899. extension KMPreferenceManager {
  900. var viewPageDisplayType: KMPDFDisplayType {
  901. get {
  902. if let type = self.getData(forKey: KMViewPageDisplayTypeKey) as? Int {
  903. if (type == 0) {
  904. return .singlePage
  905. } else if type == 1 {
  906. return .singlePageContinuous
  907. } else if (type == 2) {
  908. return .twoUp
  909. } else if (type == 3) {
  910. return .twoUpContinuous
  911. } else if (type == 4) {
  912. return .bookMode
  913. } else if (type == 5) {
  914. return .bookContinuous
  915. }
  916. }
  917. return .singlePageContinuous
  918. }
  919. set {
  920. if (newValue == .singlePage || newValue == .singlePageContinuous || newValue == .twoUp ||
  921. newValue == .twoUpContinuous || newValue == .bookMode || newValue == .bookContinuous) {
  922. let _ = self.setData(data: newValue.rawValue, forKey: KMViewPageDisplayTypeKey)
  923. }
  924. }
  925. }
  926. var viewZoomScaleType: KMPDFZoomType {
  927. get {
  928. if let type = self.getData(forKey: KMViewZoomScaleTypeKey) as? Int {
  929. if (type == 0) {
  930. return .width
  931. } else if (type == 1) {
  932. return .fit
  933. } else if (type == 2) {
  934. return .actualSize
  935. }
  936. }
  937. return .width
  938. }
  939. set {
  940. if (newValue == .width || newValue == .fit || newValue == .actualSize) {
  941. let _ = self.setData(data: newValue.rawValue, forKey: KMViewZoomScaleTypeKey)
  942. }
  943. }
  944. }
  945. var leftSideDisplayType: KMPreferenceLeftSideDisplayType {
  946. get {
  947. if let type = self.getData(forKey: KMLeftSideDisplayTypeKey) as? Int {
  948. if type == 0 {
  949. return .closeWhenOpenFile
  950. } else if (type == 1) {
  951. return .openAppSaveLastSelect
  952. } else if (type == 2) {
  953. return .showOutlineIfHas
  954. }
  955. }
  956. return .closeWhenOpenFile
  957. }
  958. set {
  959. if (newValue == .closeWhenOpenFile || newValue == .openAppSaveLastSelect || newValue == .showOutlineIfHas) {
  960. let _ = self.setData(data: newValue.rawValue, forKey: KMLeftSideDisplayTypeKey)
  961. }
  962. }
  963. }
  964. var propertyPanelExpandType: KMPreferencePropertyPanelExpandType {
  965. get {
  966. if let type = self.getData(forKey: KMPropertyPanelExpandTypeKey) as? Int {
  967. if (type == 0) {
  968. return .auto
  969. } else if (type == 1) {
  970. return .manual
  971. }
  972. }
  973. return .auto
  974. }
  975. set {
  976. if (newValue == .auto || newValue == .manual) {
  977. let _ = self.setData(data: newValue.rawValue, forKey: KMPropertyPanelExpandTypeKey)
  978. }
  979. }
  980. }
  981. var highlightForms: Bool {
  982. get {
  983. return self.getData(forKey: KMHighlightFormsKey) as? Bool ?? true
  984. }
  985. set {
  986. let _ = self.setData(data: newValue, forKey: KMHighlightFormsKey)
  987. }
  988. }
  989. var pageIndicatorType: KMPreferencePageIndicatorDisplayType {
  990. get {
  991. if let type = self.getData(forKey: KMPageIndicatorTypeKey) as? Int {
  992. if (type == 2) {
  993. return .never
  994. } else if (type == 0) {
  995. return .automatic
  996. } else if (type == 1) {
  997. return .always
  998. }
  999. }
  1000. return .never
  1001. }
  1002. set {
  1003. if (newValue == .automatic || newValue == .always || newValue == .never) {
  1004. let _ = self.setData(data: newValue.rawValue, forKey: KMPageIndicatorTypeKey)
  1005. }
  1006. }
  1007. }
  1008. var highlightLinks: Bool {
  1009. get {
  1010. return self.getData(forKey: KMHighlightLinksKey) as? Bool ?? true
  1011. }
  1012. set {
  1013. let _ = self.setData(data: newValue, forKey: KMHighlightLinksKey)
  1014. }
  1015. }
  1016. var displayBackgroundNormalColor: NSColor {
  1017. get {
  1018. if let color = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  1019. return color
  1020. }
  1021. return self.getColor(forKey: KMDisplayBackgroundNormalColorKey)
  1022. }
  1023. set {
  1024. self._syncDataToUserDefault(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  1025. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  1026. }
  1027. }
  1028. var displayBackgroundFullScreenColor: NSColor {
  1029. get {
  1030. if let color = UserDefaults.standard.color(forKey: KMFullScreenBackgroundColorKey) {
  1031. return color
  1032. }
  1033. return self.getColor(forKey: KMDisplayBackgroundFullScreenColorKey)
  1034. }
  1035. set {
  1036. self._syncDataToUserDefault(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  1037. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  1038. }
  1039. }
  1040. var displayFieldHighlightingColor: NSColor {
  1041. get {
  1042. return self.getColor(forKey: KMDisplayFieldHighlightingColorKey)
  1043. }
  1044. set {
  1045. let _ = self.setColor(newValue, forKey: KMDisplayFieldHighlightingColorKey)
  1046. }
  1047. }
  1048. var displayRequiredFieldHighlightingColor: NSColor {
  1049. get {
  1050. return self.getColor(forKey: KMDisplayRequiredFieldHighlightingColorKey)
  1051. }
  1052. set {
  1053. let _ = self.setColor(newValue, forKey: KMDisplayRequiredFieldHighlightingColorKey)
  1054. }
  1055. }
  1056. // Tip: 新补充
  1057. var thumbPageSize: Float {
  1058. get {
  1059. let num = KMDataManager.ud_object(forKey: KMThumbnailSizeKey) as? NSNumber
  1060. return num?.floatValue ?? self.thumbPageSizeDefaultValue()
  1061. // return self.getData(forKey: KMThumbPageSizeKey) as? Float ?? self.thumbPageSizeDefaultValue()
  1062. }
  1063. set {
  1064. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMThumbPageSizeKey)
  1065. let _ = self.setData(data: NSNumber(value: newValue), forKey: KMThumbPageSizeKey)
  1066. }
  1067. }
  1068. var thumbSnapshotSize: Float {
  1069. get {
  1070. // return self.getData(forKey: KMThumbSnapshotSizeKey) as? Float ?? self.thumbSnapshotSizeDefaultValue()
  1071. let num = KMDataManager.ud_object(forKey: KMSnapshotThumbnailSizeKey) as? NSNumber
  1072. return num?.floatValue ?? self.thumbSnapshotSizeDefaultValue()
  1073. }
  1074. set {
  1075. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMThumbSnapshotSizeKey)
  1076. let _ = self.setData(data: newValue, forKey: KMThumbSnapshotSizeKey)
  1077. }
  1078. }
  1079. var discreteSize: Bool {
  1080. get {
  1081. return self.getData(forKey: KMDiscreteSizeKey) as? Bool ?? self.discreteSizeDefaultValue()
  1082. }
  1083. set {
  1084. let _ = self.setData(data: newValue, forKey: KMDiscreteSizeKey)
  1085. }
  1086. }
  1087. var outlineFontSize: Float {
  1088. get {
  1089. let num = KMDataManager.ud_object(forKey: KMTableFontSizeKey) as? NSNumber
  1090. return num?.floatValue ?? self.outlineFontSizeDefaultValue()
  1091. // return self.getData(forKey: KMOutlineFontSizeKey) as? Float ?? self.outlineFontSizeDefaultValue()
  1092. }
  1093. set {
  1094. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMOutlineFontSizeKey)
  1095. let _ = self.setData(data: newValue, forKey: KMOutlineFontSizeKey)
  1096. }
  1097. }
  1098. var greekThreshold: Float {
  1099. get {
  1100. let num = KMDataManager.ud_object(forKey: KMGreekingThresholdKey) as? NSNumber
  1101. return num?.floatValue ?? self.greekThresholdDefaultValue()
  1102. // return self.getData(forKey: KMGreekThresholdKey) as? Float ?? self.greekThresholdDefaultValue()
  1103. }
  1104. set {
  1105. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: KMGreekThresholdKey)
  1106. let _ = self.setData(data: newValue, forKey: KMGreekThresholdKey)
  1107. }
  1108. }
  1109. var antiAliasText: Bool {
  1110. get {
  1111. // return self.getData(forKey: KMAntiAliasTextKey) as? Bool ?? self.antiAliasTextDefaultValue()
  1112. return KMDataManager.ud_bool(forKey: KMShouldAntiAliasKey)
  1113. }
  1114. set {
  1115. self._syncDataToUserDefault(newValue, forKey: KMAntiAliasTextKey)
  1116. let _ = self.setData(data: newValue, forKey: KMAntiAliasTextKey)
  1117. }
  1118. }
  1119. var readBarColor: NSColor {
  1120. get {
  1121. if let color = UserDefaults.standard.color(forKey: KMReadingBarColorKey) {
  1122. return color
  1123. }
  1124. return self.getColor(forKey: KMReadBarColorKey)
  1125. }
  1126. set {
  1127. self._syncDataToUserDefault(newValue, forKey: KMReadBarColorKey)
  1128. let _ = self.setColor(newValue, forKey: KMReadBarColorKey)
  1129. }
  1130. }
  1131. var invertBar: Bool {
  1132. get {
  1133. return KMDataManager.ud_bool(forKey: KMReadingBarInvertKey)
  1134. // return self.getData(forKey: KMInvertBarKey) as? Bool ?? self.invertBarDefaultValue()
  1135. }
  1136. set {
  1137. self._syncDataToUserDefault(newValue, forKey: KMInvertBarKey)
  1138. let _ = self.setData(data: newValue, forKey: KMInvertBarKey)
  1139. }
  1140. }
  1141. }
  1142. // MARK: 扩展 Markup
  1143. extension KMPreferenceManager {
  1144. var markupHighlightColor: NSColor {
  1145. get {
  1146. let pKey = KMPreference.markupColorHighlightKey
  1147. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1148. if let color = UserDefaults.standard.color(forKey: udKey) {
  1149. return color
  1150. }
  1151. return self.getColor(forKey: KMPreference.markupColorHighlightKey)
  1152. }
  1153. set {
  1154. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorHighlightKey)
  1155. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorHighlightKey)
  1156. let _ = self.setColor(newValue, forKey: KMPreference.markupColorHighlightKey)
  1157. }
  1158. }
  1159. var markupStrikthroughColor: NSColor {
  1160. get {
  1161. let pKey = KMPreference.markupColorStrikthroughKey
  1162. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1163. if let color = UserDefaults.standard.color(forKey: udKey) {
  1164. return color
  1165. }
  1166. return self.getColor(forKey: KMPreference.markupColorStrikthroughKey)
  1167. }
  1168. set {
  1169. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  1170. let _ = self.setColor(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  1171. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  1172. }
  1173. }
  1174. var markupUnderlineColor: NSColor {
  1175. get {
  1176. let pKey = KMPreference.markupColorUnderlineKey
  1177. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1178. if let color = UserDefaults.standard.color(forKey: udKey) {
  1179. return color
  1180. }
  1181. return self.getColor(forKey: KMPreference.markupColorUnderlineKey)
  1182. }
  1183. set {
  1184. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorUnderlineKey)
  1185. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorUnderlineKey)
  1186. let _ = self.setColor(newValue, forKey: KMPreference.markupColorUnderlineKey)
  1187. }
  1188. }
  1189. var markupPenColor: NSColor {
  1190. get {
  1191. let pKey = KMPreference.markupColorPenKey
  1192. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1193. if let color = UserDefaults.standard.color(forKey: udKey) {
  1194. return color
  1195. }
  1196. return self.getColor(forKey: KMPreference.markupColorPenKey)
  1197. }
  1198. set {
  1199. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorPenKey)
  1200. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorPenKey)
  1201. let _ = self.setColor(newValue, forKey: KMPreference.markupColorPenKey)
  1202. }
  1203. }
  1204. var markupTextColor: NSColor {
  1205. get {
  1206. let pKey = KMPreference.markupColorTextKey
  1207. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1208. if let color = UserDefaults.standard.color(forKey: udKey) {
  1209. return color
  1210. }
  1211. return self.getColor(forKey: KMPreference.markupColorTextKey)
  1212. }
  1213. set {
  1214. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorTextKey)
  1215. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorTextKey)
  1216. let _ = self.setColor(newValue, forKey: KMPreference.markupColorTextKey)
  1217. }
  1218. }
  1219. var markupNoteColor: NSColor {
  1220. get {
  1221. let pKey = KMPreference.markupColorNoteKey
  1222. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1223. if let color = UserDefaults.standard.color(forKey: udKey) {
  1224. return color
  1225. }
  1226. return self.getColor(forKey: KMPreference.markupColorNoteKey)
  1227. }
  1228. set {
  1229. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorNoteKey)
  1230. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorNoteKey)
  1231. let _ = self.setColor(newValue, forKey: KMPreference.markupColorNoteKey)
  1232. }
  1233. }
  1234. var markupRectangleFillColor: NSColor {
  1235. get {
  1236. let pKey = KMPreference.markupColorRectangleFillKey
  1237. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1238. if let color = UserDefaults.standard.color(forKey: udKey) {
  1239. return color
  1240. }
  1241. return self.getColor(forKey: KMPreference.markupColorRectangleFillKey)
  1242. }
  1243. set {
  1244. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  1245. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  1246. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  1247. }
  1248. }
  1249. var markupRectangleBorderColor: NSColor {
  1250. get {
  1251. let pKey = KMPreference.markupColorRectangleBorderKey
  1252. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1253. if let color = UserDefaults.standard.color(forKey: udKey) {
  1254. return color
  1255. }
  1256. return self.getColor(forKey: KMPreference.markupColorRectangleBorderKey)
  1257. }
  1258. set {
  1259. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  1260. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  1261. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  1262. }
  1263. }
  1264. var markupCircleFillColor: NSColor {
  1265. get {
  1266. let pKey = KMPreference.markupColorCircleFillKey
  1267. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1268. if let color = UserDefaults.standard.color(forKey: udKey) {
  1269. return color
  1270. }
  1271. return self.getColor(forKey: KMPreference.markupColorCircleFillKey)
  1272. }
  1273. set {
  1274. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleFillKey)
  1275. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorCircleFillKey)
  1276. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleFillKey)
  1277. }
  1278. }
  1279. var markupCircleBorderColor: NSColor {
  1280. get {
  1281. let pKey = KMPreference.markupColorCircleBorderKey
  1282. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1283. if let color = UserDefaults.standard.color(forKey: udKey) {
  1284. return color
  1285. }
  1286. return self.getColor(forKey: KMPreference.markupColorCircleBorderKey)
  1287. }
  1288. set {
  1289. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  1290. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  1291. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  1292. }
  1293. }
  1294. var markupLineColor: NSColor {
  1295. get {
  1296. let pKey = KMPreference.markupColorLineKey
  1297. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1298. if let color = UserDefaults.standard.color(forKey: udKey) {
  1299. return color
  1300. }
  1301. return self.getColor(forKey: KMPreference.markupColorLineKey)
  1302. }
  1303. set {
  1304. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorLineKey)
  1305. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorLineKey)
  1306. let _ = self.setColor(newValue, forKey: KMPreference.markupColorLineKey)
  1307. }
  1308. }
  1309. var markupArrowColor: NSColor {
  1310. get {
  1311. let pKey = KMPreference.markupColorArrowKey
  1312. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1313. if let color = UserDefaults.standard.color(forKey: udKey) {
  1314. return color
  1315. }
  1316. return self.getColor(forKey: KMPreference.markupColorArrowKey)
  1317. }
  1318. set {
  1319. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorArrowKey)
  1320. self._syncDataToUserDefault(newValue, forKey: KMPreference.markupColorArrowKey)
  1321. let _ = self.setColor(newValue, forKey: KMPreference.markupColorArrowKey)
  1322. }
  1323. }
  1324. var markupTextFontSize: Float {
  1325. get {
  1326. let pKey = KMPreference.markupTextFontSizeKey
  1327. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1328. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1329. return num?.floatValue ?? self.textFontSizeDefaultValue()
  1330. // return self.getData(forKey: pKey) as? Float ?? self.textFontSizeDefaultValue()
  1331. }
  1332. set {
  1333. let pKey = KMPreference.markupTextFontSizeKey
  1334. self.syncDataToPDFView(newValue, forKey: pKey)
  1335. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1336. _ = self.setData(data: newValue, forKey: pKey)
  1337. }
  1338. }
  1339. var markupNoteFontSize: Float {
  1340. get {
  1341. let pKey = KMPreference.markupNoteFontSizeKey
  1342. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1343. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1344. return num?.floatValue ?? self.noteFontSizeDefaultValue()
  1345. // return self.getData(forKey: KMPreference.markupNoteFontSizeKey) as? Float ?? self.noteFontSizeDefaultValue()
  1346. }
  1347. set {
  1348. let pKey = KMPreference.markupNoteFontSizeKey
  1349. self.syncDataToPDFView(newValue, forKey: pKey)
  1350. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1351. _ = self.setData(data: newValue, forKey: pKey)
  1352. }
  1353. }
  1354. var freeTextNoteLineWidth: Float {
  1355. get {
  1356. let pKey = KMPreference.freeTextNoteLineWidthKey
  1357. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1358. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1359. return num?.floatValue ?? self.freeTextNoteLineWidthDefaultValue()
  1360. // return self.getData(forKey: KMPreference.freeTextNoteLineWidthKey) as? Float ?? self.freeTextNoteLineWidthDefaultValue()
  1361. }
  1362. set {
  1363. let pKey = KMPreference.freeTextNoteLineWidthKey
  1364. self.syncDataToPDFView(newValue, forKey: pKey)
  1365. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1366. _ = self.setData(data: newValue, forKey: pKey)
  1367. }
  1368. }
  1369. var freeTextNoteLineStyle: Int {
  1370. get {
  1371. let pKey = KMPreference.freeTextNoteLineStyleKey
  1372. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1373. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1374. return num?.intValue ?? self.freeTextNoteLineStyleDefaultValue()
  1375. // return self.getData(forKey: KMPreference.freeTextNoteLineStyleKey) as? Int ?? self.freeTextNoteLineStyleDefaultValue()
  1376. }
  1377. set {
  1378. let pKey = KMPreference.freeTextNoteLineStyleKey
  1379. self.syncDataToPDFView(newValue, forKey: pKey)
  1380. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1381. _ = self.setData(data: newValue, forKey: pKey)
  1382. }
  1383. }
  1384. var freeTextNoteDashPattern: [CGFloat] {
  1385. get {
  1386. let pKey = KMPreference.freeTextNoteDashPatternKey
  1387. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1388. return KMDataManager.ud_array(forKey: udKey) as? [CGFloat] ?? self.freeTextNoteDashPatternDefaultValue()
  1389. // return self.getData(forKey: KMPreference.freeTextNoteDashPatternKey) as? [CGFloat] ?? self.freeTextNoteDashPatternDefaultValue()
  1390. }
  1391. set {
  1392. let pKey = KMPreference.freeTextNoteDashPatternKey
  1393. self.syncDataToPDFView(newValue, forKey: pKey)
  1394. self._syncDataToUserDefault(newValue, forKey: pKey)
  1395. _ = self.setData(data: newValue, forKey: pKey)
  1396. }
  1397. }
  1398. var circleNoteLineWidth: Float {
  1399. get {
  1400. let pKey = KMPreference.circleNoteLineWidthKey
  1401. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1402. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1403. return num?.floatValue ?? self.circleNoteLineWidthDefaultValue()
  1404. // return self.getData(forKey: KMPreference.circleNoteLineWidthKey) as? Float ?? self.circleNoteLineWidthDefaultValue()
  1405. }
  1406. set {
  1407. let pKey = KMPreference.circleNoteLineWidthKey
  1408. self.syncDataToPDFView(newValue, forKey: pKey)
  1409. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1410. _ = self.setData(data: newValue, forKey: pKey)
  1411. }
  1412. }
  1413. var circleNoteLineStyle: Int {
  1414. get {
  1415. let pKey = KMPreference.circleNoteLineStyleKey
  1416. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1417. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1418. return num?.intValue ?? self.circleNoteLineStyleDefaultValue()
  1419. // return self.getData(forKey: KMPreference.circleNoteLineStyleKey) as? Int ?? self.circleNoteLineStyleDefaultValue()
  1420. }
  1421. set {
  1422. let pKey = KMPreference.circleNoteLineStyleKey
  1423. self.syncDataToPDFView(newValue, forKey: pKey)
  1424. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1425. _ = self.setData(data: newValue, forKey: pKey)
  1426. }
  1427. }
  1428. var circleNoteDashPattern: [CGFloat] {
  1429. get {
  1430. let pKey = KMPreference.circleNoteDashPatternKey
  1431. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1432. return KMDataManager.ud_array(forKey: udKey) as? [CGFloat] ?? self.circleNoteDashPatternDefaultValue()
  1433. // return self.getData(forKey: KMPreference.circleNoteDashPatternKey) as? [CGFloat] ?? self.circleNoteDashPatternDefaultValue()
  1434. }
  1435. set {
  1436. let pKey = KMPreference.circleNoteDashPatternKey
  1437. self.syncDataToPDFView(newValue, forKey: pKey)
  1438. self._syncDataToUserDefault(newValue, forKey: pKey)
  1439. _ = self.setData(data: newValue, forKey: pKey)
  1440. }
  1441. }
  1442. var squareNoteLineWidth: Float {
  1443. get {
  1444. let pKey = KMPreference.squareNoteLineWidthKey
  1445. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1446. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1447. return num?.floatValue ?? self.squareNoteLineWidthDefaultValue()
  1448. // return self.getData(forKey: KMPreference.squareNoteLineWidthKey) as? Float ?? self.squareNoteLineWidthDefaultValue()
  1449. }
  1450. set {
  1451. let pKey = KMPreference.squareNoteLineWidthKey
  1452. self.syncDataToPDFView(newValue, forKey: pKey)
  1453. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1454. _ = self.setData(data: newValue, forKey: pKey)
  1455. }
  1456. }
  1457. var squareNoteLineStyle: Int {
  1458. get {
  1459. let pKey = KMPreference.squareNoteLineStyleKey
  1460. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1461. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1462. return num?.intValue ?? self.squareNoteLineStyleDefaultValue()
  1463. // return self.getData(forKey: KMPreference.squareNoteLineStyleKey) as? Int ?? self.squareNoteLineStyleDefaultValue()
  1464. }
  1465. set {
  1466. let pKey = KMPreference.squareNoteLineStyleKey
  1467. self.syncDataToPDFView(newValue, forKey: pKey)
  1468. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1469. _ = self.setData(data: newValue, forKey: pKey)
  1470. }
  1471. }
  1472. var squareNoteDashPattern: [CGFloat] {
  1473. get {
  1474. let pKey = KMPreference.squareNoteDashPatternKey
  1475. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1476. return KMDataManager.ud_array(forKey: udKey) as? [CGFloat] ?? self.squareNoteDashPatternDefaultValue()
  1477. // return self.getData(forKey: KMPreference.squareNoteDashPatternKey) as? [CGFloat] ?? self.squareNoteDashPatternDefaultValue()
  1478. }
  1479. set {
  1480. let pKey = KMPreference.squareNoteDashPatternKey
  1481. self.syncDataToPDFView(newValue, forKey: pKey)
  1482. self._syncDataToUserDefault(newValue, forKey: pKey)
  1483. _ = self.setData(data: newValue, forKey: pKey)
  1484. }
  1485. }
  1486. var lineNoteLineWidth: Float {
  1487. get {
  1488. let pKey = KMPreference.lineNoteLineWidthKey
  1489. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1490. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1491. return num?.floatValue ?? self.lineNoteLineWidthDefaultValue()
  1492. // return self.getData(forKey: KMPreference.lineNoteLineWidthKey) as? Float ?? self.lineNoteLineWidthDefaultValue()
  1493. }
  1494. set {
  1495. let pKey = KMPreference.lineNoteLineWidthKey
  1496. self.syncDataToPDFView(newValue, forKey: pKey)
  1497. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1498. _ = self.setData(data: newValue, forKey: pKey)
  1499. }
  1500. }
  1501. var lineNoteLineStyle: Int {
  1502. get {
  1503. let pKey = KMPreference.lineNoteLineStyleKey
  1504. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1505. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1506. return num?.intValue ?? self.lineNoteLineStyleDefaultValue()
  1507. // return self.getData(forKey: KMPreference.lineNoteLineStyleKey) as? Int ?? self.lineNoteLineStyleDefaultValue()
  1508. }
  1509. set {
  1510. let pKey = KMPreference.lineNoteLineStyleKey
  1511. self.syncDataToPDFView(newValue, forKey: pKey)
  1512. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1513. _ = self.setData(data: newValue, forKey: pKey)
  1514. }
  1515. }
  1516. var lineNoteDashPattern: [CGFloat] {
  1517. get {
  1518. let pKey = KMPreference.lineNoteDashPatternKey
  1519. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1520. return KMDataManager.ud_array(forKey: udKey) as? [CGFloat] ?? self.lineNoteDashPatternDefaultValue()
  1521. // return self.getData(forKey: KMPreference.lineNoteDashPatternKey) as? [CGFloat] ?? self.lineNoteDashPatternDefaultValue()
  1522. }
  1523. set {
  1524. let pKey = KMPreference.lineNoteDashPatternKey
  1525. self.syncDataToPDFView(newValue, forKey: pKey)
  1526. self._syncDataToUserDefault(newValue, forKey: pKey)
  1527. _ = self.setData(data: newValue, forKey: pKey)
  1528. }
  1529. }
  1530. var lineNoteStartLineStyle: Int {
  1531. get {
  1532. let pKey = KMPreference.lineNoteStartLineStyleKey
  1533. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1534. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1535. return num?.intValue ?? self.lineNoteStartLineStyleDefaultValue()
  1536. // return self.getData(forKey: KMPreference.lineNoteStartLineStyleKey) as? Int ?? self.lineNoteStartLineStyleDefaultValue()
  1537. }
  1538. set {
  1539. let pKey = KMPreference.lineNoteStartLineStyleKey
  1540. self.syncDataToPDFView(newValue, forKey: pKey)
  1541. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1542. _ = self.setData(data: newValue, forKey: pKey)
  1543. }
  1544. }
  1545. var lineNoteEndLineStyle: Int {
  1546. get {
  1547. let pKey = KMPreference.lineNoteEndLineStyleKey
  1548. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1549. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1550. return num?.intValue ?? self.lineNoteEndLineStyleDefaultValue()
  1551. // return self.getData(forKey: KMPreference.lineNoteEndLineStyleKey) as? Int ?? self.lineNoteEndLineStyleDefaultValue()
  1552. }
  1553. set {
  1554. let pKey = KMPreference.lineNoteEndLineStyleKey
  1555. self.syncDataToPDFView(newValue, forKey: pKey)
  1556. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1557. _ = self.setData(data: newValue, forKey: pKey)
  1558. }
  1559. }
  1560. var inkNoteLineWidth: Float {
  1561. get {
  1562. let pKey = KMPreference.inkNoteLineWidthKey
  1563. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1564. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1565. return num?.floatValue ?? self.inkNoteLineWidthDefaultValue()
  1566. // return self.getData(forKey: KMPreference.inkNoteLineWidthKey) as? Float ?? self.inkNoteLineWidthDefaultValue()
  1567. }
  1568. set {
  1569. let pKey = KMPreference.inkNoteLineWidthKey
  1570. self.syncDataToPDFView(newValue, forKey: pKey)
  1571. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1572. _ = self.setData(data: newValue, forKey: pKey)
  1573. }
  1574. }
  1575. var inkNoteLineStyle: Int {
  1576. get {
  1577. let pKey = KMPreference.inkNoteLineStyleKey
  1578. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1579. let num = KMDataManager.ud_object(forKey: udKey) as? NSNumber
  1580. return num?.intValue ?? self.inkNoteLineStyleDefaultValue()
  1581. // return self.getData(forKey: KMPreference.inkNoteLineStyleKey) as? Int ?? self.inkNoteLineStyleDefaultValue()
  1582. }
  1583. set {
  1584. let pKey = KMPreference.inkNoteLineStyleKey
  1585. self.syncDataToPDFView(newValue, forKey: pKey)
  1586. self._syncDataToUserDefault(NSNumber(value: newValue), forKey: pKey)
  1587. _ = self.setData(data: newValue, forKey: pKey)
  1588. }
  1589. }
  1590. var inkNoteDashPattern: [CGFloat] {
  1591. get {
  1592. let pKey = KMPreference.inkNoteDashPatternKey
  1593. let udKey = KMPreferenceKeyToUDKey(pKey: pKey) ?? ""
  1594. return KMDataManager.ud_array(forKey: udKey) as? [CGFloat] ?? self.inkNoteDashPatternDefaultValue()
  1595. // return self.getData(forKey: KMPreference.inkNoteDashPatternKey) as? [CGFloat] ?? self.inkNoteDashPatternDefaultValue()
  1596. }
  1597. set {
  1598. let pKey = KMPreference.inkNoteDashPatternKey
  1599. self.syncDataToPDFView(newValue, forKey: pKey)
  1600. self._syncDataToUserDefault(newValue, forKey: pKey)
  1601. _ = self.setData(data: newValue, forKey: pKey)
  1602. }
  1603. }
  1604. }
  1605. // MARK: -
  1606. // MARK: Additions
  1607. extension KMPreferenceManager {
  1608. var markupFontTextString: String {
  1609. get {
  1610. if let string = UserDefaults.standard.object(forKey: CFreeTextNoteFontNameKey) as? String {
  1611. if (KMPreferenceManager.supportFonts.contains(string)) {
  1612. return string
  1613. }
  1614. }
  1615. return KMDefaultFontName
  1616. }
  1617. set {
  1618. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  1619. UserDefaults.standard.set(newValue, forKey: CFreeTextNoteFontNameKey)
  1620. UserDefaults.standard.synchronize()
  1621. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextStringKey : newValue])
  1622. }
  1623. }
  1624. }
  1625. var markupFontTextAligment: NSTextAlignment {
  1626. get {
  1627. let type = UserDefaults.standard.integer(forKey: CFreeTextNoteAlignmentKey)
  1628. if (type == 0 || type == 1 || type == 2) {
  1629. return NSTextAlignment(rawValue: type) ?? .left
  1630. }
  1631. return .left
  1632. }
  1633. set {
  1634. if (newValue == .left || newValue == .center || newValue == .right) {
  1635. UserDefaults.standard.set(newValue.rawValue, forKey: CFreeTextNoteAlignmentKey)
  1636. UserDefaults.standard.synchronize()
  1637. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextAligmentKey : newValue.rawValue])
  1638. }
  1639. }
  1640. }
  1641. var markupFontNoteString: String {
  1642. get {
  1643. if let fontName = KMPreferenceManager.shared.getData(forKey: KMPreference.markupFontNoteStringKey) as? String {
  1644. if (KMPreference.supportFonts.contains(fontName)) {
  1645. return fontName
  1646. }
  1647. }
  1648. return KMDefaultFontName
  1649. }
  1650. set {
  1651. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  1652. let _ = KMPreferenceManager.shared.setData(data: newValue, forKey: KMPreference.markupFontNoteStringKey)
  1653. }
  1654. }
  1655. }
  1656. class var supportFonts: [String] {
  1657. get {
  1658. var fontNames: Array<String> = []
  1659. for fontInfo in CPDFAnnotationModel.supportFonts() {
  1660. if ((fontInfo is NSDictionary) == false) {
  1661. continue
  1662. }
  1663. for (familyString, styleStrings) in (fontInfo as! NSDictionary) {
  1664. if ((styleStrings is NSArray) == false) {
  1665. break
  1666. }
  1667. for styleString in (styleStrings as! NSArray) {
  1668. fontNames.append("\(familyString)-\(styleString)")
  1669. }
  1670. }
  1671. }
  1672. return fontNames
  1673. }
  1674. }
  1675. private func syncDataToPDFView(_ data: Any, forKey key: KMPreferenceKey) {
  1676. if (key == KMMarkupColorHighlightKey) {
  1677. if let color = data as? NSColor {
  1678. UserDefaults.standard.setPDFListViewColor(color, forKey: CHighlightNoteColorKey)
  1679. }
  1680. } else if (key == KMMarkupColorUnderlineKey) {
  1681. if let color = data as? NSColor {
  1682. UserDefaults.standard.setPDFListViewColor(color, forKey: CUnderlineNoteColorKey)
  1683. }
  1684. } else if (key == KMMarkupColorStrikthroughKey) {
  1685. if let color = data as? NSColor {
  1686. UserDefaults.standard.setPDFListViewColor(color, forKey: CStrikeOutNoteColorKey)
  1687. }
  1688. } else if (key == KMMarkupColorPenKey) {
  1689. if let color = data as? NSColor {
  1690. UserDefaults.standard.setPDFListViewColor(color, forKey: CInkNoteColorKey)
  1691. }
  1692. } else if (key == KMMarkupColorNoteKey) {
  1693. if let color = data as? NSColor {
  1694. UserDefaults.standard.setPDFListViewColor(color, forKey: CAnchoredNoteColorKey)
  1695. }
  1696. } else if (key == KMMarkupColorRectangleFillKey) {
  1697. if let color = data as? NSColor {
  1698. UserDefaults.standard.setPDFListViewColor(color, forKey: CSquareNoteInteriorColorKey)
  1699. }
  1700. } else if (key == KMMarkupColorRectangleBorderKey) {
  1701. if let color = data as? NSColor {
  1702. UserDefaults.standard.setPDFListViewColor(color, forKey: CSquareNoteColorKey)
  1703. }
  1704. } else if (key == KMMarkupColorCircleFillKey) {
  1705. if let color = data as? NSColor {
  1706. UserDefaults.standard.setPDFListViewColor(color, forKey: CCircleNoteInteriorColorKey)
  1707. }
  1708. } else if (key == KMMarkupColorCircleBorderKey) {
  1709. if let color = data as? NSColor {
  1710. UserDefaults.standard.setPDFListViewColor(color, forKey: CCircleNoteColorKey)
  1711. }
  1712. } else if (key == KMMarkupColorLineKey) {
  1713. if let color = data as? NSColor {
  1714. UserDefaults.standard.setPDFListViewColor(color, forKey: CLineNoteColorKey)
  1715. }
  1716. } else if (key == KMMarkupColorArrowKey) {
  1717. if let color = data as? NSColor {
  1718. UserDefaults.standard.setPDFListViewColor(color, forKey: CArrowNoteColorKey)
  1719. }
  1720. } else if (key == KMMarkupColorTextKey) {
  1721. if let color = data as? NSColor {
  1722. UserDefaults.standard.setPDFListViewColor(color, forKey: CFreeTextNoteColorKey)
  1723. }
  1724. } else if (key == KMMarkupFontTextStringKey) {
  1725. UserDefaults.standard.set(data, forKey: CFreeTextNoteFontNameKey)
  1726. UserDefaults.standard.synchronize()
  1727. } else if (key == KMMarkupFontTextAligmentKey) {
  1728. UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1729. UserDefaults.standard.synchronize()
  1730. } else if (key == KMMarkupFontNoteStringKey) {
  1731. // UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1732. // UserDefaults.standard.synchronize()
  1733. } else if (key == KMMarkupTextFontSizeKey) {
  1734. UserDefaults.standard.set(data, forKey: CFreeTextNoteFontSizeKey)
  1735. UserDefaults.standard.synchronize()
  1736. } else if (key == KMMarkupNoteFontSizeKey) {
  1737. // UserDefaults.standard.set(data, forKey: CFreeTextNoteFontSizeKey)
  1738. // UserDefaults.standard.synchronize()
  1739. } else {
  1740. if let cKey = KMPreferenceKeyToCKey(pKey: key) {
  1741. UserDefaults.standard.set(data, forKey: cKey)
  1742. UserDefaults.standard.synchronize()
  1743. }
  1744. }
  1745. }
  1746. private func _syncDataToUserDefault(_ data: Any, forKey key: KMPreferenceKey) {
  1747. // general
  1748. if let udKey = KMPreferenceKeyToUDKey(pKey: key) {
  1749. if let color = data as? NSColor {
  1750. UserDefaults.standard.setColor(color, forKey: udKey)
  1751. } else {
  1752. KMDataManager.ud_set(data, forKey: udKey)
  1753. }
  1754. }
  1755. }
  1756. private func getDefaultColors(forKey key: KMPreferenceKey) -> [Double] {
  1757. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue] ?? [:]
  1758. for key_i in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1759. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1760. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1761. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1762. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1763. if (key == key_i) {
  1764. return markupGroupInfo[key] as? [Double] ?? []
  1765. }
  1766. }
  1767. let displayGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.display.rawValue] ?? [:]
  1768. for key_i in [KMDisplayBackgroundNormalColorKey, KMDisplayBackgroundFullScreenColorKey,
  1769. KMDisplayFieldHighlightingColorKey, KMDisplayRequiredFieldHighlightingColorKey] {
  1770. if (key == key_i) {
  1771. return displayGroupInfo[key] as? [Double] ?? []
  1772. }
  1773. }
  1774. return [0.0, 0.0, 0.0, 1.0]
  1775. }
  1776. private func getDefaultColor(forKey key: KMPreferenceKey) -> NSColor {
  1777. return self.dataToColor(colors: self.getDefaultColors(forKey: key))
  1778. }
  1779. private func resetDataToPDFView() {
  1780. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue] ?? [:]
  1781. // colors
  1782. for key in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1783. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1784. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1785. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1786. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1787. if let data = markupGroupInfo[key] as? [Double] {
  1788. self.syncDataToPDFView(self.dataToColor(colors:data ), forKey: key)
  1789. }
  1790. }
  1791. // lines
  1792. for key in [KMFreeTextNoteLineStyleKey, KMFreeTextNoteLineWidthKey, KMFreeTextNoteDashPatternKey,
  1793. KMLineNoteLineStyleKey, KMLineNoteLineWidthKey, KMLineNoteDashPatternKey,
  1794. KMInkNoteLineStyleKey, KMInkNoteLineWidthKey, KMInkNoteDashPatternKey,
  1795. KMCircleNoteLineStyleKey, KMCircleNoteLineWidthKey, KMCircleNoteDashPatternKey,
  1796. KMSquareNoteLineStyleKey, KMSquareNoteLineWidthKey, KMSquareNoteDashPatternKey] {
  1797. self.syncDataToPDFView(markupGroupInfo[key] as Any, forKey: key)
  1798. }
  1799. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontTextStringKey)
  1800. self.syncDataToPDFView(NSTextAlignment.left.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1801. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontNoteStringKey)
  1802. }
  1803. public func resumeDataToPDFView() {
  1804. self.syncDataToPDFView(KMPreferenceManager.shared.markupHighlightColor, forKey: KMMarkupColorHighlightKey)
  1805. self.syncDataToPDFView(KMPreferenceManager.shared.markupUnderlineColor, forKey: KMMarkupColorUnderlineKey)
  1806. self.syncDataToPDFView(KMPreferenceManager.shared.markupStrikthroughColor, forKey: KMMarkupColorStrikthroughKey)
  1807. self.syncDataToPDFView(KMPreferenceManager.shared.markupPenColor, forKey: KMMarkupColorPenKey)
  1808. self.syncDataToPDFView(KMPreferenceManager.shared.markupNoteColor, forKey: KMMarkupColorNoteKey)
  1809. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleFillColor, forKey: KMMarkupColorRectangleFillKey)
  1810. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleBorderColor, forKey: KMMarkupColorRectangleBorderKey)
  1811. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleFillColor, forKey: KMMarkupColorCircleFillKey)
  1812. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleBorderColor, forKey: KMMarkupColorCircleBorderKey)
  1813. self.syncDataToPDFView(KMPreferenceManager.shared.markupLineColor, forKey: KMMarkupColorLineKey)
  1814. self.syncDataToPDFView(KMPreferenceManager.shared.markupArrowColor, forKey: KMMarkupColorArrowKey)
  1815. self.syncDataToPDFView(KMPreferenceManager.shared.markupTextColor, forKey: KMMarkupColorTextKey)
  1816. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextString, forKey: KMMarkupFontTextStringKey)
  1817. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextAligment.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1818. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontNoteString, forKey: KMMarkupFontNoteStringKey)
  1819. }
  1820. private func _resetDataToUserDefault(group: KMPreferenceGroup) {
  1821. if group == .general {
  1822. KMDataManager.ud_set(nil, forKey: KMUserNameKey)
  1823. KMDataManager.ud_set(NSNumber(value: 0), forKey: KMOpenDocumentInTabKey)
  1824. KMDataManager.ud_set(false, forKey: KMReopenLastOpenFilesKey)
  1825. KMDataManager.ud_set(0, forKey: KMInitialWindowSizeOptionKey)
  1826. KMDataManager.ud_set(true, forKey: KMOpenContentsPaneOnlyForTOCKey)
  1827. KMDataManager.ud_set(false, forKey: KMRememberSnapshotsKey)
  1828. KMDataManager.ud_set(false, forKey: KMAutoSaveSkimNotesKey)
  1829. KMDataManager.ud_set(false, forKey: KMSnapshotsOnTopKey)
  1830. KMDataManager.ud_set(-1, forKey: KMSavePasswordOptionKey)
  1831. } else if group == .display {
  1832. KMDataManager.ud_set(NSNumber(value: self.thumbPageSizeDefaultValue()), forKey: KMThumbnailSizeKey)
  1833. KMDataManager.ud_set(NSNumber(value: self.thumbSnapshotSizeDefaultValue()), forKey: KMSnapshotThumbnailSizeKey)
  1834. KMDataManager.ud_set(NSNumber(value: self.outlineFontSizeDefaultValue()), forKey: KMTableFontSizeKey)
  1835. KMDataManager.ud_set(NSNumber(value: self.greekThresholdDefaultValue()), forKey: KMGreekingThresholdKey)
  1836. KMDataManager.ud_set(false, forKey: KMShouldAntiAliasKey)
  1837. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.displayBackgroundNormalColorKey), forKey: KMBackgroundColorKey)
  1838. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.displayBackgroundFullScreenColorKey), forKey: KMFullScreenBackgroundColorKey)
  1839. UserDefaults.standard.setColor(self.getDefaultColor(forKey: KMPreference.readBarColorKey), forKey: KMReadingBarColorKey)
  1840. KMDataManager.ud_set(false, forKey: KMReadingBarInvertKey)
  1841. }
  1842. }
  1843. public func initDataForAppLaunch() {
  1844. // KMPreferenceManager.shared.author = NSFullUserName()
  1845. if (KMPreferenceManager.shared.autoSave) {
  1846. if KMLightMemberManager.manager.canPayFunction() == false {
  1847. KMPreferenceManager.shared.autoSave = false
  1848. }
  1849. }
  1850. }
  1851. }
  1852. // MARK: -
  1853. // MARK: UserDefaults
  1854. fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath"
  1855. fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  1856. fileprivate let kKMViewSettingKey = "viewSetting"
  1857. fileprivate let kKMPageNumberKey = "pageNumber"
  1858. fileprivate let kKMPageScaleKey = "pageScale"
  1859. extension KMPreferenceManager {
  1860. var lastOpenFilepath: String? {
  1861. get {
  1862. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  1863. }
  1864. set {
  1865. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  1866. UserDefaults.standard.synchronize()
  1867. }
  1868. }
  1869. var lastOpenFilepaths: [String]? {
  1870. get {
  1871. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  1872. }
  1873. set {
  1874. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  1875. UserDefaults.standard.synchronize()
  1876. }
  1877. }
  1878. var viewSetting: [KMPreferenceViewSetting : Any]? {
  1879. get {
  1880. return UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [KMPreferenceViewSetting : Any]
  1881. }
  1882. set {
  1883. if (newValue == nil) {
  1884. UserDefaults.standard.set(newValue, forKey: kKMViewSettingKey)
  1885. UserDefaults.standard.synchronize()
  1886. return
  1887. }
  1888. let viewSetting: [String : Any] = UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [String : Any] ?? [:]
  1889. var info: [String : Any] = [:]
  1890. for (key, value) in viewSetting {
  1891. info.updateValue(value, forKey: key)
  1892. }
  1893. for (key, value) in newValue ?? [:] {
  1894. if (key == .pageNumber) { // 只处理 枚举
  1895. info.updateValue(value, forKey: key.rawValue)
  1896. }
  1897. }
  1898. UserDefaults.standard.set(info, forKey: kKMViewSettingKey)
  1899. UserDefaults.standard.synchronize()
  1900. }
  1901. }
  1902. func setPageNumber(_ number: Int, forKey key: String) {
  1903. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  1904. UserDefaults.standard.synchronize()
  1905. }
  1906. func getPageNumber(forKey key: String) -> Int? {
  1907. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  1908. }
  1909. func setPageScale(_ scale: Float, forKey key: String) {
  1910. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  1911. UserDefaults.standard.synchronize()
  1912. }
  1913. func getPageScale(forKey key: String) -> Float? {
  1914. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  1915. }
  1916. }
  1917. // MARK: -
  1918. // MARK: Qiuck
  1919. extension KMPreferenceManager {
  1920. internal func closeFileIsPrompt() -> Bool {
  1921. return self.closeFilePromptType == .promp
  1922. }
  1923. func leftSideNeedCloseWhenOpenFile() -> Bool {
  1924. return KMPreferenceManager.shared.leftSideDisplayType == .closeWhenOpenFile
  1925. }
  1926. var autoExpandPropertyPanel: Bool {
  1927. get {
  1928. return KMPreferenceManager.shared.propertyPanelExpandType == .auto
  1929. }
  1930. }
  1931. // 单位: 秒
  1932. var autoSaveTimeInterval: TimeInterval {
  1933. get {
  1934. var minute = KMPreferenceManager.shared.autoSavePerNumberMinute
  1935. if (minute < 5) {
  1936. minute = 5
  1937. } else if (minute > 99) {
  1938. minute = 99
  1939. }
  1940. let interval: TimeInterval = Double(minute) * 60.0
  1941. return interval
  1942. }
  1943. }
  1944. }
  1945. // MARK: - Grouping
  1946. extension KMPreferenceManager {
  1947. var grouping: Bool {
  1948. get {
  1949. return false
  1950. }
  1951. }
  1952. private static var _groupingInfosKey = "KMGroupingInfosKey"
  1953. private var _grouping_infos: [KMPreferenceKey : Any] {
  1954. get {
  1955. return (objc_getAssociatedObject(self, &Self._groupingInfosKey) as? [KMPreferenceKey : Any]) ?? [:]
  1956. }
  1957. set {
  1958. objc_setAssociatedObject(self, &Self._groupingInfosKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  1959. }
  1960. }
  1961. private static var _groupingObjectsKey = "KMGroupingObjectsKey"
  1962. private var _grouping_objects: Set<KMPreferenceGroup> {
  1963. get {
  1964. return (objc_getAssociatedObject(self, &Self._groupingObjectsKey) as? Set) ?? []
  1965. }
  1966. set {
  1967. objc_setAssociatedObject(self, &Self._groupingObjectsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  1968. }
  1969. }
  1970. private static var _groupingKey = "KMGroupingKey"
  1971. private var _grouping: Bool {
  1972. get {
  1973. return (objc_getAssociatedObject(self, &Self._groupingKey) as? Bool) ?? false
  1974. }
  1975. set {
  1976. objc_setAssociatedObject(self, &Self._groupingKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
  1977. }
  1978. }
  1979. func beginGrouping() {
  1980. self._grouping = true
  1981. self._grouping_objects = []
  1982. self._grouping_infos = [:]
  1983. }
  1984. func endGrouping() {
  1985. self._grouping = false
  1986. if self._grouping_objects.isEmpty == false { // 发送通知
  1987. var objects: [KMPreferenceGroup] = []
  1988. for data in self._grouping_objects {
  1989. objects.append(data)
  1990. }
  1991. self.postNotification(object: objects, userInfo: self._grouping_infos)
  1992. }
  1993. self._grouping_objects = []
  1994. self._grouping_infos = [:]
  1995. }
  1996. }