diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index be656f34..a232b81d 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -138,6 +138,7 @@ pub struct CollapsingHeader { label: Label, default_open: bool, id_source: Id, + enabled: bool, } impl CollapsingHeader { @@ -154,6 +155,7 @@ impl CollapsingHeader { label, default_open: false, id_source, + enabled: true, } } @@ -170,6 +172,21 @@ impl CollapsingHeader { self.id_source = Id::new(id_source); self } + + /// By default, the `CollapsingHeader` text style is `TextStyle::Button`. + /// Call `.text_style(style)` to change this. + pub fn text_style(mut self, text_style: TextStyle) -> Self { + self.label = self.label.text_style(text_style); + self + } + + /// If you set this to `false`, the `CollapsingHeader` will be grayed out and un-clickable. + /// + /// This is a convenience for [`Ui::set_enabled`]. + pub fn enabled(mut self, enabled: bool) -> Self { + self.enabled = enabled; + self + } } struct Prepared { @@ -188,6 +205,7 @@ impl CollapsingHeader { label, default_open, id_source, + enabled: _, } = self; // TODO: horizontal layout, with icon and text as labels. Insert background behind using Frame. @@ -263,37 +281,43 @@ impl CollapsingHeader { ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R, ) -> CollapsingResponse { - // Make sure contents are bellow header, - // and make sure it is one unit (necessary for putting a `CollapsingHeader` in a grid). - ui.vertical(|ui| { - let Prepared { - id, - header_response, - mut state, - } = self.begin(ui); - let ret_response = state.add_contents(ui, id, |ui| { - ui.indent(id, |ui| { - // make as wide as the header: - ui.expand_to_include_x(header_response.rect.right()); - add_contents(ui) - }) - .inner - }); - ui.memory().collapsing_headers.insert(id, state); + let header_enabled = self.enabled; + ui.wrap(|ui| { + ui.set_enabled(header_enabled); - if let Some(ret_response) = ret_response { - CollapsingResponse { + // Make sure contents are bellow header, + // and make sure it is one unit (necessary for putting a `CollapsingHeader` in a grid). + ui.vertical(|ui| { + let Prepared { + id, header_response, - body_response: Some(ret_response.response), - body_returned: Some(ret_response.inner), + mut state, + } = self.begin(ui); + let ret_response = state.add_contents(ui, id, |ui| { + ui.indent(id, |ui| { + // make as wide as the header: + ui.expand_to_include_x(header_response.rect.right()); + add_contents(ui) + }) + .inner + }); + ui.memory().collapsing_headers.insert(id, state); + + if let Some(ret_response) = ret_response { + CollapsingResponse { + header_response, + body_response: Some(ret_response.response), + body_returned: Some(ret_response.inner), + } + } else { + CollapsingResponse { + header_response, + body_response: None, + body_returned: None, + } } - } else { - CollapsingResponse { - header_response, - body_response: None, - body_returned: None, - } - } + }) + .inner }) .inner }