From 48d903d8797d0869b5d2b43a22346dede7d471d2 Mon Sep 17 00:00:00 2001 From: Gijs de Jong <14833076+oxkitsune@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:03:30 +0200 Subject: [PATCH] Include popups and tooltips in `Harness::fit_contents` (#7556) This makes `Harness::fit_contents` also use popups and tooltips to compute the size of the contents. --- crates/egui_kittest/src/lib.rs | 29 +++++++++++++++++-- .../tests/snapshots/test_tooltip_hidden.png | 3 ++ .../tests/snapshots/test_tooltip_shown.png | 3 ++ crates/egui_kittest/tests/tests.rs | 22 ++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 crates/egui_kittest/tests/snapshots/test_tooltip_hidden.png create mode 100644 crates/egui_kittest/tests/snapshots/test_tooltip_shown.png diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index 0f99da17..60d689c1 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -276,14 +276,39 @@ impl<'a, State> Harness<'a, State> { self.output = output; } + /// Calculate the rect that includes all popups and tooltips. + fn compute_total_rect_with_popups(&self) -> Option { + // Start with the standard response rect + let mut used = if let Some(response) = self.response.as_ref() { + response.rect + } else { + return None; + }; + + // Add all visible areas from other orders (popups, tooltips, etc.) + self.ctx.memory(|mem| { + mem.areas() + .visible_layer_ids() + .into_iter() + .filter(|layer_id| layer_id.order != egui::Order::Background) + .filter_map(|layer_id| mem.area_rect(layer_id.id)) + .for_each(|area_rect| used |= area_rect); + }); + + Some(used) + } + /// Resize the test harness to fit the contents. This only works when creating the Harness via /// [`Harness::new_ui`] / [`Harness::new_ui_state`] or /// [`HarnessBuilder::build_ui`] / [`HarnessBuilder::build_ui_state`]. pub fn fit_contents(&mut self) { self._step(true); - if let Some(response) = &self.response { - self.set_size(response.rect.size()); + + // Calculate size including all content (main UI + popups + tooltips) + if let Some(rect) = self.compute_total_rect_with_popups() { + self.set_size(rect.size()); } + self.run_ok(); } diff --git a/crates/egui_kittest/tests/snapshots/test_tooltip_hidden.png b/crates/egui_kittest/tests/snapshots/test_tooltip_hidden.png new file mode 100644 index 00000000..c25ae036 --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/test_tooltip_hidden.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:025942c144891b8862bf931385824e0484e60f4e7766f5d4401511c72ff20756 +size 2975 diff --git a/crates/egui_kittest/tests/snapshots/test_tooltip_shown.png b/crates/egui_kittest/tests/snapshots/test_tooltip_shown.png new file mode 100644 index 00000000..8ff6bba6 --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/test_tooltip_shown.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c267530452adb4f1ed1440df476d576ef4c2d96e6c58068bb57fed4615f5e113 +size 4453 diff --git a/crates/egui_kittest/tests/tests.rs b/crates/egui_kittest/tests/tests.rs index e18e2176..04f02ba8 100644 --- a/crates/egui_kittest/tests/tests.rs +++ b/crates/egui_kittest/tests/tests.rs @@ -16,6 +16,28 @@ fn test_shrink() { harness.snapshot("test_shrink"); } +#[test] +fn test_tooltip() { + let mut harness = Harness::new_ui(|ui| { + ui.label("Hello, world!"); + ui.separator(); + ui.label("This is a test") + .on_hover_text("This\nis\na\nvery\ntall\ntooltip!"); + }); + + harness.fit_contents(); + + #[cfg(all(feature = "snapshot", feature = "wgpu"))] + harness.snapshot("test_tooltip_hidden"); + + harness.get_by_label("This is a test").hover(); + harness.run_ok(); + harness.fit_contents(); + + #[cfg(all(feature = "snapshot", feature = "wgpu"))] + harness.snapshot("test_tooltip_shown"); +} + #[test] fn test_modifiers() { #[derive(Default)]