From 66ebf4e2287eb44f0a769befa29e65c41acfb711 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 18 Mar 2022 14:44:20 +0100 Subject: [PATCH] Add Ui::push_id --- CHANGELOG.md | 3 ++- egui/src/ui.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc18f40ced..6678da2cb1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,10 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w ## Unreleased ### Added ⭐ -* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)). +* Added `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)). * Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)). * `Context::request_repaint` will wake up UI thread, if integrations has called `Context::set_request_repaint_callback` ([#1366](https://github.com/emilk/egui/pull/1366)). +* Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)). ### Changed 🔧 * `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)). diff --git a/egui/src/ui.rs b/egui/src/ui.rs index eb44259341d..ae4a96011f1 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -1546,6 +1546,26 @@ impl Ui { crate::Frame::group(self.style()).show(self, add_contents) } + /// Create a child Ui with an explicit [`Id`]. + /// + /// ``` + /// # egui::__run_test_ui(|ui| { + /// for i in 0..10 { + /// // `ui.make_persistent_id("foo")` here will produce the same id each loop. + /// ui.push_id(i, |ui| { + /// // `ui.make_persistent_id("foo")` here will produce different id:s + /// }); + /// } + /// # }); + /// ``` + pub fn push_id( + &mut self, + id_source: impl Hash, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { + self.scope_dyn(Box::new(add_contents), Id::new(id_source)) + } + /// Create a scoped child ui. /// /// You can use this to temporarily change the [`Style`] of a sub-region, for instance: @@ -1559,16 +1579,17 @@ impl Ui { /// # }); /// ``` pub fn scope(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { - self.scope_dyn(Box::new(add_contents)) + self.scope_dyn(Box::new(add_contents), Id::new("child")) } fn scope_dyn<'c, R>( &mut self, add_contents: Box R + 'c>, + id_source: Id, ) -> InnerResponse { let child_rect = self.available_rect_before_wrap(); let next_auto_id_source = self.next_auto_id_source; - let mut child_ui = self.child_ui(child_rect, *self.layout()); + let mut child_ui = self.child_ui_with_id_source(child_rect, *self.layout(), id_source); self.next_auto_id_source = next_auto_id_source; // HACK: we want `scope` to only increment this once, so that `ui.scope` is equivalent to `ui.allocate_space`. let ret = add_contents(&mut child_ui); let response = self.allocate_rect(child_ui.min_rect(), Sense::hover());