From 8312d7fda82584433c9739fe6c8af952db23b9c8 Mon Sep 17 00:00:00 2001 From: 4JX <4JXcYvmyu3Hz8fV@protonmail.com> Date: Fri, 6 May 2022 20:34:42 +0200 Subject: [PATCH 1/3] Allow manually painting the CollapsingState icon --- egui/src/containers/collapsing_header.rs | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index b3741911847c..ae770dfc6d96 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -89,6 +89,14 @@ impl CollapsingState { /// Will toggle when clicked, etc. fn show_default_button_indented(&mut self, ui: &mut Ui) -> Response { + self.paint_button_indented(ui, paint_default_icon) + } + + fn paint_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()); @@ -106,7 +114,7 @@ impl CollapsingState { rect: icon_rect, ..response.clone() }; - paint_default_icon(ui, openness, &small_icon_response); + icon_fn(ui, openness, &small_icon_response); response } @@ -217,6 +225,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.paint_button_indented(ui, icon_fn) + } } /// From [`CollapsingState::show_header`]. From 8176f47a282f959e734cf4c1ff91d892830514d7 Mon Sep 17 00:00:00 2001 From: 4JX <79868816+4JX@users.noreply.github.com> Date: Wed, 11 May 2022 13:24:21 +0000 Subject: [PATCH 2/3] Update egui/src/containers/collapsing_header.rs Co-authored-by: Emil Ernerfeldt --- egui/src/containers/collapsing_header.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index ae770dfc6d96..c453815c3d00 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -92,7 +92,8 @@ impl CollapsingState { self.paint_button_indented(ui, paint_default_icon) } - fn paint_button_indented( + /// Will toggle when clicked, etc. + fn show_button_indented( &mut self, ui: &mut Ui, icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static, From c56c0fd3b4eb8555680af5c0fd11ec96934b22f4 Mon Sep 17 00:00:00 2001 From: 4JX <4JXcYvmyu3Hz8fV@protonmail.com> Date: Wed, 11 May 2022 16:22:15 +0200 Subject: [PATCH 3/3] Oops --- egui/src/containers/collapsing_header.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index c453815c3d00..17755a36bab1 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -89,7 +89,7 @@ impl CollapsingState { /// Will toggle when clicked, etc. fn show_default_button_indented(&mut self, ui: &mut Ui) -> Response { - self.paint_button_indented(ui, paint_default_icon) + self.show_button_indented(ui, paint_default_icon) } /// Will toggle when clicked, etc. @@ -255,7 +255,7 @@ impl CollapsingState { ui: &mut Ui, icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static, ) -> Response { - self.paint_button_indented(ui, icon_fn) + self.show_button_indented(ui, icon_fn) } }