Skip to content

Allow using RenderPlugin and WinitPlugin with WindowPlugin being disabled #19042

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Guard bevy_winit plugins, methods and systems thar rely on `WindowP…
…lugin`
  • Loading branch information
hukasu committed May 3, 2025
commit f2faef2e78e90a07ea672eaef9549648f84e37ca
15 changes: 9 additions & 6 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern crate alloc;
use bevy_derive::Deref;
use bevy_reflect::prelude::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_window::{RawHandleWrapperHolder, WindowEvent};
use bevy_window::{RawHandleWrapperHolder, WindowEvent, WindowPlugin};
use core::marker::PhantomData;
use winit::{event_loop::EventLoop, window::WindowId};

Expand Down Expand Up @@ -129,8 +129,12 @@ impl<T: Event> Plugin for WinitPlugin<T> {
.init_resource::<WinitSettings>()
.insert_resource(DisplayHandleWrapper(event_loop.owned_display_handle()))
.add_event::<RawWinitWindowEvent>()
.set_runner(|app| winit_runner(app, event_loop))
.add_systems(
.set_runner(|app| winit_runner(app, event_loop));

app.add_plugins(cursor::CursorPlugin);

if app.is_plugin_added::<WindowPlugin>() {
app.add_systems(
Last,
(
// `exit_on_all_closed` only checks if windows exist but doesn't access data,
Expand All @@ -141,9 +145,8 @@ impl<T: Event> Plugin for WinitPlugin<T> {
)
.chain(),
);

app.add_plugins(AccessKitPlugin);
app.add_plugins(cursor::CursorPlugin);
app.add_plugins(AccessKitPlugin);
}
}
}

Expand Down
24 changes: 17 additions & 7 deletions crates/bevy_winit/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bevy_input::{
};
#[cfg(any(not(target_arch = "wasm32"), feature = "custom_cursor"))]
use bevy_log::error;
use bevy_log::{trace, warn};
use bevy_log::{once, trace, warn};
#[cfg(feature = "custom_cursor")]
use bevy_math::URect;
use bevy_math::{ivec2, DVec2, Vec2};
Expand All @@ -41,7 +41,7 @@ use winit::{

use bevy_window::{
AppLifecycle, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime, RequestRedraw,
Window, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowDestroyed,
Window, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed,
WindowEvent as BevyWindowEvent, WindowFocused, WindowMoved, WindowOccluded, WindowResized,
WindowScaleFactorChanged, WindowThemeChanged,
};
Expand Down Expand Up @@ -493,8 +493,14 @@ impl<T: Event> ApplicationHandler<T> for WinitAppRunnerState<T> {
SystemState::<CreateWindowParams<Added<Window>>>::from_world(self.world_mut());
create_monitors(event_loop, create_monitor.get_mut(self.world_mut()));
create_monitor.apply(self.world_mut());
create_windows(event_loop, create_window.get_mut(self.world_mut()));
create_window.apply(self.world_mut());
if self
.world_mut()
.get_resource::<Events<WindowCreated>>()
.is_some()
{
create_windows(event_loop, create_window.get_mut(self.world_mut()));
create_window.apply(self.world_mut());
}

// TODO: This is a workaround for https://github.com/bevyengine/bevy/issues/17488
// while preserving the iOS fix in https://github.com/bevyengine/bevy/pull/11245
Expand Down Expand Up @@ -862,9 +868,13 @@ impl<T: Event> WinitAppRunnerState<T> {
}

if !buffered_events.is_empty() {
world
.resource_mut::<Events<BevyWindowEvent>>()
.send_batch(buffered_events);
if let Some(mut events) = world.get_resource_mut::<Events<BevyWindowEvent>>() {
events.send_batch(buffered_events);
} else {
once!(warn!(
"Lost windows events because `Events<WindowEvents>` is not available."
));
}
}
}

Expand Down