Skip to content
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

egui: Add collapsed and default_collapsed to Window for programatically collapsing it #5661

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions crates/egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl CollapsingState {
})
}

pub fn load_with_default_collapsed(ctx: &Context, id: Id, default_collapsed: bool) -> Self {
Self::load_with_default_open(ctx, id, !default_collapsed)
}

pub fn is_open(&self) -> bool {
self.state.open
}
Expand All @@ -66,6 +70,14 @@ impl CollapsingState {
self.state.open = open;
}

pub fn is_collapsed(&self) -> bool {
Copy link
Contributor Author

@EriKWDev EriKWDev Jan 31, 2025

Choose a reason for hiding this comment

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

Easier to read/write with when the Window contained usages of both the word 'open' and 'collapse', so I added this which is just the opposite of is_open

!self.state.open
}

pub fn set_collapsed(&mut self, collapsed: bool) {
self.state.open = !collapsed;
}

pub fn toggle(&mut self, ui: &Ui) {
self.state.open = !self.state.open;
ui.ctx().request_repaint();
Expand Down
64 changes: 54 additions & 10 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ use super::{area, resize, Area, Frame, Resize, ScrollArea};
/// Note that this is NOT a native OS window.
/// To create a new native OS window, use [`crate::Context::show_viewport_deferred`].
#[must_use = "You should call .show()"]
pub struct Window<'open> {
pub struct Window<'open, 'collapsed> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could maybe be same lifetimes for simplicitly, though since they can technically be different they should perhaps be modelled like this.

I think any usecase I would have they would have the same lifetimes though

title: WidgetText,
open: Option<&'open mut bool>,
area: Area,
frame: Option<Frame>,
resize: Resize,
scroll: ScrollArea,
collapsible: bool,
default_open: bool,
with_title_bar: bool,
fade_out: bool,
default_collapsed: bool,
collapsed: Option<&'collapsed mut bool>,
}

impl<'open> Window<'open> {
impl<'open, 'collapsed> Window<'open, 'collapsed> {
/// The window title is used as a unique [`Id`] and must be unique, and should not change.
/// This is true even if you disable the title bar with `.title_bar(false)`.
/// If you need a changing title, you must call `window.id(…)` with a fixed id.
Expand All @@ -64,9 +65,10 @@ impl<'open> Window<'open> {
.default_size([340.0, 420.0]), // Default inner size of a window
scroll: ScrollArea::neither().auto_shrink(false),
collapsible: true,
default_open: true,
with_title_bar: true,
fade_out: true,
default_collapsed: false,
collapsed: None,
}
}

Expand All @@ -88,6 +90,18 @@ impl<'open> Window<'open> {
self
}

/// Call this to have control over the window's collapsed state
///
/// * If `*collapsed == false`, the window will not be collapsed
/// * If `*open == true`, the window will be collapsed
/// * If the collapse button is pressed, `*collapsed` will be set to represent the new collapsed state
/// * If the window is double clicked, `*collapsed` will be set to represent the new collapsed state
#[inline]
pub fn collapsed(mut self, collapsed: &'collapsed mut bool) -> Self {
self.collapsed = Some(collapsed);
self
}

/// If `false` the window will be grayed out and non-interactive.
#[inline]
pub fn enabled(mut self, enabled: bool) -> Self {
Expand Down Expand Up @@ -278,9 +292,17 @@ impl<'open> Window<'open> {
}

/// Set initial collapsed state of the window
#[deprecated = "Use `default_collapsed` to set default collapsed state instead. This function might change meaning in future, but for now does the same thing as it used to"]
Copy link
Contributor Author

@EriKWDev EriKWDev Jan 31, 2025

Choose a reason for hiding this comment

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

The old function was named default_open which didn't really align with the usage of open to mean whether the window was shown or not. So, I changed to explicitly use collapsed for indicating the collapsed state of the window.

I kept the old function and its behavior though

#[inline]
pub fn default_open(mut self, default_open: bool) -> Self {
self.default_open = default_open;
self.default_collapsed = !default_open;
self
}

/// Set initial collapsed state of the window
#[inline]
pub fn default_collapsed(mut self, default_collapsed: bool) -> Self {
self.default_collapsed = default_collapsed;
self
}

Expand Down Expand Up @@ -415,7 +437,7 @@ impl<'open> Window<'open> {
}
}

impl Window<'_> {
impl Window<'_, '_> {
/// Returns `None` if the window is not open (if [`Window::open`] was called with `&mut false`).
/// Returns `Some(InnerResponse { inner: None })` if the window is collapsed.
#[inline]
Expand All @@ -440,9 +462,10 @@ impl Window<'_> {
resize,
scroll,
collapsible,
default_open,
with_title_bar,
fade_out,
collapsed,
default_collapsed,
} = self;

let header_color =
Expand All @@ -463,8 +486,11 @@ impl Window<'_> {
let area_id = area.id;
let area_layer_id = area.layer();
let resize_id = area_id.with("resize");
let mut collapsing =
CollapsingState::load_with_default_open(ctx, area_id.with("collapsing"), default_open);
let mut collapsing = CollapsingState::load_with_default_collapsed(
ctx,
area_id.with("collapsing"),
default_collapsed,
);

let is_collapsed = with_title_bar && !collapsing.is_open();
let possible = PossibleInteractions::new(&area, &resize, is_collapsed);
Expand Down Expand Up @@ -638,6 +664,7 @@ impl Window<'_> {
open,
&mut collapsing,
collapsible,
collapsed,
);
}

Expand Down Expand Up @@ -1213,6 +1240,7 @@ impl TitleBar {
open: Option<&mut bool>,
collapsing: &mut CollapsingState,
collapsible: bool,
mut collapsed: Option<&mut bool>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

has to be mut collapsed due to if let Some() = causes the mutable reference to move and I needed to use it twice..

) {
let window_frame = self.window_frame;
let title_inner_rect = self.inner_rect;
Expand All @@ -1233,7 +1261,19 @@ impl TitleBar {
let button_rect = button_rect.round_to_pixels(ui.pixels_per_point());

ui.allocate_new_ui(UiBuilder::new().max_rect(button_rect), |ui| {
collapsing.show_default_button_with_size(ui, button_size);
if let Some(collapsed) = collapsed.as_ref() {
if **collapsed != collapsing.is_collapsed() {
Copy link
Contributor Author

@EriKWDev EriKWDev Feb 1, 2025

Choose a reason for hiding this comment

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

logic in human language: "in case the user-provided state exists and has a different value than the internal 'is_collapsed', change the internal"

collapsing.set_collapsed(**collapsed);
}
}

let collapse_response = collapsing.show_default_button_with_size(ui, button_size);

if let Some(collapsed) = collapsed.as_mut() {
if collapse_response.clicked() {
**collapsed = collapsing.is_collapsed();
}
}
});
}

Expand Down Expand Up @@ -1288,6 +1328,10 @@ impl TitleBar {
&& collapsible
{
collapsing.toggle(ui);

if let Some(collapsed) = collapsed.as_mut() {
**collapsed = collapsing.is_collapsed();
}
}
}

Expand Down
Loading