From e82fb81f076f7e0f2cbecede39f335b8f4a5c424 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 7 Feb 2021 14:09:44 +0100 Subject: [PATCH] Replace (ret, response) tuples with new struct InnerResponse --- egui/src/containers/collapsing_header.rs | 18 +++--- egui/src/containers/combo_box.rs | 2 +- egui/src/containers/panel.rs | 24 ++++++-- egui/src/containers/window.rs | 12 ++-- egui/src/grid.rs | 2 +- egui/src/introspection.rs | 8 +-- egui/src/lib.rs | 2 +- egui/src/menu.rs | 2 +- egui/src/response.rs | 25 ++++++++ egui/src/style.rs | 2 +- egui/src/ui.rs | 62 +++++++++++--------- egui/src/widgets/button.rs | 2 +- egui/src/widgets/slider.rs | 2 +- egui_demo_lib/src/apps/demo/drag_and_drop.rs | 10 ++-- egui_demo_lib/src/apps/demo/toggle_switch.rs | 2 +- egui_demo_lib/src/apps/demo/widgets.rs | 2 +- 16 files changed, 109 insertions(+), 68 deletions(-) diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 59b3b6d7..62946b09 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -65,7 +65,7 @@ impl State { ui: &mut Ui, id: Id, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> Option<(R, Response)> { + ) -> Option> { let openness = self.openness(ui.ctx(), id); if openness <= 0.0 { None @@ -96,10 +96,10 @@ impl State { ret })) } else { - let (ret, response) = ui.wrap(add_contents); - let full_size = response.rect.size(); + let ret_response = ui.wrap(add_contents); + let full_size = ret_response.response.rect.size(); self.open_height = Some(full_size.y); - Some((ret, response)) + Some(ret_response) } } } @@ -258,15 +258,15 @@ impl CollapsingHeader { ui.expand_to_include_x(header_response.rect.right()); add_contents(ui) }) - .0 + .inner }); ui.memory().collapsing_headers.insert(id, state); - if let Some((ret, response)) = ret_response { + if let Some(ret_response) = ret_response { CollapsingResponse { header_response, - body_response: Some(response), - body_returned: Some(ret), + body_response: Some(ret_response.response), + body_returned: Some(ret_response.inner), } } else { CollapsingResponse { @@ -276,7 +276,7 @@ impl CollapsingHeader { } } }) - .0 + .inner } } diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index ef19b220..01f6d9dc 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -29,7 +29,7 @@ pub fn combo_box_with_label( response |= ui.add(label); response }) - .0 + .inner } /// A drop-down selection menu. diff --git a/egui/src/containers/panel.rs b/egui/src/containers/panel.rs index 9513f182..80b671a1 100644 --- a/egui/src/containers/panel.rs +++ b/egui/src/containers/panel.rs @@ -35,7 +35,11 @@ impl SidePanel { } impl SidePanel { - pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn show( + self, + ctx: &CtxRef, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { let Self { id, max_width } = self; let mut panel_rect = ctx.available_rect(); @@ -61,7 +65,7 @@ impl SidePanel { ctx.frame_state() .allocate_left_panel(used_space.expand2(frame.margin)); - (r, response) + InnerResponse::new(r, response) } } @@ -97,7 +101,11 @@ impl TopPanel { } impl TopPanel { - pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn show( + self, + ctx: &CtxRef, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { let Self { id, max_height } = self; let max_height = max_height.unwrap_or_else(|| ctx.style().spacing.interact_size.y); @@ -124,7 +132,7 @@ impl TopPanel { ctx.frame_state() .allocate_top_panel(used_space.expand2(frame.margin)); - (r, response) + InnerResponse::new(r, response) } } @@ -157,7 +165,11 @@ impl CentralPanel { } impl CentralPanel { - pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn show( + self, + ctx: &CtxRef, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { let Self { frame } = self; let panel_rect = ctx.available_rect(); @@ -184,6 +196,6 @@ impl CentralPanel { ctx.frame_state() .allocate_central_panel(used_space.expand2(frame.margin)); - (r, response) + InnerResponse::new(r, response) } } diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index fa6e8b32..c9b9b57b 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -333,7 +333,7 @@ impl<'open> Window<'open> { } }) }) - .map(|ri| ri.1); + .map(|ir| ir.response); let outer_rect = frame.end(&mut area_content_ui); @@ -670,7 +670,7 @@ fn show_title_bar( collapsing: &mut collapsing_header::State, collapsible: bool, ) -> TitleBar { - let (title_bar, response) = ui.horizontal(|ui| { + let inner_response = ui.horizontal(|ui| { let height = title_label .font_height(ui.fonts(), ui.style()) .max(ui.spacing().interact_size.y); @@ -713,10 +713,10 @@ fn show_title_bar( } }); - TitleBar { - rect: response.rect, - ..title_bar - } + let title_bar = inner_response.inner; + let rect = inner_response.response.rect; + + TitleBar { rect, ..title_bar } } impl TitleBar { diff --git a/egui/src/grid.rs b/egui/src/grid.rs index 785b1d88..4af3b6f8 100644 --- a/egui/src/grid.rs +++ b/egui/src/grid.rs @@ -328,6 +328,6 @@ impl Grid { ui.save_grid(); r }) - .0 + .inner } } diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index 36c6ab7f..cd02a449 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -50,7 +50,7 @@ impl Widget for &epaint::Texture { ui.painter().add(Shape::mesh(mesh)); }); }) - .1 + .response } } @@ -67,7 +67,7 @@ impl Widget for &mut epaint::text::FontDefinitions { } crate::reset_button(ui, self); }) - .1 + .response } } @@ -111,7 +111,7 @@ impl Widget for &epaint::stats::PaintStats { // ui.label("Total:"); // ui.label(self.total().format("")); }) - .1 + .response } } @@ -139,6 +139,6 @@ impl Widget for &mut epaint::TessellationOptions { ui.checkbox(debug_paint_clip_rects, "Paint clip rectangles (debug)"); ui.checkbox(debug_paint_text_rects, "Paint text bounds (debug)"); }) - .1 + .response } } diff --git a/egui/src/lib.rs b/egui/src/lib.rs index dfb0c568..b9c42947 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -124,7 +124,7 @@ pub use { layout::*, memory::Memory, painter::Painter, - response::Response, + response::{InnerResponse, Response}, sense::Sense, style::{Style, Visuals}, ui::Ui, diff --git a/egui/src/menu.rs b/egui/src/menu.rs index a6f6119a..4307825e 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -40,7 +40,7 @@ impl BarState { /// The menu bar goes well in `TopPanel`, /// but can also be placed in a `Window`. /// In the latter case you may want to wrap it in `Frame`. -pub fn bar(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { +pub fn bar(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { ui.horizontal(|ui| { let mut style = (**ui.style()).clone(); style.spacing.button_padding = vec2(2.0, 0.0); diff --git a/egui/src/response.rs b/egui/src/response.rs index b327f2af..a324e4a7 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -321,3 +321,28 @@ impl std::ops::BitOrAssign for Response { *self = self.union(rhs); } } + +// ---------------------------------------------------------------------------- + +/// Returned when we wrap some ui-code and want to return both +/// the results of the inner function and the ui as a whole, e.g.: +/// +/// ``` +/// # let ui = &mut egui::Ui::__test(); +/// let inner_resp = ui.horizontal(|ui| { +/// ui.label("Blah blah"); +/// 42 +/// }); +/// inner_resp.response.on_hover_text("You hovered the horizontal layout"); +/// assert_eq!(inner_resp.inner, 42); +/// ``` +pub struct InnerResponse { + pub inner: R, + pub response: Response, +} + +impl InnerResponse { + pub fn new(inner: R, response: Response) -> Self { + Self { inner, response } + } +} diff --git a/egui/src/style.rs b/egui/src/style.rs index dc07e522..74d41da9 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -709,7 +709,7 @@ fn ui_slider_vec2( ui.add(Slider::f32(&mut value.y, range.clone()).text("h")); ui.label(text); }) - .1 + .response } fn ui_color(ui: &mut Ui, srgba: &mut Color32, text: &str) { diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 3a5e37e3..1283028f 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -7,6 +7,8 @@ use crate::{ widgets::*, *, }; +// ---------------------------------------------------------------------------- + /// This is what you use to place widgets. /// /// Represents a region of the screen with a type of layout (horizontal or vertical). @@ -669,7 +671,7 @@ impl Ui { &mut self, desired_size: Vec2, add_contents: impl FnOnce(&mut Self) -> R, - ) -> (R, Response) { + ) -> InnerResponse { let item_spacing = self.spacing().item_spacing; let outer_child_rect = self.placer.next_space(desired_size, item_spacing); let inner_child_rect = self @@ -687,7 +689,7 @@ impl Ui { ); let response = self.interact(final_child_rect, child_ui.id, Sense::hover()); - (ret, response) + InnerResponse::new(ret, response) } /// Allocated the given rectangle and then adds content to that rectangle. @@ -698,7 +700,7 @@ impl Ui { &mut self, max_rect: Rect, add_contents: impl FnOnce(&mut Self) -> R, - ) -> (R, Response) { + ) -> InnerResponse { let mut child_ui = self.child_ui(max_rect, *self.layout()); let ret = add_contents(&mut child_ui); let final_child_rect = child_ui.min_rect(); @@ -710,7 +712,7 @@ impl Ui { ); let response = self.interact(final_child_rect, child_ui.id, Sense::hover()); - (ret, response) + InnerResponse::new(ret, response) } /// Convenience function to get a region to paint on @@ -763,17 +765,17 @@ impl Ui { /// Add a widget to this `Ui` with a given max size. pub fn add_sized(&mut self, max_size: Vec2, widget: impl Widget) -> Response { self.allocate_ui(max_size, |ui| { - ui.centered_and_justified(|ui| ui.add(widget)).0 + ui.centered_and_justified(|ui| ui.add(widget)).inner }) - .0 + .inner } /// Add a widget to this `Ui` at a specific location (manual layout). pub fn put(&mut self, max_rect: Rect, widget: impl Widget) -> Response { self.allocate_ui_at_rect(max_rect, |ui| { - ui.centered_and_justified(|ui| ui.add(widget)).0 + ui.centered_and_justified(|ui| ui.add(widget)).inner }) - .0 + .inner } /// Shortcut for `add(Label::new(text))` @@ -1043,13 +1045,13 @@ impl Ui { } /// Create a child ui. You can use this to temporarily change the Style of a sub-region, for instance. - pub fn wrap(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn wrap(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { let child_rect = self.available_rect_before_wrap(); let mut child_ui = self.child_ui(child_rect, *self.layout()); let ret = add_contents(&mut child_ui); let size = child_ui.min_size(); let response = self.allocate_response(size, Sense::hover()); - (ret, response) + InnerResponse::new(ret, response) } /// Redirect shapes to another paint layer. @@ -1057,7 +1059,7 @@ impl Ui { &mut self, layer_id: LayerId, add_contents: impl FnOnce(&mut Self) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.wrap(|ui| { ui.painter.set_layer_id(layer_id); add_contents(ui) @@ -1070,7 +1072,7 @@ impl Ui { desired_size: Vec2, add_contents: impl FnOnce(&mut Ui), ) -> Rect { - self.allocate_ui(desired_size, add_contents).1.rect + self.allocate_ui(desired_size, add_contents).response.rect } /// A `CollapsingHeader` that starts out collapsed. @@ -1087,7 +1089,7 @@ impl Ui { &mut self, id_source: impl Hash, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { assert!( self.layout().is_vertical(), "You can only indent vertical layouts, found {:?}", @@ -1123,7 +1125,7 @@ impl Ui { } let response = self.allocate_response(indent + size, Sense::hover()); - (ret, response) + InnerResponse::new(ret, response) } #[deprecated] @@ -1173,7 +1175,7 @@ impl Ui { /// The returned `Response` will only have checked for mouse hover /// but can be used for tooltips (`on_hover_text`). /// It also contains the `Rect` used by the horizontal layout. - pub fn horizontal(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn horizontal(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { self.horizontal_with_main_wrap(false, add_contents) } @@ -1186,7 +1188,7 @@ impl Ui { &mut self, text_style: TextStyle, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.wrap(|ui| { let font = &ui.fonts()[text_style]; let row_height = font.row_height(); @@ -1195,7 +1197,7 @@ impl Ui { spacing.interact_size.y = row_height; spacing.item_spacing.x = space_width; spacing.item_spacing.y = 0.0; - ui.horizontal(add_contents).0 + ui.horizontal(add_contents).inner }) } @@ -1215,7 +1217,7 @@ impl Ui { pub fn horizontal_wrapped( &mut self, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.horizontal_with_main_wrap(true, add_contents) } @@ -1230,7 +1232,7 @@ impl Ui { &mut self, text_style: TextStyle, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.wrap(|ui| { let font = &ui.fonts()[text_style]; let row_height = font.row_height(); @@ -1239,7 +1241,7 @@ impl Ui { spacing.interact_size.y = row_height; spacing.item_spacing.x = space_width; spacing.item_spacing.y = 0.0; - ui.horizontal_wrapped(add_contents).0 + ui.horizontal_wrapped(add_contents).inner }) } @@ -1247,7 +1249,7 @@ impl Ui { &mut self, main_wrap: bool, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { let initial_size = vec2( self.available_size_before_wrap_finite().x, self.spacing().interact_size.y, // Assume there will be something interactive on the horizontal layout @@ -1260,12 +1262,14 @@ impl Ui { } .with_main_wrap(main_wrap); - self.allocate_ui(initial_size, |ui| ui.with_layout(layout, add_contents).0) + self.allocate_ui(initial_size, |ui| { + ui.with_layout(layout, add_contents).inner + }) } /// Start a ui with vertical layout. /// Widgets will be left-justified. - pub fn vertical(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) { + pub fn vertical(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { self.with_layout(Layout::top_down(Align::Min), add_contents) } @@ -1274,7 +1278,7 @@ impl Ui { pub fn vertical_centered( &mut self, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.with_layout(Layout::top_down(Align::Center), add_contents) } @@ -1283,7 +1287,7 @@ impl Ui { pub fn vertical_centered_justified( &mut self, add_contents: impl FnOnce(&mut Ui) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.with_layout( Layout::top_down(Align::Center).with_cross_justify(true), add_contents, @@ -1294,20 +1298,20 @@ impl Ui { &mut self, layout: Layout, add_contents: impl FnOnce(&mut Self) -> R, - ) -> (R, Response) { + ) -> InnerResponse { let mut child_ui = self.child_ui(self.available_rect_before_wrap(), layout); - let ret = add_contents(&mut child_ui); + let inner = add_contents(&mut child_ui); let rect = child_ui.min_rect(); let item_spacing = self.spacing().item_spacing; self.placer.advance_after_rects(rect, rect, item_spacing); - (ret, self.interact(rect, child_ui.id, Sense::hover())) + InnerResponse::new(inner, self.interact(rect, child_ui.id, Sense::hover())) } /// This will make the next added widget centered and justified in the available space. pub fn centered_and_justified( &mut self, add_contents: impl FnOnce(&mut Self) -> R, - ) -> (R, Response) { + ) -> InnerResponse { self.with_layout( Layout::centered_and_justified(Direction::TopDown), add_contents, diff --git a/egui/src/widgets/button.rs b/egui/src/widgets/button.rs index b0fd356a..a581b38d 100644 --- a/egui/src/widgets/button.rs +++ b/egui/src/widgets/button.rs @@ -149,7 +149,7 @@ impl Widget for Button { ui.set_enabled(false); self.enabled_ui(ui) }) - .0 + .inner } } } diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 15f38e5b..fdbb72e1 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -413,7 +413,7 @@ impl<'a> Widget for Slider<'a> { self.label_ui(ui); slider_response }) - .0 + .inner } else { let response = self.allocate_slider_space(ui, height); self.slider_ui(ui, &response); diff --git a/egui_demo_lib/src/apps/demo/drag_and_drop.rs b/egui_demo_lib/src/apps/demo/drag_and_drop.rs index 78b26c72..c0a2ba07 100644 --- a/egui_demo_lib/src/apps/demo/drag_and_drop.rs +++ b/egui_demo_lib/src/apps/demo/drag_and_drop.rs @@ -4,7 +4,7 @@ pub fn drag_source(ui: &mut Ui, id: Id, body: impl FnOnce(&mut Ui)) { let is_being_dragged = ui.memory().is_being_dragged(id); if !is_being_dragged { - let response = ui.wrap(body).1; + let response = ui.wrap(body).response; // Check for drags: let response = ui.interact(response.rect, id, Sense::drag()); @@ -16,7 +16,7 @@ pub fn drag_source(ui: &mut Ui, id: Id, body: impl FnOnce(&mut Ui)) { // Paint the body to a new layer: let layer_id = LayerId::new(Order::Tooltip, id); - let response = ui.with_layer_id(layer_id, body).1; + let response = ui.with_layer_id(layer_id, body).response; // Now we move the visuals of the body to where the mouse is. // Normally you need to decide a location for a widget first, @@ -36,7 +36,7 @@ pub fn drop_target( ui: &mut Ui, can_accept_what_is_being_dragged: bool, body: impl FnOnce(&mut Ui) -> R, -) -> (R, Response) { +) -> InnerResponse { let is_being_dragged = ui.memory().is_anything_being_dragged(); let margin = Vec2::splat(4.0); @@ -73,7 +73,7 @@ pub fn drop_target( }, ); - (ret, response) + InnerResponse::new(ret, response) } pub struct DragAndDropDemo { @@ -135,7 +135,7 @@ impl super::View for DragAndDropDemo { } } }) - .1; + .response; let is_being_dragged = ui.memory().is_anything_being_dragged(); if is_being_dragged && can_accept_what_is_being_dragged && response.hovered() { diff --git a/egui_demo_lib/src/apps/demo/toggle_switch.rs b/egui_demo_lib/src/apps/demo/toggle_switch.rs index bf170068..aae23f0b 100644 --- a/egui_demo_lib/src/apps/demo/toggle_switch.rs +++ b/egui_demo_lib/src/apps/demo/toggle_switch.rs @@ -86,7 +86,7 @@ pub fn demo(ui: &mut egui::Ui, on: &mut bool) { toggle(ui, on).on_hover_text("Click to toggle"); ui.add(crate::__egui_github_link_file!()); }) - .1 + .response .on_hover_text( "It's easy to create your own widgets!\n\ This toggle switch is just one function and 20 lines of code.", diff --git a/egui_demo_lib/src/apps/demo/widgets.rs b/egui_demo_lib/src/apps/demo/widgets.rs index 4a593f5a..562bba57 100644 --- a/egui_demo_lib/src/apps/demo/widgets.rs +++ b/egui_demo_lib/src/apps/demo/widgets.rs @@ -112,7 +112,7 @@ impl Widgets { ui.label(format!("≈ {:.3}τ", self.angle / std::f32::consts::TAU)) .on_hover_text("Each τ represents one turn (τ = 2π)"); }) - .1 + .response .on_hover_text("The angle is stored in radians, but presented in degrees"); ui.separator();