-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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, | ||
} | ||
} | ||
|
||
|
@@ -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 { | ||
|
@@ -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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old function was named 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 | ||
} | ||
|
||
|
@@ -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] | ||
|
@@ -440,9 +462,10 @@ impl Window<'_> { | |
resize, | ||
scroll, | ||
collapsible, | ||
default_open, | ||
with_title_bar, | ||
fade_out, | ||
collapsed, | ||
default_collapsed, | ||
} = self; | ||
|
||
let header_color = | ||
|
@@ -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); | ||
|
@@ -638,6 +664,7 @@ impl Window<'_> { | |
open, | ||
&mut collapsing, | ||
collapsible, | ||
collapsed, | ||
); | ||
} | ||
|
||
|
@@ -1213,6 +1240,7 @@ impl TitleBar { | |
open: Option<&mut bool>, | ||
collapsing: &mut CollapsingState, | ||
collapsible: bool, | ||
mut collapsed: Option<&mut bool>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. has to be |
||
) { | ||
let window_frame = self.window_frame; | ||
let title_inner_rect = self.inner_rect; | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
@@ -1288,6 +1328,10 @@ impl TitleBar { | |
&& collapsible | ||
{ | ||
collapsing.toggle(ui); | ||
|
||
if let Some(collapsed) = collapsed.as_mut() { | ||
**collapsed = collapsing.is_collapsed(); | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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 ofis_open