Files
craft-libs/crates/ui/src/docking/split_tests.rs
T
aiongg 99d8fe537e docking: a dragged divider shrinks the groups beyond it in turn
A divider dragged past the minimum of the group it shrinks moves on to
the next group along the axis, and so on until every group on that side
is at its minimum. The line follows same-axis splits up the tree and
ends at a split across the axis or beside a dock. Groups touching the
divider on the other side grow, up to their maximums, as before.

Stacked panels and the groups of a dock column cascade the same way.
2026-10-24 03:05:32 +00:00

804 lines
36 KiB
Rust

//! A split tree of tab groups in an editing layout like FilmCraft's: Source | Program |
//! Properties over Project | Tools | Timeline | Audio Meters, every group an ordinary
//! resizable one. One test per docking rule, the stuck cases of the FilmCraft map, and a
//! property test over random drops.
use std::collections::HashMap;
use super::floating_tests::frame_style;
use super::render::{Drawn, Sizer};
use super::testing::DockDriver;
use super::*;
use crate::layout::{SplitAxis, SplitSize};
use egui::{Modifiers, Pos2, Rect, Vec2, pos2, vec2};
const SOURCE: [u32; 4] = [1, 11, 12, 13];
const PROGRAM: u32 = 2;
const PROPERTIES: [u32; 3] = [3, 31, 32];
const PROJECT: [u32; 7] = [4, 41, 42, 43, 44, 45, 46];
const TOOLS: u32 = 5;
const TIMELINE: u32 = 6;
const METERS: u32 = 7;
const INFO: u32 = 9;
const SCREEN: Vec2 = Vec2::new(1440.0, 860.0);
/// Panel bodies by their rects; `caps` are maximum widths a host asks for, `rules` the
/// permissions of panels that differ from the default.
#[derive(Default)]
struct Film {
bodies: HashMap<u32, Rect>,
caps: HashMap<u32, f32>,
rules: HashMap<u32, Permissions>,
}
impl DockContent<u32> for Film {
fn body(&mut self, ui: &mut egui::Ui, panel: &u32) {
self.bodies.insert(*panel, ui.max_rect());
}
fn logical_label(&self, panel: &u32) -> String {
format!("Panel {panel}")
}
fn limits(&self, panel: &u32) -> PanelLimits {
match self.caps.get(panel) {
Some(width) => PanelLimits { min: Vec2::ZERO, max: vec2(*width, 1_000_000.0) },
None => PanelLimits::default(),
}
}
fn permissions(&self, panel: &u32) -> Permissions {
self.rules.get(panel).copied().unwrap_or_default()
}
}
type Driver = DockDriver<u32, Film>;
fn tabs(panels: &[u32]) -> Node<u32> {
Node::Tabs { panels: panels.to_vec(), active: 0 }
}
fn split(axis: SplitAxis, ratio: f32, first: Node<u32>, second: Node<u32>) -> Node<u32> {
Node::Split { axis, size: SplitSize::Ratio(ratio), first: Box::new(first), second: Box::new(second) }
}
fn hsplit(ratio: f32, first: Node<u32>, second: Node<u32>) -> Node<u32> {
split(SplitAxis::Horizontal, ratio, first, second)
}
/// The Editing workspace, with Info floating over Properties.
fn editing() -> Layout<u32> {
let top = hsplit(0.69, hsplit(0.5, tabs(&SOURCE), tabs(&[PROGRAM])), tabs(&PROPERTIES));
let row = hsplit(0.06, tabs(&[TOOLS]), hsplit(0.88, tabs(&[TIMELINE]), tabs(&[METERS])));
let bottom = hsplit(0.29, tabs(&PROJECT), row);
Layout {
root: Some(split(SplitAxis::Vertical, 0.57, top, bottom)),
floating: vec![Floating::single(vec![INFO], 0, [1120.0, 60.0, 280.0, 200.0])],
}
}
fn driver(layout: Layout<u32>, content: Film) -> Driver {
let mut d = DockDriver::new(layout, content).with_style(frame_style);
d.screen = Rect::from_min_size(Pos2::ZERO, SCREEN);
d.area = d.screen;
d.settle();
d
}
fn film() -> Driver {
driver(editing(), Film::default())
}
/// A group's rect: its active body and its tab strip.
fn group(d: &Driver, panel: u32) -> Rect {
let body = d.content.bodies.get(&panel).copied().unwrap_or_else(|| panic!("panel {panel} not drawn"));
Rect::from_min_max(pos2(body.left(), body.top() - 26.0), body.max)
}
fn tab(d: &Driver, panel: u32) -> Pos2 {
d.hit(&Hit::Tab(panel)).unwrap_or_else(|| panic!("tab {panel}")).center()
}
/// Drag a divider by `delta`, hovering it first as a pointer does.
fn drag_hit(d: &mut Driver, hit: Hit<u32>, delta: Vec2) {
let at = d.hit(&hit).unwrap_or_else(|| panic!("{hit:?} in {:?}", d.hits())).center();
d.move_to(at);
d.drag(at, at + delta);
d.settle();
}
fn divider(path: &[bool]) -> Hit<u32> {
Hit::Divider(Divider::Split(path.to_vec()))
}
fn sizes(d: &Driver, panels: &[u32]) -> Vec<Vec2> {
panels.iter().map(|p| group(d, *p).size()).collect()
}
fn close(a: Rect, b: Rect) -> bool {
(a.min - b.min).length() < 1.0 && (a.max - b.max).length() < 1.0
}
#[test]
fn a_group_is_a_picture_frame_whose_edges_split_it_in_half_and_whose_middle_joins_it() {
let base = film();
let timeline = group(&base, TIMELINE);
let others = [PROJECT[0], TOOLS, METERS, SOURCE[0], PROGRAM, PROPERTIES[0]];
// Halfway into each 40 pt band of the body below the tab strip.
let inset = |zone| {
let (w, h) = (20.0, 20.0);
match zone {
Zone::Left => pos2(timeline.left() + w, timeline.center().y + 13.0),
Zone::Right => pos2(timeline.right() - w, timeline.center().y + 13.0),
Zone::Top => pos2(timeline.center().x, timeline.top() + 26.0 + h),
Zone::Bottom => pos2(timeline.center().x, timeline.bottom() - h),
Zone::Center => pos2(timeline.center().x, timeline.center().y + 13.0),
}
};
for zone in [Zone::Left, Zone::Right, Zone::Top, Zone::Bottom] {
let mut d = film();
let before = sizes(&d, &others);
let at = inset(zone);
d.hold(tab(&d, INFO), at);
assert!(d.filled_polygon(&zone_shape(timeline, 26.0, 40.0, zone).unwrap()), "{zone:?}: the preview fills that band");
let out = d.release(at);
assert_eq!(out.actions, vec![Action::Move { panel: INFO, anchor: TIMELINE, placement: Placement::Split(zone) }], "{zone:?}");
d.settle();
let (info, rest) = (group(&d, INFO), group(&d, TIMELINE));
assert!(close(info.union(rest), timeline), "{zone:?}: the two halves fill the group: {info:?} {rest:?} in {timeline:?}");
assert!((info.area() - rest.area()).abs() < info.area() * 0.05, "{zone:?}: about halves");
assert_eq!(sizes(&d, &others), before, "{zone:?}: no other group changes");
}
// The middle and the tab row join the group.
let mut d = film();
let out = d.drag(tab(&d, INFO), inset(Zone::Center));
assert_eq!(out.actions, vec![Action::Move { panel: INFO, anchor: TIMELINE, placement: Placement::Split(Zone::Center) }]);
let mut d = film();
let out = d.drag(tab(&d, INFO), pos2(timeline.center().x, timeline.top() + 10.0));
assert!(matches!(&out.actions[..], [Action::Move { panel: INFO, placement: Placement::Tab { .. }, .. }]), "{:?}", out.actions);
// The gap between two groups belongs to neither: the drag cancels.
let mut d = film();
let tools = group(&d, TOOLS);
let gap = pos2(tools.right() + 2.0, tools.center().y);
let out = d.drag(tab(&d, INFO), gap);
assert!(out.actions.is_empty(), "{:?}", out.actions);
}
#[test]
fn window_edges_add_a_full_column_or_row_without_taking_the_groups_edges() {
let mut d = film();
let project = group(&d, PROJECT[0]);
let out = d.drag(tab(&d, INFO), pos2(3.0, project.center().y));
assert_eq!(out.actions, vec![Action::Move { panel: INFO, anchor: SOURCE[0], placement: Placement::Edge(Zone::Left) }]);
d.settle();
let info = group(&d, INFO);
assert_eq!((info.top(), info.bottom(), info.left()), (0.0, SCREEN.y, 0.0), "a full-height column at the left");
assert!((info.width() - SCREEN.x * 0.25).abs() < 4.0, "{info:?}");
for (at, zone) in [(pos2(SCREEN.x - 3.0, 300.0), Zone::Right), (pos2(700.0, 3.0), Zone::Top), (pos2(700.0, SCREEN.y - 3.0), Zone::Bottom)] {
let mut d = film();
let out = d.drag(tab(&d, INFO), at);
assert!(matches!(&out.actions[..], [Action::Move { placement: Placement::Edge(z), .. }] if *z == zone), "{zone:?}: {:?}", out.actions);
}
// Past the window band, the group's own edge band splits that group.
let mut d = film();
let out = d.drag(tab(&d, INFO), pos2(project.left() + 20.0, project.center().y + 13.0));
assert_eq!(out.actions, vec![Action::Move { panel: INFO, anchor: PROJECT[0], placement: Placement::Split(Zone::Left) }]);
}
#[test]
fn groups_have_one_small_minimum_and_no_maximum() {
let mut d = film();
let tools = group(&d, TOOLS).width();
drag_hit(&mut d, divider(&[true, true]), vec2(400.0, 0.0));
assert!((group(&d, TOOLS).width() - (tools + 400.0)).abs() < 1.0, "Tools grows as wide as it is dragged");
drag_hit(&mut d, divider(&[true, true]), vec2(-2000.0, 0.0));
assert_eq!(group(&d, TOOLS).width(), 40.0, "and shrinks to the minimum, no further");
// A group too narrow for two halves takes a drop on its edge as a tab.
let tools = group(&d, TOOLS);
let out = d.drag(tab(&d, INFO), pos2(tools.right() - 4.0, tools.center().y + 30.0));
assert!(
matches!(&out.actions[..], [Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::Split(Zone::Center) | Placement::Tab { .. } }]),
"{:?}",
out.actions
);
}
#[test]
fn a_divider_resizes_only_the_groups_that_share_it() {
// Timeline | Meters: Project and Tools keep their widths.
let mut d = film();
let (project, tools, timeline, meters) = (group(&d, PROJECT[0]), group(&d, TOOLS), group(&d, TIMELINE), group(&d, METERS));
drag_hit(&mut d, divider(&[true, true, true]), vec2(-100.0, 0.0));
assert_eq!((group(&d, PROJECT[0]), group(&d, TOOLS)), (project, tools));
assert!((group(&d, TIMELINE).width() - (timeline.width() - 100.0)).abs() < 1.0);
assert!((group(&d, METERS).width() - (meters.width() + 100.0)).abs() < 1.0);
// Project | the rest: only Tools, on the other side, gives way; Timeline and Meters stay.
let mut d = film();
drag_hit(&mut d, divider(&[true]), vec2(15.0, 0.0));
assert!((group(&d, PROJECT[0]).width() - (project.width() + 15.0)).abs() < 1.0);
assert!((group(&d, TOOLS).width() - (tools.width() - 15.0)).abs() < 1.0);
assert!(close(group(&d, TIMELINE), timeline) && close(group(&d, METERS), meters), "{:?}", d.layout);
// Top row: Source | Program leaves Properties alone.
let mut d = film();
let properties = group(&d, PROPERTIES[0]);
drag_hit(&mut d, divider(&[false, false]), vec2(80.0, 0.0));
assert_eq!(group(&d, PROPERTIES[0]), properties);
}
#[test]
fn a_divider_dragged_past_a_groups_minimum_shrinks_the_next_group_along_the_row() {
// A | B | C | D, 357 pt each, as (A | B) | (C | D): the cascade runs on past B's split.
let row = hsplit(0.5, hsplit(0.5, tabs(&[1]), tabs(&[2])), hsplit(0.5, tabs(&[3]), tabs(&[4])));
let mut d = driver(Layout { root: Some(row), floating: Vec::new() }, Film::default());
let widths = |d: &Driver| [1, 2, 3, 4].map(|panel| group(d, panel).width().round());
assert_eq!(widths(&d), [357.0, 357.0, 357.0, 357.0]);
// B gives way down to its 40 pt minimum, then C.
drag_hit(&mut d, divider(&[false]), vec2(400.0, 0.0));
assert_eq!(widths(&d), [757.0, 40.0, 274.0, 357.0]);
// The drag stops when B, C and D are all at their minimums.
drag_hit(&mut d, divider(&[false]), vec2(670.0, 0.0));
assert_eq!(widths(&d), [1308.0, 40.0, 40.0, 40.0]);
// Back the other way, A gives way and only B, which touches the divider, grows.
drag_hit(&mut d, divider(&[false]), vec2(-1300.0, 0.0));
assert_eq!(widths(&d), [40.0, 1308.0, 40.0, 40.0]);
}
#[test]
fn an_intersection_moves_both_dividers() {
let mut d = film();
let (project, tools, source) = (group(&d, PROJECT[0]), group(&d, TOOLS), group(&d, SOURCE[0]));
let hit = Hit::Divider(Divider::Intersection(vec![], vec![true]));
let at = d.hit(&hit).unwrap_or_else(|| panic!("{:?}", d.hits()));
assert!(at.contains(pos2(project.right() + 2.0, project.top() - 2.0)), "{at:?} at Project's top right corner");
drag_hit(&mut d, hit, vec2(15.0, -60.0));
assert!((group(&d, PROJECT[0]).width() - (project.width() + 15.0)).abs() < 1.0);
assert!((group(&d, PROJECT[0]).height() - (project.height() + 60.0)).abs() < 1.0);
assert!((group(&d, TOOLS).width() - (tools.width() - 15.0)).abs() < 1.0);
assert!((group(&d, SOURCE[0]).height() - (source.height() - 60.0)).abs() < 1.0);
assert_eq!(group(&d, SOURCE[0]).width(), source.width());
}
#[test]
fn closing_a_group_gives_its_space_to_its_neighbours() {
let mut d = film();
let (tools, meters) = (group(&d, TOOLS), group(&d, METERS));
d.layout.apply(Action::CloseGroup { panel: TIMELINE }).unwrap();
d.settle();
let meters_now = group(&d, METERS);
assert_eq!(group(&d, TOOLS), tools);
assert_eq!((meters_now.right(), meters_now.top(), meters_now.bottom()), (meters.right(), meters.top(), meters.bottom()));
assert!((meters_now.left() - (tools.right() + 4.0)).abs() < 1.0, "the row has no hole: {meters_now:?}");
}
// The stuck cases of the FilmCraft map (map.md section 3).
#[test]
fn info_dropped_onto_a_capped_group_lifts_the_cap() {
let content = Film { caps: HashMap::from([(METERS, 116.0)]), ..Film::default() };
let mut d = driver(editing(), content);
let meters = group(&d, METERS);
d.drag(tab(&d, INFO), pos2(meters.center().x, meters.center().y + 13.0));
d.settle();
assert!(d.layout.location(&INFO).is_ok_and(|l| l.anchor == Some(METERS)), "{:?}", d.layout);
// Info is the group's shown panel.
let width = group(&d, INFO).width();
drag_hit(&mut d, divider(&[true, true, true]), vec2(-200.0, 0.0));
assert!((group(&d, INFO).width() - (width + 200.0)).abs() < 1.0, "the group grows past the cap of one of its panels");
drag_hit(&mut d, divider(&[true, true, true]), vec2(150.0, 0.0));
assert!((group(&d, INFO).width() - (width + 50.0)).abs() < 1.0, "and shrinks again");
}
#[test]
fn a_drop_beside_a_narrow_group_splits_that_group_only() {
let mut d = film();
drag_hit(&mut d, divider(&[true, true]), vec2(80.0, 0.0));
let (tools, project, timeline, meters) = (group(&d, TOOLS), group(&d, PROJECT[0]), group(&d, TIMELINE), group(&d, METERS));
assert!(tools.width() > 120.0, "{tools:?}");
let at = pos2(tools.right() - 8.0, tools.center().y + 13.0);
d.hold(tab(&d, INFO), at);
assert!(d.filled_polygon(&zone_shape(tools, 26.0, 40.0, Zone::Right).unwrap()), "the preview fills Tools' right band");
let out = d.release(at);
assert_eq!(out.actions, vec![Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::Split(Zone::Right) }]);
d.settle();
assert!(close(group(&d, TOOLS).union(group(&d, INFO)), tools));
assert_eq!((group(&d, PROJECT[0]), group(&d, TIMELINE), group(&d, METERS)), (project, timeline, meters));
}
#[test]
fn every_divider_travels_after_drops() {
let mut d = film();
let tools = group(&d, TOOLS);
d.drag(tab(&d, INFO), pos2(tools.right() - 8.0, tools.center().y + 13.0));
d.settle();
let meters = group(&d, METERS);
d.drag(tab(&d, PROGRAM), pos2(meters.center().x, meters.center().y + 13.0));
d.settle();
let timeline = group(&d, TIMELINE);
d.drag(tab(&d, PROPERTIES[0]), pos2(timeline.center().x, timeline.bottom() - 10.0));
d.settle();
assert_dividers_travel(&mut d);
}
/// Each split divider, in the docked layout and in floating windows, moves both ways when
/// dragged, from the layout as it is.
fn assert_dividers_travel(d: &mut Driver) {
let start = d.layout.clone();
let dividers: Vec<Hit<u32>> = d
.hits()
.into_iter()
.filter_map(|(hit, _)| matches!(hit, Hit::Divider(Divider::Split(_) | Divider::WindowSplit(..))).then_some(hit))
.collect();
assert!(!dividers.is_empty());
// A floating window may cover the dividers of the main area and of other windows.
let covers: Vec<(Rect, &Floating<u32>)> = start
.floating
.iter()
.map(|w| {
let drawn = w.panels().filter(|p| d.content.bodies.contains_key(p)).map(|p| group(d, *p));
let rect = Rect::from_min_size(pos2(w.rect[0], w.rect[1]), vec2(w.rect[2], w.rect[3]));
(drawn.fold(rect, |a, b| a.union(b)), w)
})
.collect();
for hit in dividers {
let own = |w: &Floating<u32>| matches!(&hit, Hit::Divider(Divider::WindowSplit(key, _)) if w.contains(key));
if d.hit(&hit).is_some_and(|r| covers.iter().any(|(c, w)| c.intersects(r) && !own(w))) {
continue;
}
for step in [12.0, -24.0] {
let Some(rect) = d.hit(&hit) else { continue };
let delta = if rect.width() < rect.height() { vec2(step, 0.0) } else { vec2(0.0, step) };
let before = d.layout.clone();
drag_hit(d, hit.clone(), delta);
assert_ne!(d.layout, before, "divider {hit:?} does not move by {step}");
}
d.layout = start.clone();
d.settle();
}
}
/// Every group at or above its minimum, and every divider with room both ways, in the docked
/// layout and in each floating window. Reads the bodies drawn last.
fn assert_sizes(d: &Driver, context: &str) {
let mut style = DockStyle::default();
frame_style(&mut style);
let sizer = Sizer { style: &style, content: &d.content };
let mut trees: Vec<(&Node<u32>, Rect)> = d.layout.root.iter().map(|root| (root, d.area)).collect();
for window in d.layout.floating.iter().filter(|w| !w.minimized) {
// The window's body is the union of its groups.
let drawn = window.panels().filter(|p| d.content.bodies.contains_key(p)).map(|p| group(d, *p));
if let Some(rect) = drawn.reduce(|a, b| a.union(b)) {
trees.push((&window.root, rect));
}
}
for (root, area) in trees {
let mut groups: Vec<Drawn<'_, u32>> = Vec::new();
sizer.groups(root, area, &mut groups);
for g in &groups {
let room = g.rect.size() - g.limits.min;
assert!(room.x >= -0.01 && room.y >= -0.01, "{context}: {:?} is below its minimum {:?}", g.rect, g.limits.min);
}
let mut nodes = vec![(root, area, Vec::new())];
while let Some((node, rect, path)) = nodes.pop() {
let Some([a, b]) = sizer.children(node, rect) else { continue };
let (lo, hi) = sizer.range((root, area), &path);
assert!(lo < -0.5 && hi > 0.5, "{context}: a divider is stuck ({lo}, {hi}) at {rect:?} in {:?}", d.layout);
for (second, (child, rect)) in [(false, a), (true, b)] {
let mut path = path.clone();
path.push(second);
nodes.push((child, rect, path));
}
}
}
}
/// A small deterministic generator, so a failing sequence repeats.
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
self.0 >> 33
}
fn below(&mut self, n: usize) -> usize {
(self.next() % n.max(1) as u64) as usize
}
fn unit(&mut self) -> f32 {
(self.next() % 1000) as f32 / 1000.0
}
}
#[test]
fn random_drops_leave_every_group_its_minimum_and_every_divider_travel() {
// Drops land in the docked layout and in two floating windows, one of them split; tabs and
// drag bars move panels and groups between all three, and Ctrl/Cmd floats some.
let windows = || {
let mut layout = editing();
layout.floating.push(Floating::new(hsplit(0.5, tabs(&[8]), tabs(&[50, 51])), [80.0, 420.0, 640.0, 380.0]));
layout
};
for seed in 1..=6u64 {
let mut rng = Lcg(seed);
let mut d = driver(windows(), Film::default());
let all = panel_set(&d.layout);
for step in 0..16 {
// Only the bodies drawn now.
d.content.bodies.clear();
d.settle();
let panels: Vec<u32> = d.layout.panels().into_iter().copied().collect();
let panel = panels[rng.below(panels.len())];
let bar = d.hit(&Hit::DragBar(panel)).filter(|_| rng.below(3) == 0);
let Some(from) = bar.or_else(|| d.hit(&Hit::Tab(panel))).map(|r| r.center()) else { continue };
let target = panels[rng.below(panels.len())];
let Some(rect) = d.content.bodies.get(&target).copied() else { continue };
let to = match rng.below(8) {
// A window edge.
0 => {
let edges = [
pos2(3.0, rect.center().y),
pos2(SCREEN.x - 3.0, rect.center().y),
pos2(rect.center().x, 3.0),
pos2(rect.center().x, SCREEN.y - 3.0),
];
edges[rng.below(4)]
}
// The tab row.
1 => pos2(rect.center().x, rect.top() - 13.0),
// Anywhere in the body: an edge band or the middle.
_ => rect.lerp_inside(vec2(0.02 + rng.unit() * 0.96, 0.02 + rng.unit() * 0.96)),
};
if rng.below(12) == 0 {
d.modifiers = Modifiers::COMMAND;
}
d.drag(from, to);
d.modifiers = Modifiers::NONE;
d.content.bodies.clear();
d.settle();
let context = format!("seed {seed}, step {step}");
assert_eq!(d.layout.validate(), Ok(()), "{context}");
assert_eq!(panel_set(&d.layout), all, "{context}: a panel was lost");
assert_sizes(&d, &context);
}
assert_dividers_travel(&mut d);
}
}
#[test]
fn ctrl_or_cmd_floats_from_a_tab_or_a_drag_bar_and_the_preview_says_so() {
// The hint names the platform's key, as `ctx.os()` reports it in tests.
let key = if egui::os::OperatingSystem::from_target_os() == egui::os::OperatingSystem::Mac { "⌘" } else { "Ctrl" };
let hint = format!("Hold {key} to undock");
let floated = |actions: &[Action<u32>], panel: u32| match actions {
[Action::Float { panel: p, .. }] => *p == panel,
[Action::MoveGroup { panel: p, to: Target::Float { .. } }] => *p == panel,
_ => false,
};
fn texts(d: &Driver) -> Vec<String> {
fn walk(shape: &egui::Shape, out: &mut Vec<String>) {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| walk(s, out)),
egui::Shape::Text(text) => out.push(text.galley.text().to_owned()),
_ => {}
}
}
let mut out = Vec::new();
d.shapes().iter().for_each(|clipped| walk(&clipped.shape, &mut out));
out
}
let d = film();
let over = group(&d, TIMELINE).center() + vec2(0.0, 13.0);
let from_tab = tab(&d, PROJECT[0]);
let from_bar = d.hit(&Hit::DragBar(PROJECT[0])).unwrap().center();
for from in [from_tab, from_bar] {
// Without the modifier, a release over a group docks there; the preview names the modifier.
let mut d = film();
d.hold(from, over);
assert!(texts(&d).contains(&hint), "{from:?}: {:?}", texts(&d));
let out = d.release(over);
assert!(!out.actions.is_empty() && !floated(&out.actions, PROJECT[0]), "{from:?}: {:?}", out.actions);
// Held, it floats, and the hint is gone.
let mut d = film();
d.modifiers = Modifiers::COMMAND;
d.hold(from, over);
assert!(!texts(&d).contains(&hint), "{from:?}");
let out = d.release(over);
assert!(floated(&out.actions, PROJECT[0]), "{from:?}: {:?}", out.actions);
}
}
/// The Editing workspace under a 24 pt menu bar and above a 20 pt status bar.
fn film_in_window() -> Driver {
let mut d = DockDriver::new(editing(), Film::default()).with_style(frame_style);
d.screen = Rect::from_min_size(Pos2::ZERO, SCREEN);
d.area = Rect::from_min_max(pos2(0.0, 24.0), pos2(SCREEN.x, SCREEN.y - 20.0));
d.settle();
d
}
#[test]
fn a_release_over_no_target_inside_the_window_cancels_the_drag() {
let d = film_in_window();
let (source, program) = (group(&d, SOURCE[0]), group(&d, PROGRAM));
let gap = pos2((source.right() + program.left()) * 0.5, source.center().y);
let from_tab = tab(&d, PROJECT[0]);
let from_bar = d.hit(&Hit::DragBar(PROJECT[0])).unwrap().center();
for from in [from_tab, from_bar] {
for to in [pos2(600.0, 10.0), pos2(600.0, SCREEN.y - 8.0), gap] {
let mut d = film_in_window();
let before = d.layout.clone();
let out = d.drag(from, to);
d.settle();
assert!(out.actions.is_empty(), "{from:?} to {to:?}: {:?}", out.actions);
assert_eq!(d.layout, before, "{from:?} to {to:?}");
}
// Ctrl/Cmd held floats it there.
let mut d = film_in_window();
d.modifiers = Modifiers::COMMAND;
d.drag(from, pos2(600.0, 10.0));
d.settle();
assert!(d.layout.floating.iter().any(|w| w.contains(&PROJECT[0])), "{from:?}: {:?}", d.layout.floating);
}
}
#[test]
fn a_release_outside_the_window_floats() {
let d = film_in_window();
let from_tab = tab(&d, PROJECT[0]);
let from_bar = d.hit(&Hit::DragBar(PROJECT[0])).unwrap().center();
for from in [from_tab, from_bar] {
let mut d = film_in_window();
d.hold(from, pos2(600.0, 10.0));
d.leave();
d.settle();
let window = d.layout.floating.iter().find(|w| w.contains(&PROJECT[0]));
assert!(window.is_some(), "{from:?}: {:?}", d.layout);
// A tab takes one panel; a drag bar takes the group.
assert_eq!(window.unwrap().contains(&PROJECT[1]), from == from_bar, "{from:?}");
}
}
#[test]
fn the_title_bar_moves_a_floating_window_without_docking() {
let mut d = film();
let title = d.hit(&Hit::Title(INFO)).unwrap().center();
let timeline = group(&d, TIMELINE);
d.drag(title, timeline.center());
d.settle();
let window = d.layout.floating.iter().find(|w| w.contains(&INFO)).expect("still floating");
assert!(window.rect[0] < 1000.0 && window.rect[1] > 400.0, "moved towards the drop: {:?}", window.rect);
assert_eq!(d.layout.root, editing().root);
}
fn panel_set(layout: &Layout<u32>) -> Vec<u32> {
let mut panels: Vec<u32> = layout.panels().into_iter().copied().collect();
panels.sort_unstable();
panels
}
fn window_of(layout: &Layout<u32>, panel: u32) -> &Floating<u32> {
layout.floating.iter().find(|w| w.contains(&panel)).unwrap_or_else(|| panic!("{panel} does not float: {layout:?}"))
}
#[test]
fn tabs_and_drag_bars_dock_into_a_floating_window_and_out_of_it() {
let mut d = film();
let all = panel_set(&d.layout);
let info = group(&d, INFO);
let out = d.drag(tab(&d, PROGRAM), pos2(info.left() + 10.0, info.center().y + 13.0));
assert_eq!(out.actions, vec![Action::Move { panel: PROGRAM, anchor: INFO, placement: Placement::Split(Zone::Left) }]);
d.settle();
assert!(matches!(window_of(&d.layout, INFO).root, Node::Split { axis: SplitAxis::Horizontal, .. }), "{:?}", d.layout.floating);
let (program, info) = (group(&d, PROGRAM), group(&d, INFO));
assert!(program.right() < info.left() && (program.width() - info.width()).abs() < 2.0, "{program:?} {info:?}");
// A drag bar inside the window moves its group to a group of the main area.
let timeline = group(&d, TIMELINE);
let bar = d.hit(&Hit::DragBar(INFO)).unwrap().center();
let out = d.drag(bar, timeline.center() + vec2(0.0, 13.0));
assert_eq!(out.actions, vec![Action::MoveGroup { panel: INFO, to: Target::At { anchor: TIMELINE, placement: Placement::Split(Zone::Center) } }]);
d.settle();
assert_eq!(window_of(&d.layout, PROGRAM).root, tabs(&[PROGRAM]));
assert_eq!(panel_set(&d.layout), all);
}
#[test]
fn a_floating_window_tree_resizes_by_its_dividers_and_intersections() {
let mut layout = editing();
let tree = hsplit(0.5, tabs(&[INFO]), split(SplitAxis::Vertical, 0.5, tabs(&[50]), tabs(&[51])));
layout.floating = vec![Floating::new(tree, [700.0, 100.0, 600.0, 500.0])];
let mut d = driver(layout, Film::default());
let ratios = |d: &Driver| match &d.layout.floating[0].root {
Node::Split { size: SplitSize::Ratio(outer), second, .. } => match &**second {
Node::Split { size: SplitSize::Ratio(inner), .. } => (*outer, *inner),
other => panic!("{other:?}"),
},
other => panic!("{other:?}"),
};
let outer = Hit::Divider(Divider::WindowSplit(INFO, Vec::new()));
let inner = Hit::Divider(Divider::WindowSplit(INFO, vec![true]));
for (hit, delta) in [(&outer, vec2(30.0, 0.0)), (&outer, vec2(-60.0, 0.0)), (&inner, vec2(0.0, 30.0)), (&inner, vec2(0.0, -60.0))] {
let before = ratios(&d);
drag_hit(&mut d, hit.clone(), delta);
let after = ratios(&d);
let moved = if hit == &outer { (after.0 - before.0) * delta.x } else { (after.1 - before.1) * delta.y };
assert!(moved > 0.0, "{hit:?} by {delta:?}: {before:?} to {after:?}");
assert_eq!(d.layout.root, editing().root, "the main area keeps its sizes");
}
// The four-way handle where the inner divider meets the outer one moves both.
let before = ratios(&d);
drag_hit(&mut d, Hit::Divider(Divider::WindowIntersection(INFO, Vec::new(), vec![true])), vec2(25.0, 25.0));
let after = ratios(&d);
assert!(after.0 > before.0 && after.1 > before.1, "{before:?} to {after:?}");
}
#[test]
fn moves_between_windows_are_one_action_and_lose_no_panel() {
let mut layout = editing();
layout.floating.push(Floating::single(vec![50, 51], 0, [200.0, 520.0, 320.0, 260.0]));
let mut d = driver(layout, Film::default());
let all = panel_set(&d.layout);
// Floating to floating: a whole group, by its drag bar, beside Info.
let info = group(&d, INFO);
let bar = d.hit(&Hit::DragBar(50)).unwrap().center();
let out = d.drag(bar, pos2(info.right() - 10.0, info.center().y + 13.0));
assert_eq!(out.actions, vec![Action::MoveGroup { panel: 50, to: Target::At { anchor: INFO, placement: Placement::Split(Zone::Right) } }]);
d.settle();
assert_eq!(d.layout.floating.len(), 1);
assert_eq!(panel_set(&d.layout), all);
// Main to floating: a tab joins the window's group.
let fifty = group(&d, 50);
let out = d.drag(tab(&d, SOURCE[1]), fifty.center() + vec2(0.0, 13.0));
assert_eq!(out.actions, vec![Action::Move { panel: SOURCE[1], anchor: 50, placement: Placement::Split(Zone::Center) }]);
d.settle();
assert!(window_of(&d.layout, 50).contains(&SOURCE[1]));
assert_eq!(panel_set(&d.layout), all);
assert_eq!(d.layout.validate(), Ok(()));
}
#[test]
fn a_closed_floating_window_closes_its_panels_and_each_reopens_where_it_was() {
let mut layout = editing();
let tree = hsplit(0.4, tabs(&[INFO]), tabs(&[50]));
layout.floating = vec![Floating::new(tree.clone(), [700.0, 100.0, 600.0, 400.0])];
let mut d = driver(layout, Film::default());
let locations: Vec<_> = [INFO, 50].into_iter().map(|p| (p, d.layout.location(&p).unwrap())).collect();
let close = d.hit(&Hit::Close(INFO)).unwrap().center();
let out = d.click(close);
assert_eq!(out.actions, vec![Action::CloseWindow { panel: INFO }]);
assert!(d.layout.floating.is_empty() && !d.layout.contains(&50));
for (panel, location) in &locations {
d.layout.restore(*panel, location).unwrap();
}
let window = &d.layout.floating[0];
assert_eq!((&window.root, window.rect), (&tree, [700.0, 100.0, 600.0, 400.0]));
}
/// The zone outlines of the last frame: closed 1 pt paths without a fill.
fn zone_outlines(d: &Driver) -> Vec<Vec<Pos2>> {
d.shapes()
.iter()
.filter_map(|clipped| match &clipped.shape {
egui::Shape::Path(path) if path.closed && path.fill == egui::Color32::TRANSPARENT && path.stroke.width == 1.0 => {
Some(path.points.clone())
}
_ => None,
})
.collect()
}
fn same_polygon(a: &[Pos2], b: &[Pos2]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.distance(*b) <= 0.5)
}
fn corners(rect: Rect) -> Vec<Pos2> {
vec![rect.left_top(), rect.right_top(), rect.right_bottom(), rect.left_bottom()]
}
#[test]
fn the_preview_fills_the_zone_under_the_pointer_and_no_result_rect() {
let d = film();
let timeline = group(&d, TIMELINE);
let strip = Rect::from_min_size(timeline.min, vec2(timeline.width(), 26.0));
// A side band: its trapezoid, without the half the panel takes.
for (zone, at) in
[(Zone::Left, pos2(timeline.left() + 8.0, timeline.center().y + 13.0)), (Zone::Bottom, pos2(timeline.center().x, timeline.bottom() - 20.0))]
{
let mut d = film();
d.hold(tab(&d, INFO), at);
assert!(d.filled_polygon(&zone_shape(timeline, 26.0, 40.0, zone).unwrap()), "{zone:?}");
assert!(!d.outlined(zone_rect(timeline, zone).unwrap().shrink(1.0)), "{zone:?}");
d.key(egui::Key::Escape);
}
// The middle and the tab row: the center rectangle and the strip.
for at in [timeline.center() + vec2(0.0, 13.0), strip.center()] {
let mut d = film();
d.hold(tab(&d, INFO), at);
assert!(d.filled_polygon(&zone_shape(timeline, 26.0, 40.0, Zone::Center).unwrap()), "{at:?}");
assert!(d.filled_polygon(&corners(strip)), "{at:?}");
assert!(!d.outlined(timeline.shrink(1.0)), "{at:?}");
d.key(egui::Key::Escape);
}
// A group's drag bar shows the same.
let mut d = film();
let bar = d.hit(&Hit::DragBar(PROJECT[0])).unwrap().center();
d.hold(bar, pos2(timeline.left() + 8.0, timeline.center().y + 13.0));
assert!(d.filled_polygon(&zone_shape(timeline, 26.0, 40.0, Zone::Left).unwrap()));
assert_eq!(zone_outlines(&d).len(), 6);
d.key(egui::Key::Escape);
// The dock-and-canvas arrangement keeps its filled half.
let mut d = DockDriver::new(editing(), Film::default());
d.screen = Rect::from_min_size(Pos2::ZERO, SCREEN);
d.area = d.screen;
d.settle();
d.hold(tab(&d, INFO), pos2(timeline.left() + 8.0, timeline.center().y + 13.0));
assert!(!d.filled_polygon(&zone_shape(timeline, 26.0, 40.0, Zone::Left).unwrap()));
assert!(zone_outlines(&d).is_empty());
assert!(d.filled(zone_rect(timeline, Zone::Left).unwrap().shrink(1.0)));
}
#[test]
fn a_drag_over_a_group_outlines_all_its_zones_and_fills_the_one_drop_zone_names() {
let d = film();
let timeline = group(&d, TIMELINE);
let strip = Rect::from_min_size(timeline.min, vec2(timeline.width(), 26.0));
let shape = |zone| zone_shape(timeline, 26.0, 40.0, zone).unwrap();
let all: Vec<Vec<Pos2>> =
[Zone::Left, Zone::Right, Zone::Top, Zone::Bottom, Zone::Center].map(shape).into_iter().chain([corners(strip)]).collect();
let body = Rect::from_min_max(pos2(timeline.left(), strip.bottom()), timeline.max);
// Each band, near its corners too, the middle, and the tab row.
let points = [
pos2(body.left() + 30.0, body.center().y),
pos2(body.left() + 30.0, body.top() + 34.0),
pos2(body.right() - 2.0, body.bottom() - 12.0),
pos2(body.center().x, body.top() + 5.0),
pos2(body.left() + 50.0, body.bottom() - 30.0),
body.center(),
pos2(body.left() + 45.0, body.top() + 45.0),
strip.center(),
];
for at in points {
let mut d = film();
d.hold(tab(&d, INFO), at);
let outlines = zone_outlines(&d);
assert_eq!(outlines.len(), all.len(), "{at:?}: four sides, the center and the strip");
assert!(all.iter().all(|zone| outlines.iter().any(|o| same_polygon(o, zone))), "{at:?}: only Timeline's zones");
let zone = drop_zone(timeline, 26.0, 40.0, at).unwrap();
assert!(d.filled_polygon(&shape(zone)), "{at:?}: {zone:?} is filled");
let filled = all.iter().filter(|z| d.filled_polygon(z)).count();
assert_eq!(filled, if zone == Zone::Center { 2 } else { 1 }, "{at:?}: only {zone:?}");
d.key(egui::Key::Escape);
}
}
#[test]
fn zones_a_group_does_not_take_are_not_drawn() {
let timeline = group(&film(), TIMELINE);
let strip = Rect::from_min_size(timeline.min, vec2(timeline.width(), 26.0));
let shape = |zone| zone_shape(timeline, 26.0, 40.0, zone).unwrap();
let at = pos2(timeline.left() + 8.0, timeline.center().y + 13.0);
let content = |rules: Permissions| Film { rules: HashMap::from([(TIMELINE, rules)]), ..Film::default() };
// No splits: the center and the strip only, and the left band under the pointer neither
// outlined nor filled.
let mut d = driver(editing(), content(Permissions { accepts_splits: false, ..Permissions::default() }));
d.hold(tab(&d, INFO), at);
let outlines = zone_outlines(&d);
assert_eq!(outlines.len(), 2, "{outlines:?}");
assert!(outlines.iter().any(|o| same_polygon(o, &shape(Zone::Center))) && outlines.iter().any(|o| same_polygon(o, &corners(strip))));
assert!(!d.filled_polygon(&shape(Zone::Left)));
d.key(egui::Key::Escape);
// No tabs: the four sides only.
let mut d = driver(editing(), content(Permissions { accepts_tabs: false, ..Permissions::default() }));
d.hold(tab(&d, INFO), at);
let outlines = zone_outlines(&d);
assert_eq!(outlines.len(), 4, "{outlines:?}");
assert!([Zone::Left, Zone::Right, Zone::Top, Zone::Bottom].into_iter().all(|z| outlines.iter().any(|o| same_polygon(o, &shape(z)))));
assert!(d.filled_polygon(&shape(Zone::Left)));
d.key(egui::Key::Escape);
// A tab over its own single-tab group: nothing to drop on.
let mut d = film();
d.hold(tab(&d, TIMELINE), timeline.center() + vec2(0.0, 13.0));
assert!(zone_outlines(&d).is_empty());
}