Configurable label text style for CollapsingHeader (#200)

CollapsingHeader: change label text style & enable options
This commit is contained in:
Bradley Smith 2021-03-09 10:13:21 -08:00 committed by GitHub
parent 6fb4e19e9e
commit bd34cfd43e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -138,6 +138,7 @@ pub struct CollapsingHeader {
label: Label, label: Label,
default_open: bool, default_open: bool,
id_source: Id, id_source: Id,
enabled: bool,
} }
impl CollapsingHeader { impl CollapsingHeader {
@ -154,6 +155,7 @@ impl CollapsingHeader {
label, label,
default_open: false, default_open: false,
id_source, id_source,
enabled: true,
} }
} }
@ -170,6 +172,21 @@ impl CollapsingHeader {
self.id_source = Id::new(id_source); self.id_source = Id::new(id_source);
self 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 { struct Prepared {
@ -188,6 +205,7 @@ impl CollapsingHeader {
label, label,
default_open, default_open,
id_source, id_source,
enabled: _,
} = self; } = self;
// TODO: horizontal layout, with icon and text as labels. Insert background behind using Frame. // TODO: horizontal layout, with icon and text as labels. Insert background behind using Frame.
@ -263,37 +281,43 @@ impl CollapsingHeader {
ui: &mut Ui, ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> CollapsingResponse<R> { ) -> CollapsingResponse<R> {
// Make sure contents are bellow header, let header_enabled = self.enabled;
// and make sure it is one unit (necessary for putting a `CollapsingHeader` in a grid). ui.wrap(|ui| {
ui.vertical(|ui| { ui.set_enabled(header_enabled);
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);
if let Some(ret_response) = ret_response { // Make sure contents are bellow header,
CollapsingResponse { // and make sure it is one unit (necessary for putting a `CollapsingHeader` in a grid).
ui.vertical(|ui| {
let Prepared {
id,
header_response, header_response,
body_response: Some(ret_response.response), mut state,
body_returned: Some(ret_response.inner), } = 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 { .inner
header_response,
body_response: None,
body_returned: None,
}
}
}) })
.inner .inner
} }