// // KMPreferenceGeneralController.swift // PDF Master // // Created by tangchao on 2023/2/3. // import Cocoa /// 偏好设置 通用控制器 class KMPreferenceGeneralController: NSViewController { @IBOutlet weak var filesTitleLabel: NSTextField! @IBOutlet weak var filesBox: NSBox! @IBOutlet weak var openLastUnCloseedDocumentCheck: NSButton! @IBOutlet weak var openLastUnlockedDocumentCheck: NSButton! @IBOutlet weak var maxDisplayNumberLabel: NSTextField! @IBOutlet weak var maxDisplayNumberStepper: NSView! private var maxDisplayNumberStepperView = KMPreferenceStepper() @IBOutlet weak var maxDisplayNumberDespLabel: NSTextField! @IBOutlet weak var saveFilesCheck: NSButton! @IBOutlet weak var saveFileStepper: KMPreferenceStepper! @IBOutlet weak var saveFilesDespLabl: NSTextField! @IBOutlet weak var closeFileLabel: NSTextField! @IBOutlet weak var closeFilePopupPromptRadio: NSButton! @IBOutlet weak var closeFileNoPromptRadio: NSButton! @IBOutlet weak var openImageFileLabel: NSTextField! @IBOutlet weak var openImageFileBox: NSBox! @IBOutlet weak var openImageFileAutoRadio: NSButton! @IBOutlet weak var openImageFilePromptRadio: NSButton! @IBOutlet weak var setDefaultPDFReaderCheck: NSButton! @IBOutlet weak var savePasswordLabel: NSTextField! @IBOutlet weak var savePasswordAlwaysRadio: NSButton! @IBOutlet weak var savePasswordNeverRadio: NSButton! @IBOutlet weak var savePasswordAskRadio: NSButton! @IBOutlet weak var authorLabel: NSTextField! @IBOutlet weak var authorTextField: NSTextField! convenience init() { self.init(nibName: "KMPreferenceGeneralController", bundle: nil) } override func viewDidLoad() { super.viewDidLoad() self.filesTitleLabel.stringValue = NSLocalizedString("Files", comment: "") self.openLastUnCloseedDocumentCheck.title = NSLocalizedString("Open the last unclosed document when the APP starts", comment: "") self.openLastUnlockedDocumentCheck.title = NSLocalizedString("Restore the last view setting when opening a file", comment: "") self.maxDisplayNumberLabel.stringValue = NSLocalizedString("Maximum number of documents displayed in recently opened files", comment: "") self.maxDisplayNumberDespLabel.stringValue = NSLocalizedString("(10 to 50)", comment: "") self.saveFilesCheck.title = NSLocalizedString("Save files automatically, every", comment: "") self.closeFileLabel.stringValue = NSLocalizedString("Close File:", comment: "") self.closeFilePopupPromptRadio.title = NSLocalizedString("Prompt to \"save/don't save\"", comment: "") self.closeFileNoPromptRadio.title = NSLocalizedString("No prompt, save directly", comment: "") self.openImageFileLabel.stringValue = NSLocalizedString("Open Image File", comment: "") self.openImageFileAutoRadio.title = NSLocalizedString("Automatically scan and recognize text", comment: "") self.openImageFilePromptRadio.title = NSLocalizedString("Prompt to scan and recognize text", comment: "") self.setDefaultPDFReaderCheck.title = NSLocalizedString("Set as default PDF reader", comment: "") self.savePasswordLabel.stringValue = NSLocalizedString("Save the password in Keychain:", comment: "") self.savePasswordAlwaysRadio.title = NSLocalizedString("Always", comment: "") self.savePasswordNeverRadio.title = NSLocalizedString("Never", comment: "") self.savePasswordAskRadio.title = NSLocalizedString("Ask", comment: "") self.authorLabel.stringValue = NSLocalizedString("Author:", comment: "") self.initUIProperty() /// 设置数据 self.initData() for radio in [self.closeFilePopupPromptRadio, self.closeFileNoPromptRadio, self.openImageFileAutoRadio, self.openImageFilePromptRadio, self.savePasswordAlwaysRadio, self.savePasswordNeverRadio, self.savePasswordAskRadio] { radio?.target = self radio?.action = #selector(radioAction) } self.openLastUnlockedDocumentCheck.target = self self.openLastUnlockedDocumentCheck.action = #selector(checkAction) self.openLastUnCloseedDocumentCheck.target = self self.openLastUnCloseedDocumentCheck.action = #selector(checkAction) self.saveFilesCheck.target = self self.saveFilesCheck.action = #selector(checkAction) self.setDefaultPDFReaderCheck.target = self self.setDefaultPDFReaderCheck.action = #selector(checkAction) let maxDisplayNumberStepperView = self.maxDisplayNumberStepperView self.maxDisplayNumberStepper.addSubview(maxDisplayNumberStepperView) maxDisplayNumberStepperView.frame = self.maxDisplayNumberStepper.bounds maxDisplayNumberStepperView.autoresizingMask = [.width, .height] maxDisplayNumberStepperView.stepper.minValue = 10 maxDisplayNumberStepperView.stepper.maxValue = 50 maxDisplayNumberStepperView.valueDidChange = { value in KMPreferenceManager.shared.documentMaximunDisplayNumber = Int(value) } self.saveFileStepper.stepper.minValue = 5 self.saveFileStepper.stepper.maxValue = 99 self.saveFileStepper.valueDidChange = { value in KMPreferenceManager.shared.autoSavePerNumberMinute = Int(value ) } self.authorTextField.delegate = self } private func initUIProperty() { self.filesBox.borderColor = NSColor(white: 0, alpha: 0.05) self.filesBox.fillColor = NSColor(white: 0, alpha: 0.05) self.openImageFileBox.borderColor = NSColor(white: 0, alpha: 0.05) self.openImageFileBox.fillColor = NSColor(white: 0, alpha: 0.05) self.filesTitleLabel.textColor = NSColor.km_init(hex: "#252629") self.openLastUnCloseedDocumentCheck.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.openLastUnlockedDocumentCheck.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.maxDisplayNumberLabel.textColor = NSColor.km_init(hex: "#000000D9") self.saveFilesCheck.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.closeFileLabel.textColor = NSColor.km_init(hex: "#000000D9") self.closeFilePopupPromptRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.closeFileNoPromptRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.openImageFileLabel.textColor = NSColor.km_init(hex: "#252629") self.openImageFileAutoRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.openImageFilePromptRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.setDefaultPDFReaderCheck.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.savePasswordLabel.textColor = NSColor.km_init(hex: "#000000D9") self.savePasswordAlwaysRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.savePasswordNeverRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.savePasswordAskRadio.setTitleColor(color: NSColor.km_init(hex: "#000000D9")) self.authorLabel.textColor = NSColor.km_init(hex: "#000000D9") self.maxDisplayNumberDespLabel.textColor = NSColor(white: 0, alpha: 0.5) self.updateSaveFilesDespLabl(enabled: true) } private func initData() { if (KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart) { self.openLastUnCloseedDocumentCheck.state = .on } else { self.openLastUnCloseedDocumentCheck.state = .off } if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) { self.openLastUnlockedDocumentCheck.state = .on } else { self.openLastUnlockedDocumentCheck.state = .off } self.maxDisplayNumberStepperView.value = Double(KMPreferenceManager.shared.documentMaximunDisplayNumber) if (KMPreferenceManager.shared.autoSave) { self.saveFilesCheck.state = .on } else { self.saveFilesCheck.state = .off } self.saveFileStepper.value = Double(KMPreferenceManager.shared.autoSavePerNumberMinute) if (KMPreferenceManager.shared.closeFilePromptType == .promp) { self.selectCloseFileRadio(self.closeFilePopupPromptRadio) } else { self.selectCloseFileRadio(self.closeFileNoPromptRadio) } if (KMPreferenceManager.shared.openImageFileType == 0) { self.selectOpenImageFileRadio(self.openImageFileAutoRadio) } else { self.selectOpenImageFileRadio(self.openImageFilePromptRadio) } if (KMPreferenceManager.shared.setDefaultPDFReader) { self.setDefaultPDFReaderCheck.state = .on } else { self.setDefaultPDFReaderCheck.state = .off } let value = KMPreferenceManager.shared.savePasswordType if (value == .always) { self.selectSavePasswordRadio(self.savePasswordAlwaysRadio) } else if (value == .never) { self.selectSavePasswordRadio(self.savePasswordNeverRadio) } else if (value == .ask) { self.selectSavePasswordRadio(self.savePasswordAskRadio) } self.authorTextField.stringValue = KMPreferenceManager.shared.author } override func viewWillAppear() { super.viewWillAppear() Task { @MainActor in if await KMLightMemberManager.manager.canPayFunction() == false { self.saveFilesCheck.isEnabled = false self.saveFileStepper.enabled = false self.saveFilesDespLabl.isEnabled = false self.updateSaveFilesDespLabl(enabled: false) } else { self.saveFilesCheck.isEnabled = true self.saveFileStepper.enabled = true self.saveFilesDespLabl.isEnabled = true self.updateSaveFilesDespLabl(enabled: true) } } } // MARK: User Actions @objc private func radioAction(sender: NSButton) { if (sender.isEqual(to: self.closeFilePopupPromptRadio)) { self.selectCloseFileRadio(sender) KMPreferenceManager.shared.closeFilePromptType = .promp } else if (sender.isEqual(to: self.closeFileNoPromptRadio)) { self.selectCloseFileRadio(sender) KMPreferenceManager.shared.closeFilePromptType = .noPromp } else if (sender.isEqual(to: self.openImageFileAutoRadio)) { self.selectOpenImageFileRadio(sender) KMPreferenceManager.shared.openImageFileType = 0 } else if (sender.isEqual(to: self.openImageFilePromptRadio)) { self.selectOpenImageFileRadio(sender) KMPreferenceManager.shared.openImageFileType = 1 } else if (sender.isEqual(to: self.savePasswordAlwaysRadio)) { self.selectSavePasswordRadio(sender) KMPreferenceManager.shared.savePasswordType = .always } else if (sender.isEqual(to: self.savePasswordNeverRadio)) { self.selectSavePasswordRadio(sender) KMPreferenceManager.shared.savePasswordType = .never } else if (sender.isEqual(to: self.savePasswordAskRadio)) { self.selectSavePasswordRadio(sender) KMPreferenceManager.shared.savePasswordType = .ask } } @objc private func checkAction(sender: NSButton) { if (sender.isEqual(to: self.openLastUnCloseedDocumentCheck)) { KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart = sender.state == .on } else if (sender.isEqual(to: self.openLastUnlockedDocumentCheck)) { KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart = sender.state == .on } else if (sender.isEqual(to: self.saveFilesCheck)) { KMPreferenceManager.shared.autoSave = sender.state == .on } else if (sender.isEqual(to: self.setDefaultPDFReaderCheck)) { KMPreferenceManager.shared.setDefaultPDFReader = sender.state == .on } } private func selectCloseFileRadio(_ sender: NSButton) { for radio in [self.closeFilePopupPromptRadio, self.closeFileNoPromptRadio] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } private func selectOpenImageFileRadio(_ sender: NSButton) { for radio in [self.openImageFileAutoRadio, self.openImageFilePromptRadio] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } private func selectSavePasswordRadio(_ sender: NSButton) { for radio in [self.savePasswordAlwaysRadio, self.savePasswordNeverRadio, self.savePasswordAskRadio] { if (sender.isEqual(to: radio)) { radio?.state = .on } else { radio?.state = .off } } } private func updateSaveFilesDespLabl(enabled: Bool) { let _block = { if (enabled) { let string = "\(NSLocalizedString("minutes", comment: "")) " let attriText = NSMutableAttributedString(attributedString: NSAttributedString(string: string,attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.85)])) attriText.append(NSAttributedString(string: NSLocalizedString("(5 to 99)", comment: ""),attributes: [.foregroundColor : NSColor(white: 0, alpha: 0.5)])) self.saveFilesDespLabl.attributedStringValue = attriText } else { let string = "\(NSLocalizedString("minutes", comment: "")) " let attriText = NSMutableAttributedString(attributedString: NSAttributedString(string: string,attributes: [.foregroundColor : NSColor.lightGray])) attriText.append(NSAttributedString(string: NSLocalizedString("(5 to 99)", comment: ""),attributes: [.foregroundColor : NSColor.lightGray])) self.saveFilesDespLabl.attributedStringValue = attriText } } if (Thread.isMainThread) { _block() } else { DispatchQueue.main.async { _block() } } } } extension KMPreferenceGeneralController: KMPreferenceDealDataChangeProtocol { func refreshUI() { DispatchQueue.main.async { self.initData() } } } extension KMPreferenceGeneralController: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { if (self.authorTextField.isEqual(to: obj.object)) { let author = self.authorTextField.stringValue KMPreferenceManager.shared.author = author } } }