From d6c813e9badf133ade0deb50a9375c94592e86f4 Mon Sep 17 00:00:00 2001 From: lucasmerlin Date: Thu, 17 Apr 2025 00:41:11 +0200 Subject: [PATCH] Allow customizing id --- crates/egui/src/widget_layout.rs | 13 ++++++++++++- crates/egui/src/widgets/button.rs | 4 +++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/widget_layout.rs b/crates/egui/src/widget_layout.rs index b77e4a7df..12ad2f985 100644 --- a/crates/egui/src/widget_layout.rs +++ b/crates/egui/src/widget_layout.rs @@ -26,6 +26,7 @@ impl SizedAtomicKind<'_> { /// AtomicLayout pub struct WidgetLayout<'a> { + id: Option, pub atomics: Atomics<'a>, gap: Option, pub(crate) frame: Frame, @@ -37,6 +38,7 @@ pub struct WidgetLayout<'a> { impl<'a> WidgetLayout<'a> { pub fn new(atomics: impl IntoAtomics<'a>) -> Self { Self { + id: None, atomics: atomics.into_atomics(), gap: None, frame: Frame::default(), @@ -77,8 +79,14 @@ impl<'a> WidgetLayout<'a> { self } + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + pub fn show(self, ui: &mut Ui) -> AtomicLayoutResponse { let Self { + id, atomics, gap, frame, @@ -87,6 +95,8 @@ impl<'a> WidgetLayout<'a> { min_size, } = self; + let id = id.unwrap_or_else(|| ui.next_auto_id()); + let fallback_text_color = self .fallback_text_color .unwrap_or_else(|| ui.style().visuals.text_color()); @@ -178,7 +188,8 @@ impl<'a> WidgetLayout<'a> { let content_size = Vec2::new(desired_width, height); let frame_size = (content_size + margin.sum()).at_least(min_size); - let (rect, response) = ui.allocate_at_least(frame_size, sense); + let (_, rect) = ui.allocate_space(frame_size); + let response = ui.interact(rect, id, sense); let mut response = AtomicLayoutResponse { response, diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index 5e10e6ee8..3d15dc2a5 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -259,7 +259,9 @@ impl<'a> Button<'a> { } } - let response = ui.ctx().read_response(ui.next_auto_id()); + let id = ui.next_auto_id().with("egui::button"); + wl = wl.id(id); + let response = ui.ctx().read_response(id); let visuals = response.map_or(&ui.style().visuals.widgets.inactive, |response| { ui.style().interact(&response)