Skip to content

Commit

Permalink
Add an event for when a window closes
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Oct 2, 2021
1 parent 2f48b22 commit a2e12c7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub struct WindowCloseRequested {
pub id: WindowId,
}

/// An event that is sent whenever a window is closed.
/// This will only be sent in response to the [`Window::close`] method.
///
/// [`Window::close`]: `crate::window::Window::close`
#[derive(Debug, Clone)]
pub struct WindowClosed {
pub id: WindowId,
}

#[derive(Debug, Clone)]
pub struct CursorMoved {
pub id: WindowId,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Plugin for WindowPlugin {
app.add_event::<WindowResized>()
.add_event::<CreateWindow>()
.add_event::<WindowCreated>()
.add_event::<WindowClosed>()
.add_event::<WindowCloseRequested>()
.add_event::<CursorMoved>()
.add_event::<CursorEntered>()
Expand Down
12 changes: 8 additions & 4 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use bevy_math::{ivec2, Vec2};
use bevy_utils::tracing::{error, trace, warn};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused,
WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowClosed, WindowCreated,
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
};
use winit::{
dpi::PhysicalPosition,
Expand Down Expand Up @@ -170,8 +170,12 @@ fn change_window(world: &mut World) {
}
}
}
for window in removed_windows {
windows.remove(window);
if removed_windows.len() > 0 {
let mut events = world.get_resource_mut::<Events<WindowClosed>>().unwrap();
for id in removed_windows {
windows.remove(id);
events.send(WindowClosed { id });
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl WinitWindows {
self.windows.remove(&winit_id)
}
}

pub fn get_fitting_videomode(
monitor: &winit::monitor::MonitorHandle,
width: u32,
Expand Down

0 comments on commit a2e12c7

Please sign in to comment.