KMHomeDragView.swift 6.0 KB

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