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"); +}