diff --git a/CHANGELOG.md b/CHANGELOG.md index 2251e3b3..b817ea51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added ⭐ * `SelectableLabel` (`ui.selectable_label` and `ui.selectable_value`): a text-button that can be selected +* Add `Resize::id_source` and `ScrollArea::id_source` to let the user avoid Id clashes. + +### Changed 🔧 * Changed default font to [Ubuntu-Light](https://fonts.google.com/specimen/Ubuntu). diff --git a/egui/src/containers/resize.rs b/egui/src/containers/resize.rs index ce0118f7..7585dc9f 100644 --- a/egui/src/containers/resize.rs +++ b/egui/src/containers/resize.rs @@ -20,6 +20,7 @@ pub(crate) struct State { #[derive(Clone, Copy, Debug)] pub struct Resize { id: Option, + id_source: Option, /// If false, we are no enabled resizable: bool, @@ -36,6 +37,7 @@ impl Default for Resize { fn default() -> Self { Self { id: None, + id_source: None, resizable: true, min_size: Vec2::splat(16.0), max_size: Vec2::splat(f32::INFINITY), @@ -52,6 +54,12 @@ impl Resize { self } + /// A source for the unique `Id`, e.g. `.id_source("second_resize_area")` or `.id_source(loop_index)`. + pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self { + self.id_source = Some(Id::new(id_source)); + self + } + /// Preferred / suggested width. Actual width will depend on contents. /// /// Examples: @@ -146,7 +154,10 @@ struct Prepared { impl Resize { fn begin(&mut self, ui: &mut Ui) -> Prepared { let position = ui.available().min; - let id = self.id.unwrap_or_else(|| ui.make_persistent_id("resize")); + let id = self.id.unwrap_or_else(|| { + let id_source = self.id_source.unwrap_or_else(|| Id::new("resize")); + ui.make_persistent_id(id_source) + }); let mut state = ui.memory().resize.get(&id).cloned().unwrap_or_else(|| { ui.ctx().request_repaint(); // counter frame delay diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 92a645aa..ded2c6e0 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -30,6 +30,7 @@ impl Default for State { pub struct ScrollArea { max_height: f32, always_show_scroll: bool, + id_source: Option, } impl ScrollArea { @@ -43,6 +44,7 @@ impl ScrollArea { Self { max_height, always_show_scroll: false, + id_source: None, } } @@ -52,6 +54,12 @@ impl ScrollArea { self.always_show_scroll = always_show_scroll; self } + + /// A source for the unique `Id`, e.g. `.id_source("second_scroll_area")` or `.id_source(loop_index)`. + pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self { + self.id_source = Some(Id::new(id_source)); + self + } } struct Prepared { @@ -68,11 +76,13 @@ impl ScrollArea { let Self { max_height, always_show_scroll, + id_source, } = self; let ctx = ui.ctx().clone(); - let id = ui.make_persistent_id("scroll_area"); + let id_source = id_source.unwrap_or_else(|| Id::new("scroll_area")); + let id = ui.make_persistent_id(id_source); let state = ctx .memory() .scroll_areas diff --git a/egui/src/widgets/text_edit.rs b/egui/src/widgets/text_edit.rs index f687df39..b295b257 100644 --- a/egui/src/widgets/text_edit.rs +++ b/egui/src/widgets/text_edit.rs @@ -169,6 +169,7 @@ impl<'t> TextEdit<'t> { self } + /// A source for the unique `Id`, e.g. `.id_source("second_text_edit_field")` or `.id_source(loop_index)`. pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self { self.id_source = Some(Id::new(id_source)); self