diff --git a/crates/ui-contract/src/contract/overlay/dismissal.rs b/crates/ui-contract/src/contract/overlay/dismissal.rs index 98da73aecb..d6dd99542d 100644 --- a/crates/ui-contract/src/contract/overlay/dismissal.rs +++ b/crates/ui-contract/src/contract/overlay/dismissal.rs @@ -81,21 +81,23 @@ pub(super) fn dismissal( /// own pass decides: a layer that closes on outside presses closes on one in the opener outside it and its anchor /// (absorbed or not, as its rule says), and on one in any other window of the app; a popup also closes once the app /// has no focus. An absorbing layer can't keep a press in another OS window from reaching that window. A press recorded -/// in the pass the layer appears in is not outside it: it was made before the layer opened. +/// in the pass the layer appears in is not outside it: it was made before the layer opened. That pass is the popup +/// window's, read as `pass` before it ran, and not an earlier pass of the frame. pub(super) fn popup_dismissal( ctx: &Context, layer: &Layer<'_>, rect: Rect, anchor: Rect, popup: egui::ViewportId, + pass: u64, appears: bool, ) -> Option { let here = match layer.overlay.dismiss.outside { Outside::Keep => return None, // A layer that appears in this pass opened after the press was made, so the press is not outside it, as in // `dismissal`: the pass that shows the layer is the popup window's, which runs inside this one, and its first - // appearance in that pass is what the frame holds. - Outside::Absorb => overlay::absorbed_press(ctx).is_some() && !overlay::appeared_in_frame(ctx, popup, layer.id), + // appearance is read by that pass's number, not by the frame's. + Outside::Absorb => overlay::absorbed_press(ctx).is_some() && !overlay::appeared_in_pass(ctx, popup, layer.id, pass), Outside::Close => press_outside(ctx, layer, rect, anchor), }; let away = layer.overlay.rank == Rank::Popup && crate::runtime::windows::app_away(ctx); diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index 25ebaa196e..15ae11ac66 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -311,6 +311,11 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut let spec = WindowSpec::popup().takes_focus(!tooltip && (scope.is_some() || overlay.rank.takes_keys())).passes_pointer(tooltip); let known = measured.map(|size| Rect::from_min_size(unplaced.at, size)); let open = PopupOpen { id: layer.id, spec: &spec, rect: known.map(|rect| rect.translate(popup.origin.to_vec2())) }; + // The popup's pass runs nested in this one and its begin pass stamps a layer's first appearance with its own + // number: read that number before the pass runs, so this pass tells a layer appearing in it from one that appeared + // in an earlier pass of the frame. + let viewport = crate::runtime::egui::viewports::viewport_id(layer.id); + let popup_pass = crate::runtime::egui::pass::pass_nr(ctx, viewport); let mut shown = None; windows::show_popup(ctx, &open, |ui| { let at = unplaced.at.to_vec2(); @@ -335,8 +340,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut entry.window = true; entry.popup = true; overlay::record(ctx, entry); - let viewport = crate::runtime::egui::viewports::viewport_id(layer.id); - let dismissed = if layer.exiting { None } else { dismissed.or_else(|| popup_dismissal(ctx, layer, rect, anchor, viewport, appears)) }; + let dismissed = if layer.exiting { None } else { dismissed.or_else(|| popup_dismissal(ctx, layer, rect, anchor, viewport, popup_pass, appears)) }; close_family(ctx, layer, layer.exiting || dismissed.is_some(), &mut memory); remember(layer, ctx, memory); LayerOut { dismissed } diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 81c205ebb8..c7a34000be 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -616,13 +616,15 @@ pub fn opening(ctx: &Context, id: Id) -> bool { with_persist(ctx, viewport, |persist| persist.appeared.get(&id) == Some(&now)) } -/// Whether the layer `id` first ran its body in the running frame, in the stack of `viewport`: a layer shown in a popup -/// window appears in the popup's pass, which runs inside its opener's, so the opener's pass reads it once that pass ended, -/// where [`opening`] reads the layer's own. Only the frame tells the two apart then: `pass_nr` of a passed viewport names -/// the pass after the one that showed the layer. -pub(crate) fn appeared_in_frame(ctx: &Context, viewport: ViewportId, id: Id) -> bool { - let frame = pass::root_frame_nr(ctx); - with_persist(ctx, viewport, |persist| persist.appeared.get(&id).is_some_and(|(first, _)| *first == frame)) +/// Whether the layer `id` first ran its body in the pass `pass` of the popup window `viewport`, in the running frame: a +/// layer shown in a popup window appears in the popup's pass, which runs inside its opener's, so the opener's pass reads +/// it once that pass ended, where [`opening`] reads the layer's own. `pass_nr` of a passed viewport names the pass after +/// the one that showed the layer, but the opener's pass reads the popup's number before it runs, which is the number the +/// popup's begin pass stamps the layer with: the two numbers name one pass, so a layer that appeared in an earlier pass of +/// the same frame (a discard) is not one that appears in this one. +pub(crate) fn appeared_in_pass(ctx: &Context, viewport: ViewportId, id: Id, pass: u64) -> bool { + let now = (pass::root_frame_nr(ctx), pass); + with_persist(ctx, viewport, |persist| persist.appeared.get(&id) == Some(&now)) } /// Queues a close of every layer opened from inside the layer `id`, so the open state of a popup in its body does diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index 92c8e03eb3..490b807bda 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -1845,8 +1845,8 @@ fn a_context_menu_a_palette_opens_is_not_closed_by_the_press_that_opened_it() { assert!(!palette_menu_shows(&dock, &out, "First action"), "the first menu closed"); // The double right-click on the first target: the press that begins it closes the second menu, its release opens the // first target's menu in the pass of the second press, which the pass records as an outside press. - out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![moved(first)]); - out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![secondary(first, true)]); + collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![moved(first)]); + collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![secondary(first, true)]); out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![secondary(first, false), secondary(first, true)]); // A menu a pass closes leaves the tree in the pass after it, while it fades; nothing presses again in these. for _ in 0..3 {