2020-04-25 01:55:15 +00:00
|
|
|
use crate::{WindowCloseRequested, WindowId};
|
2020-06-04 06:53:00 +00:00
|
|
|
use bevy_app::{AppExit, EventReader, Events};
|
2020-04-19 19:13:04 +00:00
|
|
|
use legion::prelude::*;
|
|
|
|
|
2020-06-04 06:53:00 +00:00
|
|
|
pub fn exit_on_window_close_system(window_id: Option<WindowId>) -> Box<dyn Schedulable> {
|
|
|
|
let mut window_close_requested_event_reader = EventReader::<WindowCloseRequested>::default();
|
2020-04-19 19:13:04 +00:00
|
|
|
SystemBuilder::new("exit_on_window_close")
|
|
|
|
.read_resource::<Events<WindowCloseRequested>>()
|
|
|
|
.write_resource::<Events<AppExit>>()
|
2020-04-25 01:55:15 +00:00
|
|
|
.build(
|
|
|
|
move |_, _, (ref window_close_requested_events, ref mut app_exit_events), _| {
|
|
|
|
for window_close_requested_event in
|
2020-04-28 17:59:42 +00:00
|
|
|
window_close_requested_event_reader.iter(window_close_requested_events)
|
2020-04-25 01:55:15 +00:00
|
|
|
{
|
|
|
|
match window_id.as_ref() {
|
|
|
|
Some(window_id) => {
|
|
|
|
if *window_id == window_close_requested_event.id {
|
|
|
|
app_exit_events.send(AppExit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if window_close_requested_event.is_primary {
|
|
|
|
app_exit_events.send(AppExit);
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 19:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 01:55:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|