Rework Drag-And-Drop API (#4079)

* Add cursor position drag and drop events.
* Reword drag events to match pointer ones.
* appkit: Use `convertPoint_fromView` for coordinate conversion.
* appkit: use ProtocolObject<dyn NSDraggingInfo>.
* x11: store dnd.position as pair of i16

  It's what translate_coords takes anyway, so the extra precision is
  misleading if we're going to cast it to i16 everywhere it's used.

  We can also simplify the "unpacking" from the XdndPosition message--we
  can and should use the value of 16 as the shift instead of
  size_of::<c_short> * 2 or something like that, because the specification
  gives us the constant 16.
* x11: store translated DnD coords.
* x11: don't emit DragLeave without DragEnter.
* windows: only emit DragEnter if valid.
* windows: in DnD, always set pdwEffect.

  It appears other apps (like Chromium) set pdwEffect on Drop too:
  61a391b86b/ui/base/dragdrop/drop_target_win.cc
* docs: make it clearer that drag events are for dragged *files*.
* examples/dnd: handle RedrawRequested event.

Co-authored-by: amrbashir <amr.bashir2015@gmail.com>
This commit is contained in:
valadaptive 2025-01-28 15:10:40 -05:00 committed by GitHub
parent 77f1c73f06
commit f5dcd2aabe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 327 additions and 121 deletions

View file

@ -470,14 +470,19 @@ impl EventProcessor {
let source_window = xev.data.get_long(0) as xproto::Window;
// Equivalent to `(x << shift) | y`
// where `shift = mem::size_of::<c_short>() * 8`
// https://www.freedesktop.org/wiki/Specifications/XDND/#xdndposition
// Note that coordinates are in "desktop space", not "window space"
// (in X11 parlance, they're root window coordinates)
// let packed_coordinates = xev.data.get_long(2);
// let shift = mem::size_of::<libc::c_short>() * 8;
// let x = packed_coordinates >> shift;
// let y = packed_coordinates & !(x << shift);
let packed_coordinates = xev.data.get_long(2);
let x = (packed_coordinates >> 16) as i16;
let y = (packed_coordinates & 0xffff) as i16;
let coords = self
.target
.xconn
.translate_coords(self.target.root, window, x, y)
.expect("Failed to translate window coordinates");
self.dnd.position = PhysicalPosition::new(coords.dst_x as f64, coords.dst_y as f64);
// By our own state flow, `version` should never be `None` at this point.
let version = self.dnd.version.unwrap_or(5);
@ -502,21 +507,19 @@ impl EventProcessor {
}
self.dnd.source_window = Some(source_window);
if self.dnd.result.is_none() {
let time = if version >= 1 {
xev.data.get_long(3) as xproto::Timestamp
} else {
// In version 0, time isn't specified
x11rb::CURRENT_TIME
};
let time = if version == 0 {
// In version 0, time isn't specified
x11rb::CURRENT_TIME
} else {
xev.data.get_long(3) as xproto::Timestamp
};
// Log this timestamp.
self.target.xconn.set_timestamp(time);
// Log this timestamp.
self.target.xconn.set_timestamp(time);
// This results in the `SelectionNotify` event below
unsafe {
self.dnd.convert_selection(window, time);
}
// This results in the `SelectionNotify` event below
unsafe {
self.dnd.convert_selection(window, time);
}
unsafe {
@ -530,13 +533,12 @@ impl EventProcessor {
if xev.message_type == atoms[XdndDrop] as c_ulong {
let (source_window, state) = if let Some(source_window) = self.dnd.source_window {
if let Some(Ok(ref path_list)) = self.dnd.result {
for path in path_list {
let event = Event::WindowEvent {
window_id,
event: WindowEvent::DroppedFile(path.clone()),
};
callback(&self.target, event);
}
let event = WindowEvent::DragDropped {
paths: path_list.iter().map(Into::into).collect(),
position: self.dnd.position,
};
callback(&self.target, Event::WindowEvent { window_id, event });
}
(source_window, DndState::Accepted)
} else {
@ -557,9 +559,14 @@ impl EventProcessor {
}
if xev.message_type == atoms[XdndLeave] as c_ulong {
if self.dnd.dragging {
let event = Event::WindowEvent {
window_id,
event: WindowEvent::DragLeft { position: Some(self.dnd.position) },
};
callback(&self.target, event);
}
self.dnd.reset();
let event = Event::WindowEvent { window_id, event: WindowEvent::HoveredFileCancelled };
callback(&self.target, event);
}
}
@ -583,15 +590,19 @@ impl EventProcessor {
self.dnd.result = None;
if let Ok(mut data) = unsafe { self.dnd.read_data(window) } {
let parse_result = self.dnd.parse_data(&mut data);
if let Ok(ref path_list) = parse_result {
for path in path_list {
let event = Event::WindowEvent {
window_id,
event: WindowEvent::HoveredFile(path.clone()),
};
callback(&self.target, event);
}
let event = if self.dnd.dragging {
WindowEvent::DragMoved { position: self.dnd.position }
} else {
let paths = path_list.iter().map(Into::into).collect();
self.dnd.dragging = true;
WindowEvent::DragEntered { paths, position: self.dnd.position }
};
callback(&self.target, Event::WindowEvent { window_id, event });
}
self.dnd.result = Some(parse_result);
}
}