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

Allow manually painting the CollapsingState icon #1592

Merged
merged 3 commits into from
May 11, 2022
Merged
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
42 changes: 41 additions & 1 deletion egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ impl CollapsingState {

/// Will toggle when clicked, etc.
fn show_default_button_indented(&mut self, ui: &mut Ui) -> Response {
self.show_button_indented(ui, paint_default_icon)
}

/// Will toggle when clicked, etc.
fn show_button_indented(
&mut self,
ui: &mut Ui,
icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static,
) -> Response {
let size = Vec2::new(ui.spacing().indent, ui.spacing().icon_width);
let (_id, rect) = ui.allocate_space(size);
let response = ui.interact(rect, self.id, Sense::click());
Expand All @@ -106,7 +115,7 @@ impl CollapsingState {
rect: icon_rect,
..response.clone()
};
paint_default_icon(ui, openness, &small_icon_response);
icon_fn(ui, openness, &small_icon_response);
response
}

Expand Down Expand Up @@ -217,6 +226,37 @@ impl CollapsingState {
Some(ret_response)
}
}

/// Paint this [CollapsingState](CollapsingState)'s toggle button. Takes an [IconPainter](IconPainter) as the icon.
/// ```
/// # egui::__run_test_ui(|ui| {
/// fn circle_icon(ui: &mut egui::Ui, openness: f32, response: &egui::Response) {
/// let stroke = ui.style().interact(&response).fg_stroke;
/// let radius = egui::lerp(2.0..=3.0, openness);
/// ui.painter().circle_filled(response.rect.center(), radius, stroke.color);
/// }
///
/// let mut state = egui::collapsing_header::CollapsingState::load_with_default_open(
/// ui.ctx(),
/// ui.make_persistent_id("my_collapsing_state"),
/// false,
/// );
///
/// let header_res = ui.horizontal(|ui| {
/// ui.label("Header");
/// state.show_toggle_button(ui, circle_icon);
/// });
///
/// state.show_body_indented(&header_res.response, ui, |ui| ui.label("Body"));
/// # });
/// ```
pub fn show_toggle_button(
&mut self,
ui: &mut Ui,
icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static,
) -> Response {
self.show_button_indented(ui, icon_fn)
}
}

/// From [`CollapsingState::show_header`].
Expand Down