123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // KMConvertOCRSettingItemView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/6.
- //
- import KMComponentLibrary
- 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 lazy var checkView_: ComponentCheckBox = {
- let view = ComponentCheckBox()
- view.properties = ComponentCheckBoxProperty(size: .s,
- state: .normal,
- isDisabled: false,
- showhelp: false,
- text: NSLocalizedString("Recognize text if needed", comment: ""),
- checkboxType: .normal)
- return view
- }()
- var checkView: ComponentCheckBox {
- get {
- return checkView_
- }
- }
-
- private var _languageVC: KMDesignSelect?
- var progressController: SKProgressController?
-
- private lazy var languageView_: ComponentSelect = {
- let view = ComponentSelect()
-
- view.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- isDisabled: false,
- isError: false,
- leftIcon: false,
- placeholder: nil,
- errorText: nil,
- creatable: false,
- text: KMLocalizedString("Odd Pages Only", comment: ""))
- view.delegate = self
- return view
- }()
-
- private lazy var languages_: [String] = {
- return ["Chinese", "Chinese Traditional", "English", "Japanese",
- "Kroean",
- "Serbian",
- "Occitan",
- "Danish",
- "German",
- "French",
- "Italian",
- "Spanish",
- "Portuguese",
- "Maori",
- "Malay",
- "Maltese",
- "Dutch",
- "Norwegian",
- "Polish",
- "Romanian",
- "Slovak",
- "Slovenian",
- "Albanian",
- "Swedish",
- "Swahili",
- "Tagalog",
- "Turish",
- "Uzbek",
- "Vietnamese",
- "Afrikaans",
- "Azerbaijani",
- "Bosnian",
- "Czech",
- "Welsh",
- "Estonian",
- "Irish",
- "Croatian",
- "Hungarian",
- "Indonesian",
- "Icelandic",
- "Kurdish",
- "Lithuanian",
- "Latvian"]
- }()
- private var _languageIndex: Int = 0
- var languageIndex: Int {
- get {
- return self._languageIndex
- }
- set {
- self._languageIndex = newValue
-
- self.languageView_.properties.text = self.languages_.safe_element(for: newValue) as? String ?? ""
- self.languageView_.selectItemAtIndex(newValue)
- }
- }
-
- var lanugageDidSelected: KMValueDidChangeBlock?
- var reachabilityAlert: NSAlert?
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- self._checkAction()
- }
-
- override func initSubViews() {
- super.initSubViews()
-
- self.checkBox.contentView = self.checkView_
-
- self._languageVC = KMDesignSelect(withType: .Combox)
- self.languageBox.contentView = self._languageVC?.view
- }
-
- override func initDefaultVlaue() {
- super.initDefaultVlaue()
-
- self.titleLabel.stringValue = NSLocalizedString("Text Recognition Settings", comment: "")
- self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
- self.titleLabel.font = .SFProTextRegularFont(12)
-
- self.checkBox.fillColor = .clear
- self.checkView_.setTarget(self, action: #selector(checkBoxAction))
-
- self.languageBox.fillColor = .clear
- self.languageBox.contentView = self.languageView_
-
- var menuItemArr: [ComponentMenuitemProperty] = []
- for language in languages_ {
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: language)
- menuItemArr.append(itemProperty)
- }
-
- languageView_.updateMenuItemsArr(menuItemArr)
-
- languageView_.selectItemAtIndex(0)
- languageView_.properties.creatable = false
- languageView_.properties.text = ""
- languageView_.reloadData()
-
- }
-
- @objc func checkBoxAction(sender: NSButton) {
- self._checkAction()
- }
-
- #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() {
- let state = self.checkView_.properties.checkboxType
- languageView_.properties.isDisabled = state != .selected
- languageView_.reloadData()
- }
-
- 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)
- }
- }
- }
- extension KMConvertOCRSettingItemView: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- guard let index = view?.indexOfSelect(), index >= 0 else {
- return
- }
- self._languageIndex = index
-
- guard let callback = self.lanugageDidSelected else {
- return
- }
- callback(index, nil)
- }
- }
|