KMPreferenceManager.swift 49 KB

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