Add Ui::push_id (#1374)
This commit is contained in:
parent
f6af7bda27
commit
12c31e980b
2 changed files with 25 additions and 3 deletions
|
@ -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)).
|
||||
|
|
|
@ -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:
|
||||
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue