Skip to content

Commit

Permalink
Add dry run feature for anchor calculation. (#1) (#1856)
Browse files Browse the repository at this point in the history
* Add dry run feature for anchor calculation. (#1)

This PR resolves issue: #1852
We introduce dry_run flag which makes component invisible until we do second pass of rendering - which allows us to properly calculate position for anchor. (This removes rapid flicker when new window is drawn for the first time).

* Change naming convention and add description
  • Loading branch information
zapp88 authored Jul 29, 2022
1 parent 278db1c commit 36a49ff
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ pub(crate) struct Prepared {
pub(crate) movable: bool,
enabled: bool,
drag_bounds: Option<Rect>,
/// Set the first frame of new windows with anchors.
///
/// This is so that we use the first frame to calculate the window size,
/// and then can correctly position the window the next frame,
/// without having one frame where the window is positioned in the wrong place.
temporarily_invisible: bool,
}

impl Area {
Expand Down Expand Up @@ -214,11 +220,11 @@ impl Area {
});
state.pos = new_pos.unwrap_or(state.pos);
state.interactable = interactable;
let mut temporarily_invisible = false;

if let Some((anchor, offset)) = anchor {
if is_new {
// unknown size
ctx.request_repaint();
temporarily_invisible = true;
} else {
let screen = ctx.available_rect();
state.pos = anchor.align_size_within_rect(state.size, screen).min + offset;
Expand All @@ -233,6 +239,7 @@ impl Area {
movable,
enabled,
drag_bounds,
temporarily_invisible,
}
}

Expand Down Expand Up @@ -314,7 +321,7 @@ impl Prepared {
clip_rect,
);
ui.set_enabled(self.enabled);

ui.set_visible(!self.temporarily_invisible);
ui
}

Expand All @@ -326,6 +333,7 @@ impl Prepared {
movable,
enabled,
drag_bounds,
temporarily_invisible: _,
} = self;

state.size = content_ui.min_rect().size();
Expand Down

0 comments on commit 36a49ff

Please sign in to comment.