KMPreferenceManager.swift 97 KB

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