KMConvertOCRSettingItemView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // KMConvertOCRSettingItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/6.
  6. //
  7. class KMConvertOCRSettingItemView: KMConvertBaseView {
  8. @IBOutlet weak var titleLabel: NSTextField!
  9. @IBOutlet weak var checkBox: NSBox!
  10. @IBOutlet weak var languageBox: NSBox!
  11. private var _checkVC: KMDesignButton?
  12. var checkVC: KMDesignButton? {
  13. get {
  14. return self._checkVC
  15. }
  16. }
  17. private var _languageVC: KMDesignSelect?
  18. var progressController: SKProgressController?
  19. private var _languageIndex: Int = 0
  20. var languageIndex: Int {
  21. get {
  22. return self._languageIndex
  23. }
  24. set {
  25. self._languageIndex = newValue
  26. self._languageVC?.selectItem(at: newValue)
  27. }
  28. }
  29. var lanugageDidSelected: KMValueDidChangeBlock?
  30. var reachabilityAlert: NSAlert?
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. self._checkAction()
  34. }
  35. override func initSubViews() {
  36. super.initSubViews()
  37. self._checkVC = KMDesignButton(withType: .CheckBox)
  38. self.checkBox.contentView = self._checkVC!.view
  39. self._languageVC = KMDesignSelect(withType: .Combox)
  40. self.languageBox.contentView = self._languageVC?.view
  41. }
  42. override func initDefaultVlaue() {
  43. super.initDefaultVlaue()
  44. self.titleLabel.stringValue = NSLocalizedString("Text Recognition Settings", comment: "")
  45. self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  46. self.titleLabel.font = .SFProTextRegularFont(12)
  47. self.checkBox.fillColor = .clear
  48. self._checkVC?.target = self
  49. self._checkVC?.action = #selector(checkBoxAction)
  50. self._checkVC?.stringValue = NSLocalizedString("Recognize text if needed", comment: "")
  51. self._checkVC?.checkbox_radio()
  52. self._checkVC?.state = .Custom
  53. self.languageBox.fillColor = .clear
  54. self._languageVC?.delete = self
  55. self._languageVC?.isScrollPop = true
  56. self._languageVC?.removeAllItems()
  57. // self._languageVC?.addItems(withObjectValues: KMPDFConvertManager.defaultManager.languages_asc_strings)
  58. if (self.languageIndex < self._languageVC!.numberOfItems) {
  59. self._languageVC?.selectItem(at: self.languageIndex)
  60. } else {
  61. self._languageVC?.selectItem(at: 2)
  62. }
  63. }
  64. @objc func checkBoxAction(sender: NSButton) {
  65. #if VERSION_DMG
  66. let isExist = KMResourceDownloadManager.manager.documentAIBundleExists()
  67. if isExist || self._checkVC?.state == .Checked {
  68. if let state = self._checkVC?.state {
  69. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  70. }
  71. self._checkAction()
  72. } else {
  73. let alert = NSAlert()
  74. alert.messageText = NSLocalizedString("Download OCR", comment: "")
  75. alert.informativeText = NSLocalizedString("Using OCR requires downloading additional resource pack. Do you want to continue?", comment: "")
  76. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  77. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  78. alert.beginSheetModal(for: self.window!) { [unowned self] result in
  79. if (result == .alertFirstButtonReturn) { /// 取消
  80. self.downLoad()
  81. } else if result == .alertSecondButtonReturn {
  82. return
  83. }
  84. }
  85. }
  86. #else
  87. if let state = self._checkVC?.state {
  88. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  89. }
  90. self._checkAction()
  91. #endif
  92. }
  93. #if VERSION_DMG
  94. func downLoad() {
  95. DispatchQueue.main.async {
  96. self.showProgressWindow()
  97. self.progressController?.maxValue = Double(100)
  98. }
  99. KMResourceDownloadManager.manager.downloadFramework(progress: { progress in
  100. self.progressController?.doubleValue = Double(progress*100)
  101. }, result: { result, downloadState in
  102. if result {
  103. if let state = self._checkVC?.state {
  104. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  105. }
  106. self._checkAction()
  107. if let state = self._checkVC?.state {
  108. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  109. }
  110. self._checkAction()
  111. self.hiddenProgressWindow()
  112. } else if downloadState == .retry {
  113. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [unowned self] in
  114. self.downLoad()
  115. })
  116. } else {
  117. self.hiddenProgressWindow()
  118. }
  119. })
  120. }
  121. #endif
  122. public func checkAction() {
  123. self._checkAction()
  124. }
  125. private func _checkAction() {
  126. guard let state = self._checkVC?.state else {
  127. return
  128. }
  129. self._languageVC?.enabled = state == .Checked
  130. }
  131. func showProgressWindow() {
  132. let progress = SKProgressController()
  133. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  134. progress.window?.contentView?.wantsLayer = true
  135. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  136. progress.progressField.textColor = NSColor.white
  137. progress.message = NSLocalizedString("Download...", comment: "")
  138. progress.closeBlock = { [unowned self] in
  139. #if VERSION_DMG
  140. KMResourceDownloadManager.manager.cancelDownload()
  141. #endif
  142. }
  143. self.progressController = progress
  144. self.window?.beginSheet(progress.window!)
  145. }
  146. func hiddenProgressWindow() {
  147. DispatchQueue.main.async {
  148. self.progressController?.doubleValue = 100.0
  149. if (self.progressController != nil) {
  150. self.window?.endSheet((self.progressController?.window)!)
  151. self.progressController = nil
  152. }
  153. }
  154. }
  155. }
  156. extension KMConvertOCRSettingItemView: KMSelectPopButtonDelegate {
  157. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  158. if (obj.isEqual(to: self._languageVC)) {
  159. guard let index = self._languageVC?.indexOfSelectedItem, index >= 0 else {
  160. return
  161. }
  162. self._languageIndex = index
  163. guard let callback = self.lanugageDidSelected else {
  164. return
  165. }
  166. callback(index, nil)
  167. }
  168. }
  169. }