From a02d2b71da4a81090f488c704b9668c94a3bf628 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 20:53:45 -0800 Subject: [PATCH] ui-contract: a child window egui opens again starts its pass records anew egui drops a child viewport that a pass of its parent leaves out and counts the passes of the viewport it opens again under the same id from zero, while the window stays open and the runtime keeps its records. A record stamped with a pass number from before could then match a later pass of the new count and keep that pass's state. In debug builds a tab tooltip shown in a popup window from a native palette's flyout tripped "an id is the base of two open stages in one pass" after the pointer moved from one tab to the next, because the tooltip window closed and opened again under the same id. The begin hook now records each viewport's latest pass number. When a pass begins with a number no higher than it, the viewport's pass-stamped records are dropped before the pass runs. --- crates/ui-contract/src/runtime/frame.rs | 30 +++++++++++++++++++ .../ui-contract/tests/it/runtime/windows.rs | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index bcb8b6947c..10d75c74da 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -193,6 +193,8 @@ pub(crate) struct FrameState { root_passes_hooked: u64, /// What drops the records of closed viewports as a frame begins. prunes: HashMap, + /// What drops the pass-stamped records of a viewport whose pass count egui started again. + restarts: HashMap, sweeps: HashMap, /// What a layer above the runtime reads at the begin hook, after the pointer snapshot and before any component runs. begins: HashMap, @@ -286,12 +288,20 @@ pub trait Stored: Default + Send + 'static { fn stamped(ctx: &Context, f: impl FnOnce(&mut PerViewport>) -> R) -> R { pass_state::with_records::, R>(ctx, f) } + + /// Drops the record of `viewport` when this type's records carry the viewport's pass number ([`Stamped`]): egui + /// counts the passes of that viewport from zero again, which the begin hook finds. Other records stay. + fn forget_restarted(_ctx: &Context, _viewport: ViewportId) {} } impl Stored for Stamped { fn records(ctx: &Context, f: impl FnOnce(&mut PerViewport) -> R) -> R { T::stamped(ctx, f) } + + fn forget_restarted(ctx: &Context, viewport: ViewportId) { + Self::records(ctx, |records| records.records.retain(|(id, _)| *id != viewport)); + } } impl PerViewport { @@ -335,6 +345,9 @@ impl PerViewport { T::records(ctx, |records| records.records.retain(|(viewport, _)| live.contains(viewport))); } register_prune(ctx, TypeId::of::(), prune::); + pass_state::with::(ctx, |state| { + state.restarts.entry(TypeId::of::()).or_insert(T::forget_restarted); + }); } } @@ -395,6 +408,14 @@ fn prune_closed(ctx: &Context) { } } +/// Drops the pass-stamped records of `viewport`, whose passes egui counts from zero again. +fn forget_restarted(ctx: &Context, viewport: ViewportId) { + let restarts: Vec = pass_state::with::(ctx, |state| state.restarts.values().copied().collect()); + for restart in restarts { + restart(ctx, viewport); + } +} + /// Registers a sweep the end hook runs for the running viewport's pass, once per service type. pub(crate) fn register_end_sweep(ctx: &Context, key: TypeId, sweep: fn(&Context)) { pass_state::with::(ctx, |state| { @@ -675,6 +696,8 @@ struct ViewportRecord { press_cancelled: bool, /// `None` until the viewport's first pass begins. now: Option, + /// The pass number of the viewport's latest pass the begin hook ran for. + began: Option, } impl Stored for ViewportRecord {} @@ -820,6 +843,13 @@ pub(crate) fn begin_pass(ui: &mut Ui) { // H5: read the clock once; the runtime cancelled a captured press since this viewport's last pass. let now = pass::now(&ctx); let (pass_nr, frame_nr) = (pass::pass_nr(&ctx, viewport), ctx.cumulative_frame_nr_for(viewport)); + // egui drops a child viewport that a pass of its parent leaves out, and counts the passes of the viewport it opens + // again under that id from zero. Its window stays open, and so do its records: a stamp from before the restart can + // equal the number of a later pass. + let restarted = PerViewport::::with(&ctx, viewport, |record| record.began.replace(pass_nr).is_some_and(|began| pass_nr <= began)); + if restarted { + forget_restarted(&ctx, viewport); + } let (elapsed, cancelled) = PerViewport::::with(&ctx, viewport, |record| { // The events this frame's input hook held arrived at the frame's input time. record.held.stamp(now); diff --git a/crates/ui-contract/tests/it/runtime/windows.rs b/crates/ui-contract/tests/it/runtime/windows.rs index 73327a58b3..760e2cc279 100644 --- a/crates/ui-contract/tests/it/runtime/windows.rs +++ b/crates/ui-contract/tests/it/runtime/windows.rs @@ -1805,3 +1805,33 @@ fn dropped_window_input_leaves_a_hosted_window_only_injected_input() { assert!(seen.get().primary, "{policy:?}: the injected click reaches the window: {:?}", seen.get()); } } + +/// A child window that one pass of a frame leaves out and the next shows again, as a popup that closes and opens +/// again within a frame, while its OS window stays open: egui drops the viewport and counts its passes from zero again. +/// A component that the window shows before it closes, and again from its third pass after it opens, runs once in +/// each of its passes, as it would in a window opened for the first time. +#[trial] +fn a_child_window_shown_again_in_a_later_pass_of_its_frame_runs_its_component_once_a_pass() { + let windows = Windows::new(); + let seen = RefCell::new(Seen::default()); + // The window's passes in each frame: 0, 1 and 2 with the component; it opens again as pass 0 in the second pass + // of frame 3, then 1 without the component and 2 and 3 with it. + let frames = [(false, true), (false, true), (false, true), (true, false), (false, false), (false, true), (false, true)]; + for (frame, (reopens, shows)) in frames.into_iter().enumerate() { + let mut passes = 0; + windows.frame(vec![], vec![], |ui| { + passes += 1; + if reopens && passes == 1 { + ui.ctx().request_discard("the child window opens again in the next pass"); + return; + } + ui.ctx().show_viewport_immediate(child(), ViewportBuilder::default(), |cui, _| { + if shows { + Probe::new(&seen).label("Kid").id_salt("kid").show(cui); + } + }); + }); + assert_eq!(passes, if reopens { 2 } else { 1 }, "frame {frame}"); + } + assert!(seen.borrow().id.is_some(), "the child window ran its component"); +}