KMBlankView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if customView == nil {
  34. } else {
  35. customView.addSubview(button!)
  36. }
  37. registerForDraggedTypes([.fileURL])
  38. }
  39. override func layout() {
  40. super.layout()
  41. button!.frame = imageView.frame
  42. }
  43. //MARK: Drag
  44. override func draggingExited(_ sender: NSDraggingInfo?) {
  45. }
  46. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  47. let pboard = sender.draggingPasteboard
  48. var isCanDrag = false
  49. var result: NSDragOperation = []
  50. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  51. if let fileURLs = pboard.readObjects(forClasses: [NSURL.self], options: nil) as? [URL] {
  52. for fileURL in fileURLs {
  53. if allowedFileTypes!.contains(fileURL.pathExtension.lowercased()) {
  54. isCanDrag = true
  55. } else {
  56. isCanDrag = false
  57. break
  58. }
  59. }
  60. }
  61. }
  62. layer?.borderColor = NSColor.clear.cgColor
  63. if isCanDrag {
  64. result = [.copy]
  65. }
  66. return result
  67. }
  68. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  69. let pboard = sender.draggingPasteboard
  70. if (pboard.pasteboardItems == nil) {
  71. return true
  72. }
  73. var files: [String] = []
  74. for item in pboard.pasteboardItems! {
  75. let fileURL = item.string(forType: .fileURL)
  76. if (fileURL == nil) {
  77. continue
  78. }
  79. let path = URL.init(string: fileURL!)
  80. if (path == nil) {
  81. continue
  82. }
  83. let filePath: String = path!.path
  84. files.append(filePath)
  85. }
  86. if let successBlock = self.dragSuccessBlock {
  87. successBlock(files)
  88. }
  89. // if let fileURL = pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) {
  90. // let fileNames: String = pboard.propertyList(forType: .fileURL) as! String
  91. // let path = URL.init(string: fileNames)
  92. // let filePath: String = path!.path
  93. // if let successBlock = self.dragSuccessBlock {
  94. // successBlock([filePath])
  95. // }
  96. // }
  97. return true
  98. }
  99. //MARK: mouse Methods
  100. func addTrackingArea() {
  101. let trackingArea = NSTrackingArea(rect: self.imageView.bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil)
  102. self.imageView.addTrackingArea(trackingArea)
  103. }
  104. override func mouseEntered(with event: NSEvent) {
  105. super.mouseEntered(with: event)
  106. if let mouseActionCallBack = self.mouseActionCallBack {
  107. mouseActionCallBack(.mouseEnter)
  108. }
  109. }
  110. override func mouseExited(with event: NSEvent) {
  111. super.mouseExited(with: event)
  112. if let mouseActionCallBack = self.mouseActionCallBack {
  113. mouseActionCallBack(.mouseExit)
  114. }
  115. }
  116. override func mouseDown(with event: NSEvent) {
  117. super.mouseDown(with: event)
  118. var point = event.locationInWindow
  119. point = self.convert(point, to: self.imageView)
  120. if let mouseActionCallBack = self.mouseActionCallBack, self.imageView.bounds.contains(point) {
  121. mouseActionCallBack(.mouseDown)
  122. }
  123. }
  124. override func mouseUp(with event: NSEvent) {
  125. super.mouseUp(with: event)
  126. let point = event.locationInWindow
  127. let pointInView = self.convert(point, to: self.imageView)
  128. if let mouseActionCallBack = self.mouseActionCallBack, self.imageView.bounds.contains(pointInView) {
  129. mouseActionCallBack(.mouseUp)
  130. }
  131. }
  132. //MARK: User Actions
  133. @objc func buttonDidClick() -> Void {
  134. if let mouseActionCallBack = self.mouseActionCallBack {
  135. mouseActionCallBack(.mouseDown)
  136. }
  137. }
  138. //MARK: Get、Set
  139. var button: NSButton? {
  140. get {
  141. if _button == nil {
  142. _button = NSButton()
  143. _button?.isBordered = false
  144. _button?.title = ""
  145. _button?.target = self
  146. _button?.action = #selector(buttonDidClick)
  147. }
  148. return _button
  149. }
  150. }
  151. }