From 224af23fd1efa2820114a431fcfd45c41fed6600 Mon Sep 17 00:00:00 2001 From: Ezra Barrow <41446523+barrowsys@users.noreply.github.com> Date: Wed, 21 Jul 2021 05:43:02 -0400 Subject: [PATCH] Pass more inner return values (#557) * add Window.show_with_return * Fixed all missed opportunities to pass an inner return value --- CHANGELOG.md | 7 +++ egui/src/containers/area.rs | 11 +++-- egui/src/containers/combo_box.rs | 66 +++++++++++++++---------- egui/src/containers/popup.rs | 84 ++++++++++++++++++++++---------- egui/src/containers/window.rs | 35 ++++++++----- egui/src/menu.rs | 56 ++++++++++++++------- egui/src/widgets/color_picker.rs | 3 +- 7 files changed, 176 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8df010d..051c0c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ ## Unreleased +* Replaced all missed opportunities to return an inner return value. (this is a breaking change!) + * `Area::show` + * `ComboBox::show_ui` + * `ComboBox::combo_box_with_label` + * `Window::show` + * `popup::*` + * `menu::menu` ### Added ⭐ * Plot: diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index bf90c255..e6e3dcba 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -219,11 +219,16 @@ impl Area { } } - pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) -> Response { + pub fn show( + self, + ctx: &CtxRef, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { let prepared = self.begin(ctx); let mut content_ui = prepared.content_ui(ctx); - add_contents(&mut content_ui); - prepared.end(ctx, content_ui) + let inner = add_contents(&mut content_ui); + let response = prepared.end(ctx, content_ui); + InnerResponse { inner, response } } pub fn show_open_close_animation(&self, ctx: &CtxRef, frame: &Frame, is_open: bool) { diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index 042ff345..acb41398 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -63,7 +63,13 @@ impl ComboBox { } /// Show the combo box, with the given ui code for the menu contents. - pub fn show_ui(self, ui: &mut Ui, menu_contents: impl FnOnce(&mut Ui)) -> Response { + /// + /// Returns `InnerResponse { inner: None }` if the combo box is closed. + pub fn show_ui( + self, + ui: &mut Ui, + menu_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse> { let Self { id_source, label, @@ -77,14 +83,16 @@ impl ComboBox { if let Some(width) = width { ui.spacing_mut().slider_width = width; // yes, this is ugly. Will remove later. } - let mut response = combo_box(ui, button_id, selected_text, menu_contents); + let mut ir = combo_box(ui, button_id, selected_text, menu_contents); if let Some(label) = label { - response.widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, label.text())); - response |= ui.add(label); + ir.response + .widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, label.text())); + ir.response |= ui.add(label); } else { - response.widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, "")); + ir.response + .widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, "")); } - response + ir }) .inner } @@ -117,14 +125,16 @@ impl ComboBox { let mut changed = false; - let mut response = slf.show_ui(ui, |ui| { - for i in 0..len { - if ui.selectable_label(i == *selected, get(i)).clicked() { - *selected = i; - changed = true; + let mut response = slf + .show_ui(ui, |ui| { + for i in 0..len { + if ui.selectable_label(i == *selected, get(i)).clicked() { + *selected = i; + changed = true; + } } - } - }); + }) + .response; if changed { response.mark_changed(); @@ -137,6 +147,8 @@ impl ComboBox { /// /// Deprecated! Use [`ComboBox`] instead! /// +/// Returns `InnerResponse { inner: None }` if the combo box is closed. +/// /// ``` /// # #[derive(Debug, PartialEq)] /// # enum Enum { First, Second, Third } @@ -149,31 +161,32 @@ impl ComboBox { /// }); /// ``` #[deprecated = "Use egui::ComboBox::from_label instead"] -pub fn combo_box_with_label( +pub fn combo_box_with_label( ui: &mut Ui, label: impl Into