Files
craft-libs/crates/ui-contract/tests/it/runtime/window_move.rs
T
aiongg b35a0faee0 Floating windows: production's bar height, grips and AppKit resize
- The floating window's title row is one text row high (HeaderStrength::Bar, size.row.text: 18 pt compact), as
  production's 18 pt bar; the tab strip below keeps the panel header height.
- A resizable or movable layer with a header keeps its top grip's band (contract::grip_band, size.handle.sm) clear
  above the header, so the top grip never lies over it; the regions start below the band or the padding, whichever
  is deeper. Dialogs keep their layout (their padding is deeper than the band).
- Grips lie inside the frame for layers too; corners reach twice the edge width along each edge (12 pt), top corners
  of a banded layer only the band's height.
- Where the OS resizes a window from its own frame (macOS Inset, resizable: windows::os_resizes) no app grips stand,
  the cursor over AppKit's resize areas is the default (craft_window::mac::in_resize_area) and the move band keeps
  the arrow, as an OS title bar does.
- DockSizes::window_chrome (title row plus the band in the app window) sizes floating windows, stepped resizes,
  collapsed windows and float previews (FloatSpace::chrome).
2026-11-20 16:57:13 +00:00

188 lines
8.2 KiB
Rust

//! The body step moves and resizes an OS window (16.3, 21.2): a move band acts only where the app draws the frame and
//! the window is not full screen (a maximized window's move is the platform's, which restores it), and grips only where
//! it is neither maximized nor full screen; a `Window` mount puts its grips on the window's edges; a double click on the
//! move band is reported once.
use craft_behavior::{Cursor, CursorIcon};
use craft_host::target::{ShellInfo, WindowChrome, WindowInfo, WindowKind};
use craft_ui::contract::overlay::Resize;
use craft_ui::contract::{
BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Mount, Props as _, Role, Show as _, ShowContainer as _, Sizing,
};
use craft_ui::runtime::InstallSpec;
use craft_ui::style::StyleCx;
use craft_ui::testing::layout::allocate;
use craft_ui::testing::pointer::{moved, press, release};
use craft_ui::testing::trial;
use craft_ui::testing::windows::FakeSystem;
use craft_ui_test_kit::contract::{PlainStyle, Probe, Seen};
use craft_ui_test_kit::windows::Windows;
use egui::viewport::ResizeDirection;
use egui::{Event, Pos2, Rect, Response, Ui, Vec2, ViewportCommand, pos2, vec2};
use std::cell::{Cell, RefCell};
use std::sync::Arc;
const HEADER: Vec2 = Vec2::new(100.0, 24.0);
/// The size of the root window in the windows kit.
const ROOT: Vec2 = Vec2::new(800.0, 600.0);
/// A container whose header is a block of a fixed size; it counts the passes in which its move band reported a
/// double click.
#[derive(craft_ui::contract::Component)]
#[component(container)]
struct Bar<'a> {
#[common]
common: Common,
#[container]
props: ContainerProps,
header: &'a Cell<Rect>,
doubles: &'a Cell<u32>,
}
impl Component for Bar<'_> {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: BarStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
impl Container for Bar<'_> {
fn header(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
self.header.set(allocate(ui, HEADER));
if cx.band_double_pressed() {
self.doubles.set(self.doubles.get() + 1);
}
}
}
/// A frame source for one container: it keeps the header rect and the double click count.
struct Rig {
header: Cell<Rect>,
doubles: Cell<u32>,
seen: RefCell<Seen>,
props: ContainerProps,
}
impl Rig {
fn new(mount: Mount) -> Self {
let props = ContainerProps::new().mount(mount).sizing(Sizing::Content).header(HeaderRegion::new().band(DragBand::Move));
Rig { header: Cell::new(Rect::NOTHING), doubles: Cell::new(0), seen: RefCell::new(Seen::default()), props }
}
fn show(&self, ui: &mut Ui) {
Bar { common: Common::new(), props: self.props.clone(), header: &self.header, doubles: &self.doubles }.id_salt("win").show(ui, |ui, _| {
Probe::new(&self.seen).size(vec2(40.0, 20.0)).show(ui);
});
}
fn frame(&self, windows: &Windows, root: Vec<Event>, child: Vec<Event>) -> Vec<ViewportCommand> {
windows.frame(root, child, |ui| self.show(ui)).commands.into_iter().map(|(_, command)| command).collect()
}
}
/// Presses at `at`, drags by `by` in four steps and releases. `events` wraps each step for the viewport under test.
fn drag(at: Pos2, by: Vec2, mut frame: impl FnMut(Vec<Event>) -> Vec<ViewportCommand>) -> Vec<ViewportCommand> {
let mut sent = frame(vec![moved(at)]);
sent.extend(frame(vec![press(at)]));
for step in 1..=4 {
sent.extend(frame(vec![moved(at + by * (step as f32 / 4.0))]));
}
sent.extend(frame(vec![release(at + by)]));
sent.extend(frame(vec![]));
sent
}
fn resizes(sent: &[ViewportCommand]) -> Vec<ResizeDirection> {
sent.iter()
.filter_map(|command| match command {
ViewportCommand::BeginResize(direction) => Some(*direction),
_ => None,
})
.collect()
}
/// The root window with the frame drawn by the app, or by the OS.
fn root(chrome: WindowChrome) -> Windows {
let shell = ShellInfo::default().window(WindowInfo::new(WindowKind::Main).chrome(chrome));
Windows::with(InstallSpec::new(shell, Arc::new(FakeSystem::new())))
}
fn root_drag(chrome: WindowChrome, at: Pos2, by: Vec2) -> (Vec<ViewportCommand>, Rig) {
let windows = root(chrome);
let rig = Rig::new(Mount::window(Resize::GRIPS));
for _ in 0..4 {
rig.frame(&windows, vec![], vec![]);
}
(drag(at, by, |events| rig.frame(&windows, events, vec![])), rig)
}
/// A grip pulled out of the window resizes it: winit reports the pointer leaving the window (`PointerGone`) while the
/// button is held, before the press has passed slop, and the moves from outside still start the drag. The resize used
/// to start only when the pointer passed slop inside the window, so a quick outward pull on an edge did nothing.
#[trial]
fn a_grip_pulled_out_of_the_window_still_resizes_it() {
let windows = root(WindowChrome::Integrated);
let rig = Rig::new(Mount::window(Resize::GRIPS));
for _ in 0..4 {
rig.frame(&windows, vec![], vec![]);
}
let edge = pos2(2.0, ROOT.y / 2.0);
let mut sent = rig.frame(&windows, vec![moved(edge)], vec![]);
sent.extend(rig.frame(&windows, vec![press(edge)], vec![]));
// The first move lands outside the window, within slop of the press; winit reports the leave with it.
sent.extend(rig.frame(&windows, vec![moved(edge - vec2(3.0, 0.0)), Event::PointerGone], vec![]));
for step in 2..=4 {
sent.extend(rig.frame(&windows, vec![moved(edge - vec2(5.0 * step as f32, 0.0))], vec![]));
}
sent.extend(rig.frame(&windows, vec![release(edge - vec2(20.0, 0.0))], vec![]));
sent.extend(rig.frame(&windows, vec![], vec![]));
assert_eq!(resizes(&sent), [ResizeDirection::West], "a pull out of the left edge resizes from the west: {sent:?}");
}
#[trial]
fn a_window_mounts_edges_resize_the_window_and_the_childs_press_keeps_its_click() {
let (sent, _) = root_drag(WindowChrome::Integrated, pos2(1.0, ROOT.y / 2.0), vec2(20.0, 0.0));
assert_eq!(resizes(&sent), [ResizeDirection::West], "a drag on the left edge resizes from the west: {sent:?}");
let (sent, _) = root_drag(WindowChrome::Native, pos2(1.0, ROOT.y / 2.0), vec2(20.0, 0.0));
assert!(resizes(&sent).is_empty(), "the OS resizes a window it draws the frame of: {sent:?}");
// The probe is the first thing in the body, under the left edge: a click on it is the probe's, not a resize.
let windows = root(WindowChrome::Integrated);
let rig = Rig::new(Mount::window(Resize::GRIPS));
for _ in 0..4 {
rig.frame(&windows, vec![], vec![]);
}
let on_probe = pos2(1.0, rig.header.get().bottom() + 5.0);
let mut sent = rig.frame(&windows, vec![moved(on_probe)], vec![]);
sent.extend(rig.frame(&windows, vec![press(on_probe)], vec![]));
sent.extend(rig.frame(&windows, vec![release(on_probe)], vec![]));
sent.extend(rig.frame(&windows, vec![], vec![]));
assert_eq!(rig.seen.borrow().activations, 1, "the child under the edge keeps its click");
assert!(resizes(&sent).is_empty(), "and the click starts no resize: {sent:?}");
}
/// Where the OS draws a resizable window's frame (macOS `Inset`), AppKit resizes the window from its edges: the app
/// registers no grips there, so a drag on an edge starts no resize of the app's, and over AppKit's resize areas the
/// cursor is the default, as the OS shows its own there.
#[trial]
fn where_appkit_resizes_a_window_the_app_registers_no_grips_and_leaves_the_cursor_to_it() {
let edge = pos2(1.0, ROOT.y / 2.0);
let (sent, _) = root_drag(WindowChrome::Inset, edge, vec2(20.0, 0.0));
assert!(resizes(&sent).is_empty(), "AppKit resizes the window, the app's grips stand down: {sent:?}");
let windows = root(WindowChrome::Inset);
let rig = Rig::new(Mount::window(Resize::GRIPS));
for _ in 0..4 {
rig.frame(&windows, vec![], vec![]);
}
rig.frame(&windows, vec![moved(edge)], vec![]);
let cursor = craft_ui::runtime::cursor::current(&windows.ctx);
assert!(matches!(cursor, None | Some(Cursor::System(CursorIcon::Default))), "over AppKit's edge the cursor is left to the OS: {cursor:?}");
}