Skip to content

Commit

Permalink
refactor: reword drag events to match pointer ones
Browse files Browse the repository at this point in the history
  • Loading branch information
valadaptive committed Jan 10, 2025
1 parent 8a1c65e commit 242c22f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
6 changes: 3 additions & 3 deletions examples/dnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl ApplicationHandler for Application {
event: WindowEvent,
) {
match event {
WindowEvent::DragLeave
| WindowEvent::DragEnter { .. }
| WindowEvent::DragOver { .. }
WindowEvent::DragLeft
| WindowEvent::DragEntered { .. }
| WindowEvent::DragMoved { .. }
| WindowEvent::DragDrop { .. } => {
println!("{:?}", event);
},
Expand Down
6 changes: 3 additions & 3 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,11 @@ impl ApplicationHandler for Application {
info!("Smart zoom");
},
WindowEvent::TouchpadPressure { .. }
| WindowEvent::DragLeave
| WindowEvent::DragLeft
| WindowEvent::KeyboardInput { .. }
| WindowEvent::PointerEntered { .. }
| WindowEvent::DragEnter { .. }
| WindowEvent::DragOver { .. }
| WindowEvent::DragEntered { .. }
| WindowEvent::DragMoved { .. }
| WindowEvent::DragDrop { .. }
| WindowEvent::Destroyed
| WindowEvent::Moved(_) => (),
Expand Down
10 changes: 5 additions & 5 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@ changelog entry.
- Reworked the file drag-and-drop API.

The `WindowEvent::DroppedFile`, `WindowEvent::HoveredFile` and `WindowEvent::HoveredFileCancelled`
events have been removed, and replaced with `WindowEvent::DragEnter`, `WindowEvent::DragOver`,
`WindowEvent::DragDrop` and `WindowEvent::DragLeave`.
events have been removed, and replaced with `WindowEvent::DragEntered`, `WindowEvent::DragMoved`,
`WindowEvent::DragDrop` and `WindowEvent::DragLeft`.

The old drag-and-drop events were emitted once per file. This occurred when files were *first*
hovered over the window, dropped, or left the window. The new drag-and-drop events are emitted
once per set of files dragged, and include a list of all dragged files. They also include the
pointer position.

The rough correspondence is:
- `WindowEvent::HoveredFile` -> `WindowEvent::DragEnter`
- `WindowEvent::HoveredFile` -> `WindowEvent::DragEntered`
- `WindowEvent::DroppedFile` -> `WindowEvent::DragDrop`
- `WindowEvent::HoveredFileCancelled` -> `WindowEvent::DragLeave`
- `WindowEvent::HoveredFileCancelled` -> `WindowEvent::DragLeft`

The `WindowEvent::DragOver` event is entirely new, and is emitted whenever the pointer moves
The `WindowEvent::DragMoved` event is entirely new, and is emitted whenever the pointer moves
whilst files are being dragged over the window. It doesn't contain any file paths, just the
pointer position.

Expand Down
29 changes: 16 additions & 13 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,33 @@ pub enum WindowEvent {
Destroyed,

/// A drag operation has entered the window.
DragEnter {
DragEntered {
/// List of paths that are being dragged onto the window.
paths: Vec<PathBuf>,
/// Position of the drag operation. May be negative on some platforms if something is
/// dragged over a window's decorations (title bar, frame, etc).
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
/// negative on some platforms if something is dragged over a window's decorations (title
/// bar, frame, etc).
position: PhysicalPosition<f64>,
},
/// A drag operation is moving over the window.
DragOver {
/// Position of the drag operation. May be negative on some platforms if something is
/// dragged over a window's decorations (title bar, frame, etc).
/// A drag operation has moved over the window.
DragMoved {
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
/// negative on some platforms if something is dragged over a window's decorations (title
/// bar, frame, etc).
position: PhysicalPosition<f64>,
},
/// The drag operation has dropped file(s) on the window.
DragDrop {
/// List of paths that are being dragged onto the window.
paths: Vec<PathBuf>,
/// Position of the drag operation. May be negative on some platforms if something is
/// dragged over a window's decorations (title bar, frame, etc).
/// (x,y) coordinates in pixels relative to the top-left corner of the window. May be
/// negative on some platforms if something is dragged over a window's decorations (title
/// bar, frame, etc).
position: PhysicalPosition<f64>,
},
/// The drag operation has been cancelled or left the window. On some platforms, this may occur
/// even if no other drag events have occurred.
DragLeave,
DragLeft,

/// The window gained or lost focus.
///
Expand Down Expand Up @@ -1224,16 +1227,16 @@ mod tests {
with_window_event(Focused(true));
with_window_event(Moved((0, 0).into()));
with_window_event(SurfaceResized((0, 0).into()));
with_window_event(DragEnter {
with_window_event(DragEntered {
paths: vec!["x.txt".into()],
position: (0, 0).into(),
});
with_window_event(DragOver { position: (0, 0).into() });
with_window_event(DragMoved { position: (0, 0).into() });
with_window_event(DragDrop {
paths: vec!["x.txt".into()],
position: (0, 0).into(),
});
with_window_event(DragLeave);
with_window_event(DragLeft);
with_window_event(Ime(Enabled));
with_window_event(PointerMoved {
device_id: None,
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/apple/appkit/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ declare_class!(
let position = LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor());


self.queue_event(WindowEvent::DragEnter { paths, position });
self.queue_event(WindowEvent::DragEntered { paths, position });

true
}
Expand All @@ -413,7 +413,7 @@ declare_class!(
let dl = self.view().convertPoint_fromView(dl, None);
let position = LogicalPosition::<f64>::from((dl.x, dl.y)).to_physical(self.scale_factor());

self.queue_event(WindowEvent::DragOver { position });
self.queue_event(WindowEvent::DragMoved { position });

true
}
Expand Down Expand Up @@ -459,7 +459,7 @@ declare_class!(
#[method(draggingExited:)]
fn dragging_exited(&self, _sender: Option<&NSObject>) {
trace_scope!("draggingExited:");
self.queue_event(WindowEvent::DragLeave);
self.queue_event(WindowEvent::DragLeft);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ impl EventProcessor {

if xev.message_type == atoms[XdndLeave] as c_ulong {
if self.dnd.has_entered {
let event = Event::WindowEvent { window_id, event: WindowEvent::DragLeave };
let event = Event::WindowEvent { window_id, event: WindowEvent::DragLeft };
callback(&self.target, event);
}
self.dnd.reset();
Expand Down Expand Up @@ -593,14 +593,14 @@ impl EventProcessor {
if self.dnd.has_entered {
callback(&self.target, Event::WindowEvent {
window_id,
event: WindowEvent::DragOver { position: self.dnd.position },
event: WindowEvent::DragMoved { position: self.dnd.position },
});
} else {
let paths = path_list.iter().map(Into::into).collect();
self.dnd.has_entered = true;
callback(&self.target, Event::WindowEvent {
window_id,
event: WindowEvent::DragEnter { paths, position: self.dnd.position },
event: WindowEvent::DragEntered { paths, position: self.dnd.position },
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/platform_impl/windows/drop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct FileDropHandlerData {
send_event: Box<dyn Fn(Event)>,
cursor_effect: u32,
enter_is_valid: bool, /* If the currently hovered item is not valid there must not be any
* `DragLeave` emitted */
* `DragLeft` emitted */
}

pub struct FileDropHandler {
Expand Down Expand Up @@ -82,7 +82,7 @@ impl FileDropHandler {
pt: POINTL,
pdwEffect: *mut u32,
) -> HRESULT {
use crate::event::WindowEvent::DragEnter;
use crate::event::WindowEvent::DragEntered;
let drop_handler = unsafe { Self::from_interface(this) };
let mut pt = POINT { x: pt.x, y: pt.y };
unsafe {
Expand All @@ -95,7 +95,7 @@ impl FileDropHandler {
if drop_handler.enter_is_valid {
drop_handler.send_event(Event::WindowEvent {
window_id: WindowId::from_raw(drop_handler.window as usize),
event: DragEnter { paths, position },
event: DragEntered { paths, position },
});
}
drop_handler.cursor_effect =
Expand All @@ -113,7 +113,7 @@ impl FileDropHandler {
pt: POINTL,
pdwEffect: *mut u32,
) -> HRESULT {
use crate::event::WindowEvent::DragOver;
use crate::event::WindowEvent::DragMoved;
let drop_handler = unsafe { Self::from_interface(this) };
if drop_handler.enter_is_valid {
let mut pt = POINT { x: pt.x, y: pt.y };
Expand All @@ -123,7 +123,7 @@ impl FileDropHandler {
let position = PhysicalPosition::new(pt.x as f64, pt.y as f64);
drop_handler.send_event(Event::WindowEvent {
window_id: WindowId::from_raw(drop_handler.window as usize),
event: DragOver { position },
event: DragMoved { position },
});
}
unsafe {
Expand All @@ -133,12 +133,12 @@ impl FileDropHandler {
}

pub unsafe extern "system" fn DragLeave(this: *mut IDropTarget) -> HRESULT {
use crate::event::WindowEvent::DragLeave;
use crate::event::WindowEvent::DragLeft;
let drop_handler = unsafe { Self::from_interface(this) };
if drop_handler.enter_is_valid {
drop_handler.send_event(Event::WindowEvent {
window_id: WindowId::from_raw(drop_handler.window as usize),
event: DragLeave,
event: DragLeft,
});
}

Expand Down

0 comments on commit 242c22f

Please sign in to comment.