Skip to content

Drag and drop

Ken Harris edited this page Sep 30, 2020 · 2 revisions

TODO: how to get dropped items on the dock icon (text; files)

Dragging file paths

When you want to pass URLs via drag-n-drop, don't send them as NSURL/URL values. Send them as plain text. Safari (and Firefox) drags (even from the favicon rather than a text field) generate only types [public.utf8-plain-text, NSStringPboardType]. Also, dragging plain text that looks like a URL to the Finder creates a .webloc file there (!). To detect a URL dropped on your app, then, look at its text and see if it looks like a URL you wish to support.

Dragging session icons

According to the AppKit 10.13 release notes (and nowhere else in any Apple documentation):

Large drag and drop images are automatically scaled to smaller sizes as the drag leaves the source view of the drag. The following general advice now applies to all sources of a drag.

  1. The drag should start with an image of the item being dragged sized as the user currently experiences it in the UI.
  2. Carefully choose the source view for the drag. Auto scaling occurs once the drag exits the bounds of the source view. For example, if re-arranging items in a table, the source view should be the table and not the cell view. In this manner the drag image won’t change size during the re-order operation unless the user drags outside of the table view.

Careful! You might think that "source" here means "the parameter named source, which is of type NSDraggingSource" (and be scared that you have to restructure your code to make your container view conform to NSDraggingSource and manage the dragging of all of the draggable views contained in it).

It appears that is not the case -- at least, it doesn't work like that.

By "source", they appear to mean "receiver". Which class you choose to use for the NSDraggingSource is irrelevant. So it's worse than you feared: you have to restructure your code to make the container view handle the mouse events and initiate the drag session -- and at that point you might as well make it the NSDraggingSource, too.

Clone this wiki locally