Add `Ui::take_available_space()` helper function, which sets the Ui's minimum size to the available space (#7573)

A shorthand, and more descriptive version of calling
`ui.set_min_size(ui.available_size())`, and mentions this on panel's
resizable functions.
This commit is contained in:
Isse 2025-10-01 14:39:32 +02:00 committed by GitHub
parent 18ea9ff0bd
commit 0888e3dc86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 4 deletions

View File

@ -144,8 +144,11 @@ impl SidePanel {
///
/// Default is `true`.
///
/// If you want your panel to be resizable you also need a widget in it that
/// takes up more space as you resize it, such as:
/// If you want your panel to be resizable you also need to make the ui use
/// the available space.
///
/// This can be done by using [`Ui::take_available_space`], or using a
/// widget in it that takes up more space as you resize it, such as:
/// * Wrapping text ([`Ui::horizontal_wrapped`]).
/// * A [`crate::ScrollArea`].
/// * A [`crate::Separator`].
@ -631,8 +634,11 @@ impl TopBottomPanel {
///
/// Default is `false`.
///
/// If you want your panel to be resizable you also need a widget in it that
/// takes up more space as you resize it, such as:
/// If you want your panel to be resizable you also need to make the ui use
/// the available space.
///
/// This can be done by using [`Ui::take_available_space`], or using a
/// widget in it that takes up more space as you resize it, such as:
/// * Wrapping text ([`Ui::horizontal_wrapped`]).
/// * A [`crate::ScrollArea`].
/// * A [`crate::Separator`].

View File

@ -944,6 +944,30 @@ impl Ui {
self.placer.set_min_height(height);
}
/// Makes the ui always fill up the available space.
///
/// This can be useful to call inside a panel with `resizable == true`
/// to make sure the resized space is used.
pub fn take_available_space(&mut self) {
self.set_min_size(self.available_size());
}
/// Makes the ui always fill up the available space in the x axis.
///
/// This can be useful to call inside a side panel with
/// `resizable == true` to make sure the resized space is used.
pub fn take_available_width(&mut self) {
self.set_min_width(self.available_width());
}
/// Makes the ui always fill up the available space in the y axis.
///
/// This can be useful to call inside a top bottom panel with
/// `resizable == true` to make sure the resized space is used.
pub fn take_available_height(&mut self) {
self.set_min_height(self.available_height());
}
// ------------------------------------------------------------------------
/// Helper: shrinks the max width to the current width,