Add CollapsingHeader::open to control if it is open or collapsed (#1006)

Closes https://github.com/emilk/egui/issues/978
This commit is contained in:
Emil Ernerfeldt 2021-12-28 10:45:32 +01:00 committed by GitHub
parent 2684929a5d
commit 369ce95bbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -19,6 +19,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* When using a custom font you can now specify a font index ([#873](https://github.com/emilk/egui/pull/873)).
* Add vertical sliders with `Slider::new(…).vertical()` ([#875](https://github.com/emilk/egui/pull/875)).
* Add `Button::image_and_text` ([#832](https://github.com/emilk/egui/pull/832)).
* Add `CollapsingHeader::open` to control if it is open or collapsed ([#1006](https://github.com/emilk/egui/pull/1006)).
### Changed 🔧
* MSRV (Minimum Supported Rust Version) is now `1.56.0`.

View file

@ -144,6 +144,7 @@ pub(crate) fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
pub struct CollapsingHeader {
text: WidgetText,
default_open: bool,
open: Option<bool>,
id_source: Id,
enabled: bool,
selectable: bool,
@ -164,6 +165,7 @@ impl CollapsingHeader {
Self {
text,
default_open: false,
open: None,
id_source,
enabled: true,
selectable: false,
@ -179,6 +181,16 @@ impl CollapsingHeader {
self
}
/// Calling `.open(Some(true))` will make the collapsing header open this frame (or stay open).
///
/// Calling `.open(Some(false))` will make the collapsing header close this frame (or stay closed).
///
/// Calling `.open(None)` has no effect (default).
pub fn open(mut self, open: Option<bool>) -> Self {
self.open = open;
self
}
/// Explicitly set the source of the `Id` of this widget, instead of using title label.
/// This is useful if the title label is dynamic or not unique.
pub fn id_source(mut self, id_source: impl Hash) -> Self {
@ -256,6 +268,7 @@ impl CollapsingHeader {
let Self {
text,
default_open,
open,
id_source,
enabled: _,
selectable: _,
@ -291,10 +304,16 @@ impl CollapsingHeader {
);
let mut state = State::from_memory_with_default_open(ui.ctx(), id, default_open);
if header_response.clicked() {
if let Some(open) = open {
if open != state.open {
state.toggle(ui);
header_response.mark_changed();
}
} else if header_response.clicked() {
state.toggle(ui);
header_response.mark_changed();
}
header_response
.widget_info(|| WidgetInfo::labeled(WidgetType::CollapsingHeader, text.text()));