SettingsGeneralView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // SettingsGeneralView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/9/26.
  6. //
  7. import Cocoa
  8. import AppKit
  9. import KMComponentLibrary
  10. class SettingsGeneralView: BaseXibView {
  11. @IBOutlet weak var appearanceLabel: NSTextField!
  12. @IBOutlet weak var authorLabel: NSTextField!
  13. @IBOutlet weak var authorInputView: ComponentInput!
  14. @IBOutlet weak var defaultSettingLabel: NSTextField!
  15. @IBOutlet weak var defaultPDFViewerCheckbox: ComponentCheckBox!
  16. @IBOutlet weak var reopenFileCheckBox: ComponentCheckBox!
  17. @IBOutlet weak var autosaveCheckBox: ComponentCheckBox!
  18. @IBOutlet weak var autosaveInputNumberView: ComponentInputNumber!
  19. @IBOutlet weak var autosaveLabel: NSTextField!
  20. @IBOutlet weak var defaultViewerBoxWidthConst: NSLayoutConstraint!
  21. @IBOutlet weak var reopenBoxWidthConst: NSLayoutConstraint!
  22. @IBOutlet weak var autosaveBoxWidthConst: NSLayoutConstraint!
  23. @IBOutlet weak var filelistLabel: NSTextField!
  24. @IBOutlet weak var filelistInputNumberView: ComponentInputNumber!
  25. @IBOutlet weak var filelistSubLabel: NSTextField!
  26. @IBOutlet weak var languageLabel: NSTextField!
  27. @IBOutlet weak var languageSelectView: ComponentSelect!
  28. @IBOutlet weak var sepDividerView: ComponentDivider!
  29. @IBOutlet weak var resetWarningLabel: NSTextField!
  30. @IBOutlet weak var resetWarningButton: ComponentButton!
  31. @IBOutlet weak var resetWarningBtnWidthConst: NSLayoutConstraint!
  32. @IBOutlet weak var savePasswordLabel: NSTextField!
  33. @IBOutlet weak var savePWAskRadio: ComponentRadio!
  34. @IBOutlet weak var savePWAlwaysRadio: ComponentRadio!
  35. @IBOutlet weak var savePWNeverRadio: ComponentRadio!
  36. @IBOutlet weak var savePWAskRadioWidthConst: NSLayoutConstraint!
  37. @IBOutlet weak var savePWAlwaysRadioWidthConst: NSLayoutConstraint!
  38. deinit {
  39. NotificationCenter.default.removeObserver(self)
  40. }
  41. override func draw(_ dirtyRect: NSRect) {
  42. super.draw(dirtyRect)
  43. // Drawing code here.
  44. self.setUp()
  45. self.reloadData()
  46. }
  47. public required init?(coder decoder: NSCoder) {
  48. super.init(coder: decoder)
  49. }
  50. override init(frame frameRect: NSRect) {
  51. super.init(frame: frameRect)
  52. }
  53. public override func awakeFromNib() {
  54. super.awakeFromNib()
  55. }
  56. func setUp() {
  57. var titleLabelColor: NSColor = NSColor.labelColor
  58. if let value = ComponentLibrary.shared.getComponentValueFromKey("colorText/2") {
  59. let currentValue = value as! [String : Any]
  60. titleLabelColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  61. }
  62. var titleLabelFont: NSFont = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  63. //Appearance
  64. self.appearanceLabel.stringValue = KMLocalizedString("Appearance", comment: "")
  65. self.appearanceLabel.textColor = titleLabelColor
  66. self.appearanceLabel.font = titleLabelFont
  67. //Author
  68. self.authorLabel.stringValue = KMLocalizedString("Author", comment: "")
  69. self.authorLabel.textColor = titleLabelColor
  70. self.authorLabel.font = titleLabelFont
  71. self.authorInputView.properties = ComponentInputProperty(size: .s,
  72. state: .normal,
  73. isError: false,
  74. showPrefix: false,
  75. showSuffix: false,
  76. showClear: true,
  77. isDisabled: false,
  78. placeholder: KMLocalizedString("Author", comment: ""),
  79. text: SettingsManager.sharedInstance.author,
  80. alignment: .left)
  81. authorInputView.delegate = self
  82. // Default Settings
  83. defaultSettingLabel.stringValue = KMLocalizedString("Default Settings", comment: "")
  84. defaultSettingLabel.textColor = titleLabelColor
  85. defaultSettingLabel.font = titleLabelFont
  86. defaultPDFViewerCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  87. state: .normal,
  88. isDisabled: false,
  89. showhelp: false,
  90. text: KMLocalizedString("Set PDF Reader Pro as the default PDF Viewer", comment: ""),
  91. checkboxType: SettingsManager.sharedInstance.defaultPDFViewer ? .selected : .normal)
  92. reopenFileCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  93. state: .normal,
  94. isDisabled: false,
  95. showhelp: false,
  96. text: KMLocalizedString("Reopen last open files at startup", comment: ""),
  97. checkboxType: SettingsManager.sharedInstance.reopenFileWhenAppStart ? .selected : .normal)
  98. autosaveCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  99. state: .normal,
  100. isDisabled: false,
  101. showhelp: false,
  102. text: KMLocalizedString("Automatically save the document every", comment: ""),
  103. checkboxType: SettingsManager.sharedInstance.autoSaveFile ? .selected : .normal)
  104. autosaveInputNumberView.properties = ComponentInputNumberProperty(alignment: .left,
  105. size: .s,
  106. state: .normal,
  107. isError: false,
  108. showErrorInfo: false,
  109. isDisabled: SettingsManager.sharedInstance.autoSaveFile == false,
  110. showPrefix: false,
  111. showSuffix: false,
  112. minSize: 5,
  113. maxSize: 99,
  114. text: SettingsManager.sharedInstance.autoSaveMinutes)
  115. autosaveInputNumberView.delegate = self
  116. autosaveLabel.stringValue = KMLocalizedString("minutes(5-99)", comment: "")
  117. defaultViewerBoxWidthConst.constant = defaultPDFViewerCheckbox.properties.propertyInfo.viewWidth
  118. reopenBoxWidthConst.constant = reopenFileCheckBox.properties.propertyInfo.viewWidth
  119. autosaveBoxWidthConst.constant = autosaveCheckBox.properties.propertyInfo.viewWidth
  120. filelistLabel.stringValue = KMLocalizedString("Documents in recently used list:", comment: "")
  121. filelistInputNumberView.properties = ComponentInputNumberProperty(alignment: .left,
  122. size: .s,
  123. state: .normal,
  124. isError: false,
  125. showErrorInfo: false,
  126. isDisabled: false,
  127. showPrefix: false,
  128. showSuffix: false,
  129. minSize: 10,
  130. maxSize: 50,
  131. text: String(SettingsManager.sharedInstance.fileListCount))
  132. filelistInputNumberView.delegate = self
  133. filelistSubLabel.stringValue = "(10-50)"
  134. defaultPDFViewerCheckbox.setTarget(self, action: #selector(defaultSettingAction(_:)))
  135. reopenFileCheckBox.setTarget(self, action: #selector(defaultSettingAction(_:)))
  136. autosaveCheckBox.setTarget(self, action: #selector(defaultSettingAction(_:)))
  137. // Language
  138. languageLabel.stringValue = KMLocalizedString("Language", comment: "")
  139. languageLabel.textColor = titleLabelColor
  140. languageLabel.font = titleLabelFont
  141. languageSelectView.properties = ComponentSelectProperties(size: .s,
  142. state: .normal,
  143. isDisabled: false,
  144. isError: false,
  145. leftIcon: false,
  146. placeholder: nil,
  147. errorText: nil,
  148. creatable: false,
  149. text: "English")
  150. if true {
  151. let languageArr = ["Follow System", "English", "Chinese_CN", "Chinese_TW", "Dutch", "French", "Spanish", "German", "Russian", "Italian", "Japanese"]
  152. var menuItemArr: [ComponentMenuitemProperty] = []
  153. for language in languageArr {
  154. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  155. itemSelected: false,
  156. isDisabled: false,
  157. keyEquivalent: nil,
  158. text: language)
  159. menuItemArr.append(itemProperty)
  160. }
  161. languageSelectView.updateMenuItemsArr(menuItemArr)
  162. }
  163. languageSelectView.delegate = self
  164. sepDividerView.properties = ComponentDividerProperty(type: .horizontal, dash: false)
  165. resetWarningLabel.stringValue = KMLocalizedString("Reset All Warnings:", comment: "")
  166. if let value = ComponentLibrary.shared.getComponentValueFromKey("comp-form/colorText-label") {
  167. let currentValue = value as! [String : Any]
  168. resetWarningLabel.textColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  169. } else {
  170. resetWarningLabel.textColor = NSColor.labelColor
  171. }
  172. resetWarningButton.properties = ComponentButtonProperty(type: .default_tertiary,
  173. size: .s,
  174. state: .normal,
  175. isDisable: false,
  176. onlyIcon: false,
  177. showLeftIcon: false,
  178. showRightIcon: false,
  179. buttonText: KMLocalizedString("Reset"),
  180. keepPressState: false)
  181. resetWarningBtnWidthConst.constant = resetWarningButton.properties.propertyInfo.viewWidth
  182. resetWarningButton.setTarget(self, action: #selector(resetWarningAction(_:)))
  183. //
  184. savePasswordLabel.stringValue = KMLocalizedString("Save the password in Keychain:", comment: "")
  185. savePasswordLabel.textColor = resetWarningLabel.textColor
  186. savePWAskRadio.properties = ComponentCheckBoxProperty(size: .s,
  187. state: .normal,
  188. isDisabled: false,
  189. showhelp: false,
  190. text: KMLocalizedString("Ask", comment: ""),
  191. checkboxType: .normal)
  192. savePWAlwaysRadio.properties = ComponentCheckBoxProperty(size: .s,
  193. state: .normal,
  194. isDisabled: false,
  195. showhelp: false,
  196. text: KMLocalizedString("Always", comment: ""),
  197. checkboxType: .normal)
  198. savePWNeverRadio.properties = ComponentCheckBoxProperty(size: .s,
  199. state: .normal,
  200. isDisabled: false,
  201. showhelp: false,
  202. text: KMLocalizedString("Never", comment: ""),
  203. checkboxType: .normal)
  204. savePWAskRadioWidthConst.constant = savePWAskRadio.properties.propertyInfo.viewWidth
  205. savePWAlwaysRadioWidthConst.constant = savePWAlwaysRadio.properties.propertyInfo.viewWidth
  206. savePWAskRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  207. savePWAlwaysRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  208. savePWNeverRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  209. }
  210. func reloadData() {
  211. //Default Settings
  212. defaultPDFViewerCheckbox.properties.checkboxType = SettingsManager.sharedInstance.defaultPDFViewer ? .selected : .normal
  213. defaultPDFViewerCheckbox.reloadData()
  214. reopenFileCheckBox.properties.checkboxType = SettingsManager.sharedInstance.reopenFileWhenAppStart ? .selected : .normal
  215. reopenFileCheckBox.reloadData()
  216. resetWarningButton.properties.isDisabled = SettingsManager.sharedInstance.resetDefaultWaringKeyEnable() ? false : true
  217. resetWarningButton.reloadData()
  218. autosaveCheckBox.properties.checkboxType = SettingsManager.sharedInstance.autoSaveFile ? .selected : .normal
  219. autosaveCheckBox.reloadData()
  220. autosaveInputNumberView.properties.isDisabled = SettingsManager.sharedInstance.autoSaveFile == false
  221. autosaveInputNumberView.properties.text = SettingsManager.sharedInstance.autoSaveMinutes
  222. autosaveInputNumberView.reloadData()
  223. filelistInputNumberView.properties.text = String(SettingsManager.sharedInstance.fileListCount)
  224. filelistInputNumberView.reloadData()
  225. languageSelectView.selectItemAtIndex(SettingsManager.sharedInstance.language.rawValue)
  226. savePWAskRadio.properties.checkboxType = .normal
  227. savePWAlwaysRadio.properties.checkboxType = .normal
  228. savePWNeverRadio.properties.checkboxType = .normal
  229. if SettingsManager.sharedInstance.keychainType == .ask {
  230. savePWAskRadio.properties.checkboxType = .selected
  231. } else if SettingsManager.sharedInstance.keychainType == .always {
  232. savePWAlwaysRadio.properties.checkboxType = .selected
  233. } else if SettingsManager.sharedInstance.keychainType == .never {
  234. savePWNeverRadio.properties.checkboxType = .selected
  235. }
  236. savePWAskRadio.reloadData()
  237. savePWAlwaysRadio.reloadData()
  238. savePWNeverRadio.reloadData()
  239. }
  240. //MARK: - Action
  241. @objc func defaultSettingAction(_ sender: NSView) {
  242. if sender == defaultPDFViewerCheckbox {
  243. SettingsManager.sharedInstance.defaultPDFViewer = defaultPDFViewerCheckbox.properties.checkboxType == .selected
  244. } else if sender == reopenFileCheckBox {
  245. SettingsManager.sharedInstance.reopenFileWhenAppStart = reopenFileCheckBox.properties.checkboxType == .selected
  246. } else if sender == autosaveCheckBox {
  247. SettingsManager.sharedInstance.autoSaveFile = autosaveCheckBox.properties.checkboxType == .selected
  248. }
  249. self.reloadData()
  250. }
  251. @objc func savePWClicked(_ sender: NSView) {
  252. if sender == savePWAskRadio {
  253. SettingsManager.sharedInstance.keychainType = .ask
  254. } else if sender == savePWAlwaysRadio {
  255. SettingsManager.sharedInstance.keychainType = .always
  256. } else if sender == savePWNeverRadio {
  257. SettingsManager.sharedInstance.keychainType = .never
  258. }
  259. self.reloadData()
  260. }
  261. @objc func resetWarningAction(_ sender: ComponentButton) {
  262. let alert = NSAlert()
  263. alert.alertStyle = .warning
  264. alert.messageText = KMLocalizedString("Are you sure you want to reset all Warnings?")
  265. alert.addButton(withTitle: KMLocalizedString("Confirm"))
  266. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  267. alert.beginSheetModal(for: self.window!, completionHandler: { (response) in
  268. if response == .alertFirstButtonReturn {
  269. for string in SettingsManager.sharedInstance.resetDefaultWaringKeys {
  270. UserDefaults.standard.removeObject(forKey: string)
  271. }
  272. UserDefaults.standard.synchronize()
  273. self.reloadData()
  274. }
  275. })
  276. }
  277. //MARK: - Mouse Event
  278. override func mouseDown(with event: NSEvent) {
  279. super.mouseDown(with: event)
  280. self.window?.makeFirstResponder(nil)
  281. }
  282. }
  283. extension SettingsGeneralView: ComponentInputDelegate {
  284. func componentInputDidEndEditing(inputView: ComponentInput) {
  285. if inputView == authorInputView {
  286. SettingsManager.sharedInstance.author = inputView.properties.text
  287. }
  288. }
  289. }
  290. extension SettingsGeneralView: ComponentInputNumberDelegate {
  291. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  292. if inputNumber == autosaveInputNumberView {
  293. SettingsManager.sharedInstance.autoSaveMinutes = autosaveInputNumberView.properties.text
  294. } else if inputNumber == filelistInputNumberView {
  295. let text: String = filelistInputNumberView.properties.text
  296. SettingsManager.sharedInstance.fileListCount = Int(text)!
  297. }
  298. self.reloadData()
  299. }
  300. }
  301. extension SettingsGeneralView: ComponentSelectDelegate {
  302. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  303. let selectIndex: NSInteger = view?.indexOfSelect() ?? 0
  304. if let _ = menuItemProperty {
  305. if selectIndex >= 0 {
  306. let language = settingsLanguage(rawValue: selectIndex) ?? .FollowSystem
  307. if SettingsManager.sharedInstance.language != language {
  308. SettingsManager.sharedInstance.language = language
  309. NotificationCenter.default.post(name: APPLanguageChangedNotificationName, object: nil)
  310. self.setUp()
  311. self.reloadData()
  312. }
  313. }
  314. }
  315. }
  316. }