KMPreferenceManager.swift 97 KB

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