Merge pull request #361 from clarkmoody/pane-grid-spacing

Proper Pane Grid spacing
This commit is contained in:
Héctor Ramón 2020-05-28 03:45:22 +02:00 committed by GitHub
commit ead4186870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 181 additions and 23 deletions

View File

@ -14,37 +14,39 @@ impl Axis {
&self, &self,
rectangle: &Rectangle, rectangle: &Rectangle,
ratio: f32, ratio: f32,
halved_spacing: f32, spacing: f32,
) -> (Rectangle, Rectangle) { ) -> (Rectangle, Rectangle) {
match self { match self {
Axis::Horizontal => { Axis::Horizontal => {
let height_top = (rectangle.height * ratio).round(); let height_top =
let height_bottom = rectangle.height - height_top; (rectangle.height * ratio - spacing / 2.0).round();
let height_bottom = rectangle.height - height_top - spacing;
( (
Rectangle { Rectangle {
height: height_top - halved_spacing, height: height_top,
..*rectangle ..*rectangle
}, },
Rectangle { Rectangle {
y: rectangle.y + height_top + halved_spacing, y: rectangle.y + height_top + spacing,
height: height_bottom - halved_spacing, height: height_bottom,
..*rectangle ..*rectangle
}, },
) )
} }
Axis::Vertical => { Axis::Vertical => {
let width_left = (rectangle.width * ratio).round(); let width_left =
let width_right = rectangle.width - width_left; (rectangle.width * ratio - spacing / 2.0).round();
let width_right = rectangle.width - width_left - spacing;
( (
Rectangle { Rectangle {
width: width_left - halved_spacing, width: width_left,
..*rectangle ..*rectangle
}, },
Rectangle { Rectangle {
x: rectangle.x + width_left + halved_spacing, x: rectangle.x + width_left + spacing,
width: width_right - halved_spacing, width: width_right,
..*rectangle ..*rectangle
}, },
) )
@ -52,3 +54,161 @@ impl Axis {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
enum Case {
Horizontal {
overall_height: f32,
spacing: f32,
top_height: f32,
bottom_y: f32,
bottom_height: f32,
},
Vertical {
overall_width: f32,
spacing: f32,
left_width: f32,
right_x: f32,
right_width: f32,
},
}
#[test]
fn split() {
let cases = vec![
// Even height, even spacing
Case::Horizontal {
overall_height: 10.0,
spacing: 2.0,
top_height: 4.0,
bottom_y: 6.0,
bottom_height: 4.0,
},
// Odd height, even spacing
Case::Horizontal {
overall_height: 9.0,
spacing: 2.0,
top_height: 4.0,
bottom_y: 6.0,
bottom_height: 3.0,
},
// Even height, odd spacing
Case::Horizontal {
overall_height: 10.0,
spacing: 1.0,
top_height: 5.0,
bottom_y: 6.0,
bottom_height: 4.0,
},
// Odd height, odd spacing
Case::Horizontal {
overall_height: 9.0,
spacing: 1.0,
top_height: 4.0,
bottom_y: 5.0,
bottom_height: 4.0,
},
// Even width, even spacing
Case::Vertical {
overall_width: 10.0,
spacing: 2.0,
left_width: 4.0,
right_x: 6.0,
right_width: 4.0,
},
// Odd width, even spacing
Case::Vertical {
overall_width: 9.0,
spacing: 2.0,
left_width: 4.0,
right_x: 6.0,
right_width: 3.0,
},
// Even width, odd spacing
Case::Vertical {
overall_width: 10.0,
spacing: 1.0,
left_width: 5.0,
right_x: 6.0,
right_width: 4.0,
},
// Odd width, odd spacing
Case::Vertical {
overall_width: 9.0,
spacing: 1.0,
left_width: 4.0,
right_x: 5.0,
right_width: 4.0,
},
];
for case in cases {
match case {
Case::Horizontal {
overall_height,
spacing,
top_height,
bottom_y,
bottom_height,
} => {
let a = Axis::Horizontal;
let r = Rectangle {
x: 0.0,
y: 0.0,
width: 10.0,
height: overall_height,
};
let (top, bottom) = a.split(&r, 0.5, spacing);
assert_eq!(
top,
Rectangle {
height: top_height,
..r
}
);
assert_eq!(
bottom,
Rectangle {
y: bottom_y,
height: bottom_height,
..r
}
);
}
Case::Vertical {
overall_width,
spacing,
left_width,
right_x,
right_width,
} => {
let a = Axis::Vertical;
let r = Rectangle {
x: 0.0,
y: 0.0,
width: overall_width,
height: 10.0,
};
let (left, right) = a.split(&r, 0.5, spacing);
assert_eq!(
left,
Rectangle {
width: left_width,
..r
}
);
assert_eq!(
right,
Rectangle {
x: right_x,
width: right_width,
..r
}
);
}
}
}
}
}

View File

@ -56,7 +56,7 @@ impl Node {
let mut regions = HashMap::new(); let mut regions = HashMap::new();
self.compute_regions( self.compute_regions(
spacing / 2.0, spacing,
&Rectangle { &Rectangle {
x: 0.0, x: 0.0,
y: 0.0, y: 0.0,
@ -83,7 +83,7 @@ impl Node {
let mut splits = HashMap::new(); let mut splits = HashMap::new();
self.compute_splits( self.compute_splits(
spacing / 2.0, spacing,
&Rectangle { &Rectangle {
x: 0.0, x: 0.0,
y: 0.0, y: 0.0,
@ -185,7 +185,7 @@ impl Node {
fn compute_regions( fn compute_regions(
&self, &self,
halved_spacing: f32, spacing: f32,
current: &Rectangle, current: &Rectangle,
regions: &mut HashMap<Pane, Rectangle>, regions: &mut HashMap<Pane, Rectangle>,
) { ) {
@ -193,11 +193,10 @@ impl Node {
Node::Split { Node::Split {
axis, ratio, a, b, .. axis, ratio, a, b, ..
} => { } => {
let (region_a, region_b) = let (region_a, region_b) = axis.split(current, *ratio, spacing);
axis.split(current, *ratio, halved_spacing);
a.compute_regions(halved_spacing, &region_a, regions); a.compute_regions(spacing, &region_a, regions);
b.compute_regions(halved_spacing, &region_b, regions); b.compute_regions(spacing, &region_b, regions);
} }
Node::Pane(pane) => { Node::Pane(pane) => {
let _ = regions.insert(*pane, *current); let _ = regions.insert(*pane, *current);
@ -207,7 +206,7 @@ impl Node {
fn compute_splits( fn compute_splits(
&self, &self,
halved_spacing: f32, spacing: f32,
current: &Rectangle, current: &Rectangle,
splits: &mut HashMap<Split, (Axis, Rectangle, f32)>, splits: &mut HashMap<Split, (Axis, Rectangle, f32)>,
) { ) {
@ -219,13 +218,12 @@ impl Node {
b, b,
id, id,
} => { } => {
let (region_a, region_b) = let (region_a, region_b) = axis.split(current, *ratio, spacing);
axis.split(current, *ratio, halved_spacing);
let _ = splits.insert(*id, (*axis, *current, *ratio)); let _ = splits.insert(*id, (*axis, *current, *ratio));
a.compute_splits(halved_spacing, &region_a, splits); a.compute_splits(spacing, &region_a, splits);
b.compute_splits(halved_spacing, &region_b, splits); b.compute_splits(spacing, &region_b, splits);
} }
Node::Pane(_) => {} Node::Pane(_) => {}
} }