KMBlankView.swift 5.6 KB

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