Skip to content

Commit

Permalink
Avoid sending multiple redundant RequestRedraws
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximetinu committed Apr 23, 2024
1 parent cbecf17 commit 04d2c2d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ pub struct EguiRenderOutput {
pub textures_delta: egui::TexturesDelta,
}

impl EguiRenderOutput {
/// Returns `true` if the output has no Egui shapes and no textures delta
pub fn is_empty(&self) -> bool {
self.paint_jobs.is_empty() && self.textures_delta.is_empty()
}
}

/// Is used for storing Egui output.
#[derive(Component, Clone, Default)]
pub struct EguiOutput {
Expand Down
14 changes: 8 additions & 6 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ pub fn process_output_system(
mut event: EventWriter<RequestRedraw>,
#[cfg(windows)] mut last_cursor_icon: Local<bevy::utils::HashMap<Entity, egui::CursorIcon>>,
) {
let mut should_request_redraw = false;

for mut context in contexts.iter_mut() {
let ctx = context.ctx.get_mut();
let full_output = ctx.end_frame();
Expand Down Expand Up @@ -532,12 +534,8 @@ pub fn process_output_system(
#[cfg(not(windows))]
set_icon();

let needs_repaint = !context.render_output.paint_jobs.is_empty()
&& !context.render_output.textures_delta.is_empty();

if ctx.has_requested_repaint() && needs_repaint {
event.send(RequestRedraw);
}
let needs_repaint = !context.render_output.is_empty();
should_request_redraw |= ctx.has_requested_repaint() && needs_repaint;

#[cfg(feature = "open_url")]
if let Some(egui::output::OpenUrl { url, new_tab }) = platform_output.open_url {
Expand All @@ -558,6 +556,10 @@ pub fn process_output_system(
}
}
}

if should_request_redraw {
event.send(RequestRedraw);
}
}

fn egui_to_winit_cursor_icon(cursor_icon: egui::CursorIcon) -> Option<bevy::window::CursorIcon> {
Expand Down

0 comments on commit 04d2c2d

Please sign in to comment.