Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support WindowEvent::DroppedFile #524

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/dropped_file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dropped_file"
version = "0.1.0"
edition = "2021"

[dependencies]
floem = { path = "../.." }
34 changes: 34 additions & 0 deletions examples/dropped_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use floem::{
event::EventListener,
keyboard::{Key, Modifiers, NamedKey},
unit::UnitExt,
views::{dyn_view, Decorators},
IntoView, View,
};

fn app_view() -> impl IntoView {
let view = dyn_view(move || format!("dropped file")).style(|s| {
s.size(100.pct(), 100.pct())
.flex_col()
.items_center()
.justify_center()
});

let id = view.id();
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
.on_event_stop(EventListener::PointerMove, |x| {
println!("PointerMove {:?}", x.point());
})
.on_event_stop(EventListener::DroppedFile, |x| {
println!("DroppedFile {:?}", x);
})
.on_event_stop(EventListener::DroppedFileWithPosition, |x| {
println!("DroppedFileWithPosition {:?}", x.point());
})
}

fn main() {
floem::launch(app_view);
}
4 changes: 3 additions & 1 deletion src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ impl ApplicationHandle {
WindowEvent::Destroyed => {
self.close_window(window_id, event_loop);
}
WindowEvent::DroppedFile(_) => {}
WindowEvent::DroppedFile(path) => {
window_handle.dropped_file(path);
}
WindowEvent::HoveredFile(_) => {}
WindowEvent::HoveredFileCancelled => {}
WindowEvent::Focused(focused) => {
Expand Down
13 changes: 13 additions & 0 deletions src/dropped_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use peniko::kurbo::Point;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct DroppedFileWithPositionEvent {
pub path: PathBuf,
pub pos: Point,
}

#[derive(Debug, Clone)]
pub struct DroppedFileEvent {
pub path: PathBuf,
}
38 changes: 32 additions & 6 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use floem_winit::{
use peniko::kurbo::{Point, Size};

use crate::{
dropped_file::{DroppedFileEvent, DroppedFileWithPositionEvent},
keyboard::KeyEvent,
pointer::{PointerInputEvent, PointerMoveEvent, PointerWheelEvent},
};
Expand Down Expand Up @@ -94,6 +95,10 @@ pub enum EventListener {
WindowLostFocus,
/// Receives [`Event::WindowMaximizeChanged`]
WindowMaximizeChanged,
/// Receives [`Event::DroppedFile`]
DroppedFile,
/// Receives [`Event::DroppedFileWithPosition`]
DroppedFileWithPosition,
}

#[derive(Debug, Clone)]
Expand All @@ -103,6 +108,8 @@ pub enum Event {
PointerMove(PointerMoveEvent),
PointerWheel(PointerWheelEvent),
PointerLeave,
DroppedFile(DroppedFileEvent),
DroppedFileWithPosition(DroppedFileWithPositionEvent),
KeyDown(KeyEvent),
KeyUp(KeyEvent),
ImeEnabled,
Expand Down Expand Up @@ -143,7 +150,9 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowMaximizeChanged(_)
| Event::WindowGotFocus
| Event::WindowLostFocus => false,
| Event::WindowLostFocus
| Event::DroppedFile(_)
| Event::DroppedFileWithPosition(_) => false,
Event::KeyDown(_) | Event::KeyUp(_) => true,
}
}
Expand All @@ -169,7 +178,9 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowMaximizeChanged(_)
| Event::WindowGotFocus
| Event::WindowLostFocus => false,
| Event::WindowLostFocus
| Event::DroppedFile(_)
| Event::DroppedFileWithPosition(_) => false,
}
}

Expand Down Expand Up @@ -209,7 +220,9 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowGotFocus
| Event::WindowMaximizeChanged(_)
| Event::WindowLostFocus => true,
| Event::WindowLostFocus
| Event::DroppedFile(_)
| Event::DroppedFileWithPosition(_) => true,
}
}

Expand All @@ -220,6 +233,7 @@ impl Event {
}
Event::PointerMove(pointer_event) => Some(pointer_event.pos),
Event::PointerWheel(pointer_event) => Some(pointer_event.pos),
Event::DroppedFileWithPosition(event) => Some(event.pos),
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand All @@ -235,7 +249,8 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowMaximizeChanged(_)
| Event::WindowGotFocus
| Event::WindowLostFocus => None,
| Event::WindowLostFocus
| Event::DroppedFile(_) => None,
}
}

Expand All @@ -253,6 +268,10 @@ impl Event {
pointer_event.pos.x /= scale;
pointer_event.pos.y /= scale;
}
Event::DroppedFileWithPosition(event) => {
event.pos.x /= scale;
event.pos.y /= scale;
}
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand All @@ -268,7 +287,8 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowMaximizeChanged(_)
| Event::WindowGotFocus
| Event::WindowLostFocus => {}
| Event::WindowLostFocus
| Event::DroppedFile(_) => {}
}
self
}
Expand All @@ -284,6 +304,9 @@ impl Event {
Event::PointerWheel(pointer_event) => {
pointer_event.pos -= offset;
}
Event::DroppedFileWithPosition(event) => {
event.pos -= offset;
}
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand All @@ -299,7 +322,8 @@ impl Event {
| Event::WindowMoved(_)
| Event::WindowMaximizeChanged(_)
| Event::WindowGotFocus
| Event::WindowLostFocus => {}
| Event::WindowLostFocus
| Event::DroppedFile(_) => {}
}
self
}
Expand All @@ -326,6 +350,8 @@ impl Event {
Event::FocusLost => Some(EventListener::FocusLost),
Event::FocusGained => Some(EventListener::FocusGained),
Event::ThemeChanged(_) => Some(EventListener::ThemeChanged),
Event::DroppedFile(_) => Some(EventListener::DroppedFile),
Event::DroppedFileWithPosition(_) => Some(EventListener::DroppedFileWithPosition),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ mod app_handle;
pub(crate) mod app_state;
mod clipboard;
pub mod context;
pub mod dropped_file;
pub mod event;
pub mod ext_event;
pub mod file;
Expand Down
14 changes: 14 additions & 0 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
cell::RefCell,
mem,
path::PathBuf,
rc::Rc,
sync::Arc,
time::{Duration, Instant},
Expand All @@ -27,6 +28,7 @@ use crate::{
context::{
ComputeLayoutCx, EventCx, FrameUpdate, LayoutCx, PaintCx, PaintState, StyleCx, UpdateCx,
},
dropped_file::{DroppedFileEvent, DroppedFileWithPositionEvent},
event::{Event, EventListener},
id::ViewId,
inspector::{self, Capture, CaptureState, CapturedView},
Expand Down Expand Up @@ -76,6 +78,7 @@ pub(crate) struct WindowHandle {
pub(crate) last_pointer_down: Option<(u8, Point, Instant)>,
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
pub(crate) context_menu: RwSignal<Option<(Menu, Point)>>,
dropper_file: Option<PathBuf>,
}

impl WindowHandle {
Expand Down Expand Up @@ -144,6 +147,7 @@ impl WindowHandle {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
context_menu,
last_pointer_down: None,
dropper_file: None,
};
window_handle.app_state.set_root_size(size.get_untracked());
if let Some(theme) = theme.get_untracked() {
Expand Down Expand Up @@ -390,7 +394,17 @@ impl WindowHandle {
}
}

pub(crate) fn dropped_file(&mut self, path: PathBuf) {
self.dropper_file = Some(path.clone());
self.event(Event::DroppedFile(DroppedFileEvent { path }));
}

pub(crate) fn pointer_move(&mut self, pos: Point) {
if let Some(path) = self.dropper_file.take() {
self.event(Event::DroppedFileWithPosition(
DroppedFileWithPositionEvent { path, pos },
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cursor position is stored in self.cursor_position. So we should be able to send the dropped file event with the cursor position when the dropped file event was trigger from winit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried. It's not accurate. The mouse coordinates are not updated when you drag and drop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's something at winit's end. It should be fixed when this is implemented rust-windowing/winit#3833

Does it always emit pointer move after the file was dropped? If so, we don't need the drop file event without mouse position.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, my observation is this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we just have one event DroppedFile with both path and pos please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I just kept DroppedFileWithPositionEvent

if self.cursor_position != pos {
self.cursor_position = pos;
let event = PointerMoveEvent {
Expand Down