SettingsManager.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //
  2. // SettingsManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/9/26.
  6. //
  7. import Cocoa
  8. @objc public enum appearanceMode: Int {
  9. case lightMode
  10. case darkMode
  11. case autoMode
  12. }
  13. @objc public enum keyChainPasswordType : Int {
  14. case ask = 0
  15. case always
  16. case never
  17. }
  18. @objc public enum pageLayoutType : Int {
  19. case singlePage = 0
  20. case singlePageContinue
  21. case twoPage
  22. case twoPageContinue
  23. case bookMode
  24. case bookModeContinue
  25. }
  26. @objc public enum leftSidePanelType : Int {
  27. case defaultOpen = 0
  28. case sameAsLastOpen
  29. case hideLeftSide
  30. case prioritizeOutline
  31. }
  32. @objc public enum defaultOpenType: Int {
  33. case thumbnail = 0
  34. case outline
  35. case bookmark
  36. case annotation
  37. }
  38. @objc public enum zoomInfoType : Int {
  39. case adaptationWidth = 0
  40. case adapPage
  41. case actualSize
  42. case percent_10
  43. case percent_25
  44. case percent_50
  45. case percent_75
  46. case percent_100
  47. case percent_150
  48. case percent_200
  49. case percent_400
  50. case percent_800
  51. }
  52. @objc public enum panelOpenInteractionType : Int {
  53. case automatic = 0
  54. case hidePropertyPanel
  55. }
  56. @objc public enum settingsLanguage: Int {
  57. case English = 0
  58. case Chinese_CN
  59. case Chinese_TW
  60. case Dutch
  61. case French
  62. case Spanish
  63. case German
  64. case Russian
  65. case Italian
  66. case Polish
  67. case Japanese
  68. }
  69. let APPAppearanceChangedNotificationName = NSNotification.Name("APPAppearanceChangedNotificationName")
  70. let APPLanguageChangedNotificationName = NSNotification.Name("APPLanguageChangedNotificationName")
  71. //General
  72. let settingsAppearanceKey = "settingsAppearanceKey"
  73. let settingsAuthorKey = "settingsAuthorKey"
  74. let settingsDefaultPDFViewerKey = "settingsDefaultPDFViewerKey"
  75. var settingsReopenFileKey = "settingsReopenFileKey"
  76. var settingsAutoSaveFileKey = "settingsAutoSaveFileKey"
  77. var settingsAutoSaveMinutesKey = "settingsAutoSaveMinutesKey"
  78. var settingsFileListCountKey = "settingsFileListCountKey"
  79. var settingsLanguageDidChangedKey = "settingsLanguageDidChangedKey" //记录是否有手动修改language信息
  80. var settingsLanguageTypeKey = "settingsLanguageTypeKey"
  81. var settingsKeychainTypeKey = "settingsKeychainTypeKey"
  82. //PageDisplay
  83. var settingsPageLayoutTypeKey = "settingsPageLayoutTypeKey"
  84. var settingsZoomTypeKey = "settingsZoomTypeKey"
  85. var settingsLeftSidePanelKey = "settingsLeftSidePanelKey"
  86. var settingsDefaultOpenTypeKey = "settingsDefaultOpenTypeKey"
  87. var settingsPanelOpenTypeKey = "settingsPanelOpenTypeKey"
  88. var settingsShowQuickActionBarKey = "settingsShowQuickActionBarKey"
  89. var settingsHighlightLinkKey = "settingsHighlightLinkKey"
  90. var settingsHighlightFormFieldKey = "settingsHighlightFormFieldKey"
  91. class SettingsManager: NSObject {
  92. static let sharedInstance = SettingsManager.init()
  93. //General
  94. private var _appearance: appearanceMode = .autoMode
  95. private var _author: String = ""
  96. private var _defaultPDFViewer: Bool = true
  97. private var _reopenFile: Bool = false
  98. private var _autoSaveFile: Bool = false
  99. private var _autoSaveMinutes: String = "15"
  100. private var _fileListCount: Int = 10
  101. private var _language: settingsLanguage = .English
  102. private var _keychainType: keyChainPasswordType = .ask
  103. //PageDisplay
  104. private var _layoutType: pageLayoutType = .singlePage
  105. private var _zoomType: zoomInfoType = .adaptationWidth
  106. private var _leftPanelType: leftSidePanelType = .defaultOpen
  107. private var _defaultOpen: defaultOpenType = .thumbnail
  108. private var _panelOpenType: panelOpenInteractionType = .automatic
  109. private var _showQuickActionBar: Bool = true
  110. private var _highlightLink: Bool = true
  111. private var _highlightFormField: Bool = true
  112. override init() {
  113. super.init()
  114. self.config()
  115. }
  116. func config() {
  117. if UserDefaults.standard.object(forKey: "savedSettingsKey") != nil {
  118. self.loadData()
  119. } else {
  120. self.resetDefaultData()
  121. UserDefaults.standard.setValue("didSaved", forKey: "savedSettingsKey")
  122. }
  123. // if appearance == .autoMode {
  124. //
  125. // } else if appearance == .lightMode {
  126. // NSApplication.shared.appearance = NSAppearance(named: .aqua)
  127. // } else if appearance == .darkMode {
  128. // NSApplication.shared.appearance = NSAppearance(named: .darkAqua)
  129. // }
  130. }
  131. private func loadData() {
  132. //General
  133. self.appearance = appearanceMode(rawValue: UserDefaults.standard.integer(forKey: settingsAppearanceKey)) ?? .autoMode
  134. self.author = UserDefaults.standard.string(forKey: settingsAuthorKey) ?? ""
  135. self.defaultPDFViewer = UserDefaults.standard.bool(forKey: settingsDefaultPDFViewerKey)
  136. self.reopenFile = UserDefaults.standard.bool(forKey: settingsReopenFileKey)
  137. self.autoSaveFile = UserDefaults.standard.bool(forKey: settingsAutoSaveFileKey)
  138. self.autoSaveMinutes = UserDefaults.standard.string(forKey: settingsAutoSaveMinutesKey) ?? ""
  139. self.fileListCount = UserDefaults.standard.integer(forKey: settingsFileListCountKey)
  140. if UserDefaults.standard.string(forKey: settingsLanguageDidChangedKey) != nil {
  141. self.language = settingsLanguage(rawValue: UserDefaults.standard.integer(forKey: settingsLanguageTypeKey)) ?? .English
  142. } else {
  143. self.setUpLanguageKey()
  144. }
  145. self.keychainType = keyChainPasswordType(rawValue: UserDefaults.standard.integer(forKey: settingsKeychainTypeKey)) ?? .ask
  146. //PageDisplay
  147. self.layoutType = pageLayoutType(rawValue: UserDefaults.standard.integer(forKey: settingsPageLayoutTypeKey)) ?? .singlePage
  148. self.zoomType = zoomInfoType(rawValue: UserDefaults.standard.integer(forKey: settingsZoomTypeKey)) ?? .adaptationWidth
  149. self.leftPanelType = leftSidePanelType(rawValue: UserDefaults.standard.integer(forKey: settingsLeftSidePanelKey)) ?? .defaultOpen
  150. self.defaultOpen = defaultOpenType(rawValue: UserDefaults.standard.integer(forKey: settingsDefaultOpenTypeKey)) ?? .thumbnail
  151. self.panelOpenType = panelOpenInteractionType(rawValue: UserDefaults.standard.integer(forKey: settingsPanelOpenTypeKey)) ?? .automatic
  152. self.showQuickActionBar = UserDefaults.standard.bool(forKey: settingsShowQuickActionBarKey)
  153. self.highlightLink = UserDefaults.standard.bool(forKey: settingsHighlightLinkKey)
  154. self.highlightFormField = UserDefaults.standard.bool(forKey: settingsHighlightFormFieldKey)
  155. }
  156. func saveData() {
  157. //General
  158. UserDefaults.standard.setValue(appearance.rawValue, forKey: settingsAppearanceKey)
  159. UserDefaults.standard.setValue(author, forKey: settingsAuthorKey)
  160. UserDefaults.standard.setValue(defaultPDFViewer, forKey: settingsDefaultPDFViewerKey)
  161. UserDefaults.standard.setValue(reopenFile, forKey: settingsReopenFileKey)
  162. UserDefaults.standard.setValue(autoSaveFile, forKey: settingsAutoSaveFileKey)
  163. UserDefaults.standard.setValue(autoSaveMinutes, forKey: settingsAutoSaveMinutesKey)
  164. UserDefaults.standard.setValue(fileListCount, forKey: settingsFileListCountKey)
  165. UserDefaults.standard.setValue(language.rawValue, forKey: settingsLanguageTypeKey)
  166. UserDefaults.standard.setValue(keychainType.rawValue, forKey: settingsKeychainTypeKey)
  167. //PageDisplay
  168. UserDefaults.standard.setValue(layoutType.rawValue, forKey: settingsPageLayoutTypeKey)
  169. UserDefaults.standard.setValue(zoomType.rawValue, forKey: settingsZoomTypeKey)
  170. UserDefaults.standard.setValue(leftPanelType.rawValue, forKey: settingsLeftSidePanelKey)
  171. UserDefaults.standard.setValue(panelOpenType.rawValue, forKey: settingsPanelOpenTypeKey)
  172. UserDefaults.standard.setValue(defaultOpen.rawValue, forKey: settingsDefaultOpenTypeKey)
  173. UserDefaults.standard.setValue(showQuickActionBar, forKey: settingsShowQuickActionBarKey)
  174. UserDefaults.standard.setValue(highlightLink, forKey: settingsHighlightLinkKey)
  175. UserDefaults.standard.setValue(highlightFormField, forKey: settingsHighlightFormFieldKey)
  176. }
  177. //MARK: - Setter
  178. var appearance: appearanceMode {
  179. get {
  180. return _appearance
  181. }
  182. set {
  183. _appearance = newValue
  184. }
  185. }
  186. var author: String {
  187. get {
  188. return _author
  189. }
  190. set {
  191. _author = newValue
  192. }
  193. }
  194. var defaultPDFViewer: Bool {
  195. get {
  196. return _defaultPDFViewer
  197. }
  198. set {
  199. _defaultPDFViewer = newValue
  200. }
  201. }
  202. var reopenFile: Bool {
  203. get {
  204. return _reopenFile
  205. }
  206. set {
  207. _reopenFile = newValue
  208. }
  209. }
  210. var autoSaveFile: Bool {
  211. get {
  212. return _autoSaveFile
  213. }
  214. set {
  215. _autoSaveFile = newValue
  216. }
  217. }
  218. var autoSaveMinutes: String {
  219. get {
  220. return _autoSaveMinutes
  221. }
  222. set {
  223. _autoSaveMinutes = newValue
  224. }
  225. }
  226. var fileListCount: Int {
  227. get {
  228. return _fileListCount
  229. }
  230. set {
  231. _fileListCount = newValue
  232. }
  233. }
  234. var language: settingsLanguage {
  235. get {
  236. return _language
  237. }
  238. set {
  239. _language = newValue
  240. }
  241. }
  242. var keychainType: keyChainPasswordType {
  243. get {
  244. return _keychainType
  245. }
  246. set {
  247. _keychainType = newValue
  248. }
  249. }
  250. var layoutType: pageLayoutType {
  251. get {
  252. return _layoutType
  253. }
  254. set {
  255. _layoutType = newValue
  256. }
  257. }
  258. var zoomType: zoomInfoType {
  259. get {
  260. return _zoomType
  261. }
  262. set {
  263. _zoomType = newValue
  264. }
  265. }
  266. var leftPanelType: leftSidePanelType {
  267. get {
  268. return _leftPanelType
  269. }
  270. set {
  271. _leftPanelType = newValue
  272. }
  273. }
  274. var defaultOpen: defaultOpenType {
  275. get {
  276. return _defaultOpen
  277. }
  278. set {
  279. _defaultOpen = newValue
  280. }
  281. }
  282. var panelOpenType: panelOpenInteractionType {
  283. get {
  284. return _panelOpenType
  285. }
  286. set {
  287. _panelOpenType = newValue
  288. }
  289. }
  290. var showQuickActionBar: Bool {
  291. get {
  292. return _showQuickActionBar
  293. }
  294. set {
  295. _showQuickActionBar = newValue
  296. }
  297. }
  298. var highlightLink: Bool {
  299. get {
  300. return _highlightLink
  301. }
  302. set {
  303. _highlightLink = newValue
  304. }
  305. }
  306. var highlightFormField: Bool {
  307. get {
  308. return _highlightFormField
  309. }
  310. set {
  311. _highlightFormField = newValue
  312. }
  313. }
  314. //MARK: - Reset
  315. func resetDefaultData() {
  316. self.resetGeneralData()
  317. self.resetPageDisplayData()
  318. self.saveData()
  319. }
  320. func resetGeneralData() {
  321. self.appearance = .autoMode
  322. self.author = NSFullUserName()
  323. self.defaultPDFViewer = true
  324. self.reopenFile = false
  325. self.autoSaveFile = false
  326. self.autoSaveMinutes = "15"
  327. self.fileListCount = 10
  328. self.keychainType = .ask
  329. self.setUpLanguageKey()
  330. }
  331. func resetPageDisplayData() {
  332. self.layoutType = .singlePage
  333. self.zoomType = .adaptationWidth
  334. self.leftPanelType = .defaultOpen
  335. self.defaultOpen = .thumbnail
  336. self.panelOpenType = .automatic
  337. self.showQuickActionBar = true
  338. self.highlightLink = true
  339. self.highlightFormField = true
  340. }
  341. func setUpLanguageKey() {
  342. let defaultLan = NSLocalizedString("LanguageMarkKey", comment: "")
  343. if defaultLan == "English" {
  344. self.language = .English
  345. } else if defaultLan == "Simplified" {
  346. self.language = .Chinese_CN
  347. } else if defaultLan == "Traditional" {
  348. self.language = .Chinese_TW
  349. }
  350. }
  351. }
  352. //MARK: - Method
  353. public func KMLocalizedString(_ key: String, comment: String? = nil) -> String {
  354. var bundlePath: String? = nil
  355. // if SettingsManager.sharedInstance.language == .English {
  356. // bundlePath = Bundle.main.path(forResource: "en", ofType: "lproj")
  357. // } else if SettingsManager.sharedInstance.language == .Chinese_CN {
  358. // bundlePath = Bundle.main.path(forResource: "zh-Hans", ofType: "lproj")
  359. // } else if SettingsManager.sharedInstance.language == .Chinese_TW {
  360. // bundlePath = Bundle.main.path(forResource: "zh-Hant", ofType: "lproj")
  361. // }
  362. if let bPath = bundlePath, let bundle = Bundle.init(path: bPath) {
  363. return NSLocalizedString(key, tableName: "Localizable", bundle: bundle, comment: comment ?? "")
  364. } else {
  365. return NSLocalizedString(key, comment: "")
  366. }
  367. }