KMPreferenceManager.swift 82 KB

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