Skip to content

Commit

Permalink
Handle individual windows closing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Oct 1, 2021
1 parent 88869b4 commit 8e12719
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["bevy"]

[dependencies]
# bevy
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_math = { path = "../bevy_math", version = "0.5.0" }
bevy_utils = { path = "../bevy_utils", 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 @@ -28,6 +28,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 @@ -36,7 +39,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 @@ -19,15 +19,20 @@ pub mod prelude {
use bevy_app::{prelude::*, Events};

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 @@ -66,8 +71,11 @@ impl Plugin for WindowPlugin {
});
}

if self.exit_on_close {
app.add_system(exit_on_window_close_system);
if self.exit_on_all_closed {
app.add_system(exit_on_all_closed);
}
if self.close_when_requested {
app.add_system(close_when_requested);
}
}
}
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);
}
}

0 comments on commit 8e12719

Please sign in to comment.