Files
craft-libs/crates/ui-shell/tests/it/shell/title_bar.rs
T
aiongg 5e11bad614 A container in an embedded shell acts on no OS window; its title bar is a picture of a window
os_window reads the embedded-shell signal (ShellCx::region): inline, pane and window
mounts inside an embedded shell stand for no OS window, so they get no move band or
grips and report nothing to the window. TitleBar reads the same signal: its captions
report their clicks but send no window command, a double click acts on nothing, and
it reads no window state.
2026-11-12 14:32:44 +00:00

94 lines
4.2 KiB
Rust

//! 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<ViewportCommand>, Option<Caption>) {
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<Event>| {
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");
}