Skip to content

Commit

Permalink
Use removal events as event
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Nov 16, 2022
1 parent 77076bc commit 666590c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{PrimaryWindow, Window, WindowCloseRequested, WindowClosed, WindowFocus};
use crate::{PrimaryWindow, Window, WindowCloseRequested, WindowFocus};

use bevy_app::AppExit;
use bevy_ecs::prelude::*;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Plugin for WinitPlugin {
.with_system(update_cursor_position)
.with_system(update_resize_constraints),
)
.add_system_to_stage(CoreStage::Last, despawn_window);
.add_system_to_stage(CoreStage::PreUpdate, despawn_window);

#[cfg(target_arch = "wasm32")]
app.add_plugin(web_resize::CanvasParentResizePlugin);
Expand Down Expand Up @@ -238,6 +238,7 @@ pub fn winit_runner(mut app: App) {
// TODO move all system state fetch up here?
if let Some(app_exit_events) = app.world.get_resource::<Events<AppExit>>() {
if app_exit_event_reader.iter(app_exit_events).last().is_some() {
warn!("exitting");
*control_flow = ControlFlow::Exit;
return;
}
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bevy_ecs::{
entity::{Entities, Entity},
event::EventWriter,
prelude::{Added, Changed, With},
system::{Commands, NonSendMut, Query, RemovedComponents, Res},
removal_detection::RemovedComponent,
system::{Commands, NonSendMut, Query, Res},
};
use bevy_utils::tracing::{error, info};
use bevy_window::{
Expand Down Expand Up @@ -62,19 +63,21 @@ pub fn despawn_window(
mut commands: Commands,
entities: &Entities,
primary: Option<Res<PrimaryWindow>>,
closed: RemovedComponents<Window>,
mut closed: RemovedComponent<Window>,
mut close_events: EventWriter<WindowClosed>,
mut winit_windows: NonSendMut<WinitWindows>,
) {
for window in closed.iter() {
info!("closing window: {:?}", window);
winit_windows.remove_window(window);

if entities.contains(window) {
commands.entity(window).despawn();
}
//if entities.contains(window) {
//commands.entity(window).despawn();
//}

if let Some(ref primary) = primary {
if primary.window == window {
info!("removing primary");
commands.remove_resource::<PrimaryWindow>();
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn remove_component(
}
}

fn react_on_removal(removed: RemovedComponents<MyComponent>, mut query: Query<&mut Sprite>) {
fn react_on_removal(mut removed: RemovedComponent<MyComponent>, mut query: Query<&mut Sprite>) {
// `RemovedComponents<T>::iter()` returns an interator with the `Entity`s that had their
// `Component` `T` (in this case `MyComponent`) removed at some point earlier during the frame.
for entity in removed.iter() {
Expand Down
3 changes: 2 additions & 1 deletion examples/window/multiple_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
// A window bundle inserted as a resource acts as the descriptor
// for a primary window.
.insert_resource(WindowBundle::default())
.insert_resource(RemovedComponents::default())
.add_startup_system(setup_scene)
.add_startup_system(setup_extra_windows)
.add_system(bevy::window::close_on_esc)
Expand Down Expand Up @@ -51,7 +52,7 @@ fn setup_extra_windows(mut commands: Commands) {
.spawn_bundle(WindowBundle {
title: WindowTitle::new("Second window"),
// A window can start minimized.
state: WindowState::Minimized,
//state: WindowState::Minimized,
..Default::default()
})
.id();
Expand Down

0 comments on commit 666590c

Please sign in to comment.