From 6a0285acdea6f9d3a20967f41f594ffdb3842e30 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 13:18:51 +0000 Subject: [PATCH] Test harness: a child viewport stays listed across the root's frames until closed --- .../ui-contract/src/runtime/egui/fixture.rs | 68 +++++++++++++++++-- crates/ui-contract/src/testing/frames.rs | 8 ++- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index c5f87706f..386f89f21 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -137,6 +137,45 @@ fn window_focus_facts(ctx: &Context, events: &[Event], native_pixels_per_point: #[derive(Default)] struct WindowFocus(Option); +/// The child windows of the root that are open: each opens with its first frame and stays listed in every frame's +/// viewports, as eframe lists every open viewport, until a test closes it ([`close_viewport`]) or the app sends it +/// `ViewportCommand::Close`. +#[derive(Default)] +struct OpenChildren(Vec); + +/// The open child windows, with `viewport` opened first when it is a child. +fn open_children(ctx: &Context, viewport: egui::ViewportId) -> Vec { + Service::::run(ctx, |open| { + if viewport != egui::ViewportId::ROOT && !open.0.contains(&viewport) { + open.0.push(viewport); + } + open.0.clone() + }) +} + +/// One run of `ui` on `input`; a root pass declares the open `children` after it, as an eframe app shows each of its +/// windows every frame: egui keeps a child viewport only while its parent's passes show it. Each child's content runs +/// in its own frames, so the declaration shows nothing. +fn run_ui(ctx: &Context, input: RawInput, children: &[egui::ViewportId], mut ui: impl FnMut(&mut Ui)) -> egui::FullOutput { + let root = input.viewport_id == egui::ViewportId::ROOT; + ctx.run_ui(input, |top| { + ui(top); + if root && !children.is_empty() { + let embedded = ctx.embed_viewports(); + ctx.set_embed_viewports(false); + for child in children { + ctx.show_viewport_deferred(*child, egui::ViewportBuilder::default(), |_, _| {}); + } + ctx.set_embed_viewports(embedded); + } + }) +} + +/// Closes the child window `viewport`: later frames no longer list it. +pub(crate) fn close_viewport(ctx: &Context, viewport: egui::ViewportId) { + Service::::run(ctx, |open| open.0.retain(|held| *held != viewport)); +} + /// The fixture's reset bridge, as egui-winit keeps one per window: the reset request it holds for each viewport, and /// the requests it dropped, which the next frame reports as `Event::ImeResetRetired`. #[derive(Default)] @@ -187,7 +226,8 @@ impl ImeResets { } /// Runs one pass of `viewport` at `time` seconds with `events` and `files`, on a window of `native_pixels_per_point` -/// (egui's default when `None`). A viewport other than the root is a child window of the root, which stays live. +/// (egui's default when `None`). A viewport other than the root is a child window of the root, which stays live; the +/// child stays open in every later frame, the root's included, until it is closed. #[expect(clippy::too_many_arguments, reason = "the arguments are the facts of one frame, and every caller passes them all")] pub(crate) fn run_frame( ctx: &Context, @@ -204,9 +244,10 @@ pub(crate) fn run_frame( let dropped_files = files.dropped.iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect(); Service::::run(ctx, |resets| resets.input(viewport, &mut events)); let mut viewports = window_focus_facts(ctx, &events, native_pixels_per_point); - if viewport != egui::ViewportId::ROOT { + let children = open_children(ctx, viewport); + for child in &children { viewports - .insert(viewport, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), native_pixels_per_point, ..egui::ViewportInfo::default() }); + .insert(*child, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), native_pixels_per_point, ..egui::ViewportInfo::default() }); } let input = RawInput { screen_rect: Some(screen), @@ -218,7 +259,7 @@ pub(crate) fn run_frame( viewports, ..RawInput::default() }; - let mut output = drive(|| ctx.run_ui(input, ui)); + let mut output = drive(|| run_ui(ctx, input, &children, ui)); Service::::run(ctx, |resets| resets.output(viewport, &output.platform_output)); let mut flat = Vec::new(); for clipped in &output.shapes { @@ -257,6 +298,15 @@ pub(crate) fn run_frame( }) .collect(); let closed = output.viewport_output.values().any(|viewport| viewport.commands.iter().any(|command| matches!(command, ViewportCommand::Close))); + let closing: Vec = output + .viewport_output + .iter() + .filter(|(id, out)| **id != egui::ViewportId::ROOT && out.commands.iter().any(|command| matches!(command, ViewportCommand::Close))) + .map(|(id, _)| *id) + .collect(); + for child in closing { + close_viewport(ctx, child); + } let repaint_delay = output.viewport_output.get(&egui::ViewportId::ROOT).map_or(std::time::Duration::MAX, |viewport| viewport.repaint_delay); let copied = output .platform_output @@ -366,8 +416,14 @@ pub(crate) fn reordered_fonts() -> egui::FontDefinitions { /// Runs one frame like `run_frame` and returns the shapes it painted, nested shapes flattened. pub(crate) fn run_frame_shapes(ctx: &Context, time: f64, screen: Vec2, ui: impl FnMut(&mut Ui)) -> Vec { let screen = Rect::from_min_size(Pos2::ZERO, screen); - let input = RawInput { screen_rect: Some(screen), time: Some(time), ..RawInput::default() }; - let mut output = drive(|| ctx.run_ui(input, ui)); + let children = open_children(ctx, egui::ViewportId::ROOT); + let mut viewports = egui::ViewportIdMap::default(); + viewports.insert(egui::ViewportId::ROOT, egui::ViewportInfo::default()); + for child in &children { + viewports.insert(*child, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), ..egui::ViewportInfo::default() }); + } + let input = RawInput { screen_rect: Some(screen), time: Some(time), viewports, ..RawInput::default() }; + let mut output = drive(|| run_ui(ctx, input, &children, ui)); output.textures_delta.clear(); let mut flat = Vec::new(); for clipped in output.shapes { diff --git a/crates/ui-contract/src/testing/frames.rs b/crates/ui-contract/src/testing/frames.rs index 0b27e14fe..d6a856419 100644 --- a/crates/ui-contract/src/testing/frames.rs +++ b/crates/ui-contract/src/testing/frames.rs @@ -186,7 +186,8 @@ impl Frames { } /// One frame of `viewport`, a child window of the root while the root stays live, with `events`; `ui` runs once - /// per pass. Returns the number of passes. + /// per pass. Returns the number of passes. The child opens with its first frame and stays open, listed in the root's + /// frames too, until [`close_viewport`](Self::close_viewport) or a `ViewportCommand::Close` for it. pub fn frame_in_viewport(&mut self, viewport: egui::ViewportId, events: Vec, mut ui: impl FnMut(&mut Ui)) -> usize { set_input(&self.ctx, self.input()); let mut passes = 0; @@ -253,6 +254,11 @@ impl Frames { self.time += seconds; } + /// Closes the child window `viewport`, as its user would: later frames no longer list it. + pub fn close_viewport(&mut self, viewport: egui::ViewportId) { + fixture::close_viewport(&self.ctx, viewport); + } + /// `n` frames with no events. pub fn idle(&mut self, n: usize, mut ui: impl FnMut(&mut Ui)) { for _ in 0..n {