KMPreferenceManager.swift 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. //
  2. // KMPreferenceManager.swift
  3. // PDF Master
  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. /// markup
  51. private let KMMarkupColorHighlightKey: KMPreferenceKey = "KMMarkupColorHighlightKey"
  52. private let KMMarkupColorStrikthroughKey: KMPreferenceKey = "KMMarkupColorStrikthroughKey"
  53. private let KMMarkupColorUnderlineKey: KMPreferenceKey = "KMMarkupColorUnderlineKey"
  54. private let KMMarkupColorPenKey: KMPreferenceKey = "KMMarkupColorPenKey"
  55. private let KMMarkupColorTextKey: KMPreferenceKey = "KMMarkupColorTextKey"
  56. private let KMMarkupColorNoteKey: KMPreferenceKey = "KMMarkupColorNoteKey"
  57. private let KMMarkupColorRectangleFillKey: KMPreferenceKey = "KMMarkupColorRectangleFillKey"
  58. private let KMMarkupColorRectangleBorderKey: KMPreferenceKey = "KMMarkupColorRectangleBorderKey"
  59. private let KMMarkupColorCircleFillKey: KMPreferenceKey = "KMMarkupColorCircleFillKey"
  60. private let KMMarkupColorCircleBorderKey: KMPreferenceKey = "KMMarkupColorCircleBorderKey"
  61. private let KMMarkupColorLineKey: KMPreferenceKey = "KMMarkupColorLineKey"
  62. private let KMMarkupColorArrowKey: KMPreferenceKey = "KMMarkupColorArrowKey"
  63. private let KMMarkupFontTextStringKey: KMPreferenceKey = "KMMarkupFontTextStringKey"
  64. private let KMMarkupFontTextAligmentKey: KMPreferenceKey = "KMMarkupFontTextAligmentKey"
  65. private let KMMarkupFontNoteStringKey: KMPreferenceKey = "KMMarkupFontNoteStringKey"
  66. /// 偏好设置已改变
  67. private let KMPreferenceDidChangeNotificationName = "KMPreferenceDidChangeNotificationName"
  68. private let KMDefaultFontName = "Helvetica-Oblique"
  69. typealias KMPreference = KMPreferenceManager
  70. @objcMembers class KMPreferenceManager: NSObject {
  71. static let shared = KMPreferenceManager()
  72. private var generalGroupKeys: Array<KMPreferenceKey> = []
  73. private var displayGroupKeys: Array<KMPreferenceKey> = []
  74. private var markupGroupKeys: Array<KMPreferenceKey> = []
  75. public static let didChangeNotification = Notification.Name(KMPreferenceDidChangeNotificationName)
  76. /// general
  77. /// files
  78. public static let openLastUnclosedDocumentWhenAppStartKey = KMOpenLastUnclosedDocumentWhenAppStartKey
  79. public static let openLastUnlockedDocumentWhenAppStartKey = KMOpenLastUnlockedDocumentWhenAppStartKey
  80. public static let documentMaximunDisplayNumberKey = KMDocumentMaximunDisplayNumberKey
  81. public static let autoSaveKey = KMAutoSaveKey
  82. public static let autoSavePerNumberMinuteKey = KMAutoSavePerNumberMinuteKey
  83. public static let closeFilePromptTypeKey = KMCloseFilePromptTypeKey
  84. /// open image file
  85. public static let openImageFileTypeKey = KMOpenImageFileTypeKey
  86. public static let setDefaultPDFReaderKey = KMSetDefaultPDFReaderKey
  87. /// save password
  88. public static let savePasswordTypeKey = KMSavePasswordTypeKey
  89. public static let generalAuthorNameKey = KMGeneralAuthorNameKey
  90. // Tip: 新补充
  91. public static let openDocumentTypeKey = KMOpenDocumentTypeKey
  92. public static let showInMenuBarKey = KMGeneralShowInMenuBarKey
  93. public static let openFileTypeKey = KMOpenFileTypeKey
  94. public static let showLeftSideBarKey = KMShowLeftSideBarKey
  95. public static let rememberSnapshotKey = KMRememberSnapshotKey
  96. public static let revertInitPDFViewSettingTypeKey = KMRevertInitPDFViewSettingTypeKey
  97. public static let autoSaveNoteBackupKey = KMAutoSaveNoteBackupKey
  98. public static let keepSnapshotWindowToTopKey = KMKeepSnapshotWindowToTopKey
  99. /// display
  100. public static let viewPageDisplayTypeKey = KMViewPageDisplayTypeKey
  101. public static let viewZoomScaleTypeKey = KMViewZoomScaleTypeKey
  102. public static let leftSideDisplayTypeKey = KMLeftSideDisplayTypeKey
  103. public static let showOutlineListKey = KMShowOutlineListKey
  104. public static let leftSideExpandTypeKey = KMLeftSideExpandTypeKey
  105. public static let propertyPanelExpandTypeKey = KMPropertyPanelExpandTypeKey
  106. public static let displayBackgroundNormalColorKey = KMDisplayBackgroundNormalColorKey
  107. public static let displayBackgroundFullScreenColorKey = KMDisplayBackgroundFullScreenColorKey
  108. public static let highlightFormsKey = KMHighlightFormsKey
  109. public static let displayFieldHighlightingColorKey = KMDisplayFieldHighlightingColorKey
  110. public static let displayRequiredFieldHighlightingColorKey = KMDisplayRequiredFieldHighlightingColorKey
  111. public static let pageIndicatorTypeKey = KMPageIndicatorTypeKey
  112. public static let highlightLinksKey = KMHighlightLinksKey
  113. /// markup
  114. public static let markupColorHighlightKey = KMMarkupColorHighlightKey
  115. public static let markupColorStrikthroughKey = KMMarkupColorStrikthroughKey
  116. public static let markupColorUnderlineKey = KMMarkupColorUnderlineKey
  117. public static let markupColorPenKey = KMMarkupColorPenKey
  118. public static let markupColorTextKey = KMMarkupColorTextKey
  119. public static let markupColorNoteKey = KMMarkupColorNoteKey
  120. public static let markupColorRectangleFillKey = KMMarkupColorRectangleFillKey
  121. public static let markupColorRectangleBorderKey = KMMarkupColorRectangleBorderKey
  122. public static let markupColorCircleFillKey = KMMarkupColorCircleFillKey
  123. public static let markupColorCircleBorderKey = KMMarkupColorCircleBorderKey
  124. public static let markupColorLineKey = KMMarkupColorLineKey
  125. public static let markupColorArrowKey = KMMarkupColorArrowKey
  126. public static let markupFontTextStringKey = KMMarkupFontTextStringKey
  127. public static let markupFontTextAligmentKey = KMMarkupFontTextAligmentKey
  128. public static let markupFontNoteStringKey = KMMarkupFontNoteStringKey
  129. override init() {
  130. super.init()
  131. /// 初始化
  132. generalGroupKeys = self.getDefaultKeys(for: .general)
  133. displayGroupKeys = self.getDefaultKeys(for: .display)
  134. markupGroupKeys = self.getDefaultKeys(for: .markup)
  135. let info = UserDefaults.standard.value(forKey: KMPreferenceInfoKey)
  136. if (info == nil) {
  137. let info = self.getDefaultInfo()
  138. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  139. UserDefaults.standard.synchronize()
  140. }
  141. }
  142. // MARK: 保存/获取数据
  143. public func setData(data: Any,forKey key: KMPreferenceKey) -> Bool {
  144. return self.setData(data: data, forKey: key, in: findGroup(forKey: key))
  145. }
  146. public func setData(data: Any,forKey key: KMPreferenceKey, in group: KMPreferenceGroup) -> Bool {
  147. var info: [String : Any]?
  148. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  149. info = self.getDefaultInfo()
  150. } else {
  151. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as! [String : Any])
  152. }
  153. var groupInfo: [String : Any] = info![group.rawValue] as! [String : Any]
  154. groupInfo.updateValue(data, forKey: key)
  155. info?.updateValue(groupInfo, forKey: group.rawValue)
  156. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  157. UserDefaults.standard.synchronize()
  158. self.postNotification(object: [group], userInfo: [key : data])
  159. return true
  160. }
  161. public func getData(forKey key: KMPreferenceKey) -> Any {
  162. var info: [String : Any]?
  163. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  164. info = self.getDefaultInfo()
  165. } else {
  166. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as! [String : Any])
  167. }
  168. let groupInfo: [String : Any] = info![findGroup(forKey: key).rawValue] as! [String : Any]
  169. return groupInfo[key] as Any
  170. }
  171. public func resetData(_ group: KMPreferenceGroup) {
  172. var info: [String : Any]?
  173. if (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) == nil) {
  174. info = self.getDefaultInfo()
  175. } else {
  176. info = (UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as! [String : Any])
  177. }
  178. let groupInfo = self.getDefaultInfo()[group.rawValue]
  179. info?.updateValue(groupInfo as Any, forKey: group.rawValue)
  180. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  181. UserDefaults.standard.synchronize()
  182. self.resetDataToPDFView()
  183. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  184. let info: [String : Any]? = UserDefaults.standard.value(forKey: KMPreferenceInfoKey) as? [String : Any]
  185. var groupInfo: [KMPreferenceKey : Any]?
  186. if (info != nil) {
  187. groupInfo = info![group.rawValue] as? [KMPreferenceKey : Any]
  188. }
  189. self.postNotification(object: [group], userInfo: groupInfo != nil ? groupInfo : [:])
  190. }
  191. }
  192. public func resetAllData() {
  193. let info = self.getDefaultInfo()
  194. UserDefaults.standard.set(info, forKey: KMPreferenceInfoKey)
  195. UserDefaults.standard.synchronize()
  196. self.resetDataToPDFView()
  197. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  198. var groppInfos: [KMPreferenceKey : Any] = [:]
  199. for groupInfo in info.values {
  200. for (key, value) in groupInfo {
  201. groppInfos.updateValue(value, forKey: key)
  202. }
  203. }
  204. self.postNotification(object: [KMPreferenceGroup.general, KMPreferenceGroup.display, KMPreferenceGroup.markup, KMPreferenceGroup.infomation, KMPreferenceGroup.other], userInfo: groppInfos)
  205. }
  206. }
  207. // MARK: 注册 key
  208. public func register(_ key: KMPreferenceKey, to group: KMPreferenceGroup) -> Bool {
  209. if (group == .general) {
  210. if (self.generalGroupKeys.contains(key)) {
  211. return false
  212. }
  213. self.generalGroupKeys.append(key)
  214. }
  215. return true
  216. }
  217. // MARK: -
  218. // MARK: 发布通知
  219. private func postNotification(object: [KMPreferenceGroup]?, userInfo: [KMPreferenceKey : Any]?) {
  220. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  221. NotificationCenter.default.post(name: KMPreferenceManager.didChangeNotification, object: object, userInfo: userInfo)
  222. }
  223. }
  224. // MARK: 获取信息
  225. private func findGroup(forKey key: KMPreferenceKey) -> KMPreferenceGroup {
  226. if (self.generalGroupKeys.contains(key)) {
  227. return .general
  228. } else if (self.displayGroupKeys.contains(key)) {
  229. return .display
  230. } else if (self.markupGroupKeys.contains(key)) {
  231. return .markup
  232. }
  233. return .other
  234. }
  235. private func getDefaultKeys(for group: KMPreferenceGroup) -> Array<KMPreferenceKey> {
  236. if (group == .general) {
  237. return [KMOpenLastUnclosedDocumentWhenAppStartKey,
  238. KMOpenLastUnlockedDocumentWhenAppStartKey,
  239. KMDocumentMaximunDisplayNumberKey,
  240. KMAutoSaveKey,
  241. KMAutoSavePerNumberMinuteKey,
  242. KMCloseFilePromptTypeKey,
  243. KMOpenImageFileTypeKey,
  244. KMSetDefaultPDFReaderKey,
  245. KMSavePasswordTypeKey,
  246. KMGeneralAuthorNameKey,
  247. // Tip: 新补充
  248. KMOpenDocumentTypeKey, KMGeneralShowInMenuBarKey, KMOpenFileTypeKey, KMShowLeftSideBarKey,
  249. KMRememberSnapshotKey, KMRevertInitPDFViewSettingTypeKey, KMAutoSaveNoteBackupKey, KMKeepSnapshotWindowToTopKey]
  250. } else if (group == .display) {
  251. return [KMViewPageDisplayTypeKey,
  252. KMViewZoomScaleTypeKey,
  253. KMLeftSideDisplayTypeKey,
  254. KMShowOutlineListKey,
  255. KMLeftSideExpandTypeKey,
  256. KMPropertyPanelExpandTypeKey,
  257. KMDisplayBackgroundNormalColorKey,
  258. KMDisplayBackgroundFullScreenColorKey,
  259. KMHighlightFormsKey,
  260. KMDisplayFieldHighlightingColorKey,
  261. KMDisplayRequiredFieldHighlightingColorKey,
  262. KMPageIndicatorTypeKey,
  263. KMHighlightLinksKey]
  264. } else if (group == .markup) {
  265. return [KMMarkupColorHighlightKey,
  266. KMMarkupColorUnderlineKey,
  267. KMMarkupColorStrikthroughKey,
  268. KMMarkupColorPenKey,
  269. KMMarkupColorLineKey,
  270. KMMarkupColorArrowKey,
  271. KMMarkupColorNoteKey,
  272. KMMarkupColorTextKey,
  273. KMMarkupColorCircleFillKey,
  274. KMMarkupColorCircleBorderKey,
  275. KMMarkupColorRectangleFillKey,
  276. KMMarkupColorRectangleBorderKey,
  277. KMMarkupFontTextStringKey,
  278. KMMarkupFontTextAligmentKey,
  279. KMMarkupFontNoteStringKey]
  280. }
  281. return []
  282. }
  283. private func getDefaultInfo() -> Dictionary<String, Dictionary<String, Any>> {
  284. return [KMPreferenceGroup.general.rawValue : [KMOpenLastUnclosedDocumentWhenAppStartKey : false,
  285. KMOpenLastUnlockedDocumentWhenAppStartKey : true,
  286. KMDocumentMaximunDisplayNumberKey : 10,
  287. KMAutoSaveKey : true,
  288. KMAutoSavePerNumberMinuteKey : 5,
  289. KMCloseFilePromptTypeKey : 0,
  290. KMOpenImageFileTypeKey : 0,
  291. KMSetDefaultPDFReaderKey : true,
  292. KMSavePasswordTypeKey : 2,
  293. KMGeneralAuthorNameKey : NSFullUserName(),
  294. // Tip: 新补充
  295. KMOpenDocumentTypeKey : self.openDocumentTypeDefaultValue.rawValue,
  296. KMGeneralShowInMenuBarKey : self.showInMenuBarDefaultValue,
  297. KMOpenFileTypeKey : self.openFileTypeDefaultValue.rawValue,
  298. KMShowLeftSideBarKey : self.showLeftSideBarDefaultValue,
  299. KMRememberSnapshotKey : self.rememberSnapshotDefaultValue,
  300. KMRevertInitPDFViewSettingTypeKey : self.revertInitPDFViewSettingTypeDefaultValue.rawValue,
  301. KMAutoSaveNoteBackupKey : self.autoSaveNoteBackupDefaultValue,
  302. KMKeepSnapshotWindowToTopKey : self.keepSnapshotWindowToTopDefaultValue()],
  303. KMPreferenceGroup.display.rawValue : [KMViewPageDisplayTypeKey : 1,
  304. KMViewZoomScaleTypeKey : 0,
  305. KMLeftSideDisplayTypeKey : 0,
  306. KMShowOutlineListKey: true,
  307. KMLeftSideExpandTypeKey : 0,
  308. KMPropertyPanelExpandTypeKey : 0,
  309. KMDisplayBackgroundNormalColorKey : self.displayBackgroundNormalColorValues,
  310. KMDisplayBackgroundFullScreenColorKey : self.displayBackgroundFullScreenColorValues,
  311. KMHighlightFormsKey : true,
  312. KMDisplayFieldHighlightingColorKey : self.displayFieldHighlightingColorValues,
  313. KMDisplayRequiredFieldHighlightingColorKey : self.displayRequiredFieldHighlightingColorValues,
  314. KMPageIndicatorTypeKey : 0,
  315. KMHighlightLinksKey : true],
  316. KMPreferenceGroup.markup.rawValue : [KMMarkupColorHighlightKey : self.markupHighlightColorValues,
  317. KMMarkupColorStrikthroughKey : self.markupStrikthroughColorValues,
  318. KMMarkupColorUnderlineKey : self.markupUnderlineColorValues,
  319. KMMarkupColorPenKey : self.markupPenColorValues,
  320. KMMarkupColorTextKey : self.markupTextColorValues,
  321. KMMarkupColorNoteKey : self.markupNoteColorValues,
  322. KMMarkupColorRectangleFillKey : self.markupRectangleFillColorValues,
  323. KMMarkupColorRectangleBorderKey : self.markupRectangleBorderColorValues,
  324. KMMarkupColorCircleFillKey : self.markupCircleFillColorValues,
  325. KMMarkupColorCircleBorderKey : self.markupCircleBorderColorValues,
  326. KMMarkupColorLineKey : self.markupLineColorValues,
  327. KMMarkupColorArrowKey : self.markupArrowColorValues,
  328. KMMarkupFontTextStringKey : KMDefaultFontName,
  329. KMMarkupFontTextAligmentKey : 0,
  330. KMMarkupFontNoteStringKey : KMDefaultFontName],
  331. KMPreferenceGroup.infomation.rawValue : [:],
  332. KMPreferenceGroup.other.rawValue : [:]]
  333. }
  334. }
  335. // MARK: - 默认值
  336. extension KMPreferenceManager {
  337. var openDocumentTypeDefaultValue: KMPreferenceOpenDocumentType {
  338. get {
  339. return .inSameWindow
  340. }
  341. }
  342. var showInMenuBarDefaultValue: Bool {
  343. get {
  344. return false
  345. }
  346. }
  347. var openFileTypeDefaultValue: KMPreferenceOpenFileType {
  348. get {
  349. return .default
  350. }
  351. }
  352. var showLeftSideBarDefaultValue: Bool {
  353. get {
  354. return true
  355. }
  356. }
  357. var rememberSnapshotDefaultValue: Bool {
  358. get {
  359. return true
  360. }
  361. }
  362. var revertInitPDFViewSettingTypeDefaultValue: KMPreferenceRevertInitSettingType {
  363. get {
  364. return .normal
  365. }
  366. }
  367. var autoSaveNoteBackupDefaultValue: Bool {
  368. get {
  369. return false
  370. }
  371. }
  372. // 内联函数
  373. @inline (__always) func keepSnapshotWindowToTopDefaultValue() -> Bool {
  374. return true
  375. }
  376. }
  377. //protocol KMPreferenceManagerColorPart: NSObjectProtocol {
  378. // var markupHighlightColorValues: [Double] { get }
  379. //}
  380. // MARK: - 扩展 颜色
  381. extension KMPreferenceManager {
  382. fileprivate var displayBackgroundNormalColorValues: [Double] {
  383. get {
  384. return [206/255.0, 208/255.0, 212/255.0, 1.0]
  385. }
  386. }
  387. fileprivate var displayBackgroundFullScreenColorValues: [Double] {
  388. get {
  389. return [54/255.0, 56/255.0, 59/255.0, 1.0]
  390. }
  391. }
  392. fileprivate var displayFieldHighlightingColorValues: [Double] {
  393. get {
  394. return [189/255.0, 223/255.0, 253/255.0, 1.0]
  395. }
  396. }
  397. fileprivate var displayRequiredFieldHighlightingColorValues: [Double] {
  398. get {
  399. return [23/255.0, 112/255.0, 224/255.0, 1.0]
  400. }
  401. }
  402. fileprivate var markupHighlightColorValues: [Double] {
  403. get {
  404. return [255/255.0, 199/255.0, 0.0, 1.0]
  405. }
  406. }
  407. fileprivate var markupStrikthroughColorValues: [Double] {
  408. get {
  409. return [252/255.0, 31/255.0, 31/255.0, 1.0]
  410. }
  411. }
  412. fileprivate var markupUnderlineColorValues: [Double] {
  413. get {
  414. return [30/255.0, 137/255.0, 86/255.0, 1.0]
  415. }
  416. }
  417. fileprivate var markupPenColorValues: [Double] {
  418. get {
  419. self.markupStrikthroughColorValues
  420. }
  421. }
  422. fileprivate var markupTextColorValues: [Double] {
  423. get {
  424. return [37/255.0, 38/255.0, 41/255.0, 1.0]
  425. }
  426. }
  427. fileprivate var markupNoteColorValues: [Double] {
  428. get {
  429. return [255/255.0, 213/255.0, 115/255.0, 1.0]
  430. }
  431. }
  432. fileprivate var markupRectangleFillColorValues: [Double] {
  433. get {
  434. return [0/255.0, 0/255.0, 0/255.0, 0.0]
  435. }
  436. }
  437. fileprivate var markupRectangleBorderColorValues: [Double] {
  438. get {
  439. self.markupStrikthroughColorValues
  440. }
  441. }
  442. fileprivate var markupCircleFillColorValues: [Double] {
  443. get {
  444. self.markupRectangleFillColorValues
  445. }
  446. }
  447. fileprivate var markupCircleBorderColorValues: [Double] {
  448. get {
  449. return self.markupRectangleBorderColorValues
  450. }
  451. }
  452. fileprivate var markupLineColorValues: [Double] {
  453. get {
  454. self.markupStrikthroughColorValues
  455. }
  456. }
  457. fileprivate var markupArrowColorValues: [Double] {
  458. get {
  459. self.markupStrikthroughColorValues
  460. }
  461. }
  462. private func setColor(_ color: NSColor, forKey key: KMPreferenceKey) -> Bool {
  463. var red: CGFloat = 0.0
  464. var green: CGFloat = 0.0
  465. var blue: CGFloat = 0.0
  466. var alpha: CGFloat = 0.0
  467. color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  468. return KMPreferenceManager.shared.setData(data: [red, green, blue, alpha], forKey: key)
  469. }
  470. private func getColor(forKey key: KMPreferenceKey) -> NSColor {
  471. var colors: [Double]? = self.getData(forKey: key) as? [Double]
  472. if (colors == nil) {
  473. colors = self.getDefaultColors(forKey: key)
  474. }
  475. return NSColor(red: colors![0], green: colors![1], blue: colors![2], alpha: colors![3])
  476. }
  477. private func dataToColor(colors:[Double]) -> NSColor {
  478. NSColor(red: colors[0], green: colors[1], blue: colors[2], alpha: colors[3])
  479. }
  480. }
  481. // MARK: 扩展 General
  482. extension KMPreferenceManager {
  483. var openLastUnclosedDocumentWhenAppStart: Bool {
  484. get {
  485. return self.getData(forKey: KMOpenLastUnclosedDocumentWhenAppStartKey) as! Bool
  486. }
  487. set {
  488. let _ = self.setData(data: newValue, forKey: KMOpenLastUnclosedDocumentWhenAppStartKey)
  489. }
  490. }
  491. var openLastUnlockedDocumentWhenAppStart: Bool {
  492. get {
  493. return self.getData(forKey: KMOpenLastUnlockedDocumentWhenAppStartKey) as! Bool
  494. }
  495. set {
  496. let _ = self.setData(data: newValue, forKey: KMOpenLastUnlockedDocumentWhenAppStartKey)
  497. }
  498. }
  499. var documentMaximunDisplayNumber: Int {
  500. get {
  501. return self.getData(forKey: KMDocumentMaximunDisplayNumberKey) as! Int
  502. }
  503. set {
  504. let _ = self.setData(data: newValue, forKey: KMDocumentMaximunDisplayNumberKey)
  505. }
  506. }
  507. var autoSave: Bool {
  508. get {
  509. return self.getData(forKey: KMAutoSaveKey) as! Bool
  510. }
  511. set {
  512. let _ = self.setData(data: newValue, forKey: KMAutoSaveKey)
  513. }
  514. }
  515. var autoSavePerNumberMinute: Int {
  516. get {
  517. return self.getData(forKey: KMAutoSavePerNumberMinuteKey) as! Int
  518. }
  519. set {
  520. let _ = self.setData(data: newValue, forKey: KMAutoSavePerNumberMinuteKey)
  521. }
  522. }
  523. var closeFilePromptType: KMPreferenceCloseFilePromptType {
  524. get {
  525. let type: Int? = self.getData(forKey: KMCloseFilePromptTypeKey) as? Int
  526. if (type != nil && type! == 1) { // 无提示,直接保存
  527. return .noPromp
  528. }
  529. return .promp
  530. }
  531. set {
  532. if (newValue == .promp || newValue == .noPromp) {
  533. let _ = self.setData(data: newValue.rawValue, forKey: KMCloseFilePromptTypeKey)
  534. }
  535. }
  536. }
  537. var openImageFileType: Int {
  538. get {
  539. return self.getData(forKey: KMOpenImageFileTypeKey) as! Int
  540. }
  541. set {
  542. let _ = self.setData(data: newValue, forKey: KMOpenImageFileTypeKey)
  543. }
  544. }
  545. var author: String {
  546. get {
  547. return self.getData(forKey: KMGeneralAuthorNameKey) as! String
  548. }
  549. set {
  550. let _ = self.setData(data: newValue, forKey: KMGeneralAuthorNameKey)
  551. }
  552. }
  553. var setDefaultPDFReader: Bool {
  554. get {
  555. return self.getData(forKey: KMSetDefaultPDFReaderKey) as! Bool
  556. }
  557. set {
  558. let result = KMTools.setDefaultPDFReader(newValue)
  559. if (result) {
  560. let _ = self.setData(data: newValue, forKey: KMSetDefaultPDFReaderKey)
  561. }
  562. }
  563. }
  564. var savePasswordType: KMPreferenceSavePasswordType {
  565. get {
  566. let type: Int? = self.getData(forKey: KMSavePasswordTypeKey) as? Int
  567. if (type == nil || type! == 2) {
  568. return .ask
  569. }
  570. if (type! == 0) {
  571. return .always
  572. } else if (type! == 1) {
  573. return .never
  574. }
  575. return .ask
  576. }
  577. set {
  578. if (newValue == .always || newValue == .never || newValue == .ask) {
  579. let _ = self.setData(data: newValue.rawValue, forKey: KMSavePasswordTypeKey)
  580. }
  581. }
  582. }
  583. var openDocumentType: KMPreferenceOpenDocumentType {
  584. get {
  585. guard let type = self.getData(forKey: KMOpenDocumentTypeKey) as? Int else {
  586. return self.openDocumentTypeDefaultValue
  587. }
  588. if type == 1 {
  589. return .newWindow
  590. }
  591. return .inSameWindow
  592. }
  593. set {
  594. if (newValue == .inSameWindow || newValue == .newWindow) {
  595. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenDocumentTypeKey)
  596. }
  597. }
  598. }
  599. var showInMenuBar: Bool {
  600. get {
  601. return self.getData(forKey: KMGeneralShowInMenuBarKey) as? Bool ?? self.showInMenuBarDefaultValue
  602. }
  603. set {
  604. _ = self.setData(data: newValue, forKey: KMGeneralShowInMenuBarKey)
  605. }
  606. }
  607. var openFileType: KMPreferenceOpenFileType {
  608. get {
  609. guard let type = self.getData(forKey: KMOpenFileTypeKey) as? Int else {
  610. return .default
  611. }
  612. if type == 1 {
  613. return .maxim
  614. } else if type == 2 {
  615. return .fit
  616. }
  617. return .default
  618. }
  619. set {
  620. if (newValue == .default || newValue == .maxim || newValue == .fit) {
  621. let _ = self.setData(data: newValue.rawValue, forKey: KMOpenFileTypeKey)
  622. }
  623. }
  624. }
  625. var showLeftSideBar: Bool {
  626. get {
  627. return self.getData(forKey: KMShowLeftSideBarKey) as? Bool ?? self.showLeftSideBarDefaultValue
  628. }
  629. set {
  630. _ = self.setData(data: newValue, forKey: KMShowLeftSideBarKey)
  631. }
  632. }
  633. var rememberSnapshot: Bool {
  634. get {
  635. return self.getData(forKey: KMRememberSnapshotKey) as? Bool ?? self.rememberSnapshotDefaultValue
  636. }
  637. set {
  638. _ = self.setData(data: newValue, forKey: KMRememberSnapshotKey)
  639. }
  640. }
  641. var revertInitPDFViewSettingType: KMPreferenceRevertInitSettingType {
  642. get {
  643. guard let type = self.getData(forKey: KMRevertInitPDFViewSettingTypeKey) as? Int else {
  644. return self.revertInitPDFViewSettingTypeDefaultValue
  645. }
  646. if type == 1 {
  647. return .fullScreen
  648. }
  649. return .normal
  650. }
  651. set {
  652. if (newValue == .normal || newValue == .fullScreen) {
  653. let _ = self.setData(data: newValue.rawValue, forKey: KMRevertInitPDFViewSettingTypeKey)
  654. }
  655. }
  656. }
  657. var autoSaveNoteBackup: Bool {
  658. get {
  659. return self.getData(forKey: KMAutoSaveNoteBackupKey) as? Bool ?? self.autoSaveNoteBackupDefaultValue
  660. }
  661. set {
  662. _ = self.setData(data: newValue, forKey: KMAutoSaveNoteBackupKey)
  663. }
  664. }
  665. var keepSnapshotWindowToTop: Bool {
  666. get {
  667. return self.getData(forKey: KMKeepSnapshotWindowToTopKey) as? Bool ?? self.keepSnapshotWindowToTopDefaultValue()
  668. }
  669. set {
  670. _ = self.setData(data: newValue, forKey: KMKeepSnapshotWindowToTopKey)
  671. }
  672. }
  673. }
  674. // MARK: 扩展 Display
  675. extension KMPreferenceManager {
  676. var viewPageDisplayType: KMPDFDisplayType {
  677. get {
  678. let type: Int? = self.getData(forKey: KMViewPageDisplayTypeKey) as? Int
  679. if (type == nil || type == 1) {
  680. return .singlePageContinuous
  681. }
  682. if (type! == 0) {
  683. return .singlePage
  684. } else if (type! == 2) {
  685. return .twoUp
  686. } else if (type! == 3) {
  687. return .twoUpContinuous
  688. } else if (type! == 4) {
  689. return .bookMode
  690. } else if (type! == 5) {
  691. return .bookContinuous
  692. }
  693. return .singlePageContinuous
  694. }
  695. set {
  696. if (newValue == .singlePage || newValue == .singlePageContinuous || newValue == .twoUp ||
  697. newValue == .twoUpContinuous || newValue == .bookMode || newValue == .bookContinuous) {
  698. let _ = self.setData(data: newValue.rawValue, forKey: KMViewPageDisplayTypeKey)
  699. }
  700. }
  701. }
  702. var viewZoomScaleType: KMPDFZoomType {
  703. get {
  704. let type: Int? = self.getData(forKey: KMViewZoomScaleTypeKey) as? Int
  705. if (type == nil || type! == 0) {
  706. return .width
  707. } else if (type! == 1) {
  708. return .fit
  709. } else if (type! == 2) {
  710. return .actualSize
  711. }
  712. return .width
  713. }
  714. set {
  715. if (newValue == .width || newValue == .fit || newValue == .actualSize) {
  716. let _ = self.setData(data: newValue.rawValue, forKey: KMViewZoomScaleTypeKey)
  717. }
  718. }
  719. }
  720. var leftSideDisplayType: KMPreferenceLeftSideDisplayType {
  721. get {
  722. let type: Int? = self.getData(forKey: KMLeftSideDisplayTypeKey) as? Int
  723. if (type == nil || type! == 0) {
  724. return .closeWhenOpenFile
  725. } else if (type! == 1) {
  726. return .openAppSaveLastSelect
  727. } else if (type! == 2) {
  728. return .showOutlineIfHas
  729. }
  730. return .closeWhenOpenFile
  731. }
  732. set {
  733. if (newValue == .closeWhenOpenFile || newValue == .openAppSaveLastSelect || newValue == .showOutlineIfHas) {
  734. let _ = self.setData(data: newValue.rawValue, forKey: KMLeftSideDisplayTypeKey)
  735. }
  736. }
  737. }
  738. // var showOutlineList: Bool {
  739. // get {
  740. // return self.getData(forKey: KMShowOutlineListKey) as! Bool
  741. // }
  742. // set {
  743. // let _ = self.setData(data: newValue, forKey: KMShowOutlineListKey)
  744. // }
  745. // }
  746. // var leftSideExpandType: Int {
  747. // get {
  748. // return self.getData(forKey: KMLeftSideExpandTypeKey) as! Int
  749. // }
  750. // set {
  751. // let _ = self.setData(data: newValue, forKey: KMLeftSideExpandTypeKey)
  752. // }
  753. // }
  754. var propertyPanelExpandType: KMPreferencePropertyPanelExpandType {
  755. get {
  756. let type: Int? = self.getData(forKey: KMPropertyPanelExpandTypeKey) as? Int
  757. if (type == nil || type! == 0) {
  758. return .auto
  759. } else if (type! == 1) {
  760. return .manual
  761. }
  762. return .auto
  763. }
  764. set {
  765. if (newValue == .auto || newValue == .manual) {
  766. let _ = self.setData(data: newValue.rawValue, forKey: KMPropertyPanelExpandTypeKey)
  767. }
  768. }
  769. }
  770. var highlightForms: Bool {
  771. get {
  772. return self.getData(forKey: KMHighlightFormsKey) as! Bool
  773. }
  774. set {
  775. let _ = self.setData(data: newValue, forKey: KMHighlightFormsKey)
  776. }
  777. }
  778. var pageIndicatorType: KMPreferencePageIndicatorDisplayType {
  779. get {
  780. let type: Int? = self.getData(forKey: KMPageIndicatorTypeKey) as? Int
  781. if (type == nil || type! == 2) {
  782. return .never
  783. } else if (type! == 0) {
  784. return .automatic
  785. } else if (type! == 1) {
  786. return .always
  787. }
  788. return .never
  789. }
  790. set {
  791. if (newValue == .automatic || newValue == .always || newValue == .never) {
  792. let _ = self.setData(data: newValue.rawValue, forKey: KMPageIndicatorTypeKey)
  793. }
  794. }
  795. }
  796. var highlightLinks: Bool {
  797. get {
  798. return self.getData(forKey: KMHighlightLinksKey) as! Bool
  799. }
  800. set {
  801. let _ = self.setData(data: newValue, forKey: KMHighlightLinksKey)
  802. }
  803. }
  804. var displayBackgroundNormalColor: NSColor {
  805. get {
  806. return self.getColor(forKey: KMDisplayBackgroundNormalColorKey)
  807. }
  808. set {
  809. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundNormalColorKey)
  810. }
  811. }
  812. var displayBackgroundFullScreenColor: NSColor {
  813. get {
  814. return self.getColor(forKey: KMDisplayBackgroundFullScreenColorKey)
  815. }
  816. set {
  817. let _ = self.setColor(newValue, forKey: KMDisplayBackgroundFullScreenColorKey)
  818. }
  819. }
  820. var displayFieldHighlightingColor: NSColor {
  821. get {
  822. return self.getColor(forKey: KMDisplayFieldHighlightingColorKey)
  823. }
  824. set {
  825. let _ = self.setColor(newValue, forKey: KMDisplayFieldHighlightingColorKey)
  826. }
  827. }
  828. var displayRequiredFieldHighlightingColor: NSColor {
  829. get {
  830. return self.getColor(forKey: KMDisplayRequiredFieldHighlightingColorKey)
  831. }
  832. set {
  833. let _ = self.setColor(newValue, forKey: KMDisplayRequiredFieldHighlightingColorKey)
  834. }
  835. }
  836. }
  837. // MARK: 扩展 Markup
  838. extension KMPreferenceManager {
  839. var markupHighlightColor: NSColor {
  840. get {
  841. return self.getColor(forKey: KMPreference.markupColorHighlightKey)
  842. }
  843. set {
  844. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorHighlightKey)
  845. let _ = self.setColor(newValue, forKey: KMPreference.markupColorHighlightKey)
  846. }
  847. }
  848. var markupStrikthroughColor: NSColor {
  849. get {
  850. return self.getColor(forKey: KMPreference.markupColorStrikthroughKey)
  851. }
  852. set {
  853. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  854. let _ = self.setColor(newValue, forKey: KMPreference.markupColorStrikthroughKey)
  855. }
  856. }
  857. var markupUnderlineColor: NSColor {
  858. get {
  859. return self.getColor(forKey: KMPreference.markupColorUnderlineKey)
  860. }
  861. set {
  862. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorUnderlineKey)
  863. let _ = self.setColor(newValue, forKey: KMPreference.markupColorUnderlineKey)
  864. }
  865. }
  866. var markupPenColor: NSColor {
  867. get {
  868. return self.getColor(forKey: KMPreference.markupColorPenKey)
  869. }
  870. set {
  871. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorPenKey)
  872. let _ = self.setColor(newValue, forKey: KMPreference.markupColorPenKey)
  873. }
  874. }
  875. var markupTextColor: NSColor {
  876. get {
  877. return self.getColor(forKey: KMPreference.markupColorTextKey)
  878. }
  879. set {
  880. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorTextKey)
  881. let _ = self.setColor(newValue, forKey: KMPreference.markupColorTextKey)
  882. }
  883. }
  884. var markupNoteColor: NSColor {
  885. get {
  886. return self.getColor(forKey: KMPreference.markupColorNoteKey)
  887. }
  888. set {
  889. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorNoteKey)
  890. let _ = self.setColor(newValue, forKey: KMPreference.markupColorNoteKey)
  891. }
  892. }
  893. var markupRectangleFillColor: NSColor {
  894. get {
  895. return self.getColor(forKey: KMPreference.markupColorRectangleFillKey)
  896. }
  897. set {
  898. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  899. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleFillKey)
  900. }
  901. }
  902. var markupRectangleBorderColor: NSColor {
  903. get {
  904. return self.getColor(forKey: KMPreference.markupColorRectangleBorderKey)
  905. }
  906. set {
  907. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  908. let _ = self.setColor(newValue, forKey: KMPreference.markupColorRectangleBorderKey)
  909. }
  910. }
  911. var markupCircleFillColor: NSColor {
  912. get {
  913. return self.getColor(forKey: KMPreference.markupColorCircleFillKey)
  914. }
  915. set {
  916. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleFillKey)
  917. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleFillKey)
  918. }
  919. }
  920. var markupCircleBorderColor: NSColor {
  921. get {
  922. return self.getColor(forKey: KMPreference.markupColorCircleBorderKey)
  923. }
  924. set {
  925. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  926. let _ = self.setColor(newValue, forKey: KMPreference.markupColorCircleBorderKey)
  927. }
  928. }
  929. var markupLineColor: NSColor {
  930. get {
  931. return self.getColor(forKey: KMPreference.markupColorLineKey)
  932. }
  933. set {
  934. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorLineKey)
  935. let _ = self.setColor(newValue, forKey: KMPreference.markupColorLineKey)
  936. }
  937. }
  938. var markupArrowColor: NSColor {
  939. get {
  940. return self.getColor(forKey: KMPreference.markupColorArrowKey)
  941. }
  942. set {
  943. self.syncDataToPDFView(newValue, forKey: KMPreference.markupColorArrowKey)
  944. let _ = self.setColor(newValue, forKey: KMPreference.markupColorArrowKey)
  945. }
  946. }
  947. }
  948. // MARK: -
  949. // MARK: Additions
  950. extension KMPreferenceManager {
  951. var markupFontTextString: String {
  952. get {
  953. let string: String? = UserDefaults.standard.object(forKey: CFreeTextNoteFontNameKey) as? String
  954. if (string == nil) {
  955. return KMDefaultFontName
  956. }
  957. if (KMPreferenceManager.supportFonts.contains(string!)) {
  958. return string!
  959. }
  960. return KMDefaultFontName
  961. }
  962. set {
  963. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  964. UserDefaults.standard.set(newValue, forKey: CFreeTextNoteFontNameKey)
  965. UserDefaults.standard.synchronize()
  966. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextStringKey : newValue])
  967. }
  968. }
  969. }
  970. var markupFontTextAligment: NSTextAlignment {
  971. get {
  972. let type: Int? = UserDefaults.standard.integer(forKey: CFreeTextNoteAlignmentKey)
  973. if (type == nil) {
  974. return .left
  975. }
  976. if (type! == 0 || type! == 1 || type == 2) {
  977. return NSTextAlignment(rawValue: type!)!
  978. }
  979. return .left
  980. }
  981. set {
  982. if (newValue == .left || newValue == .center || newValue == .right) {
  983. UserDefaults.standard.set(newValue.rawValue, forKey: CFreeTextNoteAlignmentKey)
  984. UserDefaults.standard.synchronize()
  985. self.postNotification(object: [.markup], userInfo: [KMMarkupFontTextAligmentKey : newValue.rawValue])
  986. }
  987. }
  988. }
  989. var markupFontNoteString: String {
  990. get {
  991. let fontName: String? = KMPreferenceManager.shared.getData(forKey: KMPreference.markupFontNoteStringKey) as? String
  992. if (fontName == nil) {
  993. return KMDefaultFontName
  994. }
  995. if (KMPreference.supportFonts.contains(fontName!)) {
  996. return fontName!
  997. }
  998. return KMDefaultFontName
  999. }
  1000. set {
  1001. if (KMPreferenceManager.supportFonts.contains(newValue)) {
  1002. let _ = KMPreferenceManager.shared.setData(data: newValue, forKey: KMPreference.markupFontNoteStringKey)
  1003. }
  1004. }
  1005. }
  1006. class var supportFonts: [String] {
  1007. get {
  1008. var fontNames: Array<String> = []
  1009. for fontInfo in CPDFAnnotationModel.supportFonts() {
  1010. if ((fontInfo is NSDictionary) == false) {
  1011. continue
  1012. }
  1013. for (familyString, styleStrings) in (fontInfo as! NSDictionary) {
  1014. if ((styleStrings is NSArray) == false) {
  1015. break
  1016. }
  1017. for styleString in (styleStrings as! NSArray) {
  1018. fontNames.append("\(familyString)-\(styleString)")
  1019. }
  1020. }
  1021. }
  1022. return fontNames
  1023. }
  1024. }
  1025. private func syncDataToPDFView(_ data: Any, forKey key: KMPreferenceKey) {
  1026. if (key == KMMarkupColorHighlightKey) {
  1027. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CHighlightNoteColorKey)
  1028. } else if (key == KMMarkupColorUnderlineKey) {
  1029. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CUnderlineNoteColorKey)
  1030. } else if (key == KMMarkupColorStrikthroughKey) {
  1031. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CStrikeOutNoteColorKey)
  1032. } else if (key == KMMarkupColorPenKey) {
  1033. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CInkNoteColorKey)
  1034. } else if (key == KMMarkupColorNoteKey) {
  1035. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CAnchoredNoteColorKey)
  1036. } else if (key == KMMarkupColorRectangleFillKey) {
  1037. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CSquareNoteInteriorColorKey)
  1038. } else if (key == KMMarkupColorRectangleBorderKey) {
  1039. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CSquareNoteColorKey)
  1040. } else if (key == KMMarkupColorCircleFillKey) {
  1041. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CCircleNoteInteriorColorKey)
  1042. } else if (key == KMMarkupColorCircleBorderKey) {
  1043. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CCircleNoteColorKey)
  1044. } else if (key == KMMarkupColorLineKey) {
  1045. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CLineNoteColorKey)
  1046. } else if (key == KMMarkupColorArrowKey) {
  1047. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CArrowNoteColorKey)
  1048. } else if (key == KMMarkupColorTextKey) {
  1049. UserDefaults.standard.setPDFListViewColor((data as! NSColor), forKey: CFreeTextNoteColorKey)
  1050. } else if (key == KMMarkupFontTextStringKey) {
  1051. UserDefaults.standard.set(data, forKey: CFreeTextNoteFontNameKey)
  1052. UserDefaults.standard.synchronize()
  1053. } else if (key == KMMarkupFontTextAligmentKey) {
  1054. UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1055. UserDefaults.standard.synchronize()
  1056. } else if (key == KMMarkupFontNoteStringKey) {
  1057. // UserDefaults.standard.set(data, forKey: CFreeTextNoteAlignmentKey)
  1058. // UserDefaults.standard.synchronize()
  1059. }
  1060. }
  1061. private func getDefaultColors(forKey key: KMPreferenceKey) -> [Double] {
  1062. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue]!
  1063. for key_i in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1064. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1065. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1066. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1067. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1068. if (key == key_i) {
  1069. return markupGroupInfo[key] as! [Double]
  1070. }
  1071. }
  1072. let displayGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.display.rawValue]!
  1073. for key_i in [KMDisplayBackgroundNormalColorKey, KMDisplayBackgroundFullScreenColorKey,
  1074. KMDisplayFieldHighlightingColorKey, KMDisplayRequiredFieldHighlightingColorKey] {
  1075. if (key == key_i) {
  1076. return displayGroupInfo[key] as! [Double]
  1077. }
  1078. }
  1079. return [0.0, 0.0, 0.0, 1.0]
  1080. }
  1081. private func getDefaultColor(forKey key: KMPreferenceKey) -> NSColor {
  1082. return self.dataToColor(colors: self.getDefaultColors(forKey: key))
  1083. }
  1084. private func resetDataToPDFView() {
  1085. let markupGroupInfo: [KMPreferenceKey : Any] = self.getDefaultInfo()[KMPreferenceGroup.markup.rawValue]!
  1086. for key in [KMMarkupColorHighlightKey, KMMarkupColorUnderlineKey, KMMarkupColorStrikthroughKey,
  1087. KMMarkupColorPenKey, KMMarkupColorNoteKey, KMMarkupColorTextKey,
  1088. KMMarkupColorRectangleFillKey, KMMarkupColorRectangleBorderKey,
  1089. KMMarkupColorCircleFillKey, KMMarkupColorCircleBorderKey,
  1090. KMMarkupColorLineKey, KMMarkupColorArrowKey] {
  1091. self.syncDataToPDFView(self.dataToColor(colors: markupGroupInfo[key] as! [Double]), forKey: key)
  1092. }
  1093. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontTextStringKey)
  1094. self.syncDataToPDFView(NSTextAlignment.left.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1095. self.syncDataToPDFView(KMDefaultFontName, forKey: KMMarkupFontNoteStringKey)
  1096. }
  1097. public func resumeDataToPDFView() {
  1098. self.syncDataToPDFView(KMPreferenceManager.shared.markupHighlightColor, forKey: KMMarkupColorHighlightKey)
  1099. self.syncDataToPDFView(KMPreferenceManager.shared.markupUnderlineColor, forKey: KMMarkupColorUnderlineKey)
  1100. self.syncDataToPDFView(KMPreferenceManager.shared.markupStrikthroughColor, forKey: KMMarkupColorStrikthroughKey)
  1101. self.syncDataToPDFView(KMPreferenceManager.shared.markupPenColor, forKey: KMMarkupColorPenKey)
  1102. self.syncDataToPDFView(KMPreferenceManager.shared.markupNoteColor, forKey: KMMarkupColorNoteKey)
  1103. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleFillColor, forKey: KMMarkupColorRectangleFillKey)
  1104. self.syncDataToPDFView(KMPreferenceManager.shared.markupRectangleBorderColor, forKey: KMMarkupColorRectangleBorderKey)
  1105. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleFillColor, forKey: KMMarkupColorCircleFillKey)
  1106. self.syncDataToPDFView(KMPreferenceManager.shared.markupCircleBorderColor, forKey: KMMarkupColorCircleBorderKey)
  1107. self.syncDataToPDFView(KMPreferenceManager.shared.markupLineColor, forKey: KMMarkupColorLineKey)
  1108. self.syncDataToPDFView(KMPreferenceManager.shared.markupArrowColor, forKey: KMMarkupColorArrowKey)
  1109. self.syncDataToPDFView(KMPreferenceManager.shared.markupTextColor, forKey: KMMarkupColorTextKey)
  1110. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextString, forKey: KMMarkupFontTextStringKey)
  1111. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontTextAligment.rawValue, forKey: KMMarkupFontTextAligmentKey)
  1112. self.syncDataToPDFView(KMPreferenceManager.shared.markupFontNoteString, forKey: KMMarkupFontNoteStringKey)
  1113. }
  1114. public func initDataForAppLaunch() {
  1115. // KMPreferenceManager.shared.author = NSFullUserName()
  1116. if (KMPreferenceManager.shared.autoSave) {
  1117. Task { @MainActor in
  1118. if await KMLightMemberManager.manager.canPayFunction() == false {
  1119. KMPreferenceManager.shared.autoSave = false
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. // MARK: -
  1126. // MARK: UserDefaults
  1127. fileprivate let kKMLastOpenFilepathKey = "lastOpenFilepath"
  1128. fileprivate let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  1129. fileprivate let kKMViewSettingKey = "viewSetting"
  1130. fileprivate let kKMPageNumberKey = "pageNumber"
  1131. fileprivate let kKMPageScaleKey = "pageScale"
  1132. extension KMPreferenceManager {
  1133. var lastOpenFilepath: String? {
  1134. get {
  1135. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  1136. }
  1137. set {
  1138. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  1139. UserDefaults.standard.synchronize()
  1140. }
  1141. }
  1142. var lastOpenFilepaths: [String]? {
  1143. get {
  1144. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  1145. }
  1146. set {
  1147. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  1148. UserDefaults.standard.synchronize()
  1149. }
  1150. }
  1151. var viewSetting: [KMPreferenceViewSetting : Any]? {
  1152. get {
  1153. return UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [KMPreferenceViewSetting : Any]
  1154. }
  1155. set {
  1156. if (newValue == nil) {
  1157. UserDefaults.standard.set(newValue, forKey: kKMViewSettingKey)
  1158. UserDefaults.standard.synchronize()
  1159. return
  1160. }
  1161. let viewSetting: [String : Any]? = UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [String : Any]
  1162. var info: [String : Any] = [:]
  1163. if (viewSetting != nil) {
  1164. for (key, value) in viewSetting! {
  1165. info.updateValue(value, forKey: key)
  1166. }
  1167. }
  1168. for (key, value) in newValue! {
  1169. if (key == .pageNumber) { // 只处理 枚举
  1170. info.updateValue(value, forKey: key.rawValue)
  1171. }
  1172. }
  1173. UserDefaults.standard.set(info, forKey: kKMViewSettingKey)
  1174. UserDefaults.standard.synchronize()
  1175. }
  1176. }
  1177. func setPageNumber(_ number: Int, forKey key: String) {
  1178. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  1179. UserDefaults.standard.synchronize()
  1180. }
  1181. func getPageNumber(forKey key: String) -> Int? {
  1182. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  1183. }
  1184. func setPageScale(_ scale: Float, forKey key: String) {
  1185. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  1186. UserDefaults.standard.synchronize()
  1187. }
  1188. func getPageScale(forKey key: String) -> Float? {
  1189. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  1190. }
  1191. }
  1192. // MARK: -
  1193. // MARK: Qiuck
  1194. extension KMPreferenceManager {
  1195. internal func closeFileIsPrompt() -> Bool {
  1196. return self.closeFilePromptType == .promp
  1197. }
  1198. func leftSideNeedCloseWhenOpenFile() -> Bool {
  1199. return KMPreferenceManager.shared.leftSideDisplayType == .closeWhenOpenFile
  1200. }
  1201. var autoExpandPropertyPanel: Bool {
  1202. get {
  1203. return KMPreferenceManager.shared.propertyPanelExpandType == .auto
  1204. }
  1205. }
  1206. // 单位: 秒
  1207. var autoSaveTimeInterval: TimeInterval {
  1208. get {
  1209. var minute = KMPreferenceManager.shared.autoSavePerNumberMinute
  1210. if (minute < 5) {
  1211. minute = 5
  1212. } else if (minute > 99) {
  1213. minute = 99
  1214. }
  1215. let interval: TimeInterval = Double(minute) * 60.0
  1216. return interval
  1217. }
  1218. }
  1219. }