`Area::new` now takes an `Id` by argument (#4115)

This makes it more explicit that you are responsible for assigning a
globally unique `Id`.
This commit is contained in:
Emil Ernerfeldt 2024-02-29 15:34:16 +01:00 committed by GitHub
parent 86d7f296ae
commit e29022efc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 11 deletions

View File

@ -52,7 +52,7 @@ impl State {
///
/// ```
/// # egui::__run_test_ctx(|ctx| {
/// egui::Area::new("my_area")
/// egui::Area::new(egui::Id::new("my_area"))
/// .fixed_pos(egui::pos2(32.0, 32.0))
/// .show(ctx, |ui| {
/// ui.label("Floating text!");
@ -79,9 +79,10 @@ pub struct Area {
}
impl Area {
pub fn new(id: impl Into<Id>) -> Self {
/// The `id` must be globally unique.
pub fn new(id: Id) -> Self {
Self {
id: id.into(),
id,
movable: true,
interactable: true,
constrain: false,
@ -96,6 +97,9 @@ impl Area {
}
}
/// Let's you change the `id` that you assigned in [`Self::new`].
///
/// The `id` must be globally unique.
#[inline]
pub fn id(mut self, id: Id) -> Self {
self.id = id;

View File

@ -69,30 +69,25 @@ impl super::View for PanZoom {
}
}
for (id, pos, callback) in [
for (i, (pos, callback)) in [
(
"a",
egui::Pos2::new(0.0, 0.0),
Box::new(|ui: &mut egui::Ui, _: &mut Self| ui.button("top left!"))
as Box<dyn Fn(&mut egui::Ui, &mut Self) -> egui::Response>,
),
(
"b",
egui::Pos2::new(0.0, 120.0),
Box::new(|ui: &mut egui::Ui, _| ui.button("bottom left?")),
),
(
"c",
egui::Pos2::new(120.0, 120.0),
Box::new(|ui: &mut egui::Ui, _| ui.button("right bottom :D")),
),
(
"d",
egui::Pos2::new(120.0, 0.0),
Box::new(|ui: &mut egui::Ui, _| ui.button("right top ):")),
),
(
"e",
egui::Pos2::new(60.0, 60.0),
Box::new(|ui, state| {
use egui::epaint::*;
@ -110,8 +105,11 @@ impl super::View for PanZoom {
ui.add(egui::Slider::new(&mut state.drag_value, 0.0..=100.0).text("My value"))
}),
),
] {
let id = egui::Area::new(id)
]
.into_iter()
.enumerate()
{
let id = egui::Area::new(id.with(("subarea", i)))
.default_pos(pos)
// Need to cover up the pan_zoom demo window,
// but may also cover over other windows.