Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ui::push_id #1374

Merged
merged 1 commit into from Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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)).
Expand Down
25 changes: 23 additions & 2 deletions egui/src/ui.rs
Expand Up @@ -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<R>(
&mut self,
id_source: impl Hash,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
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:
Expand All @@ -1559,16 +1579,17 @@ impl Ui {
/// # });
/// ```
pub fn scope<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
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<dyn FnOnce(&mut Ui) -> R + 'c>,
id_source: Id,
) -> InnerResponse<R> {
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());
Expand Down