KMPreferenceGeneralController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // KMPreferenceGeneralController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/3.
  6. //
  7. import Cocoa
  8. /// 偏好设置 通用控制器
  9. class KMPreferenceGeneralController: NSViewController {
  10. @IBOutlet weak var filesTitleLabel: NSTextField!
  11. @IBOutlet weak var filesBox: NSBox!
  12. @IBOutlet weak var openLastUnCloseedDocumentCheck: NSButton!
  13. @IBOutlet weak var openLastUnlockedDocumentCheck: NSButton!
  14. @IBOutlet weak var maxDisplayNumberLabel: NSTextField!
  15. @IBOutlet weak var maxDisplayNumberStepper: NSView!
  16. private var maxDisplayNumberStepperView = KMPreferenceStepper()
  17. @IBOutlet weak var maxDisplayNumberDespLabel: NSTextField!
  18. @IBOutlet weak var saveFilesCheck: NSButton!
  19. @IBOutlet weak var saveFileStepper: KMPreferenceStepper!
  20. @IBOutlet weak var saveFilesDespLabl: NSTextField!
  21. @IBOutlet weak var closeFileLabel: NSTextField!
  22. @IBOutlet weak var closeFilePopupPromptRadio: NSButton!
  23. @IBOutlet weak var closeFileNoPromptRadio: NSButton!
  24. @IBOutlet weak var openImageFileLabel: NSTextField!
  25. @IBOutlet weak var openImageFileBox: NSBox!
  26. @IBOutlet weak var openImageFileAutoRadio: NSButton!
  27. @IBOutlet weak var openImageFilePromptRadio: NSButton!
  28. @IBOutlet weak var setDefaultPDFReaderCheck: NSButton!
  29. @IBOutlet weak var savePasswordLabel: NSTextField!
  30. @IBOutlet weak var savePasswordAlwaysRadio: NSButton!
  31. @IBOutlet weak var savePasswordNeverRadio: NSButton!
  32. @IBOutlet weak var savePasswordAskRadio: NSButton!
  33. @IBOutlet weak var authorLabel: NSTextField!
  34. @IBOutlet weak var authorTextField: NSTextField!
  35. convenience init() {
  36. self.init(nibName: "KMPreferenceGeneralController", bundle: nil)
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. self.filesTitleLabel.stringValue = NSLocalizedString("Files", comment: "")
  41. self.openLastUnCloseedDocumentCheck.title = NSLocalizedString("Open the last unclosed document when the APP starts", comment: "")
  42. self.openLastUnlockedDocumentCheck.title = NSLocalizedString("Restore the last view setting when opening a file", comment: "")
  43. self.maxDisplayNumberLabel.stringValue = NSLocalizedString("Maximum number of documents displayed in recently opened files", comment: "")
  44. self.maxDisplayNumberDespLabel.stringValue = NSLocalizedString("(10 to 50)", comment: "")
  45. self.saveFilesCheck.title = NSLocalizedString("Save files automatically, every", comment: "")
  46. self.closeFileLabel.stringValue = NSLocalizedString("Close File:", comment: "")
  47. self.closeFilePopupPromptRadio.title = NSLocalizedString("Prompt to \"save/don't save\"", comment: "")
  48. self.closeFileNoPromptRadio.title = NSLocalizedString("No prompt, save directly", comment: "")
  49. self.openImageFileLabel.stringValue = NSLocalizedString("Open Image File", comment: "")
  50. self.openImageFileAutoRadio.title = NSLocalizedString("Automatically scan and recognize text", comment: "")
  51. self.openImageFilePromptRadio.title = NSLocalizedString("Prompt to scan and recognize text", comment: "")
  52. self.setDefaultPDFReaderCheck.title = NSLocalizedString("Set as default PDF reader", comment: "")
  53. self.savePasswordLabel.stringValue = NSLocalizedString("Save the password in Keychain:", comment: "")
  54. self.savePasswordAlwaysRadio.title = NSLocalizedString("Always", comment: "")
  55. self.savePasswordNeverRadio.title = NSLocalizedString("Never", comment: "")
  56. self.savePasswordAskRadio.title = NSLocalizedString("Ask", comment: "")
  57. self.authorLabel.stringValue = NSLocalizedString("Author:", comment: "")
  58. self.initUIProperty()
  59. /// 设置数据
  60. self.initData()
  61. for radio in [self.closeFilePopupPromptRadio, self.closeFileNoPromptRadio,
  62. self.openImageFileAutoRadio, self.openImageFilePromptRadio,
  63. self.savePasswordAlwaysRadio, self.savePasswordNeverRadio, self.savePasswordAskRadio] {
  64. radio?.target = self
  65. radio?.action = #selector(radioAction)
  66. }
  67. self.openLastUnlockedDocumentCheck.target = self
  68. self.openLastUnlockedDocumentCheck.action = #selector(checkAction)
  69. self.openLastUnCloseedDocumentCheck.target = self
  70. self.openLastUnCloseedDocumentCheck.action = #selector(checkAction)
  71. self.saveFilesCheck.target = self
  72. self.saveFilesCheck.action = #selector(checkAction)
  73. self.setDefaultPDFReaderCheck.target = self
  74. self.setDefaultPDFReaderCheck.action = #selector(checkAction)
  75. let maxDisplayNumberStepperView = self.maxDisplayNumberStepperView
  76. self.maxDisplayNumberStepper.addSubview(maxDisplayNumberStepperView)
  77. maxDisplayNumberStepperView.frame = self.maxDisplayNumberStepper.bounds
  78. maxDisplayNumberStepperView.autoresizingMask = [.width, .height]
  79. maxDisplayNumberStepperView.stepper.minValue = 10
  80. maxDisplayNumberStepperView.stepper.maxValue = 50
  81. maxDisplayNumberStepperView.valueDidChange = { value in
  82. KMPreferenceManager.shared.documentMaximunDisplayNumber = Int(value)
  83. }
  84. self.saveFileStepper.stepper.minValue = 5
  85. self.saveFileStepper.stepper.maxValue = 99
  86. self.saveFileStepper.valueDidChange = { value in
  87. KMPreferenceManager.shared.autoSavePerNumberMinute = Int(value )
  88. }
  89. self.authorTextField.delegate = self
  90. }
  91. private func initUIProperty() {
  92. self.filesBox.borderColor = NSColor(white: 0, alpha: 0.05)
  93. self.filesBox.fillColor = NSColor(white: 0, alpha: 0.05)
  94. self.openImageFileBox.borderColor = NSColor(white: 0, alpha: 0.05)
  95. self.openImageFileBox.fillColor = NSColor(white: 0, alpha: 0.05)
  96. self.filesTitleLabel.textColor = NSColor(hex: "#252629")
  97. self.openLastUnCloseedDocumentCheck.setTitleColor(NSColor(hex: "#000000D9"))
  98. self.openLastUnlockedDocumentCheck.setTitleColor(NSColor(hex: "#000000D9"))
  99. self.maxDisplayNumberLabel.textColor = NSColor(hex: "#000000D9")
  100. self.saveFilesCheck.setTitleColor(NSColor(hex: "#000000D9"))
  101. self.closeFileLabel.textColor = NSColor(hex: "#000000D9")
  102. self.closeFilePopupPromptRadio.setTitleColor(NSColor(hex: "#000000D9"))
  103. self.closeFileNoPromptRadio.setTitleColor(NSColor(hex: "#000000D9"))
  104. self.openImageFileLabel.textColor = NSColor(hex: "#252629")
  105. self.openImageFileAutoRadio.setTitleColor(NSColor(hex: "#000000D9"))
  106. self.openImageFilePromptRadio.setTitleColor(NSColor(hex: "#000000D9"))
  107. self.setDefaultPDFReaderCheck.setTitleColor(NSColor(hex: "#000000D9"))
  108. self.savePasswordLabel.textColor = NSColor(hex: "#000000D9")
  109. self.savePasswordAlwaysRadio.setTitleColor(NSColor(hex: "#000000D9"))
  110. self.savePasswordNeverRadio.setTitleColor(NSColor(hex: "#000000D9"))
  111. self.savePasswordAskRadio.setTitleColor(NSColor(hex: "#000000D9"))
  112. self.authorLabel.textColor = NSColor(hex: "#000000D9")
  113. self.maxDisplayNumberDespLabel.textColor = NSColor(white: 0, alpha: 0.5)
  114. let string = "\(NSLocalizedString("minutes", comment: "")) "
  115. let attriText = NSMutableAttributedString(attributedString: NSAttributedString(string: string,attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.85)]))
  116. attriText.append(NSAttributedString(string: NSLocalizedString("(5 to 99)", comment: ""),attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.5)]))
  117. self.saveFilesDespLabl.attributedStringValue = attriText
  118. }
  119. private func initData() {
  120. if (KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart) {
  121. self.openLastUnCloseedDocumentCheck.state = .on
  122. } else {
  123. self.openLastUnCloseedDocumentCheck.state = .off
  124. }
  125. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  126. self.openLastUnlockedDocumentCheck.state = .on
  127. } else {
  128. self.openLastUnlockedDocumentCheck.state = .off
  129. }
  130. self.maxDisplayNumberStepperView.value = Double(KMPreferenceManager.shared.documentMaximunDisplayNumber)
  131. if (KMPreferenceManager.shared.autoSave) {
  132. self.saveFilesCheck.state = .on
  133. } else {
  134. self.saveFilesCheck.state = .off
  135. }
  136. self.saveFileStepper.value = Double(KMPreferenceManager.shared.autoSavePerNumberMinute)
  137. if (KMPreferenceManager.shared.closeFilePromptType == .promp) {
  138. self.selectCloseFileRadio(self.closeFilePopupPromptRadio)
  139. } else {
  140. self.selectCloseFileRadio(self.closeFileNoPromptRadio)
  141. }
  142. if (KMPreferenceManager.shared.openImageFileType == 0) {
  143. self.selectOpenImageFileRadio(self.openImageFileAutoRadio)
  144. } else {
  145. self.selectOpenImageFileRadio(self.openImageFilePromptRadio)
  146. }
  147. if (KMPreferenceManager.shared.setDefaultPDFReader) {
  148. self.setDefaultPDFReaderCheck.state = .on
  149. } else {
  150. self.setDefaultPDFReaderCheck.state = .off
  151. }
  152. let value = KMPreferenceManager.shared.savePasswordType
  153. if (value == .always) {
  154. self.selectSavePasswordRadio(self.savePasswordAlwaysRadio)
  155. } else if (value == .never) {
  156. self.selectSavePasswordRadio(self.savePasswordNeverRadio)
  157. } else if (value == .ask) {
  158. self.selectSavePasswordRadio(self.savePasswordAskRadio)
  159. }
  160. self.authorTextField.stringValue = KMPreferenceManager.shared.author
  161. }
  162. // MARK: User Actions
  163. @objc private func radioAction(sender: NSButton) {
  164. if (sender.isEqual(to: self.closeFilePopupPromptRadio)) {
  165. self.selectCloseFileRadio(sender)
  166. KMPreferenceManager.shared.closeFilePromptType = .promp
  167. } else if (sender.isEqual(to: self.closeFileNoPromptRadio)) {
  168. self.selectCloseFileRadio(sender)
  169. KMPreferenceManager.shared.closeFilePromptType = .noPromp
  170. } else if (sender.isEqual(to: self.openImageFileAutoRadio)) {
  171. self.selectOpenImageFileRadio(sender)
  172. KMPreferenceManager.shared.openImageFileType = 0
  173. } else if (sender.isEqual(to: self.openImageFilePromptRadio)) {
  174. self.selectOpenImageFileRadio(sender)
  175. KMPreferenceManager.shared.openImageFileType = 1
  176. } else if (sender.isEqual(to: self.savePasswordAlwaysRadio)) {
  177. self.selectSavePasswordRadio(sender)
  178. KMPreferenceManager.shared.savePasswordType = .always
  179. } else if (sender.isEqual(to: self.savePasswordNeverRadio)) {
  180. self.selectSavePasswordRadio(sender)
  181. KMPreferenceManager.shared.savePasswordType = .never
  182. } else if (sender.isEqual(to: self.savePasswordAskRadio)) {
  183. self.selectSavePasswordRadio(sender)
  184. KMPreferenceManager.shared.savePasswordType = .ask
  185. }
  186. }
  187. @objc private func checkAction(sender: NSButton) {
  188. if (sender.isEqual(to: self.openLastUnCloseedDocumentCheck)) {
  189. KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on
  190. } else if (sender.isEqual(to: self.openLastUnlockedDocumentCheck)) {
  191. KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart = sender.state == .on
  192. } else if (sender.isEqual(to: self.saveFilesCheck)) {
  193. KMPreferenceManager.shared.autoSave = sender.state == .on
  194. } else if (sender.isEqual(to: self.setDefaultPDFReaderCheck)) {
  195. KMPreferenceManager.shared.setDefaultPDFReader = sender.state == .on
  196. }
  197. }
  198. private func selectCloseFileRadio(_ sender: NSButton) {
  199. for radio in [self.closeFilePopupPromptRadio, self.closeFileNoPromptRadio] {
  200. if (sender.isEqual(to: radio)) {
  201. radio?.state = .on
  202. } else {
  203. radio?.state = .off
  204. }
  205. }
  206. }
  207. private func selectOpenImageFileRadio(_ sender: NSButton) {
  208. for radio in [self.openImageFileAutoRadio, self.openImageFilePromptRadio] {
  209. if (sender.isEqual(to: radio)) {
  210. radio?.state = .on
  211. } else {
  212. radio?.state = .off
  213. }
  214. }
  215. }
  216. private func selectSavePasswordRadio(_ sender: NSButton) {
  217. for radio in [self.savePasswordAlwaysRadio, self.savePasswordNeverRadio, self.savePasswordAskRadio] {
  218. if (sender.isEqual(to: radio)) {
  219. radio?.state = .on
  220. } else {
  221. radio?.state = .off
  222. }
  223. }
  224. }
  225. }
  226. extension KMPreferenceGeneralController: KMPreferenceDealDataChangeProtocol {
  227. func refreshUI() {
  228. DispatchQueue.main.async {
  229. self.initData()
  230. }
  231. }
  232. }
  233. extension KMPreferenceGeneralController: NSTextFieldDelegate {
  234. func controlTextDidChange(_ obj: Notification) {
  235. if (self.authorTextField.isEqual(to: obj.object)) {
  236. let author = self.authorTextField.stringValue
  237. KMPreferenceManager.shared.author = author
  238. }
  239. }
  240. }