SettingsGeneralView.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.reopenFile ? .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. var languageArr = ["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", comment: ""))
  180. resetWarningBtnWidthConst.constant = resetWarningButton.properties.propertyInfo.viewWidth
  181. //
  182. savePasswordLabel.stringValue = KMLocalizedString("Save the password in Keychain:", comment: "")
  183. savePasswordLabel.textColor = resetWarningLabel.textColor
  184. savePWAskRadio.properties = ComponentCheckBoxProperty(size: .s,
  185. state: .normal,
  186. isDisabled: false,
  187. showhelp: false,
  188. text: KMLocalizedString("Ask", comment: ""),
  189. checkboxType: .normal)
  190. savePWAlwaysRadio.properties = ComponentCheckBoxProperty(size: .s,
  191. state: .normal,
  192. isDisabled: false,
  193. showhelp: false,
  194. text: KMLocalizedString("Always", comment: ""),
  195. checkboxType: .normal)
  196. savePWNeverRadio.properties = ComponentCheckBoxProperty(size: .s,
  197. state: .normal,
  198. isDisabled: false,
  199. showhelp: false,
  200. text: KMLocalizedString("Never", comment: ""),
  201. checkboxType: .normal)
  202. savePWAskRadioWidthConst.constant = savePWAskRadio.properties.propertyInfo.viewWidth
  203. savePWAlwaysRadioWidthConst.constant = savePWAlwaysRadio.properties.propertyInfo.viewWidth
  204. savePWAskRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  205. savePWAlwaysRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  206. savePWNeverRadio.setTarget(self, action: #selector(savePWClicked(_:)))
  207. }
  208. func reloadData() {
  209. //Default Settings
  210. defaultPDFViewerCheckbox.properties.checkboxType = SettingsManager.sharedInstance.defaultPDFViewer ? .selected : .normal
  211. defaultPDFViewerCheckbox.reloadData()
  212. reopenFileCheckBox.properties.checkboxType = SettingsManager.sharedInstance.reopenFile ? .selected : .normal
  213. reopenFileCheckBox.reloadData()
  214. autosaveCheckBox.properties.checkboxType = SettingsManager.sharedInstance.autoSaveFile ? .selected : .normal
  215. autosaveCheckBox.reloadData()
  216. autosaveInputNumberView.properties.isDisabled = SettingsManager.sharedInstance.autoSaveFile == false
  217. autosaveInputNumberView.properties.text = SettingsManager.sharedInstance.autoSaveMinutes
  218. autosaveInputNumberView.reloadData()
  219. filelistInputNumberView.properties.text = String(SettingsManager.sharedInstance.fileListCount)
  220. filelistInputNumberView.reloadData()
  221. //
  222. //
  223. languageSelectView.selectItemAtIndex(SettingsManager.sharedInstance.language.rawValue)
  224. //
  225. //
  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.reopenFile = 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. //MARK: - Mouse Event
  262. override func mouseDown(with event: NSEvent) {
  263. super.mouseDown(with: event)
  264. self.window?.makeFirstResponder(nil)
  265. }
  266. }
  267. extension SettingsGeneralView: ComponentInputDelegate {
  268. func componentInputDidEndEditing(inputView: ComponentInput) {
  269. if inputView == authorInputView {
  270. SettingsManager.sharedInstance.author = inputView.properties.text
  271. }
  272. }
  273. }
  274. extension SettingsGeneralView: ComponentInputNumberDelegate {
  275. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  276. if inputNumber == autosaveInputNumberView {
  277. SettingsManager.sharedInstance.autoSaveMinutes = autosaveInputNumberView.properties.text ?? "5"
  278. } else if inputNumber == filelistInputNumberView {
  279. if let text: String = filelistInputNumberView.properties.text {
  280. SettingsManager.sharedInstance.fileListCount = Int(text)!
  281. }
  282. }
  283. self.reloadData()
  284. }
  285. }
  286. extension SettingsGeneralView: ComponentSelectDelegate {
  287. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  288. let selectIndex: NSInteger = view?.indexOfSelect() ?? 0
  289. if let item = menuItemProperty {
  290. if selectIndex >= 0 {
  291. SettingsManager.sharedInstance.language = settingsLanguage(rawValue: selectIndex)!
  292. UserDefaults.standard.setValue("didChanged", forKey: settingsLanguageDidChangedKey)
  293. }
  294. }
  295. }
  296. }