// // 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() } } }