1
0
Fork 0

Add coordinates to drag events

This commit is contained in:
Jussi Viiri 2023-06-05 20:48:06 +03:00
parent 1c50c326cb
commit 1a8484bc55
2 changed files with 31 additions and 4 deletions

View file

@ -80,18 +80,22 @@ pub enum MouseEvent {
// TODO: Document // TODO: Document
DragEntered { DragEntered {
coordinates: (i32, i32),
data: DropData, data: DropData,
}, },
DragMoved { DragMoved {
coordinates: (i32, i32),
data: DropData, data: DropData,
}, },
DragLeft { DragLeft {
coordinates: (i32, i32),
data: DropData, data: DropData,
}, },
DragDropped { DragDropped {
coordinates: (i32, i32),
data: DropData, data: DropData,
}, },
} }

View file

@ -801,9 +801,9 @@ pub struct DropTarget {
window_state: *mut WindowState, window_state: *mut WindowState,
// Drop data is cached since DragOver callbacks don't provide drop data // These are cached since DragOver and DragLeave callbacks don't provide them,
// and handling drag move events gets awkward without having access to // and handling drag move events gets awkward on the client end otherwise
// drop data drag_coordinates: (i32, i32),
drop_data: DropData, drop_data: DropData,
} }
@ -827,6 +827,7 @@ impl DropTarget {
window_state, window_state,
drag_coordinates: (0, 0),
drop_data: DropData::None, drop_data: DropData::None,
} }
} }
@ -852,6 +853,18 @@ impl DropTarget {
} }
} }
fn parse_coordinates(&mut self, pt: *const POINTL) {
// There's a bug in winapi: the POINTL pointer should actually be a POINTL structure
// This happens to work on 64-bit platforms because two c_longs (that translate to
// 32-bit signed integers) happen to be the same size as a 64-bit pointer...
// For now, just hack around that bug
let x = pt as i64 & u32::MAX as i64;
let y = pt as i64 >> 32;
self.drag_coordinates = (x as i32, y as i32)
}
fn parse_drop_data(&mut self, data_object: &IDataObject) { fn parse_drop_data(&mut self, data_object: &IDataObject) {
let format = FORMATETC { let format = FORMATETC {
cfFormat: CF_HDROP as u16, cfFormat: CF_HDROP as u16,
@ -944,8 +957,11 @@ impl DropTarget {
{ {
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
drop_target.parse_coordinates(pt);
drop_target.parse_drop_data(&*pDataObj); drop_target.parse_drop_data(&*pDataObj);
let event = MouseEvent::DragEntered { let event = MouseEvent::DragEntered {
coordinates: drop_target.drag_coordinates,
data: drop_target.drop_data.clone(), data: drop_target.drop_data.clone(),
}; };
@ -962,7 +978,10 @@ impl DropTarget {
{ {
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
drop_target.parse_coordinates(pt);
let event = MouseEvent::DragMoved { let event = MouseEvent::DragMoved {
coordinates: drop_target.drag_coordinates,
data: drop_target.drop_data.clone(), data: drop_target.drop_data.clone(),
}; };
@ -974,6 +993,7 @@ impl DropTarget {
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
let event = MouseEvent::DragLeft { let event = MouseEvent::DragLeft {
coordinates: drop_target.drag_coordinates,
data: drop_target.drop_data.clone(), data: drop_target.drop_data.clone(),
}; };
@ -991,8 +1011,11 @@ impl DropTarget {
{ {
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
drop_target.parse_coordinates(pt);
drop_target.parse_drop_data(&*pDataObj); drop_target.parse_drop_data(&*pDataObj);
let event = MouseEvent::DragDropped { let event = MouseEvent::DragDropped {
coordinates: drop_target.drag_coordinates,
data: drop_target.drop_data.clone(), data: drop_target.drop_data.clone(),
}; };