From 6a10035f153dae907c42d60825fae81a91866920 Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Sat, 22 Feb 2025 16:39:17 -0800 Subject: [PATCH 1/7] Fixes missed RequestRedraw events in about_to_wait() This fixes RequestRedraw events that can be missed in `about_to_wait()` which was reading from the cursor before `run_app_update()` is called. This adds a subsequent read to update the redraw_requested flag if any `RequestRedraw` were sent. Was reported in #16817 --- crates/bevy_winit/src/state.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/bevy_winit/src/state.rs b/crates/bevy_winit/src/state.rs index f2e2a09a6e..60ccff3538 100644 --- a/crates/bevy_winit/src/state.rs +++ b/crates/bevy_winit/src/state.rs @@ -651,6 +651,15 @@ impl WinitAppRunnerState { #[cfg(not(feature = "custom_cursor"))] self.update_cursors(); self.ran_update_since_last_redraw = true; + + // Read RequestRedraw events that may have been sent during the update + if let Some(app_redraw_events) = + self.world().get_resource::>() + { + if redraw_event_reader.read(app_redraw_events).last().is_some() { + self.redraw_requested = true; + } + } } else { self.redraw_requested = true; } From 81f82377949af1e58c087362855778a5e99a5e7c Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sat, 22 Mar 2025 19:15:44 -0400 Subject: [PATCH 2/7] Add test for missed request redraw events --- Cargo.toml | 8 +++ tests/window/desktop_request_redraw.rs | 87 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 tests/window/desktop_request_redraw.rs diff --git a/Cargo.toml b/Cargo.toml index af2d9d356c..cb14720a69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3635,6 +3635,14 @@ doc-scrape-examples = true [package.metadata.example.minimizing] hidden = true +[[example]] +name = "desktop_request_redraw" +path = "tests/window/desktop_request_redraw.rs" +doc-scrape-examples = true + +[package.metadata.example.desktop_request_redraw] +hidden = true + [[example]] name = "window_resizing" path = "examples/window/window_resizing.rs" diff --git a/tests/window/desktop_request_redraw.rs b/tests/window/desktop_request_redraw.rs new file mode 100644 index 0000000000..cb0ab38f1d --- /dev/null +++ b/tests/window/desktop_request_redraw.rs @@ -0,0 +1,87 @@ +//! Desktop request redraw +use bevy::{prelude::*, window::RequestRedraw, winit::WinitSettings}; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(MeshPickingPlugin) + .insert_resource(WinitSettings::desktop_app()) + .add_systems(Startup, setup) + .add_systems(Update, (update, redraw.after(update))) + .run(); +} + +#[derive(Component)] +struct AnimationActive; + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.spawn(( + Camera3d::default(), + Transform::from_xyz(0.0, 5.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y), + )); + + commands.spawn(( + PointLight { + intensity: 1e6, + ..Default::default() + }, + Transform::from_xyz(-1.0, 5.0, 1.0), + )); + + let node = Node { + display: Display::Block, + padding: UiRect::all(Val::Px(10.0)), + row_gap: Val::Px(10.0), + ..Default::default() + }; + + commands.spawn(( + node.clone(), + children![ + ( + node.clone(), + children![Text::new("Right click cube to pause animation")] + ), + ( + node.clone(), + children![Text::new("Left click cube to start animation")] + ) + ], + )); + + commands + .spawn(( + Mesh3d(meshes.add(Cuboid::from_length(1.0))), + MeshMaterial3d(materials.add(Color::WHITE)), + AnimationActive, + )) + .observe( + |trigger: Trigger>, mut commands: Commands| match trigger.button { + PointerButton::Primary => { + commands.entity(trigger.target()).insert(AnimationActive); + } + PointerButton::Secondary => { + commands + .entity(trigger.target()) + .remove::(); + } + _ => {} + }, + ); +} + +fn update(time: Res