12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- //
- // KMCompareFilesView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/15.
- //
- import Cocoa
- typealias KMCompareFilesViewDragEnteredCallBack = (Bool) -> Void
- typealias KMCompareFilesViewDragSuccessCallBack = (String) -> Void
- typealias KMCompareFilesViewMouseUpCallBack = (KMCompareFilesView) -> Void
- class KMCompareFilesView: NSView {
- var dragEnteredBlock: KMCompareFilesViewDragEnteredCallBack?
- var dragSuccessBlock: KMCompareFilesViewDragSuccessCallBack?
- var mouseUpBack: KMCompareFilesViewMouseUpCallBack?
- override func awakeFromNib() {
- super.awakeFromNib()
- registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
- }
- override func draggingExited(_ sender: NSDraggingInfo?) {
- if let block = dragEnteredBlock {
- block(false)
- }
- }
- override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
- if let block = dragEnteredBlock {
- block(true)
- }
- guard let pboard = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? [String: Any],
- let fileNames = pboard[NSPasteboard.PasteboardType.fileURL.rawValue] as? [String],
- let path = fileNames.first, (path as NSString).pathExtension.lowercased() == "pdf" else {
- self.layer?.borderColor = NSColor.clear.cgColor
- return []
- }
- return NSDragOperation.copy
- }
- override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
- guard let pboard = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? [String: Any],
- let fileNames = pboard[NSPasteboard.PasteboardType.fileURL.rawValue] as? [String],
- let path = fileNames.first, (path as NSString).pathExtension.lowercased() == "pdf" else {
- return false
- }
- #if VERSION_DMG
- // Add code for VERSION_DMG
- #else
- // let url = URL(fileURLWithPath: path)
- // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) {
- // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url)
- // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString))
- // }
- #endif
- if let block = dragSuccessBlock {
- block(path)
- }
- return true
- }
- override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
- if let block = mouseUpBack {
- block(self)
- }
- }
- }
|