Skip to content

Commit

Permalink
support WindowEvent::DroppedFile (#524)
Browse files Browse the repository at this point in the history
* support WindowEvent::DroppedFile

* support WindowEvent::DroppedFile

* support WindowEvent::DroppedFile

* rename event
  • Loading branch information
jm-observer authored Jul 29, 2024
1 parent 3160d2a commit e1a7f99
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
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 = "../.." }
31 changes: 31 additions & 0 deletions examples/dropped_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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);
})
}

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
8 changes: 8 additions & 0 deletions src/dropped_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use peniko::kurbo::Point;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct DroppedFileEvent {
pub path: PathBuf,
pub pos: Point,
}
22 changes: 19 additions & 3 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,
keyboard::KeyEvent,
pointer::{PointerInputEvent, PointerMoveEvent, PointerWheelEvent},
};
Expand Down Expand Up @@ -94,6 +95,8 @@ pub enum EventListener {
WindowLostFocus,
/// Receives [`Event::WindowMaximizeChanged`]
WindowMaximizeChanged,
/// Receives [`Event::DroppedFile`]
DroppedFile,
}

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

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

Expand All @@ -220,6 +227,7 @@ impl Event {
}
Event::PointerMove(pointer_event) => Some(pointer_event.pos),
Event::PointerWheel(pointer_event) => Some(pointer_event.pos),
Event::DroppedFile(event) => Some(event.pos),
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand Down Expand Up @@ -253,6 +261,10 @@ impl Event {
pointer_event.pos.x /= scale;
pointer_event.pos.y /= scale;
}
Event::DroppedFile(event) => {
event.pos.x /= scale;
event.pos.y /= scale;
}
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand Down Expand Up @@ -284,6 +296,9 @@ impl Event {
Event::PointerWheel(pointer_event) => {
pointer_event.pos -= offset;
}
Event::DroppedFile(event) => {
event.pos -= offset;
}
Event::PointerLeave
| Event::KeyDown(_)
| Event::KeyUp(_)
Expand Down Expand Up @@ -326,6 +341,7 @@ impl Event {
Event::FocusLost => Some(EventListener::FocusLost),
Event::FocusGained => Some(EventListener::FocusGained),
Event::ThemeChanged(_) => Some(EventListener::ThemeChanged),
Event::DroppedFile(_) => Some(EventListener::DroppedFile),
}
}
}
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
11 changes: 11 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,
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,14 @@ impl WindowHandle {
}
}

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

pub(crate) fn pointer_move(&mut self, pos: Point) {
if let Some(path) = self.dropper_file.take() {
self.event(Event::DroppedFile(DroppedFileEvent { path, pos }));
}
if self.cursor_position != pos {
self.cursor_position = pos;
let event = PointerMoveEvent {
Expand Down

0 comments on commit e1a7f99

Please sign in to comment.