diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index f1ad5734d3..0d5521bf3c 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -604,7 +604,7 @@ impl ShapeHooks for BodyHooks<'_> { self.surface_press(comp.sense().contains(craft_behavior::Sense::SURFACE), cx); let band = props.header.is_some_and(|header| header.band == DragBand::Move); if (band || matches!(props.mount, Mount::Viewport { .. } | Mount::Window { .. })) - && let Some(viewport) = os_window(props, cx.core.memory.ctx(), cx.core.id) + && let Some(viewport) = os_window(props, cx.core.memory.ctx(), cx.core.id, &cx.core.env) { self.window_os(viewport, cx); self.window_movable(viewport, cx); diff --git a/crates/ui-contract/src/contract/body/window.rs b/crates/ui-contract/src/contract/body/window.rs index e81523958c..505b2f5b7b 100644 --- a/crates/ui-contract/src/contract/body/window.rs +++ b/crates/ui-contract/src/contract/body/window.rs @@ -8,6 +8,7 @@ use crate::contract::{Container, ContainerProps, Cx, Mount, PartsCx}; use crate::runtime::commands::{self, CommandSet as _, WindowCommand}; use crate::runtime::egui::pass; use crate::runtime::egui::{input, viewports}; +use crate::runtime::env::{EnvView, ShellEntry}; use crate::runtime::memory::sub; use crate::runtime::windows::{self, ResizeEdge, WindowAction, WindowHit}; use crate::runtime::{keys, pointer}; @@ -35,6 +36,8 @@ struct WindowParts { impl WindowParts { /// The parts of a layer or a full-window mount, which move the container and never the OS window. const LAYER: WindowParts = WindowParts { band: true, grips: true }; + /// The parts of a container that stands for no OS window and is no layer: none. + const NONE: WindowParts = WindowParts { band: false, grips: false }; fn of(ctx: &Context, viewport: ViewportId) -> WindowParts { let window = windows::shell_of(ctx, viewport).window; @@ -54,12 +57,15 @@ fn answered_by_platform(ctx: &Context, viewport: ViewportId) -> bool { /// The viewport of the OS window a container's move and resize parts act on: a viewport mount's own window, the /// window a `Window`, `Pane` or `Inline` container is shown in. A layer (also a viewport mount's fallback layer) -/// and a full-window mount are not an OS window of their own. -pub(super) fn os_window(props: &ContainerProps, ctx: &Context, id: Id) -> Option { +/// and a full-window mount are not an OS window of their own. Nor is a `Window`, `Pane` or `Inline` container inside an +/// embedded shell (`ShellCx::region`): the shell fills a region of another window and never acts on that window, so a +/// title bar in an app preview moves, frames and reports nothing. +pub(super) fn os_window(props: &ContainerProps, ctx: &Context, id: Id, env: &EnvView<'_>) -> Option { + let embedded = env.get::().0.is_some_and(|shell| shell.region().is_some()); match &props.mount { Mount::Viewport { .. } if viewports::available(ctx) => Some(viewports::viewport_id(id)), - Mount::Window { .. } | Mount::Pane(_) | Mount::Inline => Some(pass::current_viewport(ctx)), - Mount::Viewport { .. } | Mount::Layer(_) | Mount::FullWindow => None, + Mount::Window { .. } | Mount::Pane(_) | Mount::Inline if !embedded => Some(pass::current_viewport(ctx)), + Mount::Window { .. } | Mount::Pane(_) | Mount::Inline | Mount::Viewport { .. } | Mount::Layer(_) | Mount::FullWindow => None, } } @@ -451,7 +457,7 @@ impl BodyHooks<'_> { if !matches!(props.mount, Mount::Viewport { .. }) { return; } - let Some(viewport) = os_window(props, &cx.core.ctx, cx.core.id) else { return }; + let Some(viewport) = os_window(props, &cx.core.ctx, cx.core.id, &cx.core.env) else { return }; let Some(facts) = windows::WindowHost::of(&cx.core.ctx).facts(viewport) else { return }; let now = facts.outer.unwrap_or(facts.inner); let placed = &mut cx.core.memory.shown::(sub(sub(cx.core.id, "body"), "window_placed")).0; @@ -472,8 +478,13 @@ impl BodyHooks<'_> { let ctx = pcx.ui.ctx().clone(); let handle = pcx.tokens.tokens().size.handle.sm; let reason = "the Move and Size commands of the window command set do the same"; - let os = os_window(props, &ctx, pcx.id); - let stands = os.map_or(WindowParts::LAYER, |viewport| WindowParts::of(&ctx, viewport)); + let os = os_window(props, &ctx, pcx.id, &pcx.env); + let stands = match (&props.mount, os) { + (_, Some(viewport)) => WindowParts::of(&ctx, viewport), + (Mount::Layer(_) | Mount::Viewport { .. } | Mount::FullWindow, None) => WindowParts::LAYER, + // A container of an embedded shell: no OS window to move or resize. + (Mount::Window { .. } | Mount::Pane(_) | Mount::Inline, None) => WindowParts::NONE, + }; let exclusive = |hit: WindowHit| os.is_some().then_some(hit); let moves_window = matches!(&props.mount, Mount::Layer(overlay) | Mount::Viewport { fallback: overlay, .. } if overlay.moves_window); // A layer moves when its overlay says so; other mounts follow the header's band. diff --git a/crates/ui-shell/src/shell/title_bar/bar.rs b/crates/ui-shell/src/shell/title_bar/bar.rs index 0c4963fcb1..10b22496c6 100644 --- a/crates/ui-shell/src/shell/title_bar/bar.rs +++ b/crates/ui-shell/src/shell/title_bar/bar.rs @@ -29,7 +29,8 @@ use egui::{Response, Ui, pos2}; pub struct TitleBarOutput { /// The bar's response. pub response: Response, - /// The caption button clicked in this pass. Minimize and Maximize have already acted on the window. + /// The caption button clicked in this pass. Minimize and Maximize have already acted on the window, unless the bar + /// is in an embedded shell. pub caption_clicked: Option, /// The Close button was clicked. The bar never closes the window: the app runs its unsaved-documents path. pub close_requested: bool, @@ -55,7 +56,10 @@ impl Outcome for TitleBarOutput { /// A double click maximizes or restores. /// /// In full screen the bar shows no captions and reserves no room. The free area is the container's move band: -/// pressing and dragging it moves the window, and a double click acts as above. The body is the leading content; the +/// pressing and dragging it moves the window, and a double click acts as above. +/// +/// In an embedded shell (an app preview, a story) the bar is a picture of a window: it acts on no window and reads +/// none. The captions show hover and report their clicks, but send no window command, and the bar has no move band. The body is the leading content; the /// trailing content ([`trailing`](TitleBar::trailing)) sits before the end's captions. [`show_titled`](TitleBar::show_titled) /// adds the title in the gap between them. Close never closes the window; see [`TitleBarOutput`]. #[derive(crate::contract::Component, crate::contract::Props)] @@ -91,6 +95,8 @@ struct Window { maximized: bool, double_click: DoubleClick, ends: Ends, + /// Whether the bar acts on the running window: false in an embedded shell. + acts: bool, } impl<'a> TitleBar<'a> { @@ -146,14 +152,15 @@ impl<'a> TitleBar<'a> { let ctx = cx.memory.ctx().clone(); let shell = windows::shell_of(&ctx, pass::current_viewport(&ctx)); let frame = self.frame.clone().unwrap_or_else(|| WindowHost::of(&ctx).frame()); - let state = shell.window.state; + let acts = !cx.shell().is_some_and(|shell| shell.region().is_some()); + let state = if acts { shell.window.state } else { WindowState::Normal }; let rtl = cx.env.direction().is_rtl(); let ends = Ends::of(&frame, state != WindowState::Fullscreen, rtl); - Window { frame, maximized: self.maximized.unwrap_or(state == WindowState::Maximized), double_click: shell.double_click, ends } + Window { frame, maximized: self.maximized.unwrap_or(state == WindowState::Maximized), double_click: shell.double_click, ends, acts } } - /// Shows one end's room and buttons, and acts on a click: Minimize and Maximize go to the window, Close is - /// reported. + /// Shows one end's room and buttons, and acts on a click: Minimize and Maximize go to the window the bar acts on, + /// Close is reported. fn show_end(&mut self, ui: &mut Ui, cx: &BodyCx<'_>, end: &End, window: &Window) { let height = SlotSize::TitleBar.resolve(&Env::of(ui).tokens); let mut buttons: Vec> = end @@ -180,6 +187,7 @@ impl<'a> TitleBar<'a> { } self.clicked = Some(slot.caption); match slot.caption { + Caption::Minimize | Caption::Maximize if !window.acts => {} Caption::Minimize => { host.act(viewport, WindowAction::Minimize); } @@ -243,7 +251,8 @@ impl Container for TitleBar<'_> { /// band acts here, once a pass. fn header(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) { let window = self.window(cx); - if cx.band_double_pressed() + if window.acts + && cx.band_double_pressed() && let Some(action) = windows::double_click_action(&window.frame, window.double_click, window.maximized) { let ctx = cx.memory.ctx().clone(); diff --git a/crates/ui-shell/tests/it/shell/mod.rs b/crates/ui-shell/tests/it/shell/mod.rs index decdc7ce91..0a7c9fd8de 100644 --- a/crates/ui-shell/tests/it/shell/mod.rs +++ b/crates/ui-shell/tests/it/shell/mod.rs @@ -2,4 +2,5 @@ mod caption_button; mod screen_mode; +mod title_bar; mod window_parts; diff --git a/crates/ui-shell/tests/it/shell/title_bar.rs b/crates/ui-shell/tests/it/shell/title_bar.rs new file mode 100644 index 0000000000..d4c1940547 --- /dev/null +++ b/crates/ui-shell/tests/it/shell/title_bar.rs @@ -0,0 +1,93 @@ +//! A title bar in an embedded shell is a picture of a window: it reports its captions and acts on no window. + +use craft_host::target::ShellInfo; +use craft_ui::contract::Role; +use craft_ui::runtime::InstallSpec; +use craft_ui::runtime::env::WindowKey; +use craft_ui::shell::{AppShell, Caption, CaptionFamily, Captions, Frame, Title, TitleBar}; +use craft_ui::testing::pointer::{moved, press, release}; +use craft_ui::testing::trial; +use craft_ui::testing::windows::NoWindows; +use craft_ui_test_kit::windows::Windows; +use craft_window::policy::{Decision, Desktop, DesktopKind}; +use egui::{Event, Pos2, Rect, Ui, ViewportCommand, ViewportId, vec2}; +use std::cell::Cell; +use std::sync::Arc; + +/// The window commands a title bar sends for a click on its Maximize button, a drag of its free band and a double +/// click on the band, and the caption it reports. With `embedded` the bar is the title bar of an embedded shell. +fn sent(embedded: bool) -> (Vec, Option) { + let layout = craft_window::Layout { left: vec![], right: vec![Caption::Minimize, Caption::Maximize, Caption::Close] }; + let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout }); + let desktop = Desktop { kind: DesktopKind::Other, ssd: None, gtk_theme: None, icon_theme: None }; + let windows = Windows::with(InstallSpec::new(ShellInfo::default(), Arc::new(NoWindows)).frame(Decision { frame, overridden: None, desktop })); + let (bar, clicked) = (Cell::new(Rect::NOTHING), Cell::new(None)); + let show_bar = |ui: &mut Ui| { + let shown = TitleBar::new().show_titled(ui, Title::new("Landscape.psd"), |_, _| {}, |_, _| {}); + bar.set(shown.response.rect); + clicked.set(clicked.get().or(shown.caption_clicked)); + }; + let frame = |events: Vec| { + windows.frame(events, vec![], |ui| { + if embedded { + let _ = AppShell::embedded(WindowKey::new("preview")).title_bar(|ui, _, _: &mut ()| show_bar(ui)).show(ui, &mut ()); + } else { + show_bar(ui); + } + }) + }; + frame(vec![]); + let settled = frame(vec![]); + let tree = &settled.trees.iter().rev().find(|(viewport, _)| *viewport == ViewportId::ROOT).expect("the root pass ran").1; + let maximize = tree + .with_role(Role::BUTTON) + .into_iter() + .find(|key| tree.name_of(*key) == Some("Maximize")) + .and_then(|key| tree.bounds_of(key)) + .expect("the bar shows its Maximize button") + .center(); + // The title is no control: the band lies under it. + let band = Pos2::new(bar.get().center().x, bar.get().center().y); + let to = band + vec2(40.0, 0.0); + let steps = [ + vec![moved(maximize)], + vec![press(maximize)], + vec![release(maximize)], + vec![moved(band)], + vec![press(band)], + vec![moved(to)], + vec![release(to)], + vec![moved(band)], + vec![press(band)], + vec![release(band)], + vec![press(band)], + vec![release(band)], + ]; + let mut commands = Vec::new(); + for events in steps { + commands.extend(frame(events).commands.into_iter().map(|(_, command)| command).filter(|command| { + matches!( + command, + ViewportCommand::StartDrag + | ViewportCommand::Maximized(_) + | ViewportCommand::Minimized(_) + | ViewportCommand::OuterPosition(_) + | ViewportCommand::BeginResize(_) + ) + })); + } + (commands, clicked.get()) +} + +/// A title bar in an embedded shell (an app preview in a gallery card) reports a click on its Maximize button and +/// sends no window command for it, for a drag of its free band or for a double click on the band. The same bar in a +/// window acts on the window. +#[trial] +fn a_title_bar_in_an_embedded_shell_acts_on_no_window() { + let (commands, clicked) = sent(true); + assert_eq!(clicked, Some(Caption::Maximize), "the embedded bar reports the caption"); + assert!(commands.is_empty(), "the embedded bar sends nothing to the window: {commands:?}"); + let (commands, clicked) = sent(false); + assert_eq!(clicked, Some(Caption::Maximize), "the window's bar reports the caption"); + assert!(!commands.is_empty(), "the window's bar acts on the window"); +}