KMBlankView.swift 5.5 KB

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