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

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

Merged
merged 1 commit into from
Dec 28, 2021
Merged
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
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 ([#1006](https://github.com/emilk/egui/pull/1006)).

### 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