简体   繁体   中英

Implementation of NSURL/File QuickLook Preview & NSURL/File dragging

I have a file (let's say a PDF) I have stored to disk on my macos Cocoa application. I have the NSURL that contains the path to this file.

I am currently showing that this file exists to the user using an NSView, which contains a label displaying the filename, and an NSImageView, displaying the file's icon (using NSWorkspace.shared.icon(forFile:url.path)).

My question is, how can I allow the user to 'select' or 'highlight' this NSView and when the space bar key is tapped have the QuickLook preview pop up for that particular file.

In addition, how would I have the NSView be draggable, with the end result of being able to drag the NSView to a Finder window and have the file copied to the dragged destination.

For both the QuickLook and dragging functionalities, I have gone through the Apple documentation but haven't found anything that accurately describes what I'm after.

For "selecting" a view, you should have the view accept first responder, and draw a focus ring (or whatever highlight is appropriate for you) when the view is first responder. Override acceptsFirstResponder (return true), becomeFirstResponder and resignFirstResponder (keep a flag for whether the view is first responder, and trigger drawing with setNeedsDisplay or whatever is appropriate), and the drawing mechanism ( drawRect or whatever is approproiate if you're using CALayers).

Override keyDown for handling the spacebar.

Override mouseDragged and initiate a drag session, and simply create an NSDraggingItem with the pasteboardWriter being the NSURL itself.

For displaying QuickLook: QLPreviewPanel https://developer.apple.com/documentation/quartz/qlpreviewpanel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM