diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 7f870b4386..76324ede98 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -154,6 +154,14 @@ impl ShapeHooks for BodyHooks<'_> { fn read(&mut self, comp: &mut C, stage: &mut MemoryStage, id: Id, env: &Env) { let ctx = stage.ctx().clone(); + // A header the OS title bar stands for is gone in a window whose frame the OS draws: every later step and + // region reads the props without it. + if let Mount::Viewport { spec, .. } = &self.props.mount + && self.props.header.is_some_and(|header| header.os_title) + && windows::os_title_bar(&ctx, id, spec.as_ref()) + { + Arc::make_mut(&mut self.props).header = None; + } let props = Arc::clone(&self.props); let props = &*props; let scroll = scroll_of(props); diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 877b18a967..c516c4d1cb 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -379,6 +379,10 @@ pub struct HeaderRegion { pub inset: Pad, /// The space between a stacked header and the body, none by default. pub gap: Option, + /// Whether the OS title bar stands for the header: where the container is a viewport mount in an OS window of its + /// own whose frame the OS draws (`WindowChrome::Native`), the title bar carries its title and close button, and + /// the container has no header. As a layer, or in a window the app frames, it keeps the header. + pub os_title: bool, } impl HeaderRegion { @@ -394,9 +398,16 @@ impl HeaderRegion { fill: None, inset: Pad::None, gap: None, + os_title: false, } } + /// Whether the OS title bar stands for the header (see [`HeaderRegion::os_title`]). + pub const fn os_title(mut self, on: bool) -> Self { + self.os_title = on; + self + } + /// The surface of a band behind the header. pub const fn fill(mut self, kind: SurfaceKind) -> Self { self.fill = Some(BandFill::Surface(kind)); diff --git a/crates/ui-contract/src/runtime/windows.rs b/crates/ui-contract/src/runtime/windows.rs index 565e62bf95..b8cb5b89fe 100644 --- a/crates/ui-contract/src/runtime/windows.rs +++ b/crates/ui-contract/src/runtime/windows.rs @@ -480,6 +480,12 @@ pub(crate) fn mount_window(ctx: &Context, id: Id, spec: Option<&WindowSpec>) -> opens_over(ctx, opener) } +/// Whether the viewport mount `id` opening as `spec` shows in an OS window of its own whose frame, title bar included, +/// the OS draws. A mount without a spec takes its frame by rank and is not counted. +pub(crate) fn os_title_bar(ctx: &Context, id: Id, spec: Option<&WindowSpec>) -> bool { + spec.is_some_and(|spec| spec::frame_of_spec(ctx, spec).0 == WindowChrome::Native) && mount_window(ctx, id, spec) +} + /// Whether a viewport mount opened from `opener`'s window shows in an OS window of its own: the rule of /// [`mount_window`]. fn opens_over(ctx: &Context, opener: ViewportId) -> bool { diff --git a/crates/ui-contract/tests/it/runtime/windows.rs b/crates/ui-contract/tests/it/runtime/windows.rs index df12ba1dd4..da48a8cd80 100644 --- a/crates/ui-contract/tests/it/runtime/windows.rs +++ b/crates/ui-contract/tests/it/runtime/windows.rs @@ -1064,6 +1064,60 @@ fn a_viewport_mount_that_opens_over_full_screen_keeps_its_own_window() { } } +/// A container whose header the OS title bar stands for, recording whether its header ran. +#[derive(craft_ui::contract::Component)] +#[component(container)] +struct Titled { + #[common] + common: Common, + #[container] + props: ContainerProps, + header: Rc>, +} + +impl Component for Titled { + const ROLE: Role = Role::GROUP; + type Style = PlainStyle; + type Output = Response; + + fn style(_: &StyleCx<'_>, _: TitledStyleKey) -> PlainStyle { + PlainStyle::min(Vec2::new(120.0, 60.0)) + } + + fn finish(self, cx: Cx<'_, Self>) -> Response { + cx.response.clone() + } +} + +impl Container for Titled { + fn header(&mut self, _: &mut egui::Ui, _: &mut craft_ui::contract::BodyCx<'_>) { + self.header.set(true); + } +} + +/// A header the OS title bar stands for is left out in an OS window of its own whose frame the OS draws, and kept in +/// a window the app frames and in the layer that stands in for the window over a full-screen main window. +#[trial] +fn the_os_title_bar_stands_for_a_header_only_in_an_os_framed_window() { + let header_ran = |windows: &Windows, chrome: WindowChrome| { + let ran = Rc::new(Cell::new(false)); + for _ in 0..3 { + ran.set(false); + windows.frame(vec![], vec![], |ui| { + let mount = Mount::viewport_with(Overlay::floating(), WindowSpec::document().chrome(chrome)); + let props = ContainerProps::new().mount(mount).header(craft_ui::contract::HeaderRegion::new().os_title(true)); + Titled { common: Common::new(), props, header: Rc::clone(&ran) }.label("Second").id_salt("second").show(ui, |_, _| {}); + }); + } + ran.get() + }; + assert!(!header_ran(&Windows::new(), WindowChrome::Native), "the OS title bar stands for the header"); + assert!(header_ran(&Windows::new(), WindowChrome::Integrated), "a window the app frames keeps the header"); + let layer = Windows::new(); + layer.root_info(ViewportInfo { fullscreen: Some(true), ..ViewportInfo::default() }); + assert!(header_ran(&layer, WindowChrome::Native), "the layer keeps the header"); +} + /// A window sized by its content sends no size before it has reported its place, so it never opens at what its body /// took in room the OS has not given; once it has reported, it takes its content's size. #[trial] diff --git a/crates/ui-panels/src/dialog/modal.rs b/crates/ui-panels/src/dialog/modal.rs index eb07dc9962..20aa2e55d6 100644 --- a/crates/ui-panels/src/dialog/modal.rs +++ b/crates/ui-panels/src/dialog/modal.rs @@ -198,6 +198,16 @@ impl<'a> Dialog<'a> { Dialog { props: self.props.mount(mount), ..self } } + /// A window of the app rather than a dialog: the body fills the window edge to edge, unpadded, and where the dialog + /// stands in an OS window of its own with the OS's frame, the OS title bar stands for its title row + /// (`HeaderRegion::os_title`). As a layer it keeps the title row. + #[must_use] + pub fn fills_os_window(mut self) -> Self { + self.props.pad = Pad::None; + self.props.header = self.props.header.map(|header| header.os_title(true)); + self + } + /// A tool window: no modal layer and no backdrop; its edits are the app's normal edits. #[must_use] pub fn modeless(self) -> Self { diff --git a/crates/ui-shell/src/shell/mod.rs b/crates/ui-shell/src/shell/mod.rs index 64e648880b..5499ce3f19 100644 --- a/crates/ui-shell/src/shell/mod.rs +++ b/crates/ui-shell/src/shell/mod.rs @@ -45,7 +45,7 @@ pub use start::{ FolderListing, PINNED_FILES_SHOWN, PinnedFile, PinnedFolder, StartAction, StartLayout, StartLink, StartLinkTarget, StartOutput, StartPinnedCommands, StartPlace, StartRecentCommands, StartScreen, }; -pub use viewport_window::{ViewportWindow, ViewportWindowOutput}; +pub use viewport_window::{ViewportWindow, ViewportWindowKind, ViewportWindowOutput}; // Caption buttons: the four families' button, its slot layout and the icon-theme glyphs. pub(crate) mod caption_button; diff --git a/crates/ui-shell/src/shell/viewport_window.rs b/crates/ui-shell/src/shell/viewport_window.rs index 0b52c610b8..9df9370a31 100644 --- a/crates/ui-shell/src/shell/viewport_window.rs +++ b/crates/ui-shell/src/shell/viewport_window.rs @@ -8,6 +8,7 @@ use crate::dialog::Dialog; use crate::runtime::env::Label; use crate::runtime::overlay::{Backdrop, CloseReason}; use crate::runtime::windows::WindowSpec; +use craft_host::target::WindowChrome; use egui::{Response, Ui}; /// What a window pass reports. @@ -30,6 +31,20 @@ impl Outcome for ViewportWindowOutput { } } +/// What kind of OS window a [`ViewportWindow`] opens. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum ViewportWindowKind { + /// By its rank: a dialog window for an app-modal one, else a tool window (a palette with its own title row and no + /// taskbar entry). + #[default] + Tool, + /// An ordinary window of the app, as its main window is: the OS frame with the title and close button, a taskbar + /// entry, the normal level, and a body that fills the window edge to edge (a second document window, a + /// presenter's view). As a layer, where windows are unavailable, it keeps its title row. + App, +} + /// A window of the app with a body the app fills. /// /// The app calls [`show`](ViewportWindow::show) on every pass the window should exist and stops calling it to close @@ -41,6 +56,8 @@ impl Outcome for ViewportWindowOutput { /// [`opens_over_full_screen`](ViewportWindow::opens_over_full_screen) keeps it in an OS window of its own. /// - **App-modal.** By default the window blocks nothing. [`app_modal`](ViewportWindow::app_modal) makes the app's /// other windows ignore input while it is open, and a press on one raises this window. +/// - **Kind.** By default the OS window is a tool or dialog window by its rank; [`kind`](ViewportWindow::kind) +/// `ViewportWindowKind::App` makes it an ordinary window of the app with the OS frame. /// - **Keys.** Escape asks to close an app-modal window, unless a popup in it is open; a tool window ignores it. /// [`takes_unfocused_keys`](ViewportWindow::takes_unfocused_keys) gives a layer window the keys pressed with /// nothing focused. @@ -66,12 +83,23 @@ pub struct ViewportWindow { /// window that stands beside a full-screen one, as a presenter's window beside its show, usually on another display. #[prop(flag)] opens_over_full_screen: bool, + /// What kind of OS window it opens: by its rank (the default), or an ordinary window of the app. + #[prop] + kind: ViewportWindowKind, } impl ViewportWindow { /// A window titled `title` that blocks nothing. pub fn new(title: impl Into