Skip to content

Commit

Permalink
Add CollapsingHeader::open to control if it is open or collapsed
Browse files Browse the repository at this point in the history
Closes #978
  • Loading branch information
emilk committed Dec 28, 2021
1 parent 2684929 commit f3ea937
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#1000](https://github.com/emilk/egui/pull/1000)).

### Changed 🔧
* MSRV (Minimum Supported Rust Version) is now `1.56.0`.
Expand Down
21 changes: 20 additions & 1 deletion egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -164,6 +165,7 @@ impl CollapsingHeader {
Self {
text,
default_open: false,
open: None,
id_source,
enabled: true,
selectable: false,
Expand All @@ -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 {
Expand Down Expand Up @@ -256,6 +268,7 @@ impl CollapsingHeader {
let Self {
text,
default_open,
open,
id_source,
enabled: _,
selectable: _,
Expand Down Expand Up @@ -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()));

Expand Down

0 comments on commit f3ea937

Please sign in to comment.