KMBlankView.swift 5.7 KB

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