Merge pull request #4226 from dotCooCoo/fix/native-tab-tooltip-stage-id

ui-contract: a child window egui opens again starts its pass records anew
This commit is contained in:
BFlat authored and GitHub committed 2026-11-29 12:17:08 +07:00
commit 7ea27925bd
2 files changed
+60

No files matched your search

+30
View File
@@ -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<TypeId, fn(&Context, &[ViewportId])>,
/// What drops the pass-stamped records of a viewport whose pass count egui started again.
restarts: HashMap<TypeId, fn(&Context, ViewportId)>,
sweeps: HashMap<TypeId, fn(&Context)>,
/// What a layer above the runtime reads at the begin hook, after the pointer snapshot and before any component runs.
begins: HashMap<TypeId, fn(&Context)>,
@@ -286,12 +288,20 @@ pub trait Stored: Default + Send + 'static {
fn stamped<R>(ctx: &Context, f: impl FnOnce(&mut PerViewport<Stamped<Self>>) -> R) -> R {
pass_state::with_records::<Stamped<Self>, 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<T: Stored> Stored for Stamped<T> {
fn records<R>(ctx: &Context, f: impl FnOnce(&mut PerViewport<Self>) -> R) -> R {
T::stamped(ctx, f)
}
fn forget_restarted(ctx: &Context, viewport: ViewportId) {
Self::records(ctx, |records| records.records.retain(|(id, _)| *id != viewport));
}
}
impl<T: Stored> PerViewport<T> {
@@ -335,6 +345,9 @@ impl<T: Stored> PerViewport<T> {
T::records(ctx, |records| records.records.retain(|(viewport, _)| live.contains(viewport)));
}
register_prune(ctx, TypeId::of::<T>(), prune::<T>);
pass_state::with::<FrameState, _>(ctx, |state| {
state.restarts.entry(TypeId::of::<T>()).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<fn(&Context, ViewportId)> = pass_state::with::<FrameState, _>(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::<FrameState, _>(ctx, |state| {
@@ -675,6 +696,8 @@ struct ViewportRecord {
press_cancelled: bool,
/// `None` until the viewport's first pass begins.
now: Option<f64>,
/// The pass number of the viewport's latest pass the begin hook ran for.
began: Option<u64>,
}
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::<ViewportRecord>::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::<ViewportRecord>::with(&ctx, viewport, |record| {
// The events this frame's input hook held arrived at the frame's input time.
record.held.stamp(now);
@@ -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");
}