// // PSDragView.m // Perforce // // Created by Adam Czubernat on 21.06.2014. // Copyright (c) 2014 Perforce Software, Inc. All rights reserved. // #import "PSDragView.h" @implementation PSDragView @synthesize dragObject, dragType; - (void)mouseDragged:(NSEvent *)theEvent { if (!dragObject || !dragType) return; NSBitmapImageRep *bitmap = [self bitmapImageRepForCachingDisplayInRect:self.bounds]; [self cacheDisplayInRect:self.bounds toBitmapImageRep:bitmap]; NSImage *image = [[NSImage alloc] init]; [image addRepresentation:bitmap]; // Making image appear exactly on view position CGPoint point = [self convertPoint:theEvent.locationInWindow fromView:nil]; CGPoint offset = [self.superview convertPoint:point toView:self]; point.x -= offset.x; point.y += offset.y; NSPasteboardItem *pboardItem = [[NSPasteboardItem alloc] init]; [pboardItem setPropertyList:dragObject forType:dragType]; NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pboardItem]; [dragItem setDraggingFrame:(CGRect){ point, self.frame.size } contents:image]; NSDraggingSession *session = [self beginDraggingSessionWithItems:@[ dragItem ] event:theEvent source:nil]; session.animatesToStartingPositionsOnCancelOrFail = YES; } @end