diff --git a/crates/ui-shell/src/shell/document_window.rs b/crates/ui-shell/src/shell/document_window.rs index edab6e6fa..f47febe33 100644 --- a/crates/ui-shell/src/shell/document_window.rs +++ b/crates/ui-shell/src/shell/document_window.rs @@ -11,7 +11,7 @@ use crate::contract::{ use crate::runtime::env::Label; use crate::runtime::memory::{Remember as _, sub}; use crate::runtime::overlay::CloseReason; -use crate::runtime::windows::{WindowAction, WindowHost, WindowSpec}; +use crate::runtime::windows::{WindowAction, WindowHost, WindowLevel, WindowSpec}; use crate::sections::{HeaderAction, HeaderStrength, TitleRow}; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::placement::{capped, cascade, monitor_of}; @@ -161,12 +161,30 @@ impl<'a> DocumentWindow<'a> { self } - /// Where the window opens, as its outer rect in desktop points. A saved placement comes first. + /// Where the window is, as its outer rect in desktop points. A saved placement comes first. The window is kept + /// at `rect` while the app shows it, so an app whose user moves the window keeps `rect` where the window went + /// (its [`WindowHost::facts`]); else the window goes back at the next pass. #[must_use] pub fn first_placed(self, rect: Rect) -> Self { self.overlay(|overlay| overlay.placed(rect)) } + /// Stands the window at `level` among the windows, in place of an ordinary window's. With + /// [`WindowLevel::Palette`] a document window stays above the app's main window while the app is active, as a + /// floating document does in the references, drops below a blocking dialog in another window, and hides while the + /// main window is minimized. + #[must_use] + pub fn level(mut self, level: WindowLevel) -> Self { + self.props = match std::mem::take(&mut self.props.mount) { + Mount::Viewport { fallback, spec } => { + let spec = spec.unwrap_or_else(WindowSpec::document).level(level); + self.props.mount(Mount::Viewport { fallback, spec: Some(spec) }) + } + other => self.props.mount(other), + }; + self + } + /// Whether the user can resize the window. #[must_use] pub fn resizable(self, on: bool) -> Self { diff --git a/crates/ui-shell/tests/it/shell/document_window.rs b/crates/ui-shell/tests/it/shell/document_window.rs index 5a32b5c87..eafbe519a 100644 --- a/crates/ui-shell/tests/it/shell/document_window.rs +++ b/crates/ui-shell/tests/it/shell/document_window.rs @@ -183,3 +183,28 @@ fn a_document_window_body_fills_the_windows_room_and_does_not_scroll() { assert!(height < MAX_EXTENT / 2.0, "{mount}: the body's height is the window's room, got {height}"); } } + +/// A document window at the palette level stands above the app's main window while the app is active (photocraft +/// floating documents, as the references keep them); without a level it is an ordinary window. +#[trial] +fn a_document_window_at_the_palette_level_stands_above_the_main_window() { + let level = |palette: bool| { + let windows = Windows::new(); + windows.root_info(ViewportInfo { focused: Some(true), ..ViewportInfo::default() }); + let mut seen = None; + for _ in 0..3 { + let out = windows.frame(vec![], vec![], |ui| { + windows.child_info(ViewportInfo { focused: Some(false), ..ViewportInfo::default() }); + let window = DocumentWindow::new("Landscape.psd"); + let window = if palette { window.level(craft_ui::runtime::windows::WindowLevel::Palette) } else { window }; + window.show(ui, |_, _| {}); + }); + if let Some((_, builder)) = out.builders.iter().find(|(_, builder)| builder.title.as_deref() == Some("Landscape.psd")) { + seen = builder.window_level.or(seen); + } + } + seen + }; + assert_eq!(level(true), Some(egui::WindowLevel::AlwaysOnTop), "a palette-level document window stands above the main window"); + assert_ne!(level(false), Some(egui::WindowLevel::AlwaysOnTop), "an ordinary document window does not"); +}