KMPreferenceManager.swift 96 KB

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