Skip to content

Commit

Permalink
Automatically repaint if any widget changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 1, 2024
1 parent 97221a4 commit af9aa56
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
35 changes: 22 additions & 13 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub struct WidgetRect {
/// Stores the positions of all widgets generated during a single egui update/frame.
///
/// Acgtually, only those that are on screen.
#[derive(Default, Clone)]
#[derive(Default, Clone, PartialEq, Eq)]
pub struct WidgetRects {
/// All widgets, in painting order.
pub by_layer: HashMap<LayerId, Vec<WidgetRect>>,
Expand Down Expand Up @@ -398,14 +398,6 @@ impl ContextImpl {
.native_pixels_per_point
.unwrap_or(1.0);

{
std::mem::swap(
&mut viewport.layer_rects_prev_frame,
&mut viewport.layer_rects_this_frame,
);
viewport.layer_rects_this_frame.clear();
}

let all_viewport_ids: ViewportIdSet = self.all_viewport_ids();

let viewport = self.viewports.entry(self.viewport_id()).or_default();
Expand Down Expand Up @@ -645,12 +637,12 @@ impl Default for Context {
}

impl Context {
// Do read-only (shared access) transaction on Context
/// Do read-only (shared access) transaction on Context
fn read<R>(&self, reader: impl FnOnce(&ContextImpl) -> R) -> R {
reader(&self.0.read())
}

// Do read-write (exclusive access) transaction on Context
/// Do read-write (exclusive access) transaction on Context
fn write<R>(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R {
writer(&mut self.0.write())
}
Expand Down Expand Up @@ -1820,7 +1812,24 @@ impl ContextImpl {

let shapes = viewport.graphics.drain(self.memory.areas().order());

if viewport.input.wants_repaint() {
let mut repaint_needed = false;

{
if self.memory.options.repaint_on_widget_change {
crate::profile_function!("compare-widget-rects");
if viewport.layer_rects_prev_frame != viewport.layer_rects_this_frame {
repaint_needed = true; // Some widget has moved
}
}

std::mem::swap(
&mut viewport.layer_rects_prev_frame,
&mut viewport.layer_rects_this_frame,
);
viewport.layer_rects_this_frame.clear();
}

if repaint_needed || viewport.input.wants_repaint() {
self.request_repaint(ended_viewport_id);
}

Expand Down Expand Up @@ -2194,7 +2203,7 @@ impl Context {

// We add all widgets here, even non-interactive ones,
// because we need this list not only for checking for blocking widgets,
// but also to know when we have reach the widget we are checking for cover.
// but also to know when we have reached the widget we are checking for cover.
viewport
.layer_rects_this_frame
.insert(layer_id, WidgetRect { id, rect, sense });
Expand Down
8 changes: 7 additions & 1 deletion crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl FocusDirection {
// ----------------------------------------------------------------------------

/// Some global options that you can read and write.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Options {
Expand Down Expand Up @@ -184,6 +184,11 @@ pub struct Options {
/// Controls the tessellator.
pub tessellation_options: epaint::TessellationOptions,

/// If any widget moves or changes id, repaint everything.
///
/// This is `true` by default.
pub repaint_on_widget_change: bool,

/// This is a signal to any backend that we want the [`crate::PlatformOutput::events`] read out loud.
///
/// The only change to egui is that labels can be focused by pressing tab.
Expand Down Expand Up @@ -216,6 +221,7 @@ impl Default for Options {
zoom_factor: 1.0,
zoom_with_keyboard: true,
tessellation_options: Default::default(),
repaint_on_widget_change: true,
screen_reader: false,
preload_font_glyphs: true,
warn_on_id_clash: cfg!(debug_assertions),
Expand Down

0 comments on commit af9aa56

Please sign in to comment.