Skip to content
Permalink
Browse files

drag and drop event delegate

* added a delegate system to provide a delegate and respond to drag events
* implemented first event, for processing received file paths via drag
* hooked up an event handler for that event in the app delegate, to simply update the label with the file paths received
  • Loading branch information
ejdraper committed May 19, 2015
1 parent f481721 commit aafbd686cc49a8eeea04642ddae56e5126fa9bc9
Showing with 19 additions and 0 deletions.
  1. +5 −0 app/app_delegate.rb
  2. +14 −0 app/drag_and_drop_view.rb
@@ -4,6 +4,7 @@ def applicationDidFinishLaunching(notification)
buildWindow

@drag_and_drop = DragAndDropView.alloc.initWithFrame(CGRectZero)
@drag_and_drop.delegate = self
self.set_drag_and_drop_frame
@mainWindow.contentView.addSubview(@drag_and_drop)

@@ -50,4 +51,8 @@ def windowDidResize(sender)
self.set_drag_and_drop_frame
self.set_label_frame
end

def drag_received_for_file_paths(paths)
@label.stringValue = "Received: #{paths.join(',')}"
end
end
@@ -1,4 +1,6 @@
class DragAndDropView < NSImageView
attr_accessor :delegate

def draggingEntered(info)
self.highlight!
NSDragOperationCopy
@@ -20,6 +22,11 @@ def performDragOperation(info)
image = NSImage.imageNamed("file_icon")
end
self.setImage(image)

if info.draggingPasteboard.types.include?('NSFilenamesPboardType')
files = info.draggingPasteboard.propertyListForType('NSFilenamesPboardType')
self.send_delegate_event(:drag_received_for_file_paths, files)
end
end
end

@@ -42,4 +49,11 @@ def drawRect(frame)
NSBezierPath.strokeRect(frame)
end
end

def send_delegate_event(name, arg)
return if self.delegate.nil?
return unless self.delegate.respond_to?(name.to_sym)

self.delegate.send(name, arg)
end
end

0 comments on commit aafbd68

Please sign in to comment.
You can’t perform that action at this time.