Skip to content

Update area struct to allow force resizing #7114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub struct Area {
new_pos: Option<Pos2>,
fade_in: bool,
layout: Layout,
sizing_pass: bool,
}

impl WidgetWithState for Area {
Expand All @@ -147,6 +148,7 @@ impl Area {
anchor: None,
fade_in: true,
layout: Layout::default(),
sizing_pass: false,
}
}

Expand Down Expand Up @@ -357,6 +359,23 @@ impl Area {
self.layout = layout;
self
}

/// While true, a sizing pass will be done. This means the area will be invisible
/// and the contents will be laid out to estimate the proper containing size of the area.
/// If false, there will be no change to the default area behavior. This is useful if the
/// area contents area dynamic and you need to need to make sure the area adjusts its size
/// accordingly.
///
/// # Arguments
/// - resize: If true, the area will be resized to fit its contents. False will keep the
/// default area resizing behavior.
///
/// Default: `false`.
#[inline]
pub fn sizing_pass(mut self, resize: bool) -> Self {
Comment on lines +373 to +375
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's explain that the user should only call this ONCE, i.e. not have a hard-coded .sizing_pass(true) in their code.

Copy link
Author

@blackberryfloat blackberryfloat Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the intent for helping make sure people don't misuse this but this function is meant to be used multiple times. I feel like the description effectively conveys that if it is true what the implications are and that false keeps the default behavior. I am open to wording ideas but my expectation was that people will connect that while the variable it true the content will always be invisible.

If we only needed this once, this function would be pointless since the creation of the Area would solve that singular use. My expectation and how I have used it is to call this function every frame and the flag being passed to it only gets set to true when the content within the area changes (ie len of an alerts vector changes).

I can add a note about my usage if that makes sense?

self.sizing_pass = resize;
self
}
}

pub(crate) struct Prepared {
Expand Down Expand Up @@ -410,6 +429,7 @@ impl Area {
constrain_rect,
fade_in,
layout,
sizing_pass: force_sizing_pass,
} = self;

let constrain_rect = constrain_rect.unwrap_or_else(|| ctx.screen_rect());
Expand All @@ -425,6 +445,10 @@ impl Area {
interactable,
last_became_visible_at: None,
});
if force_sizing_pass {
sizing_pass = true;
state.size = None;
}
state.pivot = pivot;
state.interactable = interactable;
if let Some(new_pos) = new_pos {
Expand Down