KMBlankView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KMBlankView.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/10/8.
  6. //
  7. import Cocoa
  8. @objc enum KMBlankViewMouseEventType: UInt {
  9. case mouseEnter
  10. case mouseExit
  11. case mouseDown
  12. case mouseUp
  13. }
  14. typealias DragSuccessCallBack = ([String]) -> Void
  15. @objcMembers
  16. class KMBlankView: NSView {
  17. @IBOutlet var titleLabel: NSTextField!
  18. @IBOutlet var secondTitleLabel: NSTextField!
  19. @IBOutlet var imageView: NSImageView!
  20. @IBOutlet var customView: KMPDFEditAppendCustomView!
  21. var allowedFileTypes: [String]?
  22. var mouseActionCallBack: ((KMBlankViewMouseEventType) -> Void)?
  23. var dragSuccessBlock: DragSuccessCallBack?
  24. private var _button: NSButton?
  25. override func awakeFromNib() {
  26. super.awakeFromNib()
  27. titleLabel.font = NSFont.systemFont(ofSize: 14, weight: .semibold)
  28. secondTitleLabel.font = NSFont.systemFont(ofSize: 14)
  29. titleLabel.stringValue = ""
  30. secondTitleLabel.stringValue = ""
  31. addTrackingArea()
  32. self.wantsLayer = true
  33. customView.addSubview(button!)
  34. registerForDraggedTypes([.fileURL])
  35. }
  36. override func layout() {
  37. super.layout()
  38. button!.frame = imageView.frame
  39. }
  40. //MARK: Drag
  41. override func draggingExited(_ sender: NSDraggingInfo?) {
  42. }
  43. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  44. let pboard = sender.draggingPasteboard
  45. var isCanDrag = false
  46. var result: NSDragOperation = []
  47. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  48. if let fileURLs = pboard.readObjects(forClasses: [NSURL.self], options: nil) as? [URL] {
  49. for fileURL in fileURLs {
  50. if allowedFileTypes!.contains(fileURL.pathExtension.lowercased()) {
  51. isCanDrag = true
  52. } else {
  53. isCanDrag = false
  54. break
  55. }
  56. }
  57. }
  58. }
  59. layer?.borderColor = NSColor.clear.cgColor
  60. if isCanDrag {
  61. result = [.copy]
  62. }
  63. return result
  64. }
  65. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  66. let pboard = sender.draggingPasteboard
  67. if (pboard.pasteboardItems == nil) {
  68. return true
  69. }
  70. var files: [String] = []
  71. for item in pboard.pasteboardItems! {
  72. let fileURL = item.string(forType: .fileURL)
  73. if (fileURL == nil) {
  74. continue
  75. }
  76. let path = URL.init(string: fileURL!)
  77. if (path == nil) {
  78. continue
  79. }
  80. let filePath: String = path!.path
  81. files.append(filePath)
  82. }
  83. if let successBlock = self.dragSuccessBlock {
  84. successBlock(files)
  85. }
  86. // if let fileURL = pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) {
  87. // let fileNames: String = pboard.propertyList(forType: .fileURL) as! String
  88. // let path = URL.init(string: fileNames)
  89. // let filePath: String = path!.path
  90. // if let successBlock = self.dragSuccessBlock {
  91. // successBlock([filePath])
  92. // }
  93. // }
  94. return true
  95. }
  96. //MARK: mouse Methods
  97. func addTrackingArea() {
  98. let trackingArea = NSTrackingArea(rect: self.imageView.bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil)
  99. self.imageView.addTrackingArea(trackingArea)
  100. }
  101. override func mouseEntered(with event: NSEvent) {
  102. super.mouseEntered(with: event)
  103. if let mouseActionCallBack = self.mouseActionCallBack {
  104. mouseActionCallBack(.mouseEnter)
  105. }
  106. }
  107. override func mouseExited(with event: NSEvent) {
  108. super.mouseExited(with: event)
  109. if let mouseActionCallBack = self.mouseActionCallBack {
  110. mouseActionCallBack(.mouseExit)
  111. }
  112. }
  113. override func mouseUp(with event: NSEvent) {
  114. super.mouseUp(with: event)
  115. let point = event.locationInWindow
  116. let pointInView = self.convert(point, to: self.imageView)
  117. if let mouseActionCallBack = self.mouseActionCallBack, self.imageView.bounds.contains(pointInView) {
  118. mouseActionCallBack(.mouseUp)
  119. }
  120. }
  121. //MARK: User Actions
  122. @objc func buttonDidClick() -> Void {
  123. if let mouseActionCallBack = self.mouseActionCallBack {
  124. mouseActionCallBack(.mouseDown)
  125. }
  126. }
  127. //MARK: Get、Set
  128. var button: NSButton? {
  129. get {
  130. if _button == nil {
  131. _button = NSButton()
  132. _button?.isBordered = false
  133. _button?.title = ""
  134. _button?.target = self
  135. _button?.action = #selector(buttonDidClick)
  136. }
  137. return _button
  138. }
  139. }
  140. }