KMPreferenceManager.swift 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. 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, 175/255.0, 37/255.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. }
  953. // MARK: -
  954. // MARK: UserDefaults
  955. internal let kKMLastOpenFilepathKey = "lastOpenFilepath"
  956. internal let kKMLastOpenFilepathKeys = "lastOpenFilepaths"
  957. internal let kKMViewSettingKey = "viewSetting"
  958. internal let kKMPageNumberKey = "pageNumber"
  959. internal let kKMPageScaleKey = "pageScale"
  960. extension KMPreferenceManager {
  961. var lastOpenFilepath: String? {
  962. get {
  963. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKey) as? String
  964. }
  965. set {
  966. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKey)
  967. UserDefaults.standard.synchronize()
  968. }
  969. }
  970. var lastOpenFilepaths: [String]? {
  971. get {
  972. return UserDefaults.standard.value(forKey: kKMLastOpenFilepathKeys) as? [String]
  973. }
  974. set {
  975. UserDefaults.standard.set(newValue, forKey: kKMLastOpenFilepathKeys)
  976. UserDefaults.standard.synchronize()
  977. }
  978. }
  979. var viewSetting: [KMPreferenceViewSetting : Any]? {
  980. get {
  981. return UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [KMPreferenceViewSetting : Any]
  982. }
  983. set {
  984. if (newValue == nil) {
  985. UserDefaults.standard.set(newValue, forKey: kKMViewSettingKey)
  986. UserDefaults.standard.synchronize()
  987. return
  988. }
  989. let viewSetting: [String : Any]? = UserDefaults.standard.value(forKey: kKMViewSettingKey) as? [String : Any]
  990. var info: [String : Any] = [:]
  991. if (viewSetting != nil) {
  992. for (key, value) in viewSetting! {
  993. info.updateValue(value, forKey: key)
  994. }
  995. }
  996. for (key, value) in newValue! {
  997. if (key == .pageNumber) { // 只处理 枚举
  998. info.updateValue(value, forKey: key.rawValue)
  999. }
  1000. }
  1001. UserDefaults.standard.set(info, forKey: kKMViewSettingKey)
  1002. UserDefaults.standard.synchronize()
  1003. }
  1004. }
  1005. func setPageNumber(_ number: Int, forKey key: String) {
  1006. UserDefaults.standard.set(number, forKey: "\(key)+\(kKMPageNumberKey)")
  1007. UserDefaults.standard.synchronize()
  1008. }
  1009. func getPageNumber(forKey key: String) -> Int? {
  1010. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageNumberKey)") as? Int
  1011. }
  1012. func setPageScale(_ scale: Float, forKey key: String) {
  1013. UserDefaults.standard.set(scale, forKey: "\(key)+\(kKMPageScaleKey)")
  1014. UserDefaults.standard.synchronize()
  1015. }
  1016. func getPageScale(forKey key: String) -> Float? {
  1017. return UserDefaults.standard.value(forKey: "\(key)+\(kKMPageScaleKey)") as? Float
  1018. }
  1019. }
  1020. // MARK: -
  1021. // MARK: Qiuck
  1022. extension KMPreferenceManager {
  1023. internal func closeFileIsPrompt() -> Bool {
  1024. return self.closeFilePromptType == .promp
  1025. }
  1026. func leftSideNeedCloseWhenOpenFile() -> Bool {
  1027. return KMPreferenceManager.shared.leftSideDisplayType == .closeWhenOpenFile
  1028. }
  1029. var autoExpandPropertyPanel: Bool {
  1030. get {
  1031. return KMPreferenceManager.shared.propertyPanelExpandType == .auto
  1032. }
  1033. }
  1034. // 单位: 秒
  1035. var autoSaveTimeInterval: TimeInterval {
  1036. get {
  1037. var minute = KMPreferenceManager.shared.autoSavePerNumberMinute
  1038. if (minute < 5) {
  1039. minute = 5
  1040. } else if (minute > 99) {
  1041. minute = 99
  1042. }
  1043. let interval: TimeInterval = Double(minute) * 60.0
  1044. return interval
  1045. }
  1046. }
  1047. }