I'm updating some older code that uses the now-deprecated [NSView 
dragImage:at:offset:pasteboard:source:slideback:] to use NSDraggingSession and 
pals. Note that this older code works just fine, I've never had any issues with 
it. I'm doing this mostly just to clear out compiler warnings, though my 
instinct tells me not to "fix" code that isn't broken.

Sadly its replacement is broken.

In order to use NSDraggingItem, I first need to make sure the stuff I'm 
dragging supports NSPasteboardWriting. I've done that, it seems straightforward 
enough. Then I build a NSDraggingItem and use the same image I previously used, 
so there's nothing new there. Finally I use [NSView 
beginDraggingSessionWithItems:event:source:].

Nothing shows up. No image, nothing drags. I can break on the 
NSPasteboardWriting methods and see that they are being called, so it seems to 
be trying to write the correct things to the pasteboard. Nothing is logged. 
After this, use of the legacy dragging methods elsewhere in the app also fail 
to work - it's like whatever I did in this drag session messes up the dragging 
system altogether. Obviously I'm doing something wrong, but I've been over the 
docs and am just not getting it.

One thing is that the docs state that this new dragging method returns 
immediately and the drag itself is somehow magically handled on the next turn 
of the event loop. I think the older approach kept control for the duration of 
the drag. That seems like a very different situation from the app's point of 
view, and makes updating legacy code that expects it to keep control quite 
difficult. That might be where things are messing up, so I'll look into that... 
unless anyone can spot an obvious problem otherwise. Or would it just be better 
to stick with working code and ignore the deprecation warnings? Those warnings 
really go against the grain of everything I've ever learned about coding which 
is "if it ain't broke don't fix it".

Here's the code:

- (void)                dragStyleOrSymbol:(id<DKOLibraryObjectProtocol>) obj 
event:(NSEvent*) event source:(id) sourceObject slideBack:(BOOL) slideBack
{
        NSAssert( obj != nil, @"can't drag a nil style or symbol");
        
        // can pass in nil to mean self
        
        if( sourceObject == nil )
                sourceObject = self;
        
        // create the drag image
        
        NSImage* dragImage = [obj image];
        NSPoint mdLoc = [self convertPoint:[event locationInWindow] 
fromView:nil];
        
        mdLoc.x -= [dragImage size].width * 0.5;
        mdLoc.y -= [dragImage size].height * 0.5;

#if (LEGACY_ CODE)
        NSPasteboard* dragPBoard = [NSPasteboard 
pasteboardWithName:NSDragPboard];
        
        // put the object on the pasteboard
        
        [obj copyToPasteboard:dragPBoard];
        
        // start dragging it
        
        [self dragImage:dragImage at:mdLoc offset:NSZeroSize event:event 
pasteboard:dragPBoard source:sourceObject slideBack:slideBack];
#else
        NSDraggingItem* dragItem = [[[NSDraggingItem alloc] 
initWithPasteboardWriter:obj] autorelease];
        
        NSRect dragFrame;
        
        dragFrame.origin = mdLoc;
        dragFrame.size = dragImage.size;
        
        [dragItem setDraggingFrame:dragFrame contents:dragImage];
        
        NSDraggingSession* session = [self 
beginDraggingSessionWithItems:@[dragItem] event:event source:sourceObject];
        
        session.animatesToStartingPositionsOnCancelOrFail = slideBack;
#endif
}


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to