KMHomeDragView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // KMHomeDragView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/24.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMHomeDragViewDelegate: NSObjectProtocol {
  10. @objc optional func homeDragView(_ viewController: KMHomeDragView, filePath: URL)
  11. @objc optional func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool)
  12. }
  13. class KMHomeDragView: NSView {
  14. @IBOutlet weak var dragView: NSView!
  15. @IBOutlet weak var dragLabel: NSTextField!
  16. @IBOutlet weak var dragViewHeight: NSLayoutConstraint!
  17. open weak var delegate: KMHomeDragViewDelegate?
  18. var isDraggingEntered = false
  19. // MARK: Init
  20. override func awakeFromNib() {
  21. super.awakeFromNib()
  22. initializeUI()
  23. dragView.isHidden = true
  24. self.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
  25. }
  26. func initializeUI() -> Void {
  27. self.wantsLayer = true
  28. dragLabel.stringValue = KMLocalizedString("Drop to open it")
  29. dragLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/white1")
  30. dragLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  31. dragViewHeight.constant = 40
  32. }
  33. // MARK: private
  34. func dragEntered(_ isDragEntered: Bool) -> Void {
  35. if isDragEntered {
  36. self.display()
  37. dragView.wantsLayer = true
  38. dragView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/dark").cgColor
  39. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/s") {
  40. let currentValue = value as! CGFloat
  41. dragView.layer?.cornerRadius = currentValue
  42. }
  43. dragView.isHidden = false
  44. isDraggingEntered = true
  45. self.needsDisplay = true
  46. } else {
  47. self.display()
  48. dragView.isHidden = true
  49. isDraggingEntered = false
  50. self.needsDisplay = true
  51. }
  52. }
  53. // MARK: Drag
  54. override func draggingExited(_ sender: NSDraggingInfo?) {
  55. dragEntered(false)
  56. }
  57. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  58. let pboard = sender.draggingPasteboard
  59. if (pboard.pasteboardItems == nil) {
  60. return []
  61. }
  62. var canAdd = true
  63. for item in pboard.pasteboardItems! {
  64. let fileURL = item.string(forType: .fileURL)
  65. if (fileURL == nil) {
  66. canAdd = false
  67. break
  68. }
  69. let path = URL.init(string: fileURL!)
  70. if (path == nil) {
  71. canAdd = false
  72. break
  73. }
  74. let type = path!.pathExtension.lowercased()
  75. if (KMTools.isPDFType(type) || KMTools.isImageType(type) || KMTools.isOfficeType(type)) {} else {
  76. canAdd = false
  77. self.delegate?.homeDragView?(self, notSupport: true)
  78. break
  79. }
  80. }
  81. if (canAdd) {
  82. dragEntered(true)
  83. return .every
  84. }
  85. self.layer?.borderColor = .clear
  86. return []
  87. }
  88. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  89. let pboard = sender.draggingPasteboard
  90. if (pboard.pasteboardItems == nil) {
  91. dragEntered(false)
  92. return true
  93. }
  94. for item in pboard.pasteboardItems! {
  95. let fileURL = item.string(forType: .fileURL)
  96. if (fileURL == nil) {
  97. continue
  98. }
  99. let path = URL.init(string: fileURL!)
  100. if (path == nil) {
  101. continue
  102. }
  103. self.delegate?.homeDragView?(self, filePath: path!)
  104. }
  105. dragEntered(false)
  106. return true
  107. }
  108. // MARK: Draw
  109. override func draw(_ dirtyRect: NSRect) {
  110. super.draw(dirtyRect)
  111. if isDraggingEntered {
  112. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 12, yRadius: 12)
  113. path.setLineDash([3, 3, 3], count: 3, phase: 0)
  114. path.lineWidth = 2.0
  115. ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/border1").setStroke()
  116. path.stroke()
  117. ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-light").setFill()
  118. path.fill()
  119. } else {
  120. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 0, yRadius: 0)
  121. path.lineWidth = 0
  122. NSColor.clear.setStroke()
  123. path.stroke()
  124. NSColor.clear.setFill()
  125. path.fill()
  126. }
  127. }
  128. }