Files
craft-libs/crates/ui-shell/tests/it/shell/title_bar.rs
T

131 lines
5.9 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, ViewportInfo, 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");
}
/// The shell sends the window title once after a change and not again while the viewport reports it; an embedded shell
/// sends none.
#[trial]
fn the_window_title_is_sent_once_per_change() {
let titles = |out: &craft_ui_test_kit::windows::Out| -> Vec<String> {
out.commands
.iter()
.filter_map(|(viewport, command)| match command {
ViewportCommand::Title(title) if *viewport == ViewportId::ROOT => Some(title.clone()),
_ => None,
})
.collect()
};
let windows = Windows::new();
let frame = |title: &str, embedded: bool| {
let key = WindowKey::new("titled");
windows.frame(vec![], vec![], |ui| {
let shell = if embedded { AppShell::embedded(key.clone()) } else { AppShell::new(key.clone()) };
let _ = shell.window_title(title).show(ui, &mut ());
})
};
let reported = |title: &str| windows.root_info(ViewportInfo { title: Some(title.to_owned()), ..ViewportInfo::default() });
assert_eq!(titles(&frame("A", false)), ["A"], "a new title is sent");
reported("A");
assert!(titles(&frame("A", false)).is_empty(), "a title the viewport reports is not sent again");
assert_eq!(titles(&frame("B", false)), ["B"], "a changed title is sent");
reported("B");
assert!(titles(&frame("B", false)).is_empty(), "the changed title is sent once");
let embedded = Windows::new();
let out = embedded.frame(vec![], vec![], |ui| {
let _ = AppShell::embedded(WindowKey::new("preview")).window_title("C").show(ui, &mut ());
});
assert!(titles(&out).is_empty(), "an embedded shell sends no title");
}