From 24e30ffacb08bbe563511d01bfaa9b532b4a2819 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 19:27:33 +0700 Subject: [PATCH] ui-shell: a document window's raise brings its fallback layer to the front in that pass (overlay::raise), as a press on it does: it only asked an OS window for focus --- crates/ui-contract/src/runtime/overlay.rs | 21 ++++++++++++- crates/ui-shell/src/shell/document_window.rs | 13 +++++--- .../tests/it/shell/document_window.rs | 31 +++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index fe8673cda..9270c74c8 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -269,6 +269,8 @@ pub(crate) struct Pass { pressed_outside: Vec, /// The popup a press that opened it ended on, and where, this pass. dragged_release: Option<(Id, Pos2)>, + /// The layers asked to come to the front this pass (`raise`). + raise: Vec, /// The dialog stack place of the layers recorded now, and the dialog they show for (`stack_place`). place: Option, dialog: Option, @@ -328,6 +330,7 @@ fn with_pass(ctx: &Context, f: impl FnOnce(&mut Pass) -> R) -> R { pass.claimed.clear(); pass.pressed_outside.clear(); pass.dragged_release = None; + pass.raise.clear(); pass.place = None; pass.dialog = None; }, @@ -638,6 +641,12 @@ pub(crate) fn released_inside(ctx: &Context, rect: Rect) -> bool { with_pass(ctx, |pass| pass.dragged_release.is_some_and(|(popup, at)| pass.drawing.iter().any(|(id, _)| *id == popup) && rect.contains(at))) } +/// Brings the family of the open layer `id` in front of the other families of its class at the end of this pass, as a +/// press on it does. +pub fn raise(ctx: &Context, id: Id) { + with_pass(ctx, |pass| pass.raise.push(id)); +} + /// Ends the body `begin_layer` started. pub(crate) fn end_layer(ctx: &Context) { with_pass(ctx, |pass| { @@ -823,7 +832,8 @@ fn families(entries: &[OpenEntry], raised: &IdMap) -> Vec> { /// open-modal fact. pub(crate) fn end_pass(ctx: &Context) { let viewport = pass::current_viewport(ctx); - let (current, last_ids) = with_pass(ctx, |pass| (pass.current.clone(), pass.last.iter().map(|entry| entry.id).collect::>())); + let (current, last_ids, raise) = + with_pass(ctx, |pass| (pass.current.clone(), pass.last.iter().map(|entry| entry.id).collect::>(), pass.raise.clone())); let pressed = pointer::frame_raw(ctx).press.map(|press| press.origin); let order = with_persist(ctx, viewport, |persist| { let stamp = |id: Id, persist: &mut Persist| { @@ -841,6 +851,15 @@ pub(crate) fn end_pass(ctx: &Context) { stamp(*root, persist); } } + for id in raise { + 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); + } + if let Some(root) = at { + stamp(root.id, persist); + } + } persist.raised.retain(|id, _| roots.contains(id)); persist.appeared.retain(|id, _| current.iter().any(|entry| entry.id == *id)); let before = families(¤t, &persist.raised); diff --git a/crates/ui-shell/src/shell/document_window.rs b/crates/ui-shell/src/shell/document_window.rs index 39cb9fe16..d3c79a0cb 100644 --- a/crates/ui-shell/src/shell/document_window.rs +++ b/crates/ui-shell/src/shell/document_window.rs @@ -10,7 +10,7 @@ use crate::contract::{ }; use crate::runtime::env::Label; use crate::runtime::memory::{Remember as _, sub}; -use crate::runtime::overlay::CloseReason; +use crate::runtime::overlay::{self, CloseReason}; use crate::runtime::windows::{WindowAction, WindowHost, WindowLevel, WindowSpec}; use crate::sections::{HeaderAction, HeaderStrength, TitleRow}; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; @@ -265,10 +265,13 @@ impl Component for DocumentWindow<'_> { fn finish(mut self, mut cx: Cx<'_, Self>) -> DocumentWindowOutput { let host = WindowHost::of(&cx.ctx); let viewport = host.viewport_of(cx.id); - if self.raise - && let Some(viewport) = viewport - { - host.act(viewport, WindowAction::Focus); + if self.raise { + match viewport { + Some(viewport) => { + host.act(viewport, WindowAction::Focus); + } + None => overlay::raise(&cx.ctx, cx.id), + } } let focused = viewport.and_then(|viewport| host.facts(viewport)).is_some_and(|facts| facts.focused); let had_focus = std::mem::replace(&mut cx.keep::().0, focused); diff --git a/crates/ui-shell/tests/it/shell/document_window.rs b/crates/ui-shell/tests/it/shell/document_window.rs index d3a261933..fab6cfbbc 100644 --- a/crates/ui-shell/tests/it/shell/document_window.rs +++ b/crates/ui-shell/tests/it/shell/document_window.rs @@ -235,3 +235,34 @@ fn a_fallback_document_window_dragged_by_its_title_reports_where_it_went() { assert!(near(after.size(), before.size()), "a move keeps the window's size: {before:?} to {after:?}"); assert_eq!(place.get(), after, "the host keeps the window where the drag reported it"); } + +/// On the fallback layer, `raise` brings a document window in front of one opened after it in the pass it is set, as a +/// press on its layer does. Guards against a raise that only asked an OS window for focus, so the layer stayed behind. +#[trial] +fn a_raised_fallback_document_window_comes_to_the_front() { + use craft_ui::runtime::overlay; + use craft_ui::testing::Fixture; + use std::cell::Cell; + use std::rc::Rc; + + let (raise, first, top) = (Rc::new(Cell::new(false)), Rc::new(Cell::new(None)), Rc::new(Cell::new(None))); + let (asked, seen, front) = (raise.clone(), first.clone(), top.clone()); + let mut fixture = Fixture::new(move |ui| { + front.set(overlay::top(ui.ctx())); + for (name, at) in [("First.psd", 40.0), ("Second.psd", 80.0)] { + let window = DocumentWindow::new(name).id_salt(name).first_placed(Rect::from_min_size(pos2(at, at), vec2(300.0, 200.0))); + let raised = name == "First.psd" && asked.get(); + let shown = if raised { window.raise() } else { window }.show(ui, |_, _| {}); + if name == "First.psd" { + seen.set(Some(shown.output.response.id)); + } + } + }); + fixture.settle(); + assert!(first.get().is_some() && top.get() != first.get(), "the window opened last is in front"); + raise.set(true); + fixture.frame(Vec::new()); + raise.set(false); + fixture.frame(Vec::new()); + assert_eq!(top.get(), first.get(), "the raised window is in front"); +}