From b30d20bca7fdcdbed3bf2e43d09a054ac115ec10 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Mon, 21 Sep 2026 15:09:01 +0200 Subject: [PATCH] Ensure nested `AtomLayout`s have unique ids (#8556) Buttons with nested atoms weren't fully clickable https://github.com/user-attachments/assets/c9bc7075-9b32-4edc-9949-5eb047234e07 --- crates/egui/src/atomics/container_atom.rs | 10 ++++++++- crates/egui/src/atomics/widget_atom.rs | 2 +- tests/egui_tests/tests/test_atoms.rs | 25 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/atomics/container_atom.rs b/crates/egui/src/atomics/container_atom.rs index 3a0f2100..e76b02a7 100644 --- a/crates/egui/src/atomics/container_atom.rs +++ b/crates/egui/src/atomics/container_atom.rs @@ -188,7 +188,15 @@ impl<'a> ContainerAtom<'a> { intrinsic_main += gap_space; } - for (idx, item) in atoms.into_iter().enumerate() { + // A nested `WidgetAtom` with no explicit id falls back to `Ui::next_auto_id`, which does + // not advance, so several of them in the same container would all get the same id, and + // only the last would be interactable. Give each one an id of its own: + let auto_id = ui.next_auto_id(); + + for (idx, mut item) in atoms.into_iter().enumerate() { + if let AtomKind::Widget(widget) = &mut item.kind { + widget.id.get_or_insert_with(|| auto_id.with(idx)); + } if item.grow { grow_count += 1; } diff --git a/crates/egui/src/atomics/widget_atom.rs b/crates/egui/src/atomics/widget_atom.rs index 698c51f9..3d6040a7 100644 --- a/crates/egui/src/atomics/widget_atom.rs +++ b/crates/egui/src/atomics/widget_atom.rs @@ -24,7 +24,7 @@ use smallvec::SmallVec; /// [`AllocatedWidgetAtom`] for interaction styling. #[derive(Clone)] pub struct WidgetAtom<'a> { - id: Option, + pub(crate) id: Option, pub(crate) sense: Sense, selectable: bool, pub container: ContainerAtom<'a>, diff --git a/tests/egui_tests/tests/test_atoms.rs b/tests/egui_tests/tests/test_atoms.rs index bffd6b52..94f271aa 100644 --- a/tests/egui_tests/tests/test_atoms.rs +++ b/tests/egui_tests/tests/test_atoms.rs @@ -5,6 +5,31 @@ use egui::{ }; use egui_kittest::{HarnessBuilder, SnapshotResult, SnapshotResults}; +/// Two nested [`WidgetAtom`]s without explicit ids must not share an id, +/// or the parent button is only clickable where the last of them is. +#[test] +fn test_button_with_nested_widget_atoms_is_clickable() { + use egui_kittest::kittest::Queryable as _; + + let mut harness = HarnessBuilder::default().build_ui_state( + |ui, clicks| { + // The text lives in the nested atoms, so name the button itself: + if ui + .button((WidgetAtom::new("abcdef"), WidgetAtom::new("123"))) + .accessible_name("abcdef 123") + .clicked() + { + *clicks += 1; + } + }, + 0, + ); + + harness.get_by_role(egui::accesskit::Role::Button).click(); + harness.run(); + assert_eq!(*harness.state(), 1); +} + #[test] fn test_atoms() { let mut results = SnapshotResults::new();