Wednesday, March 3, 2010

Poof - Drag Deleting Items in Cocoa

OSX has an idea of drag-deleting items in the UI. You can see this in the dock amoungst other places - drag an item out the dock - and the cursor changes to a pointer adorned witha cloud. Release and "Poof" the item disappears with a small animation and a sound. It is cool and intuative. It works well when moving items does not cause a scroll. I have got used to the same way of deleteing breakpoints in XCode and wanted to add the same functionality to the labels in the timeline.

Looking at XCode the way that markers work is quite straight forward
  • Drag up or down and the breakpoint marker moves
  • Drag out of the breakpoint column and the cursor changes to the disappearingItemCursor cursor.
  • Release outside this column and the animation plays

Setting the cursor is quite simple just call [[NSCursor disappearingItemCursor] set]

The animation proved a little harder to track down. I quite quickly found a private API it is listed in a number of places that apparently did the job. My experiance of private APIs is that they disappear even quicker than the public ones.

The good news is that Apple supply it as a public API - NSShowAnimationEffect. It is explained in apple's web site. I found this link in cocoadev usefull.

No comments: