KMHomeDragView.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // KMHomeDragView.swift
  3. // PDF Reader Pro
  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. if KMAppearance.isDarkMode() {
  48. self.layer?.backgroundColor = NSColor.km_init(hex: "#227AFF", alpha: 0.3).cgColor
  49. } else {
  50. self.layer?.backgroundColor = NSColor.km_init(hex: "#4982E6", alpha: 0.1).cgColor
  51. }
  52. self.layer?.cornerRadius = 16.0
  53. dragView.wantsLayer = true
  54. dragView.layer?.backgroundColor = KMDesignToken.shared.fill(withToken: "notification.toast.bg").cgColor
  55. dragView.layer?.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "notification.toast.bg").stringToCGFloat()
  56. let boxShadow = KMDesignToken.shared.boxShadow(withToken: "notification.toast.bg") as KMBoxShadowValue
  57. // let shadow = NSShadow()
  58. // shadow.shadowColor = boxShadow.color
  59. // shadow.shadowOffset = CGSizeMake(boxShadow.x.stringToCGFloat(), boxShadow.y.stringToCGFloat())
  60. // shadow.shadowBlurRadius = (boxShadow.blur.stringToCGFloat())
  61. // dragView.shadow = shadow
  62. dragView.isHidden = false
  63. isDraggingEntered = true
  64. self.needsDisplay = true
  65. } else {
  66. self.layer?.borderColor = .clear
  67. self.layer?.backgroundColor = .clear
  68. dragView.isHidden = true
  69. isDraggingEntered = false
  70. self.needsDisplay = true
  71. }
  72. }
  73. // MARK: Drag
  74. override func draggingExited(_ sender: NSDraggingInfo?) {
  75. dragEntered(false)
  76. }
  77. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  78. let pboard = sender.draggingPasteboard
  79. if (pboard.pasteboardItems == nil) {
  80. return []
  81. }
  82. var canAdd = true
  83. for item in pboard.pasteboardItems! {
  84. let fileURL = item.string(forType: .fileURL)
  85. if (fileURL == nil) {
  86. canAdd = false
  87. break
  88. }
  89. let path = URL.init(string: fileURL!)
  90. if (path == nil) {
  91. canAdd = false
  92. break
  93. }
  94. let type = path!.pathExtension.lowercased()
  95. if (KMTools.isPDFType(type) || KMTools.isImageType(type) || KMTools.isOfficeType(type)) {} else {
  96. canAdd = false
  97. self.delete?.homeDragView(self, notSupport: true)
  98. break
  99. }
  100. }
  101. if (canAdd) {
  102. dragEntered(true)
  103. return .every
  104. }
  105. self.layer?.borderColor = .clear
  106. return []
  107. }
  108. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  109. let pboard = sender.draggingPasteboard
  110. if (pboard.pasteboardItems == nil) {
  111. dragEntered(false)
  112. return true
  113. }
  114. for item in pboard.pasteboardItems! {
  115. let fileURL = item.string(forType: .fileURL)
  116. if (fileURL == nil) {
  117. continue
  118. }
  119. let path = URL.init(string: fileURL!)
  120. if (path == nil) {
  121. continue
  122. }
  123. self.delete?.homeDragView(self, filePath: path!)
  124. }
  125. dragEntered(false)
  126. return true
  127. }
  128. // MARK: Draw
  129. override func draw(_ dirtyRect: NSRect) {
  130. super.draw(dirtyRect)
  131. if isDraggingEntered {
  132. // let path = NSBezierPath()
  133. // path.move(to: CGPoint(x: 0, y: 0))
  134. // path.line(to: CGPoint(x: 0, y: self.bounds.height))
  135. //
  136. // path.move(to: CGPoint(x: 0, y: self.bounds.height))
  137. // path.line(to: CGPoint(x: self.bounds.width, y: self.bounds.height))
  138. //
  139. // path.move(to: CGPoint(x: self.bounds.width, y: self.bounds.height))
  140. // path.line(to: CGPoint(x: self.bounds.width, y: 0))
  141. //
  142. // path.move(to: CGPoint(x: self.bounds.width, y: 0))
  143. // path.line(to: CGPoint(x: 0, y: 0))
  144. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 16.0, yRadius: 16.0)
  145. path.setLineDash([3, 3, 3], count: 3, phase: 0)
  146. path.lineWidth = 2.0
  147. KMAppearance.Interactive.m0Color().setStroke()
  148. path.stroke()
  149. }
  150. }
  151. }