From c201908614dec94cf6670c3259cec0e2562e4662 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:27:18 -0800 Subject: [PATCH] overlay: a window raised after a carried raise comes in front of it A raise asked in the pass a floating window first shows is asked again in the next pass (Persist::raise_again). When another window was raised in that next pass, both went to the top of egui's area order together, and egui keeps the old order among the layers it raises in one pass. The window raised first had already gone above the second one, so it stayed in front: activating one document window and then another one frame later left the first in front. end_pass now compares egui's area order of the lifted layers with the raise order. When a carried raise stands above a raise asked in this pass, only this pass's raises go up, and every raise is asked again in the next pass, where the newer ones already stand above the carried ones. A root asked more than once in a pass is carried once, in its last place. Test: a_window_raised_after_a_carried_raise_comes_in_front_of_it. --- crates/ui-contract/src/runtime/egui/areas.rs | 6 +++ crates/ui-contract/src/runtime/overlay.rs | 47 ++++++++++++---- .../tests/it/shell/document_window.rs | 54 +++++++++++++++++++ 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/areas.rs b/crates/ui-contract/src/runtime/egui/areas.rs index be31991ac3..d1d4c46eb9 100644 --- a/crates/ui-contract/src/runtime/egui/areas.rs +++ b/crates/ui-contract/src/runtime/egui/areas.rs @@ -449,6 +449,12 @@ pub(crate) fn move_to_top(ctx: &Context, layer: LayerId) { ctx.move_to_top(layer); } +/// egui's own area order as of the running pass, bottom first: the order of the last pass, with the layers that first +/// showed in this one at the end. +pub(crate) fn area_order(ctx: &Context) -> Vec { + ctx.memory(|memory| memory.layer_ids().collect()) +} + /// Whether egui showed `layer` in the last pass. pub(crate) fn visible_last_frame(ctx: &Context, layer: LayerId) -> bool { ctx.memory(|memory| memory.areas().visible_last_frame(&layer)) diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 295f149c5a..420b12633a 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -330,7 +330,8 @@ pub(crate) struct Persist { appeared: IdMap<(u64, u64)>, /// The roots whose asked raise ran in a pass where a floating window (re)appeared: egui raises every layer that /// shows again, and its area order keeps the old order among the layers raised in one pass, so the raise is asked - /// again in the next pass. + /// again in the next pass. Also the roots raised in a pass that moved only its own raises up, past a carried one + /// that stood above them, asked again in the order they were asked. raise_again: Vec, } @@ -922,8 +923,10 @@ pub(crate) fn end_pass(ctx: &Context) { }); let pressed = pointer::frame_raw(ctx).press.map(|press| press.origin); let now = pass::root_frame_nr(ctx); - let (order, to_top, floats, asked) = with_persist(ctx, viewport, |persist| { - let raise: Vec = std::mem::take(&mut persist.raise_again).into_iter().chain(raise).collect(); + let (order, lifted, floats, asked) = with_persist(ctx, viewport, |persist| { + let carried = std::mem::take(&mut persist.raise_again); + let carried_count = carried.len(); + let raise: Vec = carried.into_iter().chain(raise).collect(); let stamp = |id: Id, persist: &mut Persist| { persist.counter += 1; persist.raised.insert(id, persist.counter); @@ -935,6 +938,8 @@ pub(crate) fn end_pass(ctx: &Context) { .map(|entry| entry.id) .collect(); let mut asked: Vec = Vec::new(); + // The roots raised by this pass's own reasons, without the raises carried from the last pass. + let mut fresh: Vec = Vec::new(); for root in &roots { let shown = current.iter().find(|entry| entry.id == *root); // A root the last pass did not have appears, and so does one that faded out then and is open again @@ -949,9 +954,10 @@ pub(crate) fn end_pass(ctx: &Context) { // its area in the pass before, so it raises none. A root that first shows there is raised by egui itself. if reopened && shown.is_some_and(|entry| !entry.window && (entry.rank == Rank::Window || entry.among_windows)) { asked.push(*root); + fresh.push(*root); } } - for id in raise { + for (at_raise, id) in raise.into_iter().enumerate() { let mut at = current.iter().find(|entry| entry.id == id); while let Some(parent) = at.and_then(|entry| entry.parent).and_then(|parent| current.iter().find(|other| other.id == parent)) { at = Some(parent); @@ -959,8 +965,20 @@ pub(crate) fn end_pass(ctx: &Context) { if let Some(root) = at { stamp(root.id, persist); asked.push(root.id); + if at_raise >= carried_count { + fresh.push(root.id); + } } } + // A root asked more than once keeps its last place, so a raise asked again in every pass carries one entry. + let mut once: Vec = Vec::new(); + for id in asked.iter().rev() { + if !once.contains(id) { + once.push(*id); + } + } + once.reverse(); + let asked = once; persist.raised.retain(|id, _| roots.contains(id)); persist.appeared.retain(|id, _| current.iter().any(|entry| entry.id == *id)); let before = families(¤t, &persist.raised); @@ -976,7 +994,9 @@ pub(crate) fn end_pass(ctx: &Context) { let families = families(¤t, &persist.raised); let mut order: Vec = Vec::new(); let mut listed: Vec = Vec::new(); - let mut to_top: Vec = Vec::new(); + // The layers an asked raise moves to the top of egui's area order, in the raise order, each with whether this + // pass asked for it. + let mut lifted: Vec<(Layer, bool)> = Vec::new(); let mut floats: Vec = Vec::new(); persist.stack.clear(); for members in &families { @@ -996,9 +1016,9 @@ pub(crate) fn end_pass(ctx: &Context) { listed.push(member.id); } else if !member.window { floats.push(member.layer); - if members.first().is_some_and(|root| asked.contains(&root.id)) { + if let Some(root) = members.first().filter(|root| asked.contains(&root.id)) { // An asked raise of a layer left to egui's area order raises it there, as a press on it does. - to_top.push(member.layer); + lifted.push((member.layer, fresh.contains(&root.id))); } } persist.stack.push(member.id); @@ -1026,13 +1046,22 @@ pub(crate) fn end_pass(ctx: &Context) { .map(|(_, members)| { members.iter().flat_map(|member| std::iter::once(member.rect).chain(member.anchor).chain(member.peers.iter().copied())).collect() }); - (order, to_top, floats, asked) + (order, lifted, floats, asked) }); + // egui keeps the old order among the layers it moves to the top in one pass. When that order differs from the + // raise order, a raise carried from the last pass and a raise asked in this one cannot go up together: the carried + // one would stay above the newer one. Only this pass's raises go up then, and `held_back` asks every raise again in + // the next pass, where this pass's raises already stand above the carried ones. + let stacked = areas::area_order(ctx); + let place = |layer: &Layer| stacked.iter().position(|at| at == layer).unwrap_or(stacked.len()); + let in_order = lifted.windows(2).all(|pair| place(&pair[0].0) <= place(&pair[1].0)); + let held_back = !in_order && lifted.iter().any(|(_, fresh)| *fresh) && lifted.iter().any(|(_, fresh)| !fresh); + let to_top: Vec = lifted.into_iter().filter(|(_, fresh)| *fresh || !held_back).map(|(layer, _)| layer).collect(); areas::set_layer_order(ctx, &order); for layer in &to_top { areas::move_to_top(ctx, *layer); } - if !to_top.is_empty() && floats.iter().any(|layer| !areas::visible_last_frame(ctx, *layer)) { + if held_back || (!to_top.is_empty() && floats.iter().any(|layer| !areas::visible_last_frame(ctx, *layer))) { with_persist(ctx, viewport, |persist| persist.raise_again = asked); } // An owner takes a close in the frame after the request at the latest; one nobody took is for a layer that is not shown. diff --git a/crates/ui-shell/tests/it/shell/document_window.rs b/crates/ui-shell/tests/it/shell/document_window.rs index 45e3832670..508e2d7536 100644 --- a/crates/ui-shell/tests/it/shell/document_window.rs +++ b/crates/ui-shell/tests/it/shell/document_window.rs @@ -306,3 +306,57 @@ fn a_window_raised_as_the_windows_show_again_comes_to_the_front() { fixture.frame(Vec::new()); assert_eq!(top.get(), first.get(), "the window raised as they showed again is in front"); } + +/// A document window raised in the passes after another one's raise was carried over (a window first showed in that +/// pass) comes in front of it, and the window raised first stays in front of the one that showed. egui keeps the old +/// order among the layers it moves to the top in one pass, so the carried raise and the newer one going up together +/// left the window raised first in front. +#[trial] +fn a_window_raised_after_a_carried_raise_comes_in_front_of_it() { + use craft_ui::testing::Fixture; + use std::cell::{Cell, RefCell}; + use std::collections::HashMap; + use std::rc::Rc; + + // First and Second overlap at `first_second`, First and Third (which shows later) at `first_third`. + let (first_second, first_third) = (pos2(300.0, 150.0), pos2(100.0, 220.0)); + let places = [("First.psd", pos2(40.0, 40.0)), ("Second.psd", pos2(200.0, 120.0)), ("Third.psd", pos2(40.0, 200.0))]; + let third_shows = Rc::new(Cell::new(false)); + let raised: Rc>> = Rc::new(Cell::new(None)); + let layers = Rc::new(RefCell::new(HashMap::new())); + let tops = Rc::new(Cell::new((None, None))); + let (shows, raise, seen, front) = (third_shows.clone(), raised.clone(), layers.clone(), tops.clone()); + let mut fixture = Fixture::new(move |ui| { + front.set((ui.ctx().layer_id_at(first_second), ui.ctx().layer_id_at(first_third))); + for (name, at) in places { + if name == "Third.psd" && !shows.get() { + continue; + } + let window = DocumentWindow::new(name).id_salt(name).first_placed(Rect::from_min_size(at, vec2(300.0, 200.0))); + if raise.get() == Some(name) { window.raise() } else { window }.show(ui, |ui, _| { + seen.borrow_mut().insert(name, ui.layer_id()); + }); + } + }); + fixture.settle(); + let layer = |name: &str| layers.borrow().get(name).copied(); + assert_eq!(tops.get().0, layer("Second.psd"), "the window opened last is in front"); + // Third shows in the pass First is raised in, so First's raise is carried to the next pass. + third_shows.set(true); + raised.set(Some("First.psd")); + fixture.frame(Vec::new()); + // Second is raised in three passes in a row, as a body that asks for it while it is current does. + // Each frame reads the order the pass before it left. + raised.set(Some("Second.psd")); + fixture.frame(Vec::new()); + fixture.frame(Vec::new()); + assert_eq!(tops.get().0, layer("Second.psd"), "the window raised last is in front of the one raised before it"); + fixture.frame(Vec::new()); + assert_eq!(tops.get().0, layer("Second.psd"), "the window raised last stays in front"); + assert_eq!(tops.get().1, layer("First.psd"), "the carried raise goes up while Second is still raised in every pass"); + raised.set(None); + fixture.frame(Vec::new()); + fixture.frame(Vec::new()); + assert_eq!(tops.get().0, layer("Second.psd"), "the window raised last stays in front once its raise ends"); + assert_eq!(tops.get().1, layer("First.psd"), "the window raised first is in front of the one that showed as it was raised"); +}