use super::{DockingTree, DockPanel, Branch, PanelNode, LeafId, BranchId, PanelRect, WindowLayout};
use super::presets::PANEL_GAP;
impl<P: DockPanel> DockingTree<P> {
pub fn layout_rects(&self, width: f32, height: f32) -> Vec<(LeafId, PanelRect)> {
let root_rect = PanelRect::new(0.0, 0.0, width, height);
let mut result = Vec::new();
self.layout_node(&PanelNode::Branch(self.root().clone()), root_rect, &mut result);
const MIN_SIZE: f32 = 1.0;
result.into_iter().map(|(id, rect)| {
(id, PanelRect::new(
rect.x.max(0.0),
rect.y.max(0.0),
rect.width.max(MIN_SIZE),
rect.height.max(MIN_SIZE),
))
}).collect()
}
pub fn branch_rects(&self, width: f32, height: f32) -> Vec<(BranchId, PanelRect, usize)> {
let root_rect = PanelRect::new(0.0, 0.0, width, height);
let mut result = Vec::new();
self.branch_rects_node(&PanelNode::Branch(self.root().clone()), root_rect, 0, &mut result);
result
}
fn branch_rects_node(&self, node: &PanelNode<P>, available_rect: PanelRect, depth: usize, result: &mut Vec<(BranchId, PanelRect, usize)>) {
match node {
PanelNode::Leaf(_) => {} PanelNode::Branch(branch) => {
if depth > 0 {
result.push((branch.id, available_rect, depth));
}
let child_rects = Self::compute_child_rects(branch, available_rect);
for (child, rect) in branch.children.iter().zip(child_rects.iter()) {
self.branch_rects_node(child, *rect, depth + 1, result);
}
}
}
}
fn layout_node(&self, node: &PanelNode<P>, available_rect: PanelRect, result: &mut Vec<(LeafId, PanelRect)>) {
match node {
PanelNode::Leaf(leaf) => {
if !leaf.hidden {
result.push((leaf.id, available_rect));
}
}
PanelNode::Branch(branch) => {
let child_rects = Self::compute_child_rects(branch, available_rect);
for (child, rect) in branch.children.iter().zip(child_rects.iter()) {
self.layout_node(child, *rect, result);
}
}
}
}
pub fn compute_child_rects(branch: &Branch<P>, parent_rect: PanelRect) -> Vec<PanelRect> {
let n = branch.children.len();
if n == 0 { return Vec::new(); }
if let Some(grid) = branch.grid.as_ref() {
if grid.cell_count() == n {
return grid.cell_rects(parent_rect, PANEL_GAP);
}
}
let has_hidden = branch.children.iter().any(|c| c.is_hidden());
if has_hidden {
let visible_indices: Vec<usize> = branch.children.iter().enumerate()
.filter(|(_, c)| !c.is_hidden())
.map(|(i, _)| i)
.collect();
let vis_n = visible_indices.len();
if vis_n == 0 {
return vec![PanelRect::zero(); n];
}
if vis_n == 1 {
let mut result = vec![PanelRect::zero(); n];
result[visible_indices[0]] = parent_rect;
return result;
}
let gap = PANEL_GAP;
let is_vertical = matches!(
branch.layout,
WindowLayout::SplitVertical | WindowLayout::ThreeRows
);
let weights: Vec<f32> = if branch.proportions.len() == n {
visible_indices.iter().map(|&ci| branch.proportions[ci] as f32).collect()
} else {
vec![1.0_f32; vis_n]
};
let weight_sum = weights.iter().sum::<f32>().max(f32::EPSILON);
let total_gap = (vis_n - 1) as f32 * gap;
let mut result = vec![PanelRect::zero(); n];
if is_vertical {
let available = (parent_rect.height - total_gap).max(0.0);
let mut y = parent_rect.y;
for (wi, &ci) in visible_indices.iter().enumerate() {
let h = available * weights[wi] / weight_sum;
result[ci] = PanelRect::new(parent_rect.x, y, parent_rect.width, h);
y += h + gap;
}
} else {
let available = (parent_rect.width - total_gap).max(0.0);
let mut x = parent_rect.x;
for (wi, &ci) in visible_indices.iter().enumerate() {
let w = available * weights[wi] / weight_sum;
result[ci] = PanelRect::new(x, parent_rect.y, w, parent_rect.height);
x += w + gap;
}
}
return result;
}
if !branch.custom_rects.is_empty() && branch.custom_rects.len() == n {
let mut old_min_x = f32::MAX;
let mut old_min_y = f32::MAX;
let mut old_max_x = f32::MIN;
let mut old_max_y = f32::MIN;
for r in &branch.custom_rects {
old_min_x = old_min_x.min(r.x);
old_min_y = old_min_y.min(r.y);
old_max_x = old_max_x.max(r.x + r.width);
old_max_y = old_max_y.max(r.y + r.height);
}
let old_w = (old_max_x - old_min_x).max(1.0);
let old_h = (old_max_y - old_min_y).max(1.0);
let tol = 2.0;
let same_bounds = (parent_rect.x - old_min_x).abs() < tol
&& (parent_rect.y - old_min_y).abs() < tol
&& (parent_rect.width - old_w).abs() < tol
&& (parent_rect.height - old_h).abs() < tol;
if same_bounds {
return branch.custom_rects.clone();
}
let sx = parent_rect.width / old_w;
let sy = parent_rect.height / old_h;
return branch.custom_rects.iter().map(|r| {
PanelRect::new(
parent_rect.x + (r.x - old_min_x) * sx,
parent_rect.y + (r.y - old_min_y) * sy,
r.width * sx,
r.height * sy,
)
}).collect();
}
let gap = PANEL_GAP;
if matches!(branch.layout, WindowLayout::Grid2x2) && n == 4 {
if let Some((xr, yr)) = branch.cross_ratio {
let w = parent_rect.width;
let h = parent_rect.height;
let left_w = ((w - gap) * xr as f32).max(50.0);
let right_w = ((w - gap) * (1.0 - xr as f32)).max(50.0);
let top_h = ((h - gap) * yr as f32).max(50.0);
let bottom_h = ((h - gap) * (1.0 - yr as f32)).max(50.0);
let ox = parent_rect.x;
let oy = parent_rect.y;
return vec![
PanelRect::new(ox, oy, left_w, top_h),
PanelRect::new(ox + left_w + gap, oy, right_w, top_h),
PanelRect::new(ox, oy + top_h + gap, left_w, bottom_h),
PanelRect::new(ox + left_w + gap, oy + top_h + gap, right_w, bottom_h),
];
}
}
if n == 3 {
if let Some((xr, yr)) = branch.cross_ratio {
let xr = xr as f32;
let yr = yr as f32;
let w = parent_rect.width;
let h = parent_rect.height;
let ox = parent_rect.x;
let oy = parent_rect.y;
match branch.layout {
WindowLayout::OneLeftTwoRight => {
let left_w = ((w - gap) * xr).max(50.0);
let right_w = ((w - gap) * (1.0 - xr)).max(50.0);
let top_h = ((h - gap) * yr).max(50.0);
let bot_h = ((h - gap) * (1.0 - yr)).max(50.0);
return vec![
PanelRect::new(ox, oy, left_w, h),
PanelRect::new(ox + left_w + gap, oy, right_w, top_h),
PanelRect::new(ox + left_w + gap, oy + top_h + gap, right_w, bot_h),
];
}
WindowLayout::TwoLeftOneRight => {
let left_w = ((w - gap) * xr).max(50.0);
let right_w = ((w - gap) * (1.0 - xr)).max(50.0);
let top_h = ((h - gap) * yr).max(50.0);
let bot_h = ((h - gap) * (1.0 - yr)).max(50.0);
return vec![
PanelRect::new(ox, oy, left_w, top_h),
PanelRect::new(ox, oy + top_h + gap, left_w, bot_h),
PanelRect::new(ox + left_w + gap, oy, right_w, h),
];
}
WindowLayout::OneTopTwoBottom => {
let top_h = ((h - gap) * yr).max(50.0);
let bot_h = ((h - gap) * (1.0 - yr)).max(50.0);
let left_w = ((w - gap) * xr).max(50.0);
let right_w = ((w - gap) * (1.0 - xr)).max(50.0);
return vec![
PanelRect::new(ox, oy, w, top_h),
PanelRect::new(ox, oy + top_h + gap, left_w, bot_h),
PanelRect::new(ox + left_w + gap, oy + top_h + gap, right_w, bot_h),
];
}
WindowLayout::TwoTopOneBottom => {
let top_h = ((h - gap) * yr).max(50.0);
let bot_h = ((h - gap) * (1.0 - yr)).max(50.0);
let left_w = ((w - gap) * xr).max(50.0);
let right_w = ((w - gap) * (1.0 - xr)).max(50.0);
return vec![
PanelRect::new(ox, oy, left_w, top_h),
PanelRect::new(ox + left_w + gap, oy, right_w, top_h),
PanelRect::new(ox, oy + top_h + gap, w, bot_h),
];
}
_ => {}
}
}
}
let is_single_axis_layout = matches!(
branch.layout,
WindowLayout::SplitHorizontal
| WindowLayout::SplitVertical
| WindowLayout::ThreeColumns
| WindowLayout::ThreeRows
);
if is_single_axis_layout && branch.proportions.len() == n {
let is_vertical = matches!(branch.layout, WindowLayout::SplitVertical | WindowLayout::ThreeRows);
let total_gap = if n > 1 { (n - 1) as f32 * gap } else { 0.0 };
let sum: f64 = branch.proportions.iter().sum();
let inv_sum: f32 = if sum > f64::EPSILON { (1.0 / sum) as f32 } else { 1.0 / n as f32 };
if is_vertical {
let available = (parent_rect.height - total_gap).max(0.0);
let mut result = Vec::with_capacity(n);
let mut y = parent_rect.y;
for i in 0..n {
let h = available * (branch.proportions[i] as f32 * inv_sum);
result.push(PanelRect::new(parent_rect.x, y, parent_rect.width, h));
y += h + gap;
}
return result;
} else {
let available = (parent_rect.width - total_gap).max(0.0);
let mut result = Vec::with_capacity(n);
let mut x = parent_rect.x;
for i in 0..n {
let w = available * (branch.proportions[i] as f32 * inv_sum);
result.push(PanelRect::new(x, parent_rect.y, w, parent_rect.height));
x += w + gap;
}
return result;
}
}
let local_rects = branch.layout.calculate_rects(parent_rect.width, parent_rect.height, n);
local_rects.into_iter()
.map(|mut r| {
r.x += parent_rect.x;
r.y += parent_rect.y;
r
})
.collect()
}
pub fn rect_for_leaf(&self, leaf_id: LeafId, width: f32, height: f32) -> Option<PanelRect> {
let root_rect = PanelRect::new(0.0, 0.0, width, height);
Self::rect_for_leaf_recursive(self.root(), leaf_id, root_rect)
}
fn rect_for_leaf_recursive(branch: &Branch<P>, target_id: LeafId, parent_rect: PanelRect) -> Option<PanelRect> {
let child_rects = Self::compute_child_rects(branch, parent_rect);
for (child, rect) in branch.children.iter().zip(child_rects.iter()) {
if child.leaf_id() == Some(target_id) {
return Some(*rect);
}
if let PanelNode::Branch(b) = child {
if let Some(found) = Self::rect_for_leaf_recursive(b, target_id, *rect) {
return Some(found);
}
}
}
None
}
pub fn rect_for_branch(&self, branch_id: BranchId, width: f32, height: f32) -> Option<PanelRect> {
let root_rect = PanelRect::new(0.0, 0.0, width, height);
if self.root().id == branch_id {
return Some(root_rect);
}
Self::rect_for_branch_recursive(self.root(), branch_id, root_rect)
}
fn rect_for_branch_recursive(branch: &Branch<P>, target_id: BranchId, parent_rect: PanelRect) -> Option<PanelRect> {
let child_rects = Self::compute_child_rects(branch, parent_rect);
for (child, rect) in branch.children.iter().zip(child_rects.iter()) {
if let PanelNode::Branch(b) = child {
if b.id == target_id {
return Some(*rect);
}
if let Some(found) = Self::rect_for_branch_recursive(b, target_id, *rect) {
return Some(found);
}
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::docking::{
DockPanel, DockingTree, Branch, PanelNode, Leaf, BranchId, LeafId,
WindowLayout, SplitKind,
};
#[derive(Clone)]
struct P;
impl DockPanel for P {
fn title(&self) -> &str { "p" }
fn type_id(&self) -> &'static str { "p" }
fn min_size(&self) -> (f32, f32) { (0.0, 0.0) }
}
fn grid2x2_branch_with_proportions() -> Branch<P> {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2), make_leaf(3), make_leaf(4)],
layout: WindowLayout::Grid2x2,
custom_rects: Vec::new(),
proportions: vec![0.3, 0.3, 0.2, 0.2],
cross_ratio: None,
preserve_if_empty: false,
magnetic: true,
grid: None,
}
}
#[test]
fn grid2x2_with_proportions_does_not_flatten() {
let branch = grid2x2_branch_with_proportions();
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
assert_eq!(rects.len(), 4, "must have 4 children");
let tl = rects[0];
let tr = rects[1];
let bl = rects[2];
let br = rects[3];
assert!(tl.x < 1.0, "TL must start at left edge, got x={}", tl.x);
assert!(bl.x < 1.0, "BL must start at left edge, got x={}", bl.x);
assert!(tr.x > 1.0, "TR must be in right column, got x={}", tr.x);
assert!(br.x > 1.0, "BR must be in right column, got x={}", br.x);
assert!(tl.y < 1.0, "TL must start at top, got y={}", tl.y);
assert!(tr.y < 1.0, "TR must start at top, got y={}", tr.y);
assert!(bl.y > 1.0, "BL must be in bottom row, got y={}", bl.y);
assert!(br.y > 1.0, "BR must be in bottom row, got y={}", br.y);
}
#[test]
fn grid2x2_cross_ratio_quadrant_sizes() {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
let branch = Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2), make_leaf(3), make_leaf(4)],
layout: WindowLayout::Grid2x2,
custom_rects: Vec::new(),
proportions: Vec::new(),
cross_ratio: Some((0.4, 0.3)),
preserve_if_empty: false,
magnetic: true,
grid: None,
};
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
let tl = rects[0];
let tr = rects[1];
let bl = rects[2];
assert!((tl.width - 400.0).abs() < 1.0,
"TL width should be ~400, got {}", tl.width);
assert!((tr.width - 600.0).abs() < 1.0,
"TR width should be ~600, got {}", tr.width);
assert!((tl.height - 240.0).abs() < 1.0,
"TL height should be ~240, got {}", tl.height);
assert!((bl.height - 560.0).abs() < 1.0,
"BL height should be ~560, got {}", bl.height);
}
#[test]
fn split_horizontal_respects_proportions() {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
let branch = Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2)],
layout: WindowLayout::SplitHorizontal,
custom_rects: Vec::new(),
proportions: vec![0.75, 0.25],
cross_ratio: None,
preserve_if_empty: false,
magnetic: true,
grid: None,
};
let parent = PanelRect::new(0.0, 0.0, 1000.0, 600.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
assert_eq!(rects.len(), 2);
assert!((rects[0].width - 750.0).abs() < 2.0,
"first child width should be ~750, got {}", rects[0].width);
assert!((rects[1].width - 250.0).abs() < 2.0,
"second child width should be ~250, got {}", rects[1].width);
}
#[test]
fn three_rows_respects_proportions() {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
let branch = Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2), make_leaf(3)],
layout: WindowLayout::ThreeRows,
custom_rects: Vec::new(),
proportions: vec![0.5, 0.25, 0.25],
cross_ratio: None,
preserve_if_empty: false,
magnetic: true,
grid: None,
};
let parent = PanelRect::new(0.0, 0.0, 800.0, 600.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
assert_eq!(rects.len(), 3);
assert!((rects[0].height - 300.0).abs() < 2.0,
"row 0 height should be ~300, got {}", rects[0].height);
assert!((rects[1].height - 150.0).abs() < 2.0,
"row 1 height should be ~150, got {}", rects[1].height);
assert!((rects[2].height - 150.0).abs() < 2.0,
"row 2 height should be ~150, got {}", rects[2].height);
}
fn find_grid2x2_branch(tree: &DockingTree<P>) -> &Branch<P> {
match &tree.root().children[0] {
PanelNode::Branch(b) => b,
PanelNode::Leaf(_) => panic!("expected Grid2x2 branch as root child"),
}
}
#[test]
fn grid2x2_set_cross_ratio_changes_quadrant_widths() {
let mut tree: DockingTree<P> = DockingTree::new();
let first_id = tree.add_leaf(P);
let leaf_ids = tree.split_leaf_with_children(first_id, SplitKind::Grid2x2, 1000.0, 800.0);
assert_eq!(leaf_ids.len(), 4);
let grid_branch_id = find_grid2x2_branch(&tree).id;
tree.set_branch_cross_ratio(grid_branch_id, 0.6, 0.5);
let grid_branch = find_grid2x2_branch(&tree);
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(grid_branch, parent);
assert_eq!(rects.len(), 4);
let tl = rects[0];
let tr = rects[1];
assert!((tl.width - 600.0).abs() < 1.0,
"TL width after cross_ratio should be ~600, got {}", tl.width);
assert!((tr.width - 400.0).abs() < 1.0,
"TR width after cross_ratio should be ~400, got {}", tr.width);
assert!(find_grid2x2_branch(&tree).proportions.is_empty(),
"proportions must stay empty on Grid2x2 cross_ratio drag");
}
#[test]
fn grid2x2_cross_ratio_y_changes_row_heights() {
let mut tree: DockingTree<P> = DockingTree::new();
let first_id = tree.add_leaf(P);
let _ = tree.split_leaf_with_children(first_id, SplitKind::Grid2x2, 1000.0, 800.0);
let grid_branch_id = find_grid2x2_branch(&tree).id;
tree.set_branch_cross_ratio(grid_branch_id, 0.5, 0.3);
let grid_branch = find_grid2x2_branch(&tree);
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(grid_branch, parent);
assert_eq!(rects.len(), 4);
let tl = rects[0];
let bl = rects[2];
assert!((tl.height - 240.0).abs() < 1.0,
"TL height should be ~240, got {}", tl.height);
assert!((bl.height - 560.0).abs() < 1.0,
"BL height should be ~560, got {}", bl.height);
}
#[test]
fn one_left_two_right_cross_ratio_rect_math() {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
let branch = Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2), make_leaf(3)],
layout: WindowLayout::OneLeftTwoRight,
custom_rects: Vec::new(),
proportions: Vec::new(),
cross_ratio: Some((0.7, 0.4)),
preserve_if_empty: false,
magnetic: true,
grid: None,
};
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
assert_eq!(rects.len(), 3, "must have 3 children");
let left = rects[0];
let rt = rects[1]; let rb = rects[2];
assert!(left.x < 1.0, "left starts at x=0, got {}", left.x);
assert!(left.y < 1.0, "left starts at y=0, got {}", left.y);
assert!((left.height - 800.0).abs() < 1.0,
"left height must equal parent height, got {}", left.height);
let gap = PANEL_GAP;
let exp_left_w = (1000.0 - gap) * 0.7;
assert!((left.width - exp_left_w).abs() < 1.0,
"left width should be ~{exp_left_w}, got {}", left.width);
let exp_right_x = left.width + gap;
assert!((rt.x - exp_right_x).abs() < 1.0,
"right-top x should be ~{exp_right_x}, got {}", rt.x);
assert!((rb.x - exp_right_x).abs() < 1.0,
"right-bot x should be ~{exp_right_x}, got {}", rb.x);
let exp_top_h = (800.0 - gap) * 0.4;
let exp_bot_h = (800.0 - gap) * 0.6;
assert!((rt.height - exp_top_h).abs() < 1.0,
"right-top height should be ~{exp_top_h}, got {}", rt.height);
assert!((rb.height - exp_bot_h).abs() < 1.0,
"right-bot height should be ~{exp_bot_h}, got {}", rb.height);
assert!(branch.proportions.is_empty(),
"proportions must remain empty when cross_ratio is set");
}
#[test]
fn two_top_one_bottom_cross_ratio_rect_math() {
let make_leaf = |id: u64| PanelNode::Leaf(Leaf::new(LeafId(id), P));
let branch = Branch {
id: BranchId(0),
children: vec![make_leaf(1), make_leaf(2), make_leaf(3)],
layout: WindowLayout::TwoTopOneBottom,
custom_rects: Vec::new(),
proportions: Vec::new(),
cross_ratio: Some((0.6, 0.3)),
preserve_if_empty: false,
magnetic: true,
grid: None,
};
let parent = PanelRect::new(0.0, 0.0, 1000.0, 800.0);
let rects = DockingTree::<P>::compute_child_rects(&branch, parent);
assert_eq!(rects.len(), 3, "must have 3 children");
let tl = rects[0]; let tr = rects[1]; let bot = rects[2];
let gap = PANEL_GAP;
let exp_top_h = (800.0 - gap) * 0.3;
assert!((tl.height - exp_top_h).abs() < 1.0,
"top-left height should be ~{exp_top_h}, got {}", tl.height);
assert!((tr.height - exp_top_h).abs() < 1.0,
"top-right height should be ~{exp_top_h}, got {}", tr.height);
let exp_bot_y = exp_top_h + gap;
assert!((bot.y - exp_bot_y).abs() < 1.0,
"bottom y should be ~{exp_bot_y}, got {}", bot.y);
assert!((bot.width - 1000.0).abs() < 1.0,
"bottom width must equal parent width, got {}", bot.width);
let exp_tl_w = (1000.0 - gap) * 0.6;
assert!((tl.width - exp_tl_w).abs() < 1.0,
"top-left width should be ~{exp_tl_w}, got {}", tl.width);
let exp_tr_x = tl.width + gap;
assert!((tr.x - exp_tr_x).abs() < 1.0,
"top-right x should be ~{exp_tr_x}, got {}", tr.x);
}
}
#[cfg(test)]
mod grid_tests {
use crate::layout::docking::{
DockPanel, DockingTree, GridAxis, GridSpec, LeafId, PanelRect, Separator,
SeparatorOrientation, WindowLayout,
};
#[derive(Clone)]
struct P;
impl DockPanel for P {
fn title(&self) -> &str { "p" }
fn type_id(&self) -> &'static str { "p" }
fn min_size(&self) -> (f32, f32) { (0.0, 0.0) }
}
fn close(a: PanelRect, b: PanelRect) -> bool {
(a.x - b.x).abs() < 0.01
&& (a.y - b.y).abs() < 0.01
&& (a.width - b.width).abs() < 0.01
&& (a.height - b.height).abs() < 0.01
}
fn grid_tree(rows: usize, cols: usize, row_r: Vec<f64>, col_r: Vec<f64>) -> (DockingTree<P>, Vec<LeafId>) {
let mut tree = DockingTree::with_grid(rows, cols, vec![P; rows * cols]).unwrap();
let root = tree.root().id;
assert!(tree.set_grid_row_ratios(root, row_r));
assert!(tree.set_grid_col_ratios(root, col_r));
let ids = tree.root().children.iter().map(|c| c.leaf_id().unwrap()).collect();
(tree, ids)
}
fn rects_in_order(tree: &DockingTree<P>, ids: &[LeafId], w: f32, h: f32) -> Vec<PanelRect> {
ids.iter().map(|id| tree.rect_for_leaf(*id, w, h).unwrap()).collect()
}
#[test]
fn rxc_2x3_uneven_ratios_lays_out_row_major() {
let (tree, ids) = grid_tree(2, 3, vec![1.0, 3.0], vec![1.0, 2.0, 1.0]);
assert_eq!(tree.layout(), WindowLayout::Custom);
let got = rects_in_order(&tree, &ids, 600.0, 400.0);
let want = [
PanelRect::new(0.0, 0.0, 150.0, 100.0),
PanelRect::new(150.0, 0.0, 300.0, 100.0),
PanelRect::new(450.0, 0.0, 150.0, 100.0),
PanelRect::new(0.0, 100.0, 150.0, 300.0),
PanelRect::new(150.0, 100.0, 300.0, 300.0),
PanelRect::new(450.0, 100.0, 150.0, 300.0),
];
assert_eq!(got.len(), 6);
for (g, w) in got.iter().zip(want.iter()) {
assert!(close(*g, *w), "got {g:?}, want {w:?}");
}
}
#[test]
fn rxc_3x2_uneven_ratios_lays_out_row_major() {
let (tree, ids) = grid_tree(3, 2, vec![0.5, 0.25, 0.25], vec![0.3, 0.7]);
let got = rects_in_order(&tree, &ids, 600.0, 400.0);
let want = [
PanelRect::new(0.0, 0.0, 180.0, 200.0),
PanelRect::new(180.0, 0.0, 420.0, 200.0),
PanelRect::new(0.0, 200.0, 180.0, 100.0),
PanelRect::new(180.0, 200.0, 420.0, 100.0),
PanelRect::new(0.0, 300.0, 180.0, 100.0),
PanelRect::new(180.0, 300.0, 420.0, 100.0),
];
assert_eq!(got.len(), 6);
for (g, w) in got.iter().zip(want.iter()) {
assert!(close(*g, *w), "got {g:?}, want {w:?}");
}
}
#[test]
fn rxc_separators_one_per_interior_boundary_spanning_the_grid() {
let (tree, _) = grid_tree(3, 2, vec![0.5, 0.25, 0.25], vec![0.3, 0.7]);
let mut seps: Vec<Separator> = Vec::new();
crate::layout::docking::lib::generate_separators(
tree.root(), PanelRect::new(0.0, 0.0, 600.0, 400.0), &mut seps,
);
assert_eq!(seps.len(), 3);
let rows: Vec<&Separator> = seps.iter()
.filter(|s| s.grid_line.map(|l| l.axis) == Some(GridAxis::Row)).collect();
let cols: Vec<&Separator> = seps.iter()
.filter(|s| s.grid_line.map(|l| l.axis) == Some(GridAxis::Col)).collect();
assert_eq!(rows.len(), 2);
assert_eq!(cols.len(), 1);
for s in &rows {
assert_eq!(s.orientation, SeparatorOrientation::Horizontal);
assert_eq!((s.start, s.length), (0.0, 600.0), "row line spans the width");
}
assert!((rows[0].position - 200.0).abs() < 0.01);
assert!((rows[1].position - 300.0).abs() < 0.01);
assert_eq!(cols[0].orientation, SeparatorOrientation::Vertical);
assert_eq!((cols[0].start, cols[0].length), (0.0, 400.0), "column line spans the height");
assert!((cols[0].position - 180.0).abs() < 0.01);
}
#[test]
fn grid_needs_matching_child_count_and_is_dropped_when_it_changes() {
let mut tree = DockingTree::with_single_leaf(P);
tree.add_leaf(P);
tree.add_leaf(P);
let root = tree.root().id;
assert!(!tree.set_branch_grid(root, 2, 2), "3 children cannot form 2×2");
assert!(tree.set_branch_grid(root, 1, 3));
assert_eq!(tree.branch_grid(root), GridSpec::new(1, 3).as_ref());
let last = tree.root().children[2].leaf_id().unwrap();
tree.remove_leaf(last);
assert!(tree.branch_grid(root).is_none(), "removing a cell drops the grid");
assert_eq!(tree.layout(), WindowLayout::SplitHorizontal);
}
#[test]
fn wrap_leaf_in_grid_nests_a_grid_in_place() {
let mut tree = DockingTree::with_single_leaf(P);
let first = tree.root().children[0].leaf_id().unwrap();
let second = tree.add_leaf(P);
let (grid_id, cells) = tree.wrap_leaf_in_grid(second, 2, 2, vec![P, P, P]).unwrap();
assert_eq!(cells.len(), 4);
assert_eq!(cells[0], second);
assert_eq!(tree.branch_grid(grid_id).map(|g| (g.rows, g.cols)), Some((2, 2)));
let r = tree.rect_for_leaf(cells[3], 800.0, 400.0).unwrap();
assert!(close(r, PanelRect::new(600.0, 200.0, 200.0, 200.0)), "{r:?}");
assert!(tree.rect_for_leaf(first, 800.0, 400.0).is_some());
assert!(tree.wrap_leaf_in_grid(first, 2, 2, vec![P]).is_none(), "wrong panel count");
}
}