Refactor Ui::next_auto_id_source

This commit is contained in:
Emil Ernerfeldt 2021-04-21 22:19:51 +02:00
parent a59c8ac2c3
commit 07da54ad08

View file

@ -38,7 +38,7 @@ pub struct Ui {
/// and the value is increment with each added child widget. /// and the value is increment with each added child widget.
/// This works as an Id source only as long as new widgets aren't added or removed. /// This works as an Id source only as long as new widgets aren't added or removed.
/// They are therefore only good for Id:s that has no state. /// They are therefore only good for Id:s that has no state.
next_auto_id: u64, next_auto_id_source: u64,
/// Specifies paint layer, clip rectangle and a reference to `Context`. /// Specifies paint layer, clip rectangle and a reference to `Context`.
painter: Painter, painter: Painter,
@ -64,7 +64,7 @@ impl Ui {
let style = ctx.style(); let style = ctx.style();
Ui { Ui {
id, id,
next_auto_id: id.with("auto").value(), next_auto_id_source: id.with("auto").value(),
painter: Painter::new(ctx, layer_id, clip_rect), painter: Painter::new(ctx, layer_id, clip_rect),
style, style,
placer: Placer::new(max_rect, Layout::default()), placer: Placer::new(max_rect, Layout::default()),
@ -74,11 +74,12 @@ impl Ui {
pub fn child_ui(&mut self, max_rect: Rect, layout: Layout) -> Self { pub fn child_ui(&mut self, max_rect: Rect, layout: Layout) -> Self {
debug_assert!(!max_rect.any_nan()); debug_assert!(!max_rect.any_nan());
self.next_auto_id = self.next_auto_id.wrapping_add(1); let next_auto_id_source = Id::new(self.next_auto_id_source).with("child").value();
self.next_auto_id_source = self.next_auto_id_source.wrapping_add(1);
Ui { Ui {
id: self.id.with("child"), id: self.id.with("child"),
next_auto_id: Id::new(self.next_auto_id).with("child").value(), next_auto_id_source,
painter: self.painter.clone(), painter: self.painter.clone(),
style: self.style.clone(), style: self.style.clone(),
placer: Placer::new(max_rect, layout), placer: Placer::new(max_rect, layout),
@ -457,20 +458,15 @@ impl Ui {
self.id.with(&id_source) self.id.with(&id_source)
} }
#[deprecated = "This id now returned from ui.allocate_space"]
pub fn make_position_id(&self) -> Id {
Id::new(self.next_auto_id)
}
pub(crate) fn next_auto_id(&self) -> Id { pub(crate) fn next_auto_id(&self) -> Id {
Id::new(self.next_auto_id.wrapping_add(1)) Id::new(self.next_auto_id_source)
} }
pub(crate) fn auto_id_with<IdSource>(&self, id_source: IdSource) -> Id pub(crate) fn auto_id_with<IdSource>(&self, id_source: IdSource) -> Id
where where
IdSource: Hash + std::fmt::Debug, IdSource: Hash + std::fmt::Debug,
{ {
Id::new(self.next_auto_id).with(id_source) Id::new(self.next_auto_id_source).with(id_source)
} }
} }
@ -633,8 +629,8 @@ impl Ui {
} }
} }
self.next_auto_id = self.next_auto_id.wrapping_add(1); let id = Id::new(self.next_auto_id_source);
let id = Id::new(self.next_auto_id); self.next_auto_id_source = self.next_auto_id_source.wrapping_add(1);
(id, rect) (id, rect)
} }
@ -671,8 +667,9 @@ impl Ui {
self.placer.debug_paint_cursor(&painter); self.placer.debug_paint_cursor(&painter);
} }
self.next_auto_id = self.next_auto_id.wrapping_add(1); let id = Id::new(self.next_auto_id_source);
Id::new(self.next_auto_id) self.next_auto_id_source = self.next_auto_id_source.wrapping_add(1);
id
} }
pub(crate) fn placer(&self) -> &Placer { pub(crate) fn placer(&self) -> &Placer {