From a8c3deaf084239412571d8e9d2b8e2d6ec86df30 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 9 May 2021 13:17:32 +0200 Subject: [PATCH] Rename `ui.wrap` to `ui.scope` --- CHANGELOG.md | 9 +++---- egui/src/containers/collapsing_header.rs | 4 ++-- egui/src/lib.rs | 13 ++++++---- egui/src/ui.rs | 25 ++++++++++++++++---- egui/src/widgets/button.rs | 2 +- egui_demo_lib/src/apps/color_test.rs | 6 ++--- egui_demo_lib/src/apps/demo/drag_and_drop.rs | 2 +- 7 files changed, 41 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc53b5f6..a6d1950e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,21 +11,22 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ * Add anchors to windows and areas so you can put a window in e.g. the top right corner. * Make labels interactive with `Label::sense(Sense::click())`. * Add `Response::request_focus` and `Response::surrender_focus`. -* Add `TextEdit::code_editor` (VERY basic) +* Add `TextEdit::code_editor` (VERY basic). * [Pan and zoom plots](https://github.com/emilk/egui/pull/317). -* [Add plot legends](https://github.com/emilk/egui/pull/349) -* [Users can now store custom state in `egui::Memory`.](https://github.com/emilk/egui/pull/257). +* [Add plot legends](https://github.com/emilk/egui/pull/349). +* [Users can now store custom state in `egui::Memory`](https://github.com/emilk/egui/pull/257). * Add `Response::on_disabled_hover_text` to show tooltip for disabled widgets. * Zoom input: ctrl-scroll and (on `egui_web`) trackpad-pinch gesture. * Support for raw [multi touch](https://github.com/emilk/egui/pull/306) events, enabling zoom, rotate, and more. Works with `egui_web` on mobile devices, and should work with `egui_glium` for certain touch devices/screens. -* Add (optional) compatability with [mint](https://docs.rs/mint) +* Add (optional) compatability with [mint](https://docs.rs/mint). ### Changed 🔧 * Make `Memory::has_focus` public (again). * `Plot` must now be given a name that is unique within its scope. * Tab only selects labels if the `screen_reader` option is turned on. +* Rename `ui.wrap` to `ui.scope`. ### Fixed 🐛 * Fix [defocus-bug on touch screens](https://github.com/emilk/egui/issues/288). diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index b7bdc8f5..18375bb8 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -67,7 +67,7 @@ impl State { if openness <= 0.0 { None } else if openness < 1.0 { - Some(ui.wrap(|child_ui| { + Some(ui.scope(|child_ui| { let max_height = if self.open && self.open_height.is_none() { // First frame of expansion. // We don't know full height yet, but we will next frame. @@ -93,7 +93,7 @@ impl State { ret })) } else { - let ret_response = ui.wrap(add_contents); + let ret_response = ui.scope(add_contents); let full_size = ret_response.response.rect.size(); self.open_height = Some(full_size.y); Some(ret_response) diff --git a/egui/src/lib.rs b/egui/src/lib.rs index a6e92a3a..56a3d9e6 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -229,11 +229,16 @@ //! ui.set_min_height(200.0); //! }); //! -//! // Change test color on subsequent widgets: -//! ui.visuals_mut().override_text_color = Some(egui::Color32::RED); +//! // A `scope` creates a temporary [`Ui`] in which you can change settings: +//! ui.scope(|ui|{ +//! // Change test color on subsequent widgets: +//! ui.visuals_mut().override_text_color = Some(egui::Color32::RED); //! -//! // Turn off text wrapping on subsequent widgets: -//! ui.style_mut().wrap = Some(false); +//! // Turn off text wrapping on subsequent widgets: +//! ui.style_mut().wrap = Some(false); +//! +//! ui.label("This text will be red, and won't wrap to a new line"); +//! }); // the temporary settings are reverted here //! ``` #![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 957fc707..996093e5 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -1195,8 +1195,18 @@ impl Ui { crate::Frame::group(self.style()).show(self, add_contents) } - /// 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) -> InnerResponse { + /// Create a scoped child ui. + /// + /// You can use this to temporarily change the [`Style`] of a sub-region, for instance: + /// + /// ``` + /// # let ui = &mut egui::Ui::__test(); + /// ui.scope(|ui|{ + /// ui.spacing_mut().slider_width = 200.0; // Temporary change + /// // … + /// }); + /// ``` + pub fn scope(&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); @@ -1204,13 +1214,18 @@ impl Ui { InnerResponse::new(ret, response) } + #[deprecated = "Renamed scope()"] + pub fn wrap(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { + self.scope(add_contents) + } + /// Redirect shapes to another paint layer. pub fn with_layer_id( &mut self, layer_id: LayerId, add_contents: impl FnOnce(&mut Self) -> R, ) -> InnerResponse { - self.wrap(|ui| { + self.scope(|ui| { ui.painter.set_layer_id(layer_id); add_contents(ui) }) @@ -1324,7 +1339,7 @@ impl Ui { text_style: TextStyle, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { - self.wrap(|ui| { + self.scope(|ui| { let row_height = ui.fonts().row_height(text_style); let space_width = ui.fonts().glyph_width(text_style, ' '); let spacing = ui.spacing_mut(); @@ -1368,7 +1383,7 @@ impl Ui { text_style: TextStyle, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { - self.wrap(|ui| { + self.scope(|ui| { let row_height = ui.fonts().row_height(text_style); let space_width = ui.fonts().glyph_width(text_style, ' '); let spacing = ui.spacing_mut(); diff --git a/egui/src/widgets/button.rs b/egui/src/widgets/button.rs index 68af1e1e..e0739c95 100644 --- a/egui/src/widgets/button.rs +++ b/egui/src/widgets/button.rs @@ -180,7 +180,7 @@ impl Widget for Button { self.enabled_ui(ui) } else { // We need get a temporary disabled `Ui` to get that grayed out look: - ui.wrap(|ui| { + ui.scope(|ui| { ui.set_enabled(false); self.enabled_ui(ui) }) diff --git a/egui_demo_lib/src/apps/color_test.rs b/egui_demo_lib/src/apps/color_test.rs index b9dd4baf..f7ea8fec 100644 --- a/egui_demo_lib/src/apps/color_test.rs +++ b/egui_demo_lib/src/apps/color_test.rs @@ -70,7 +70,7 @@ impl ColorTest { ui.heading("sRGB color test"); ui.label("Use a color picker to ensure this color is (255, 165, 0) / #ffa500"); - ui.wrap(|ui| { + ui.scope(|ui| { ui.spacing_mut().item_spacing.y = 0.0; // No spacing between gradients let g = Gradient::one_color(Color32::from_rgb(255, 165, 0)); self.vertex_gradient(ui, "orange rgb(255, 165, 0) - vertex", WHITE, &g); @@ -86,7 +86,7 @@ impl ColorTest { ui.separator(); ui.label("Test that vertex color times texture color is done in linear space:"); - ui.wrap(|ui| { + ui.scope(|ui| { ui.spacing_mut().item_spacing.y = 0.0; // No spacing between gradients let tex_color = Rgba::from_rgb(1.0, 0.25, 0.25); @@ -185,7 +185,7 @@ impl ColorTest { show_color(ui, right, color_size); }); - ui.wrap(|ui| { + ui.scope(|ui| { ui.spacing_mut().item_spacing.y = 0.0; // No spacing between gradients if is_opaque { let g = Gradient::ground_truth_linear_gradient(left, right); 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 36c44240..658bd179 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).response; + let response = ui.scope(body).response; // Check for drags: let response = ui.interact(response.rect, id, Sense::drag());