KMHomeDragView.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // KMHomeDragView.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/11/24.
  6. //
  7. import Cocoa
  8. protocol KMHomeDragViewDelegate: NSObjectProtocol {
  9. func homeDragView(_ viewController: KMHomeDragView, filePath: URL)
  10. func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool)
  11. }
  12. class KMHomeDragView: NSView {
  13. @IBOutlet weak var dragView: NSView!
  14. @IBOutlet weak var dragLabel: NSTextField!
  15. @IBOutlet weak var dragViewHeight: NSLayoutConstraint!
  16. open weak var delete: KMHomeDragViewDelegate?
  17. var isDraggingEntered = false
  18. // MARK: Init
  19. override func awakeFromNib() {
  20. super.awakeFromNib()
  21. initializeUI()
  22. dragView.isHidden = true
  23. self.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
  24. }
  25. func initializeUI() -> Void {
  26. self.wantsLayer = true;
  27. self.layer?.borderColor = .clear
  28. let typography = KMDesignToken.shared.typography(withToken: "notification.toast.mac-text")
  29. dragLabel.textColor = KMDesignToken.shared.fill(withToken: "notification.toast.mac-text")
  30. let paragraphStyle = NSMutableParagraphStyle()
  31. paragraphStyle.lineSpacing = typography.lineHeight.stringToCGFloat()
  32. dragLabel.attributedStringValue = NSAttributedString(string: NSLocalizedString("Drop to open it", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  33. var fontFamily: String = typography.fontFamily
  34. let fontWeight: String = typography.fontWeight
  35. if fontFamily.contains(" ") {
  36. fontFamily = fontFamily.replacingOccurrences(of: " ", with: "")
  37. }
  38. if fontWeight != "" {
  39. fontFamily = String(format: "%@-%@", fontFamily, fontWeight)
  40. }
  41. dragLabel.font = NSFont(name: fontFamily, size: typography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: typography.fontSize.stringToCGFloat())
  42. dragViewHeight.constant = KMDesignToken.shared.height(withToken: "notification.toast.bg").stringToCGFloat()
  43. }
  44. // MARK: private
  45. func dragEntered(_ isDragEntered: Bool) -> Void {
  46. if isDragEntered {
  47. self.layer?.backgroundColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1).cgColor
  48. self.layer?.cornerRadius = 16.0
  49. dragView.wantsLayer = true
  50. dragView.layer?.backgroundColor = KMDesignToken.shared.fill(withToken: "notification.toast.bg").cgColor
  51. dragView.layer?.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "notification.toast.bg").stringToCGFloat()
  52. let boxShadow = KMDesignToken.shared.boxShadow(withToken: "notification.toast.bg") as KMBoxShadowValue
  53. // let shadow = NSShadow()
  54. // shadow.shadowColor = boxShadow.color
  55. // shadow.shadowOffset = CGSizeMake(boxShadow.x.stringToCGFloat(), boxShadow.y.stringToCGFloat())
  56. // shadow.shadowBlurRadius = (boxShadow.blur.stringToCGFloat())
  57. // dragView.shadow = shadow
  58. dragView.isHidden = false
  59. isDraggingEntered = true
  60. self.needsDisplay = true
  61. } else {
  62. self.layer?.borderColor = .clear
  63. self.layer?.backgroundColor = .clear
  64. dragView.isHidden = true
  65. isDraggingEntered = false
  66. self.needsDisplay = true
  67. }
  68. }
  69. // MARK: Drag
  70. override func draggingExited(_ sender: NSDraggingInfo?) {
  71. dragEntered(false)
  72. }
  73. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  74. let pboard = sender.draggingPasteboard
  75. if (pboard.pasteboardItems == nil) {
  76. return []
  77. }
  78. var canAdd = true
  79. for item in pboard.pasteboardItems! {
  80. let fileURL = item.string(forType: .fileURL)
  81. if (fileURL == nil) {
  82. canAdd = false
  83. break
  84. }
  85. let path = URL.init(string: fileURL!)
  86. if (path == nil) {
  87. canAdd = false
  88. break
  89. }
  90. let type = path!.pathExtension.lowercased()
  91. if (KMTools.isPDFType(type) || KMTools.isImageType(type) || KMTools.isOfficeType(type)) {} else {
  92. canAdd = false
  93. self.delete?.homeDragView(self, notSupport: true)
  94. break
  95. }
  96. }
  97. if (canAdd) {
  98. dragEntered(true)
  99. return .every
  100. }
  101. self.layer?.borderColor = .clear
  102. return []
  103. }
  104. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  105. let pboard = sender.draggingPasteboard
  106. if (pboard.pasteboardItems == nil) {
  107. dragEntered(false)
  108. return true
  109. }
  110. for item in pboard.pasteboardItems! {
  111. let fileURL = item.string(forType: .fileURL)
  112. if (fileURL == nil) {
  113. continue
  114. }
  115. let path = URL.init(string: fileURL!)
  116. if (path == nil) {
  117. continue
  118. }
  119. self.delete?.homeDragView(self, filePath: path!)
  120. }
  121. dragEntered(false)
  122. return true
  123. }
  124. // MARK: Draw
  125. override func draw(_ dirtyRect: NSRect) {
  126. super.draw(dirtyRect)
  127. if isDraggingEntered {
  128. // let path = NSBezierPath()
  129. // path.move(to: CGPoint(x: 0, y: 0))
  130. // path.line(to: CGPoint(x: 0, y: self.bounds.height))
  131. //
  132. // path.move(to: CGPoint(x: 0, y: self.bounds.height))
  133. // path.line(to: CGPoint(x: self.bounds.width, y: self.bounds.height))
  134. //
  135. // path.move(to: CGPoint(x: self.bounds.width, y: self.bounds.height))
  136. // path.line(to: CGPoint(x: self.bounds.width, y: 0))
  137. //
  138. // path.move(to: CGPoint(x: self.bounds.width, y: 0))
  139. // path.line(to: CGPoint(x: 0, y: 0))
  140. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 16.0, yRadius: 16.0)
  141. path.setLineDash([3, 3, 3], count: 3, phase: 0)
  142. path.lineWidth = 2.0
  143. NSColor.km_init(hex: "#68ACF8").setStroke()
  144. path.stroke()
  145. }
  146. }
  147. }