KMPreferenceManager.swift 97 KB

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