Mac module emits the winit events DroppedFile and HoveredFile.

This commit is contained in:
Rob Saunders 2017-07-27 10:51:00 +08:00
parent 4ef7c71c66
commit 5f00028f6b
2 changed files with 38 additions and 12 deletions

View file

@ -26,6 +26,9 @@ pub enum WindowEvent {
/// The window has been closed. /// The window has been closed.
Closed, Closed,
/// A file is being hovered over the window.
HoveredFile(PathBuf),
/// A file has been dropped into the window. /// A file has been dropped into the window.
DroppedFile(PathBuf), DroppedFile(PathBuf),

View file

@ -117,8 +117,29 @@ impl WindowDelegate {
} }
/// Invoked when the dragged image enters destination bounds or frame /// Invoked when the dragged image enters destination bounds or frame
extern fn dragging_entered(_: &Object, _: Sel, _: id) { extern fn dragging_entered(this: &Object, _: Sel, sender: id) -> BOOL {
println!("dragging_entered"); use cocoa::appkit::NSPasteboard;
use cocoa::foundation::NSFastEnumeration;
use std::path::PathBuf;
let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
for file in unsafe { filenames.iter() } {
use cocoa::foundation::NSString;
use std::ffi::CStr;
unsafe {
let f = NSString::UTF8String(file);
let path = CStr::from_ptr(f).to_string_lossy().into_owned();
let state: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state as *mut DelegateState);
emit_event(state, WindowEvent::HoveredFile(PathBuf::from(path)));
}
};
YES
} }
/// Invoked when the image is released /// Invoked when the image is released
@ -127,27 +148,29 @@ impl WindowDelegate {
} }
/// Invoked after the released image has been removed from the screen /// Invoked after the released image has been removed from the screen
extern fn perform_drag_operation(_: &Object, _: Sel, sender: id) -> BOOL { extern fn perform_drag_operation(this: &Object, _: Sel, sender: id) -> BOOL {
use cocoa::appkit::NSPasteboard; use cocoa::appkit::NSPasteboard;
use cocoa::foundation::NSFastEnumeration;
use std::path::PathBuf;
let pb: id = unsafe { msg_send![sender, draggingPasteboard] }; let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) }; let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
use cocoa::foundation::NSFastEnumeration;
for file in unsafe { filenames.iter() } { for file in unsafe { filenames.iter() } {
use cocoa::foundation::NSString; use cocoa::foundation::NSString;
unsafe { use std::ffi::CStr;
use std::ffi::CStr;
let f = NSString::UTF8String(file);
let str = CStr::from_ptr(f).to_string_lossy().into_owned();
println!("file: {:?}", str); unsafe {
let f = NSString::UTF8String(file);
let path = CStr::from_ptr(f).to_string_lossy().into_owned();
let state: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state as *mut DelegateState);
emit_event(state, WindowEvent::DroppedFile(PathBuf::from(path)));
} }
}; };
YES YES
} }
/// Invoked when the dragging operation is complete /// Invoked when the dragging operation is complete
@ -183,7 +206,7 @@ impl WindowDelegate {
// callback for drag events // callback for drag events
decl.add_method(sel!(draggingEntered:), decl.add_method(sel!(draggingEntered:),
dragging_entered as extern fn(&Object, Sel, id)); dragging_entered as extern fn(&Object, Sel, id) -> BOOL);
decl.add_method(sel!(prepareForDragOperation:), decl.add_method(sel!(prepareForDragOperation:),
prepare_for_drag_operation as extern fn(&Object, Sel, id)); prepare_for_drag_operation as extern fn(&Object, Sel, id));
decl.add_method(sel!(performDragOperation:), decl.add_method(sel!(performDragOperation:),