SettingsGeneralView.swift 21 KB

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