Initial drag and drop support for Mac OS, printing filenames to stdout.

This commit is contained in:
Rob Saunders 2017-07-27 00:59:42 +08:00
parent 3b7dbd01d0
commit 4ef7c71c66

View file

@ -116,6 +116,50 @@ impl WindowDelegate {
}
}
/// Invoked when the dragged image enters destination bounds or frame
extern fn dragging_entered(_: &Object, _: Sel, _: id) {
println!("dragging_entered");
}
/// Invoked when the image is released
extern fn prepare_for_drag_operation(_: &Object, _: Sel, _: id) {
println!("prepare_for_drag_operation");
}
/// Invoked after the released image has been removed from the screen
extern fn perform_drag_operation(_: &Object, _: Sel, sender: id) -> BOOL {
use cocoa::appkit::NSPasteboard;
let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
use cocoa::foundation::NSFastEnumeration;
for file in unsafe { filenames.iter() } {
use cocoa::foundation::NSString;
unsafe {
use std::ffi::CStr;
let f = NSString::UTF8String(file);
let str = CStr::from_ptr(f).to_string_lossy().into_owned();
println!("file: {:?}", str);
}
};
YES
}
/// Invoked when the dragging operation is complete
extern fn conclude_drag_operation(_: &Object, _: Sel, _: id) {
println!("conclude_drag_operation");
}
/// Invoked when the dragging operation is cancelled
extern fn dragging_exited(_: &Object, _: Sel, _: id) {
println!("dragging_exited");
}
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
static INIT: std::sync::Once = std::sync::ONCE_INIT;
@ -137,6 +181,18 @@ impl WindowDelegate {
decl.add_method(sel!(windowDidResignKey:),
window_did_resign_key as extern fn(&Object, Sel, id));
// callback for drag events
decl.add_method(sel!(draggingEntered:),
dragging_entered as extern fn(&Object, Sel, id));
decl.add_method(sel!(prepareForDragOperation:),
prepare_for_drag_operation as extern fn(&Object, Sel, id));
decl.add_method(sel!(performDragOperation:),
perform_drag_operation as extern fn(&Object, Sel, id) -> BOOL);
decl.add_method(sel!(concludeDragOperation:),
conclude_drag_operation as extern fn(&Object, Sel, id));
decl.add_method(sel!(draggingExited:),
dragging_exited as extern fn(&Object, Sel, id));
// Store internal state as user data
decl.add_ivar::<*mut c_void>("winitState");
@ -263,6 +319,11 @@ impl Window {
if let Some((width, height)) = win_attribs.max_dimensions {
nswindow_set_max_dimensions(window.0, width.into(), height.into());
}
use cocoa::foundation::NSArray;
// register for drag and drop operations.
msg_send![(*window as id),
registerForDraggedTypes:NSArray::arrayWithObject(nil, appkit::NSFilenamesPboardType)];
}
let ds = DelegateState {