Skip to content

Commit f5dcd2a

Browse files
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: https://github.com/chromium/chromium/blob/61a391b86bd946d6e1105412539e77ba9fb2a6b3/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>
1 parent 77f1c73 commit f5dcd2a

File tree

11 files changed

+327
-121
lines changed

11 files changed

+327
-121
lines changed

examples/dnd.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::error::Error;
2+
3+
use winit::application::ApplicationHandler;
4+
use winit::event::WindowEvent;
5+
use winit::event_loop::{ActiveEventLoop, EventLoop};
6+
use winit::window::{Window, WindowAttributes, WindowId};
7+
8+
#[path = "util/fill.rs"]
9+
mod fill;
10+
#[path = "util/tracing.rs"]
11+
mod tracing;
12+
13+
fn main() -> Result<(), Box<dyn Error>> {
14+
tracing::init();
15+
16+
let event_loop = EventLoop::new()?;
17+
18+
let app = Application::new();
19+
Ok(event_loop.run_app(app)?)
20+
}
21+
22+
/// Application state and event handling.
23+
struct Application {
24+
window: Option<Box<dyn Window>>,
25+
}
26+
27+
impl Application {
28+
fn new() -> Self {
29+
Self { window: None }
30+
}
31+
}
32+
33+
impl ApplicationHandler for Application {
34+
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
35+
let window_attributes =
36+
WindowAttributes::default().with_title("Drag and drop files on me!");
37+
self.window = Some(event_loop.create_window(window_attributes).unwrap());
38+
}
39+
40+
fn window_event(
41+
&mut self,
42+
event_loop: &dyn ActiveEventLoop,
43+
_window_id: WindowId,
44+
event: WindowEvent,
45+
) {
46+
match event {
47+
WindowEvent::DragLeft { .. }
48+
| WindowEvent::DragEntered { .. }
49+
| WindowEvent::DragMoved { .. }
50+
| WindowEvent::DragDropped { .. } => {
51+
println!("{:?}", event);
52+
},
53+
WindowEvent::RedrawRequested => {
54+
let window = self.window.as_ref().unwrap();
55+
window.pre_present_notify();
56+
fill::fill_window(window.as_ref());
57+
},
58+
WindowEvent::CloseRequested => {
59+
event_loop.exit();
60+
},
61+
_ => {},
62+
}
63+
}
64+
}

examples/window.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,12 @@ impl ApplicationHandler for Application {
541541
info!("Smart zoom");
542542
},
543543
WindowEvent::TouchpadPressure { .. }
544-
| WindowEvent::HoveredFileCancelled
544+
| WindowEvent::DragLeft { .. }
545545
| WindowEvent::KeyboardInput { .. }
546546
| WindowEvent::PointerEntered { .. }
547-
| WindowEvent::DroppedFile(_)
548-
| WindowEvent::HoveredFile(_)
547+
| WindowEvent::DragEntered { .. }
548+
| WindowEvent::DragMoved { .. }
549+
| WindowEvent::DragDropped { .. }
549550
| WindowEvent::Destroyed
550551
| WindowEvent::Moved(_) => (),
551552
}

src/changelog/unreleased.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ changelog entry.
165165
- Rename `VideoModeHandle` to `VideoMode`, now it only stores plain data.
166166
- Make `Fullscreen::Exclusive` contain `(MonitorHandle, VideoMode)`.
167167
- On Wayland, no longer send an explicit clearing `Ime::Preedit` just prior to a new `Ime::Preedit`.
168+
- Reworked the file drag-and-drop API.
169+
170+
The `WindowEvent::DroppedFile`, `WindowEvent::HoveredFile` and `WindowEvent::HoveredFileCancelled`
171+
events have been removed, and replaced with `WindowEvent::DragEntered`, `WindowEvent::DragMoved`,
172+
`WindowEvent::DragDropped` and `WindowEvent::DragLeft`.
173+
174+
The old drag-and-drop events were emitted once per file. This occurred when files were *first*
175+
hovered over the window, dropped, or left the window. The new drag-and-drop events are emitted
176+
once per set of files dragged, and include a list of all dragged files. They also include the
177+
pointer position.
178+
179+
The rough correspondence is:
180+
- `WindowEvent::HoveredFile` -> `WindowEvent::DragEntered`
181+
- `WindowEvent::DroppedFile` -> `WindowEvent::DragDropped`
182+
- `WindowEvent::HoveredFileCancelled` -> `WindowEvent::DragLeft`
183+
184+
The `WindowEvent::DragMoved` event is entirely new, and is emitted whenever the pointer moves
185+
whilst files are being dragged over the window. It doesn't contain any file paths, just the
186+
pointer position.
168187

169188
### Removed
170189

src/event.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,42 @@ pub enum WindowEvent {
175175
/// The window has been destroyed.
176176
Destroyed,
177177

178-
/// A file is being hovered over the window.
179-
///
180-
/// When the user hovers multiple files at once, this event will be emitted for each file
181-
/// separately.
182-
HoveredFile(PathBuf),
183-
184-
/// A file has been dropped into the window.
185-
///
186-
/// When the user drops multiple files at once, this event will be emitted for each file
187-
/// separately.
188-
///
189-
/// The support for this is known to be incomplete, see [#720] for more
190-
/// information.
191-
///
192-
/// [#720]: https://github.com/rust-windowing/winit/issues/720
193-
DroppedFile(PathBuf),
194-
195-
/// A file was hovered, but has exited the window.
196-
///
197-
/// There will be a single `HoveredFileCancelled` event triggered even if multiple files were
198-
/// hovered.
199-
HoveredFileCancelled,
178+
/// A file drag operation has entered the window.
179+
DragEntered {
180+
/// List of paths that are being dragged onto the window.
181+
paths: Vec<PathBuf>,
182+
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
183+
/// negative on some platforms if something is dragged over a window's decorations (title
184+
/// bar, frame, etc).
185+
position: PhysicalPosition<f64>,
186+
},
187+
/// A file drag operation has moved over the window.
188+
DragMoved {
189+
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
190+
/// negative on some platforms if something is dragged over a window's decorations (title
191+
/// bar, frame, etc).
192+
position: PhysicalPosition<f64>,
193+
},
194+
/// The file drag operation has dropped file(s) on the window.
195+
DragDropped {
196+
/// List of paths that are being dragged onto the window.
197+
paths: Vec<PathBuf>,
198+
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
199+
/// negative on some platforms if something is dragged over a window's decorations (title
200+
/// bar, frame, etc).
201+
position: PhysicalPosition<f64>,
202+
},
203+
/// The file drag operation has been cancelled or left the window.
204+
DragLeft {
205+
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
206+
/// negative on some platforms if something is dragged over a window's decorations (title
207+
/// bar, frame, etc).
208+
///
209+
/// ## Platform-specific
210+
///
211+
/// - **Windows:** Always emits [`None`].
212+
position: Option<PhysicalPosition<f64>>,
213+
},
200214

201215
/// The window gained or lost focus.
202216
///
@@ -1221,9 +1235,16 @@ mod tests {
12211235
with_window_event(Focused(true));
12221236
with_window_event(Moved((0, 0).into()));
12231237
with_window_event(SurfaceResized((0, 0).into()));
1224-
with_window_event(DroppedFile("x.txt".into()));
1225-
with_window_event(HoveredFile("x.txt".into()));
1226-
with_window_event(HoveredFileCancelled);
1238+
with_window_event(DragEntered {
1239+
paths: vec!["x.txt".into()],
1240+
position: (0, 0).into(),
1241+
});
1242+
with_window_event(DragMoved { position: (0, 0).into() });
1243+
with_window_event(DragDropped {
1244+
paths: vec!["x.txt".into()],
1245+
position: (0, 0).into(),
1246+
});
1247+
with_window_event(DragLeft { position: Some((0, 0).into()) });
12271248
with_window_event(Ime(Enabled));
12281249
with_window_event(PointerMoved {
12291250
device_id: None,

src/platform_impl/apple/appkit/window_delegate.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use objc2::{declare_class, msg_send_id, mutability, sel, ClassType, DeclaredClas
1313
use objc2_app_kit::{
1414
NSAppKitVersionNumber, NSAppKitVersionNumber10_12, NSAppearance, NSAppearanceCustomization,
1515
NSAppearanceNameAqua, NSApplication, NSApplicationPresentationOptions, NSBackingStoreType,
16-
NSColor, NSDraggingDestination, NSFilenamesPboardType, NSPasteboard,
16+
NSColor, NSDraggingDestination, NSDraggingInfo, NSFilenamesPboardType,
1717
NSRequestUserAttentionType, NSScreen, NSToolbar, NSView, NSViewFrameDidChangeNotification,
1818
NSWindow, NSWindowButton, NSWindowDelegate, NSWindowFullScreenButton, NSWindowLevel,
1919
NSWindowOcclusionState, NSWindowOrderingMode, NSWindowSharingType, NSWindowStyleMask,
@@ -375,19 +375,45 @@ declare_class!(
375375
unsafe impl NSDraggingDestination for WindowDelegate {
376376
/// Invoked when the dragged image enters destination bounds or frame
377377
#[method(draggingEntered:)]
378-
fn dragging_entered(&self, sender: &NSObject) -> bool {
378+
fn dragging_entered(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
379379
trace_scope!("draggingEntered:");
380380

381381
use std::path::PathBuf;
382382

383-
let pb: Retained<NSPasteboard> = unsafe { msg_send_id![sender, draggingPasteboard] };
383+
let pb = unsafe { sender.draggingPasteboard() };
384384
let filenames = pb.propertyListForType(unsafe { NSFilenamesPboardType }).unwrap();
385385
let filenames: Retained<NSArray<NSString>> = unsafe { Retained::cast(filenames) };
386+
let paths = filenames
387+
.into_iter()
388+
.map(|file| PathBuf::from(file.to_string()))
389+
.collect();
386390

387-
filenames.into_iter().for_each(|file| {
388-
let path = PathBuf::from(file.to_string());
389-
self.queue_event(WindowEvent::HoveredFile(path));
390-
});
391+
let dl = unsafe { sender.draggingLocation() };
392+
let dl = self.view().convertPoint_fromView(dl, None);
393+
let position = LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor());
394+
395+
396+
self.queue_event(WindowEvent::DragEntered { paths, position });
397+
398+
true
399+
}
400+
401+
#[method(wantsPeriodicDraggingUpdates)]
402+
fn wants_periodic_dragging_updates(&self) -> bool {
403+
trace_scope!("wantsPeriodicDraggingUpdates:");
404+
true
405+
}
406+
407+
/// Invoked periodically as the image is held within the destination area, allowing modification of the dragging operation or mouse-pointer position.
408+
#[method(draggingUpdated:)]
409+
fn dragging_updated(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
410+
trace_scope!("draggingUpdated:");
411+
412+
let dl = unsafe { sender.draggingLocation() };
413+
let dl = self.view().convertPoint_fromView(dl, None);
414+
let position = LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor());
415+
416+
self.queue_event(WindowEvent::DragMoved { position });
391417

392418
true
393419
}
@@ -401,19 +427,24 @@ declare_class!(
401427

402428
/// Invoked after the released image has been removed from the screen
403429
#[method(performDragOperation:)]
404-
fn perform_drag_operation(&self, sender: &NSObject) -> bool {
430+
fn perform_drag_operation(&self, sender: &ProtocolObject<dyn NSDraggingInfo>) -> bool {
405431
trace_scope!("performDragOperation:");
406432

407433
use std::path::PathBuf;
408434

409-
let pb: Retained<NSPasteboard> = unsafe { msg_send_id![sender, draggingPasteboard] };
435+
let pb = unsafe { sender.draggingPasteboard() };
410436
let filenames = pb.propertyListForType(unsafe { NSFilenamesPboardType }).unwrap();
411437
let filenames: Retained<NSArray<NSString>> = unsafe { Retained::cast(filenames) };
438+
let paths = filenames
439+
.into_iter()
440+
.map(|file| PathBuf::from(file.to_string()))
441+
.collect();
412442

413-
filenames.into_iter().for_each(|file| {
414-
let path = PathBuf::from(file.to_string());
415-
self.queue_event(WindowEvent::DroppedFile(path));
416-
});
443+
let dl = unsafe { sender.draggingLocation() };
444+
let dl = self.view().convertPoint_fromView(dl, None);
445+
let position = LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor());
446+
447+
self.queue_event(WindowEvent::DragDropped { paths, position });
417448

418449
true
419450
}
@@ -426,9 +457,16 @@ declare_class!(
426457

427458
/// Invoked when the dragging operation is cancelled
428459
#[method(draggingExited:)]
429-
fn dragging_exited(&self, _sender: Option<&NSObject>) {
460+
fn dragging_exited(&self, info: Option<&ProtocolObject<dyn NSDraggingInfo>>) {
430461
trace_scope!("draggingExited:");
431-
self.queue_event(WindowEvent::HoveredFileCancelled);
462+
463+
let position = info.map(|info| {
464+
let dl = unsafe { info.draggingLocation() };
465+
let dl = self.view().convertPoint_fromView(dl, None);
466+
LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor())
467+
});
468+
469+
self.queue_event(WindowEvent::DragLeft { position } );
432470
}
433471
}
434472

src/platform_impl/linux/x11/dnd.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
44
use std::str::Utf8Error;
55
use std::sync::Arc;
66

7+
use dpi::PhysicalPosition;
78
use percent_encoding::percent_decode;
89
use x11rb::protocol::xproto::{self, ConnectionExt};
910

@@ -45,20 +46,33 @@ pub struct Dnd {
4546
pub type_list: Option<Vec<xproto::Atom>>,
4647
// Populated by XdndPosition event handler
4748
pub source_window: Option<xproto::Window>,
49+
// Populated by XdndPosition event handler
50+
pub position: PhysicalPosition<f64>,
4851
// Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
4952
pub result: Option<Result<Vec<PathBuf>, DndDataParseError>>,
53+
// Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
54+
pub dragging: bool,
5055
}
5156

5257
impl Dnd {
5358
pub fn new(xconn: Arc<XConnection>) -> Result<Self, X11Error> {
54-
Ok(Dnd { xconn, version: None, type_list: None, source_window: None, result: None })
59+
Ok(Dnd {
60+
xconn,
61+
version: None,
62+
type_list: None,
63+
source_window: None,
64+
position: PhysicalPosition::default(),
65+
result: None,
66+
dragging: false,
67+
})
5568
}
5669

5770
pub fn reset(&mut self) {
5871
self.version = None;
5972
self.type_list = None;
6073
self.source_window = None;
6174
self.result = None;
75+
self.dragging = false;
6276
}
6377

6478
pub unsafe fn send_status(

0 commit comments

Comments
 (0)