KMBatchOCRView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // KMBatchOCRView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2025/1/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMBatchOCRView: KMBatchSettingItemView {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var titleContentView: NSView!
  12. @IBOutlet weak var ocrPageView: KMOCRPageView!
  13. @IBOutlet weak var applyButton: ComponentButton!
  14. var groupView: ComponentGroup!
  15. var model: KMOCRModel = KMOCRModel()
  16. override func draw(_ dirtyRect: NSRect) {
  17. super.draw(dirtyRect)
  18. // Drawing code here.
  19. }
  20. override func setup() {
  21. self.titleContentView.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider"), 0.5, 0)
  22. applyButton.properties = ComponentButtonProperty(type: .primary, size: .m, showRightIcon: true, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  23. applyButton.properties.propertyInfo.rightIcon_nor = NSImage(named: "arrowDown")
  24. applyButton.properties.propertyInfo.rightIcon_press = NSImage(named: "arrowDown_press")
  25. applyButton.setTarget(self, action: #selector(saveButtonClicked(_:)))
  26. applyButton.properties.buttonText = KMLocalizedString("Save as PDF")
  27. applyButton.properties.showRightIcon = true
  28. ocrPageView.type = .batch
  29. model.pageRangeType = .all
  30. model.saveAsPDF = true
  31. }
  32. //MARK: - GroupView
  33. func showGroupView() {
  34. var viewHeight: CGFloat = 8
  35. var menuItemArr: [ComponentMenuitemProperty] = []
  36. for i in ["Save as PDF", "Save as TXT"] {
  37. if i == " " {
  38. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  39. menuItemArr.append(property)
  40. viewHeight += 8
  41. } else {
  42. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  43. itemSelected: false,
  44. isDisabled: false,
  45. keyEquivalent: nil,
  46. text: KMLocalizedString(i))
  47. menuItemArr.append(properties_Menuitem)
  48. viewHeight += 36
  49. }
  50. }
  51. if groupView == nil {
  52. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  53. }
  54. groupView.groupDelegate = self
  55. groupView?.frame = CGRectMake(310, 0, CGRectGetWidth(applyButton.bounds), viewHeight)
  56. groupView.updateGroupInfo(menuItemArr)
  57. var point = self.convert(applyButton.frame.origin, to: nil)
  58. point.y += applyButton.bounds.size.height + 4
  59. groupView.showWithPoint(point, relativeTo: applyButton)
  60. applyButton.properties.state = .pressed
  61. applyButton.reloadData()
  62. }
  63. func removeGroupView() {
  64. if groupView != nil {
  65. groupView.removeFromSuperview()
  66. }
  67. applyButton.properties.state = .normal
  68. applyButton.reloadData()
  69. }
  70. override func bacthProcessingNotification() {
  71. self.ocrPageView.isDisabled = self.isDisable
  72. self.applyButton.properties.isDisabled = self.isDisable
  73. self.applyButton.reloadData()
  74. }
  75. }
  76. //MARK: Action
  77. extension KMBatchOCRView {
  78. @objc func saveButtonClicked(_ sender: ComponentButton) {
  79. showGroupView()
  80. }
  81. }
  82. extension KMBatchOCRView: ComponentGroupDelegate {
  83. func componentGroupDidDismiss(group: ComponentGroup?) {
  84. removeGroupView()
  85. applyButton.properties.state = .normal
  86. applyButton.reloadData()
  87. }
  88. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  89. if menuItemProperty?.text == KMLocalizedString("Save as PDF") {
  90. model.saveType = .PDF
  91. } else if menuItemProperty?.text == KMLocalizedString("Save as TXT") {
  92. model.saveType = .TXT
  93. }
  94. guard let callBack = batchExport else { return }
  95. callBack(self, model)
  96. }
  97. }