Skip to content

Commit 66ebf4e

Browse files
committed
Add Ui::push_id
1 parent c69f39e commit 66ebf4e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
77
## Unreleased
88

99
### Added ⭐
10-
* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)).
10+
* Added `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)).
1111
* Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)).
1212
* `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)).
13+
* Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)).
1314

1415
### Changed 🔧
1516
* `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)).

egui/src/ui.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,26 @@ impl Ui {
15461546
crate::Frame::group(self.style()).show(self, add_contents)
15471547
}
15481548

1549+
/// Create a child Ui with an explicit [`Id`].
1550+
///
1551+
/// ```
1552+
/// # egui::__run_test_ui(|ui| {
1553+
/// for i in 0..10 {
1554+
/// // `ui.make_persistent_id("foo")` here will produce the same id each loop.
1555+
/// ui.push_id(i, |ui| {
1556+
/// // `ui.make_persistent_id("foo")` here will produce different id:s
1557+
/// });
1558+
/// }
1559+
/// # });
1560+
/// ```
1561+
pub fn push_id<R>(
1562+
&mut self,
1563+
id_source: impl Hash,
1564+
add_contents: impl FnOnce(&mut Ui) -> R,
1565+
) -> InnerResponse<R> {
1566+
self.scope_dyn(Box::new(add_contents), Id::new(id_source))
1567+
}
1568+
15491569
/// Create a scoped child ui.
15501570
///
15511571
/// You can use this to temporarily change the [`Style`] of a sub-region, for instance:
@@ -1559,16 +1579,17 @@ impl Ui {
15591579
/// # });
15601580
/// ```
15611581
pub fn scope<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
1562-
self.scope_dyn(Box::new(add_contents))
1582+
self.scope_dyn(Box::new(add_contents), Id::new("child"))
15631583
}
15641584

15651585
fn scope_dyn<'c, R>(
15661586
&mut self,
15671587
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
1588+
id_source: Id,
15681589
) -> InnerResponse<R> {
15691590
let child_rect = self.available_rect_before_wrap();
15701591
let next_auto_id_source = self.next_auto_id_source;
1571-
let mut child_ui = self.child_ui(child_rect, *self.layout());
1592+
let mut child_ui = self.child_ui_with_id_source(child_rect, *self.layout(), id_source);
15721593
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`.
15731594
let ret = add_contents(&mut child_ui);
15741595
let response = self.allocate_rect(child_ui.min_rect(), Sense::hover());

0 commit comments

Comments
 (0)