diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index c58c9e4fc8906..22606e19646e8 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -32,7 +32,7 @@ pub struct WinitPlugin; impl Plugin for WinitPlugin { fn build(&self, app: &mut App) { - app.init_resource::() + app.init_non_send_resource::() .set_runner(winit_runner) .add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system()); let event_loop = EventLoop::new(); @@ -43,7 +43,7 @@ impl Plugin for WinitPlugin { fn change_window(world: &mut World) { let world = world.cell(); - let winit_windows = world.get_resource::().unwrap(); + let winit_windows = world.get_non_send::().unwrap(); let mut windows = world.get_resource_mut::().unwrap(); for bevy_window in windows.iter_mut() { @@ -264,7 +264,7 @@ pub fn winit_runner_with(mut app: App) { .. } => { let world = app.world.cell(); - let winit_windows = world.get_resource_mut::().unwrap(); + let winit_windows = world.get_non_send_mut::().unwrap(); let mut windows = world.get_resource_mut::().unwrap(); let window_id = if let Some(window_id) = winit_windows.get_window_id(winit_window_id) { @@ -525,7 +525,7 @@ fn handle_create_window_events( create_window_event_reader: &mut ManualEventReader, ) { let world = world.cell(); - let mut winit_windows = world.get_resource_mut::().unwrap(); + let mut winit_windows = world.get_non_send_mut::().unwrap(); let mut windows = world.get_resource_mut::().unwrap(); let create_window_events = world.get_resource::>().unwrap(); let mut window_created_events = world.get_resource_mut::>().unwrap(); @@ -544,7 +544,7 @@ fn handle_create_window_events( fn handle_initial_window_events(world: &mut World, event_loop: &EventLoop<()>) { let world = world.cell(); - let mut winit_windows = world.get_resource_mut::().unwrap(); + let mut winit_windows = world.get_non_send_mut::().unwrap(); let mut windows = world.get_resource_mut::().unwrap(); let mut create_window_events = world.get_resource_mut::>().unwrap(); let mut window_created_events = world.get_resource_mut::>().unwrap(); diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index f3fd83c6e8420..1c1c0f30df887 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -9,6 +9,10 @@ pub struct WinitWindows { pub windows: HashMap, pub window_id_to_winit: HashMap, pub winit_to_window_id: HashMap, + // Some winit functions, such as `set_window_icon` can only be used from the main thread. If + // they are used in another thread, the app will hang. This marker ensures `WinitWindows` is + // only ever accessed with bevy's non-send functions and in NonSend systems. + _not_send_sync: core::marker::PhantomData<*const ()>, } impl WinitWindows {