KMBatchSelectedFilesView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // KMBatchSelectedFilesView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/29.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMBatchSelectedFilesViewCompletion = (_ data: [URL]) -> ()
  10. typealias KMBatchSelectedFilesViewAddFilesAction = (_ sender: Any) -> ()
  11. class KMBatchSelectedFilesView: BaseXibView {
  12. @IBOutlet weak var imageButton: NSButton!
  13. @IBOutlet weak var titleLabel: NSTextField!
  14. @IBOutlet weak var subtitleLabel: NSTextField!
  15. @IBOutlet weak var box: KMBox!
  16. var inputType: KMBatchCollectionViewType?
  17. var data: [URL] = []
  18. var dragEnd: KMBatchSelectedFilesViewCompletion?
  19. var addAction: KMBatchSelectedFilesViewAddFilesAction?
  20. override func draw(_ dirtyRect: NSRect) {
  21. super.draw(dirtyRect)
  22. // Drawing code here.
  23. }
  24. override func setup() {
  25. super.setup()
  26. self.registerForDraggedTypes([NSPasteboard.PasteboardType.string, NSPasteboard.PasteboardType.fileURL]) //支持拖拽
  27. self.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorPicture/empty-textDefault")
  28. self.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  29. self.subtitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorPicture/empty-textSecondary")
  30. self.subtitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  31. self.titleLabel.stringValue = KMLocalizedString("Select Files", comment: "")
  32. self.subtitleLabel.stringValue = KMLocalizedString("Drop files here or Click “+”. Drag files to reorder as you need.\nYou can also add files or folders via the lower left button.", comment: "")
  33. let title = NSLocalizedString("Drop files here or Click “+”. Drag files to reorder as you need.\nYou can also add files or folders via the lower left button.", comment: "")
  34. let paragraphStyle = NSMutableParagraphStyle()
  35. paragraphStyle.lineHeightMultiple = 1.32
  36. paragraphStyle.alignment = .center
  37. self.subtitleLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : NSColor.km_init(hex: "#94989C"), NSAttributedString.Key.font: NSFont.SFProTextRegularFont(12)])
  38. self.box.moveCallback = { [unowned self] mouseEntered, mouseBox in
  39. if mouseEntered {
  40. self.imageButton.image = NSImage(named: "icon_empty_add_hov")
  41. } else {
  42. self.imageButton.image = NSImage(named: "icon_empty_add_norm")
  43. }
  44. }
  45. }
  46. }
  47. protocol KMBatchSelectedFilesViewAction {}
  48. extension KMBatchSelectedFilesView: KMBatchSelectedFilesViewAction {
  49. @IBAction func selectFilse(_ sender: NSButton) {
  50. if (self.addAction != nil) {
  51. self.addAction!(sender)
  52. }
  53. }
  54. }
  55. protocol KMBatchSelectedFilesViewDrag {}
  56. extension KMBatchSelectedFilesView: KMBatchSelectedFilesViewDrag {
  57. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  58. KMPrint("draggingEntered")
  59. if self.allowAccept(sender) {
  60. return .copy
  61. } else {
  62. return .move
  63. }
  64. }
  65. override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
  66. if self.allowAccept(sender) {
  67. return .copy
  68. } else {
  69. return .move
  70. }
  71. }
  72. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  73. return self.allowAccept(sender)
  74. }
  75. override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
  76. if self.allowAccept(sender) {
  77. self.endDrag(sender: sender, info: [:])
  78. return true
  79. } else {
  80. return false
  81. }
  82. }
  83. override func draggingExited(_ sender: NSDraggingInfo?) {
  84. KMPrint("draggingExited")
  85. }
  86. func endDrag(sender: NSDraggingInfo, info: NSDictionary) {
  87. // CGPoint point = sender.draggedImageLocation;
  88. // point.y = sender.draggingLocation.y;
  89. //
  90. // point = [self convertPoint:point fromView:nil];
  91. KMPrint("endDrag")
  92. if (self.dragEnd != nil) {
  93. self.dragEnd!(self.data)
  94. }
  95. }
  96. func allowAccept(_ sender: NSDraggingInfo) -> Bool {
  97. var allow = false
  98. let pasteboard = sender.draggingPasteboard
  99. if (pasteboard.types?.contains(.fileURL) == true) {
  100. var array: [URL] = []
  101. for item in pasteboard.pasteboardItems! {
  102. let string = item.string(forType: NSPasteboard.PasteboardType.fileURL)!
  103. let url = NSURL(string: string)
  104. switch self.inputType {
  105. case .OCR:
  106. if (url?.pathExtension?.lowercased() == "pdf") {
  107. array.append(url! as URL)
  108. }
  109. case .imageToPDF:
  110. if (KMBatchProcessingTableViewModel.supportedImageTypes().contains((url?.pathExtension?.lowercased())!)) {
  111. array.append(url! as URL)
  112. }
  113. default:
  114. if (url?.pathExtension?.lowercased() == "pdf") {
  115. array.append(url! as URL)
  116. }
  117. KMPrint("无数据")
  118. }
  119. }
  120. if array.count != 0 {
  121. allow = true
  122. self.data = array
  123. }
  124. }
  125. return allow
  126. }
  127. }