KMConvertOCRSettingItemView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // KMConvertOCRSettingItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/6.
  6. //
  7. import KMComponentLibrary
  8. class KMConvertOCRSettingItemView: KMConvertBaseView {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var checkBox: NSBox!
  11. @IBOutlet weak var languageBox: NSBox!
  12. private var _checkVC: KMDesignButton?
  13. var checkVC: KMDesignButton? {
  14. get {
  15. return self._checkVC
  16. }
  17. }
  18. private lazy var checkView_: ComponentCheckBox = {
  19. let view = ComponentCheckBox()
  20. view.properties = ComponentCheckBoxProperty(size: .s,
  21. state: .normal,
  22. isDisabled: false,
  23. showhelp: false,
  24. text: NSLocalizedString("Recognize text if needed", comment: ""),
  25. checkboxType: .normal)
  26. return view
  27. }()
  28. var checkView: ComponentCheckBox {
  29. get {
  30. return checkView_
  31. }
  32. }
  33. private var _languageVC: KMDesignSelect?
  34. var progressController: SKProgressController?
  35. private lazy var languageView_: ComponentSelect = {
  36. let view = ComponentSelect()
  37. view.properties = ComponentSelectProperties(size: .s,
  38. state: .normal,
  39. isDisabled: false,
  40. isError: false,
  41. leftIcon: false,
  42. placeholder: nil,
  43. errorText: nil,
  44. creatable: false,
  45. text: KMLocalizedString("Odd Pages Only", comment: ""))
  46. view.delegate = self
  47. return view
  48. }()
  49. private lazy var languages_: [String] = {
  50. return ["Chinese", "Chinese Traditional", "English", "Japanese",
  51. "Kroean",
  52. "Serbian",
  53. "Occitan",
  54. "Danish",
  55. "German",
  56. "French",
  57. "Italian",
  58. "Spanish",
  59. "Portuguese",
  60. "Maori",
  61. "Malay",
  62. "Maltese",
  63. "Dutch",
  64. "Norwegian",
  65. "Polish",
  66. "Romanian",
  67. "Slovak",
  68. "Slovenian",
  69. "Albanian",
  70. "Swedish",
  71. "Swahili",
  72. "Tagalog",
  73. "Turish",
  74. "Uzbek",
  75. "Vietnamese",
  76. "Afrikaans",
  77. "Azerbaijani",
  78. "Bosnian",
  79. "Czech",
  80. "Welsh",
  81. "Estonian",
  82. "Irish",
  83. "Croatian",
  84. "Hungarian",
  85. "Indonesian",
  86. "Icelandic",
  87. "Kurdish",
  88. "Lithuanian",
  89. "Latvian"]
  90. }()
  91. private var _languageIndex: Int = 0
  92. var languageIndex: Int {
  93. get {
  94. return self._languageIndex
  95. }
  96. set {
  97. self._languageIndex = newValue
  98. self.languageView_.properties.text = self.languages_.safe_element(for: newValue) as? String ?? ""
  99. self.languageView_.selectItemAtIndex(newValue)
  100. }
  101. }
  102. var lanugageDidSelected: KMValueDidChangeBlock?
  103. var reachabilityAlert: NSAlert?
  104. override func awakeFromNib() {
  105. super.awakeFromNib()
  106. self._checkAction()
  107. }
  108. override func initSubViews() {
  109. super.initSubViews()
  110. self.checkBox.contentView = self.checkView_
  111. self._languageVC = KMDesignSelect(withType: .Combox)
  112. self.languageBox.contentView = self._languageVC?.view
  113. }
  114. override func initDefaultVlaue() {
  115. super.initDefaultVlaue()
  116. self.titleLabel.stringValue = NSLocalizedString("Text Recognition Settings", comment: "")
  117. self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  118. self.titleLabel.font = .SFProTextRegularFont(12)
  119. self.checkBox.fillColor = .clear
  120. self.checkView_.setTarget(self, action: #selector(checkBoxAction))
  121. self.languageBox.fillColor = .clear
  122. self.languageBox.contentView = self.languageView_
  123. var menuItemArr: [ComponentMenuitemProperty] = []
  124. for language in languages_ {
  125. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  126. itemSelected: false,
  127. isDisabled: false,
  128. keyEquivalent: nil,
  129. text: language)
  130. menuItemArr.append(itemProperty)
  131. }
  132. languageView_.updateMenuItemsArr(menuItemArr)
  133. languageView_.selectItemAtIndex(0)
  134. languageView_.properties.creatable = false
  135. languageView_.properties.text = ""
  136. languageView_.reloadData()
  137. }
  138. @objc func checkBoxAction(sender: NSButton) {
  139. self._checkAction()
  140. }
  141. #if VERSION_DMG
  142. func downLoad() {
  143. DispatchQueue.main.async {
  144. self.showProgressWindow()
  145. self.progressController?.maxValue = Double(100)
  146. }
  147. KMResourceDownloadManager.manager.downloadFramework(progress: { progress in
  148. self.progressController?.doubleValue = Double(progress*100)
  149. }, result: { result, downloadState in
  150. if result {
  151. if let state = self._checkVC?.state {
  152. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  153. }
  154. self._checkAction()
  155. if let state = self._checkVC?.state {
  156. self._checkVC?.state = state == .Checked ? .Norm : .Checked
  157. }
  158. self._checkAction()
  159. self.hiddenProgressWindow()
  160. } else if downloadState == .retry {
  161. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in
  162. self?.downLoad()
  163. })
  164. } else {
  165. self.hiddenProgressWindow()
  166. }
  167. })
  168. }
  169. #endif
  170. public func checkAction() {
  171. self._checkAction()
  172. }
  173. private func _checkAction() {
  174. let state = self.checkView_.properties.checkboxType
  175. languageView_.properties.isDisabled = state != .selected
  176. languageView_.reloadData()
  177. }
  178. func showProgressWindow() {
  179. let progress = SKProgressController()
  180. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  181. progress.window?.contentView?.wantsLayer = true
  182. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  183. progress.progressField.textColor = NSColor.white
  184. progress.message = NSLocalizedString("Download...", comment: "")
  185. progress.closeBlock = { [weak self] in
  186. #if VERSION_DMG
  187. KMResourceDownloadManager.manager.cancelDownload()
  188. #endif
  189. }
  190. self.progressController = progress
  191. self.window?.beginSheet(progress.window!)
  192. }
  193. func hiddenProgressWindow() {
  194. DispatchQueue.main.async {
  195. self.progressController?.doubleValue = 100.0
  196. if (self.progressController != nil) {
  197. self.window?.endSheet((self.progressController?.window)!)
  198. self.progressController = nil
  199. }
  200. }
  201. }
  202. }
  203. extension KMConvertOCRSettingItemView: KMSelectPopButtonDelegate {
  204. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  205. if (obj.isEqual(to: self._languageVC)) {
  206. guard let index = self._languageVC?.indexOfSelectedItem, index >= 0 else {
  207. return
  208. }
  209. self._languageIndex = index
  210. guard let callback = self.lanugageDidSelected else {
  211. return
  212. }
  213. callback(index, nil)
  214. }
  215. }
  216. }
  217. extension KMConvertOCRSettingItemView: ComponentSelectDelegate {
  218. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  219. guard let index = view?.indexOfSelect(), index >= 0 else {
  220. return
  221. }
  222. self._languageIndex = index
  223. guard let callback = self.lanugageDidSelected else {
  224. return
  225. }
  226. callback(index, nil)
  227. }
  228. }