diff --git a/Cargo.toml b/Cargo.toml index c4a5ca03cd622..0eb2e8f35fad5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,6 +138,10 @@ path = "examples/3d/z_sort_debug.rs" name = "custom_loop" path = "examples/app/custom_loop.rs" +[[example]] +name = "drag_and_drop" +path = "examples/app/drag_and_drop.rs" + [[example]] name = "empty_defaults" path = "examples/app/empty_defaults.rs" diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index b281d7a957564..8c97ace0b934b 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use super::{WindowDescriptor, WindowId}; use bevy_math::Vec2; @@ -77,3 +79,13 @@ pub struct WindowBackendScaleFactorChanged { pub id: WindowId, pub scale_factor: f64, } + +/// Events related to files being dragged and dropped on a window. +#[derive(Debug, Clone)] +pub enum FileDragAndDrop { + DroppedFile { id: WindowId, path_buf: PathBuf }, + + HoveredFile { id: WindowId, path_buf: PathBuf }, + + HoveredFileCancelled { id: WindowId }, +} diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index 1f7a54e5bfc6e..55c46bef8fd6f 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -11,8 +11,8 @@ pub use windows::*; pub mod prelude { pub use crate::{ - CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, WindowDescriptor, - Windows, + CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, Window, + WindowDescriptor, Windows, }; } @@ -46,6 +46,7 @@ impl Plugin for WindowPlugin { .add_event::() .add_event::() .add_event::() + .add_event::() .init_resource::(); if self.add_primary_window { diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index ce1cedb7e407d..eb038ee27b1fe 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -1,6 +1,7 @@ mod converters; mod winit_config; mod winit_windows; + use bevy_input::{ keyboard::KeyboardInput, mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel}, @@ -14,7 +15,7 @@ use bevy_ecs::{IntoSystem, Resources, World}; use bevy_math::Vec2; use bevy_utils::tracing::{error, trace, warn}; use bevy_window::{ - CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, + CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused, WindowResized, WindowScaleFactorChanged, Windows, }; @@ -397,6 +398,27 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { focused, }); } + WindowEvent::DroppedFile(path_buf) => { + let mut events = + app.resources.get_mut::>().unwrap(); + events.send(FileDragAndDrop::DroppedFile { + id: window_id, + path_buf, + }); + } + WindowEvent::HoveredFile(path_buf) => { + let mut events = + app.resources.get_mut::>().unwrap(); + events.send(FileDragAndDrop::HoveredFile { + id: window_id, + path_buf, + }); + } + WindowEvent::HoveredFileCancelled => { + let mut events = + app.resources.get_mut::>().unwrap(); + events.send(FileDragAndDrop::HoveredFileCancelled { id: window_id }); + } _ => {} } } diff --git a/examples/README.md b/examples/README.md index 16a8b4d212427..a1096797e34c9 100644 --- a/examples/README.md +++ b/examples/README.md @@ -80,6 +80,7 @@ Example | File | Description Example | File | Description --- | --- | --- `custom_loop` | [`app/custom_loop.rs`](./app/custom_loop.rs) | Demonstrates how to create a custom runner (to update an app manually). +`drag_and_drop` | [`app/empty_defaults.rs`](./app/drag_and_drop.rs) | An example that shows how to handle drag and drop in an app. `empty_defaults` | [`app/empty_defaults.rs`](./app/empty_defaults.rs) | An empty application with default plugins `empty` | [`app/empty.rs`](./app/empty.rs) | An empty application (does nothing) `headless` | [`app/headless.rs`](./app/headless.rs) | An application that runs without default plugins diff --git a/examples/app/drag_and_drop.rs b/examples/app/drag_and_drop.rs new file mode 100644 index 0000000000000..6a1ac1c152176 --- /dev/null +++ b/examples/app/drag_and_drop.rs @@ -0,0 +1,17 @@ +use bevy::prelude::*; + +fn main() { + App::build() + .add_plugins(DefaultPlugins) + .add_system(dropped_file_system.system()) + .run(); +} + +fn dropped_file_system( + mut reader: Local>, + events: Res>, +) { + for event in reader.iter(&events) { + println!("{:?}", event); + } +}