Skip to content

Commit

Permalink
Handle individual windows closing properly
Browse files Browse the repository at this point in the history
Apply @bjorn3's suggestion
  • Loading branch information
DJMcNab committed Oct 2, 2021
1 parent a2e12c7 commit 7cfa4ad
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["bevy"]

[dependencies]
# bevy
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_math = { path = "../bevy_math", version = "0.5.0" }
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub struct WindowCreated {

/// An event that is sent whenever a close was requested for a window. For example: when the "close"
/// button is pressed on a window.
///
/// By default, these events are handled by closing the corresponding [`crate::Window`].
/// To disable this behaviour, set `close_when_requested` on the [`crate::WindowPlugin`] to `false`
#[derive(Debug, Clone)]
pub struct WindowCloseRequested {
pub id: WindowId,
Expand All @@ -34,7 +37,10 @@ pub struct WindowCloseRequested {
/// 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`
/// By default, when no windows are open, the app will close.
/// To disable this behaviour, set `exit_on_all_closed` on the [`crate::WindowPlugin`] to `false`
///
/// [`Window::close`]: `crate::Window::close`
#[derive(Debug, Clone)]
pub struct WindowClosed {
pub id: WindowId,
Expand Down
16 changes: 12 additions & 4 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ use bevy_app::{prelude::*, Events};
use bevy_ecs::system::IntoSystem;

pub struct WindowPlugin {
/// Whether to add a default window based on the [`WindowDescriptor`] resource
pub add_primary_window: bool,
pub exit_on_close: bool,
/// Whether to close the app when there are no open windows
pub exit_on_all_closed: bool,
/// Whether to close windows when they are requested to be closed (i.e. when the close button is pressed)
pub close_when_requested: bool,
}

impl Default for WindowPlugin {
fn default() -> Self {
WindowPlugin {
add_primary_window: true,
exit_on_close: true,
close_when_requested: true,
exit_on_all_closed: true,
}
}
}
Expand Down Expand Up @@ -69,8 +74,11 @@ impl Plugin for WindowPlugin {
});
}

if self.exit_on_close {
app.add_system(exit_on_window_close_system.system());
if self.exit_on_all_closed {
app.add_system(exit_on_all_closed.system());
}
if self.close_when_requested {
app.add_system(close_when_requested.system());
}
}
}
19 changes: 13 additions & 6 deletions crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::WindowCloseRequested;
use crate::{Window, WindowCloseRequested, Windows};
use bevy_app::{AppExit, EventReader, EventWriter};
use bevy_ecs::prelude::{Res, ResMut};

pub fn exit_on_window_close_system(
mut app_exit_events: EventWriter<AppExit>,
mut window_close_requested_events: EventReader<WindowCloseRequested>,
) {
if window_close_requested_events.iter().next().is_some() {
pub fn exit_on_all_closed(mut app_exit_events: EventWriter<AppExit>, windows: Res<Windows>) {
if windows.iter().count() == 0 {
app_exit_events.send(AppExit);
}
}

pub fn close_when_requested(
mut windows: ResMut<Windows>,
mut closed: EventReader<WindowCloseRequested>,
) {
for event in closed.iter() {
windows.get_mut(event.id).map(Window::close);
}
}
11 changes: 7 additions & 4 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn change_window(world: &mut World) {
}
}
}
if removed_windows.len() > 0 {
if !removed_windows.is_empty() {
let mut events = world.get_resource_mut::<Events<WindowClosed>>().unwrap();
for id in removed_windows {
windows.remove(id);
Expand Down Expand Up @@ -280,16 +280,19 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
window_id
} else {
warn!(
"Skipped event for unknown winit Window Id {:?}",
winit_window_id
"Skipped event for unknown winit Window Id {:?}: {:?}",
winit_window_id, event
);
return;
};

let window = if let Some(window) = windows.get_mut(window_id) {
window
} else {
warn!("Skipped event for unknown Window Id {:?}", winit_window_id);
warn!(
"Skipped event for unknown Window Id {:?}: {:?}",
winit_window_id, event
);
return;
};

Expand Down

0 comments on commit 7cfa4ad

Please sign in to comment.