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:
parent
77f1c73f06
commit
f5dcd2aabe
11 changed files with 327 additions and 121 deletions
|
|
@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
|
|||
use std::str::Utf8Error;
|
||||
use std::sync::Arc;
|
||||
|
||||
use dpi::PhysicalPosition;
|
||||
use percent_encoding::percent_decode;
|
||||
use x11rb::protocol::xproto::{self, ConnectionExt};
|
||||
|
||||
|
|
@ -45,13 +46,25 @@ pub struct Dnd {
|
|||
pub type_list: Option<Vec<xproto::Atom>>,
|
||||
// Populated by XdndPosition event handler
|
||||
pub source_window: Option<xproto::Window>,
|
||||
// Populated by XdndPosition event handler
|
||||
pub position: PhysicalPosition<f64>,
|
||||
// Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
|
||||
pub result: Option<Result<Vec<PathBuf>, DndDataParseError>>,
|
||||
// Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
|
||||
pub dragging: bool,
|
||||
}
|
||||
|
||||
impl Dnd {
|
||||
pub fn new(xconn: Arc<XConnection>) -> Result<Self, X11Error> {
|
||||
Ok(Dnd { xconn, version: None, type_list: None, source_window: None, result: None })
|
||||
Ok(Dnd {
|
||||
xconn,
|
||||
version: None,
|
||||
type_list: None,
|
||||
source_window: None,
|
||||
position: PhysicalPosition::default(),
|
||||
result: None,
|
||||
dragging: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
|
|
@ -59,6 +72,7 @@ impl Dnd {
|
|||
self.type_list = None;
|
||||
self.source_window = None;
|
||||
self.result = None;
|
||||
self.dragging = false;
|
||||
}
|
||||
|
||||
pub unsafe fn send_status(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ impl FrameExtentsHeuristic {
|
|||
|
||||
impl XConnection {
|
||||
// This is adequate for inner_position
|
||||
pub fn translate_coords(
|
||||
pub fn translate_coords_root(
|
||||
&self,
|
||||
window: xproto::Window,
|
||||
root: xproto::Window,
|
||||
|
|
@ -103,6 +103,19 @@ impl XConnection {
|
|||
self.xcb_connection().translate_coordinates(window, root, 0, 0)?.reply().map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn translate_coords(
|
||||
&self,
|
||||
src_w: xproto::Window,
|
||||
dst_w: xproto::Window,
|
||||
src_x: i16,
|
||||
src_y: i16,
|
||||
) -> Result<xproto::TranslateCoordinatesReply, X11Error> {
|
||||
self.xcb_connection()
|
||||
.translate_coordinates(src_w, dst_w, src_x, src_y)?
|
||||
.reply()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
// This is adequate for surface_size
|
||||
pub fn get_geometry(
|
||||
&self,
|
||||
|
|
@ -189,7 +202,7 @@ impl XConnection {
|
|||
// that, fullscreen windows often aren't nested.
|
||||
let (inner_y_rel_root, child) = {
|
||||
let coords = self
|
||||
.translate_coords(window, root)
|
||||
.translate_coords_root(window, root)
|
||||
.expect("Failed to translate window coordinates");
|
||||
(coords.dst_y, coords.child)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1520,7 +1520,7 @@ impl UnownedWindow {
|
|||
// This should be okay to unwrap since the only error XTranslateCoordinates can return
|
||||
// is BadWindow, and if the window handle is bad we have bigger problems.
|
||||
self.xconn
|
||||
.translate_coords(self.xwindow, self.root)
|
||||
.translate_coords_root(self.xwindow, self.root)
|
||||
.map(|coords| (coords.dst_x.into(), coords.dst_y.into()))
|
||||
.unwrap()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue