KMPreferenceGeneralController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.km_init(hex: "#252629")
  97. self.openLastUnCloseedDocumentCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  98. self.openLastUnlockedDocumentCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  99. self.maxDisplayNumberLabel.textColor = NSColor.km_init(hex: "#000000D9")
  100. self.saveFilesCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  101. self.closeFileLabel.textColor = NSColor.km_init(hex: "#000000D9")
  102. self.closeFilePopupPromptRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  103. self.closeFileNoPromptRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  104. self.openImageFileLabel.textColor = NSColor.km_init(hex: "#252629")
  105. self.openImageFileAutoRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  106. self.openImageFilePromptRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  107. self.setDefaultPDFReaderCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  108. self.savePasswordLabel.textColor = NSColor.km_init(hex: "#000000D9")
  109. self.savePasswordAlwaysRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  110. self.savePasswordNeverRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  111. self.savePasswordAskRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  112. self.authorLabel.textColor = NSColor.km_init(hex: "#000000D9")
  113. self.maxDisplayNumberDespLabel.textColor = NSColor(white: 0, alpha: 0.5)
  114. self.updateSaveFilesDespLabl(enabled: true)
  115. }
  116. private func initData() {
  117. if (KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart) {
  118. self.openLastUnCloseedDocumentCheck.state = .on
  119. } else {
  120. self.openLastUnCloseedDocumentCheck.state = .off
  121. }
  122. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  123. self.openLastUnlockedDocumentCheck.state = .on
  124. } else {
  125. self.openLastUnlockedDocumentCheck.state = .off
  126. }
  127. self.maxDisplayNumberStepperView.value = Double(KMPreferenceManager.shared.documentMaximunDisplayNumber)
  128. if (KMPreferenceManager.shared.autoSave) {
  129. self.saveFilesCheck.state = .on
  130. } else {
  131. self.saveFilesCheck.state = .off
  132. }
  133. self.saveFileStepper.value = Double(KMPreferenceManager.shared.autoSavePerNumberMinute)
  134. if (KMPreferenceManager.shared.closeFilePromptType == .promp) {
  135. self.selectCloseFileRadio(self.closeFilePopupPromptRadio)
  136. } else {
  137. self.selectCloseFileRadio(self.closeFileNoPromptRadio)
  138. }
  139. if (KMPreferenceManager.shared.openImageFileType == 0) {
  140. self.selectOpenImageFileRadio(self.openImageFileAutoRadio)
  141. } else {
  142. self.selectOpenImageFileRadio(self.openImageFilePromptRadio)
  143. }
  144. if (KMPreferenceManager.shared.setDefaultPDFReader) {
  145. self.setDefaultPDFReaderCheck.state = .on
  146. } else {
  147. self.setDefaultPDFReaderCheck.state = .off
  148. }
  149. let value = KMPreferenceManager.shared.savePasswordType
  150. if (value == .always) {
  151. self.selectSavePasswordRadio(self.savePasswordAlwaysRadio)
  152. } else if (value == .never) {
  153. self.selectSavePasswordRadio(self.savePasswordNeverRadio)
  154. } else if (value == .ask) {
  155. self.selectSavePasswordRadio(self.savePasswordAskRadio)
  156. }
  157. self.authorTextField.stringValue = KMPreferenceManager.shared.author
  158. }
  159. override func viewWillAppear() {
  160. super.viewWillAppear()
  161. Task { @MainActor in
  162. if await KMLightMemberManager.manager.canPayFunction() == false {
  163. self.saveFilesCheck.isEnabled = false
  164. self.saveFileStepper.enabled = false
  165. self.saveFilesDespLabl.isEnabled = false
  166. self.updateSaveFilesDespLabl(enabled: false)
  167. } else {
  168. self.saveFilesCheck.isEnabled = true
  169. self.saveFileStepper.enabled = true
  170. self.saveFilesDespLabl.isEnabled = true
  171. self.updateSaveFilesDespLabl(enabled: true)
  172. }
  173. }
  174. }
  175. // MARK: User Actions
  176. @objc private func radioAction(sender: NSButton) {
  177. if (sender.isEqual(to: self.closeFilePopupPromptRadio)) {
  178. self.selectCloseFileRadio(sender)
  179. KMPreferenceManager.shared.closeFilePromptType = .promp
  180. } else if (sender.isEqual(to: self.closeFileNoPromptRadio)) {
  181. self.selectCloseFileRadio(sender)
  182. KMPreferenceManager.shared.closeFilePromptType = .noPromp
  183. } else if (sender.isEqual(to: self.openImageFileAutoRadio)) {
  184. self.selectOpenImageFileRadio(sender)
  185. KMPreferenceManager.shared.openImageFileType = 0
  186. } else if (sender.isEqual(to: self.openImageFilePromptRadio)) {
  187. self.selectOpenImageFileRadio(sender)
  188. KMPreferenceManager.shared.openImageFileType = 1
  189. } else if (sender.isEqual(to: self.savePasswordAlwaysRadio)) {
  190. self.selectSavePasswordRadio(sender)
  191. KMPreferenceManager.shared.savePasswordType = .always
  192. } else if (sender.isEqual(to: self.savePasswordNeverRadio)) {
  193. self.selectSavePasswordRadio(sender)
  194. KMPreferenceManager.shared.savePasswordType = .never
  195. } else if (sender.isEqual(to: self.savePasswordAskRadio)) {
  196. self.selectSavePasswordRadio(sender)
  197. KMPreferenceManager.shared.savePasswordType = .ask
  198. }
  199. }
  200. @objc private func checkAction(sender: NSButton) {
  201. if (sender.isEqual(to: self.openLastUnCloseedDocumentCheck)) {
  202. KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on
  203. } else if (sender.isEqual(to: self.openLastUnlockedDocumentCheck)) {
  204. KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart = sender.state == .on
  205. } else if (sender.isEqual(to: self.saveFilesCheck)) {
  206. KMPreferenceManager.shared.autoSave = sender.state == .on
  207. } else if (sender.isEqual(to: self.setDefaultPDFReaderCheck)) {
  208. KMPreferenceManager.shared.setDefaultPDFReader = sender.state == .on
  209. }
  210. }
  211. private func selectCloseFileRadio(_ sender: NSButton) {
  212. for radio in [self.closeFilePopupPromptRadio, self.closeFileNoPromptRadio] {
  213. if (sender.isEqual(to: radio)) {
  214. radio?.state = .on
  215. } else {
  216. radio?.state = .off
  217. }
  218. }
  219. }
  220. private func selectOpenImageFileRadio(_ sender: NSButton) {
  221. for radio in [self.openImageFileAutoRadio, self.openImageFilePromptRadio] {
  222. if (sender.isEqual(to: radio)) {
  223. radio?.state = .on
  224. } else {
  225. radio?.state = .off
  226. }
  227. }
  228. }
  229. private func selectSavePasswordRadio(_ sender: NSButton) {
  230. for radio in [self.savePasswordAlwaysRadio, self.savePasswordNeverRadio, self.savePasswordAskRadio] {
  231. if (sender.isEqual(to: radio)) {
  232. radio?.state = .on
  233. } else {
  234. radio?.state = .off
  235. }
  236. }
  237. }
  238. private func updateSaveFilesDespLabl(enabled: Bool) {
  239. let _block = {
  240. if (enabled) {
  241. let string = "\(NSLocalizedString("minutes", comment: "")) "
  242. let attriText = NSMutableAttributedString(attributedString: NSAttributedString(string: string,attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.85)]))
  243. attriText.append(NSAttributedString(string: NSLocalizedString("(5 to 99)", comment: ""),attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.5)]))
  244. self.saveFilesDespLabl.attributedStringValue = attriText
  245. } else {
  246. let string = "\(NSLocalizedString("minutes", comment: "")) "
  247. let attriText = NSMutableAttributedString(attributedString: NSAttributedString(string: string,attributes: [.foregroundColor : NSColor.lightGray]))
  248. attriText.append(NSAttributedString(string: NSLocalizedString("(5 to 99)", comment: ""),attributes: [.foregroundColor : NSColor.lightGray]))
  249. self.saveFilesDespLabl.attributedStringValue = attriText
  250. }
  251. }
  252. if (Thread.isMainThread) {
  253. _block()
  254. } else {
  255. DispatchQueue.main.async {
  256. _block()
  257. }
  258. }
  259. }
  260. }
  261. extension KMPreferenceGeneralController: KMPreferenceDealDataChangeProtocol {
  262. func refreshUI() {
  263. DispatchQueue.main.async {
  264. self.initData()
  265. }
  266. }
  267. }
  268. extension KMPreferenceGeneralController: NSTextFieldDelegate {
  269. func controlTextDidChange(_ obj: Notification) {
  270. if (self.authorTextField.isEqual(to: obj.object)) {
  271. let author = self.authorTextField.stringValue
  272. KMPreferenceManager.shared.author = author
  273. }
  274. }
  275. }