123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- //
- // KMHomeDragView.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/11/24.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMHomeDragViewDelegate: NSObjectProtocol {
- @objc optional func homeDragView(_ viewController: KMHomeDragView, filePath: URL)
- @objc optional func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool)
- }
- class KMHomeDragView: NSView {
-
- @IBOutlet weak var dragView: NSView!
- @IBOutlet weak var dragLabel: NSTextField!
- @IBOutlet weak var dragViewHeight: NSLayoutConstraint!
- open weak var delegate: KMHomeDragViewDelegate?
- var isDraggingEntered = false
-
- // MARK: Init
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- initializeUI()
-
- dragView.isHidden = true
-
- self.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
- }
-
- func initializeUI() -> Void {
- self.wantsLayer = true
-
- dragLabel.stringValue = KMLocalizedString("Drop to open it")
- dragLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/white1")
- dragLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
- dragViewHeight.constant = 40
- }
-
- // MARK: private
- func dragEntered(_ isDragEntered: Bool) -> Void {
- if isDragEntered {
- self.display()
-
- dragView.wantsLayer = true
- dragView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/dark").cgColor
- if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/s") {
- let currentValue = value as! CGFloat
- dragView.layer?.cornerRadius = currentValue
- }
- dragView.isHidden = false
- isDraggingEntered = true
- self.needsDisplay = true
- } else {
- self.display()
-
- dragView.isHidden = true
- isDraggingEntered = false
- self.needsDisplay = true
- }
- }
-
- // MARK: Drag
-
- override func draggingExited(_ sender: NSDraggingInfo?) {
- dragEntered(false)
- }
-
- override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
- let pboard = sender.draggingPasteboard
- if (pboard.pasteboardItems == nil) {
- return []
- }
-
- var canAdd = true
- for item in pboard.pasteboardItems! {
- let fileURL = item.string(forType: .fileURL)
- if (fileURL == nil) {
- canAdd = false
- break
- }
- let path = URL.init(string: fileURL!)
- if (path == nil) {
- canAdd = false
- break
- }
- let type = path!.pathExtension.lowercased()
- if (KMTools.isPDFType(type) || KMTools.isImageType(type) || KMTools.isOfficeType(type)) {} else {
- canAdd = false
- self.delegate?.homeDragView?(self, notSupport: true)
- break
- }
- }
-
- if (canAdd) {
- dragEntered(true)
- return .every
- }
-
- self.layer?.borderColor = .clear
- return []
- }
-
- override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
- let pboard = sender.draggingPasteboard
- if (pboard.pasteboardItems == nil) {
- dragEntered(false)
- return true
- }
-
- for item in pboard.pasteboardItems! {
- let fileURL = item.string(forType: .fileURL)
- if (fileURL == nil) {
- continue
- }
- let path = URL.init(string: fileURL!)
- if (path == nil) {
- continue
- }
-
- self.delegate?.homeDragView?(self, filePath: path!)
- }
-
- dragEntered(false)
- return true
- }
-
- // MARK: Draw
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- if isDraggingEntered {
- let path = NSBezierPath(roundedRect: self.bounds, xRadius: 12, yRadius: 12)
-
- path.setLineDash([3, 3, 3], count: 3, phase: 0)
- path.lineWidth = 2.0
- ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/border1").setStroke()
- path.stroke()
- ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-light").setFill()
- path.fill()
-
- } else {
- let path = NSBezierPath(roundedRect: self.bounds, xRadius: 0, yRadius: 0)
- path.lineWidth = 0
- NSColor.clear.setStroke()
- path.stroke()
- NSColor.clear.setFill()
- path.fill()
-
- }
- }
-
- }
|