From 369ce95bbdbf14143861c6af4afdf2d106c29e9b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 28 Dec 2021 10:45:32 +0100 Subject: [PATCH] Add `CollapsingHeader::open` to control if it is open or collapsed (#1006) Closes https://github.com/emilk/egui/issues/978 --- CHANGELOG.md | 1 + egui/src/containers/collapsing_header.rs | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03b3e3aacf3..036e675a213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 9618b5b860c..0411c163bfb 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -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, 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) -> 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()));