Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arc WinitWindow and extract to render world #12524

Closed
wants to merge 15 commits into from
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ multi-threaded = [
"bevy_asset?/multi-threaded",
"bevy_ecs/multi-threaded",
"bevy_render?/multi-threaded",
"bevy_winit?/multi-threaded",
"bevy_tasks/multi-threaded",
]
async-io = ["bevy_tasks/async-io"]
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ wayland = ["winit/wayland", "winit/wayland-csd-adwaita"]
x11 = ["winit/x11"]
accesskit_unix = ["accesskit_winit/accesskit_unix", "accesskit_winit/async-io"]
serialize = ["serde"]
multi-threaded = []

[dependencies]
# bevy
Expand Down
28 changes: 28 additions & 0 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use bevy_window::{
RawHandleWrapper, Window, WindowClosed, WindowCreated, WindowMode, WindowResized,
};

#[cfg(all(not(target_arch = "wasm32"), feature = "multi-threaded"))]
use bevy_ecs::system::Local;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use winit::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize},
Expand Down Expand Up @@ -98,6 +100,7 @@ pub(crate) fn create_windows<F: QueryFilter + 'static>(
}
}

#[cfg(any(target_arch = "wasm32", not(feature = "multi-threaded")))]
pub(crate) fn despawn_windows(
mut closed: RemovedComponents<Window>,
window_entities: Query<&Window>,
Expand All @@ -115,6 +118,31 @@ pub(crate) fn despawn_windows(
}
}

#[cfg(all(not(target_arch = "wasm32"), feature = "multi-threaded"))]
pub(crate) fn despawn_windows(
mut closed: RemovedComponents<Window>,
window_entities: Query<&Window>,
mut close_events: EventWriter<WindowClosed>,
mut winit_windows: NonSendMut<WinitWindows>,
// Winit windows are preserved for an extra frame in this vector.
// This is due to the window still being active on render world when using pipelined rendering.
mut pending_window_removal: Local<Vec<Entity>>,
) {
for window in pending_window_removal.drain(..) {
winit_windows.remove_window(window);
}
for window in closed.read() {
info!("Closing window {:?}", window);
// Guard to verify that the window is in fact actually gone,
// rather than having the component added and removed in the same frame.
if !window_entities.contains(window) {
// Delay winit window removal by a frame.
pending_window_removal.push(window);
close_events.send(WindowClosed { window });
}
}
}

/// The cached state of the window so we can check which properties were changed from within the app.
#[derive(Debug, Clone, Component)]
pub struct CachedWindow {
Expand Down