// // KMConvertOCRSettingItemView.swift // PDF Reader Pro // // Created by tangchao on 2022/12/6. // class KMConvertOCRSettingItemView: KMConvertBaseView { @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var checkBox: NSBox! @IBOutlet weak var languageBox: NSBox! private var _checkVC: KMDesignButton? var checkVC: KMDesignButton? { get { return self._checkVC } } private var _languageVC: KMDesignSelect? var progressController: SKProgressController? private var _languageIndex: Int = 0 var languageIndex: Int { get { return self._languageIndex } set { self._languageIndex = newValue self._languageVC?.selectItem(at: newValue) } } var lanugageDidSelected: KMValueDidChangeBlock? var reachabilityAlert: NSAlert? override func awakeFromNib() { super.awakeFromNib() self._checkAction() } override func initSubViews() { super.initSubViews() self._checkVC = KMDesignButton(withType: .CheckBox) self.checkBox.contentView = self._checkVC!.view self._languageVC = KMDesignSelect(withType: .Combox) self.languageBox.contentView = self._languageVC?.view } override func initDefaultVlaue() { super.initDefaultVlaue() self.titleLabel.stringValue = NSLocalizedString("OCR Settings", comment: "") self.titleLabel.textColor = NSColor.km_init(hex: "#616469") self.titleLabel.font = .SFProTextRegularFont(12) self.checkBox.fillColor = .clear self._checkVC?.target = self self._checkVC?.action = #selector(checkBoxAction) self._checkVC?.stringValue = NSLocalizedString("Recognize text if needed", comment: "") self._checkVC?.checkbox_radio() self._checkVC?.state = .Custom self.languageBox.fillColor = .clear self._languageVC?.delete = self self._languageVC?.isScrollPop = true self._languageVC?.removeAllItems() // self._languageVC?.addItems(withObjectValues: KMPDFConvertManager.defaultManager.languages_asc_strings) if (self.languageIndex < self._languageVC!.numberOfItems) { self._languageVC?.selectItem(at: self.languageIndex) } else { self._languageVC?.selectItem(at: 2) } } @objc func checkBoxAction(sender: NSButton) { #if VERSION_DMG let isExist = KMResourceDownloadManager.manager.documentAIBundleExists() if isExist || self._checkVC?.state == .Checked { if let state = self._checkVC?.state { self._checkVC?.state = state == .Checked ? .Norm : .Checked } self._checkAction() } else { let alert = NSAlert() alert.messageText = NSLocalizedString("Download OCR", comment: "") alert.informativeText = NSLocalizedString("Using OCR requires downloading additional resource pack. Do you want to continue?", comment: "") alert.addButton(withTitle: NSLocalizedString("Yes", comment: "")) alert.addButton(withTitle: NSLocalizedString("No", comment: "")) alert.beginSheetModal(for: self.window!) { [weak self] result in if (result == .alertFirstButtonReturn) { /// 取消 self?.downLoad() } else if result == .alertSecondButtonReturn { return } } } #else if let state = self._checkVC?.state { self._checkVC?.state = state == .Checked ? .Norm : .Checked } self._checkAction() #endif } #if VERSION_DMG func downLoad() { DispatchQueue.main.async { self.showProgressWindow() self.progressController?.maxValue = Double(100) } KMResourceDownloadManager.manager.downloadFramework(progress: { progress in self.progressController?.doubleValue = Double(progress*100) }, result: { result, downloadState in if result { if let state = self._checkVC?.state { self._checkVC?.state = state == .Checked ? .Norm : .Checked } self._checkAction() if let state = self._checkVC?.state { self._checkVC?.state = state == .Checked ? .Norm : .Checked } self._checkAction() self.hiddenProgressWindow() } else if downloadState == .retry { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in self?.downLoad() }) } else { self.hiddenProgressWindow() } }) } #endif public func checkAction() { self._checkAction() } private func _checkAction() { guard let state = self._checkVC?.state else { return } self._languageVC?.enabled = state == .Checked } func showProgressWindow() { let progress = SKProgressController() progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B") progress.window?.contentView?.wantsLayer = true progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor progress.progressField.textColor = NSColor.white progress.message = NSLocalizedString("Download...", comment: "") progress.closeBlock = { [weak self] in #if VERSION_DMG KMResourceDownloadManager.manager.cancelDownload() #endif } self.progressController = progress self.window?.beginSheet(progress.window!) } func hiddenProgressWindow() { DispatchQueue.main.async { self.progressController?.doubleValue = 100.0 if (self.progressController != nil) { self.window?.endSheet((self.progressController?.window)!) self.progressController = nil } } } } extension KMConvertOCRSettingItemView: KMSelectPopButtonDelegate { func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) { if (obj.isEqual(to: self._languageVC)) { guard let index = self._languageVC?.indexOfSelectedItem, index >= 0 else { return } self._languageIndex = index guard let callback = self.lanugageDidSelected else { return } callback(index, nil) } } }