KMPreferenceManager.swift 78 KB

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