2020-03-14 05:26:59 +01:00
|
|
|
use crate::{
|
2020-03-14 06:35:43 +01:00
|
|
|
pane_grid::{Axis, Pane, Split},
|
2020-03-14 05:26:59 +01:00
|
|
|
Rectangle, Size,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2020-05-22 22:15:44 +02:00
|
|
|
/// A layout node of a [`PaneGrid`].
|
|
|
|
|
///
|
|
|
|
|
/// [`PaneGrid`]: struct.PaneGrid.html
|
2020-05-09 00:16:07 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2020-03-14 05:26:59 +01:00
|
|
|
pub enum Node {
|
2020-05-22 22:15:44 +02:00
|
|
|
/// The region of this [`Node`] is split into two.
|
|
|
|
|
///
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-03-14 05:26:59 +01:00
|
|
|
Split {
|
2020-05-22 22:15:44 +02:00
|
|
|
/// The [`Split`] of this [`Node`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Split`]: struct.Split.html
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-03-14 06:35:43 +01:00
|
|
|
id: Split,
|
2020-05-22 22:15:44 +02:00
|
|
|
|
|
|
|
|
/// The direction of the split.
|
2020-03-14 06:32:56 +01:00
|
|
|
axis: Axis,
|
2020-05-22 22:15:44 +02:00
|
|
|
|
|
|
|
|
/// The ratio of the split in [0.0, 1.0].
|
2020-05-09 00:16:07 +02:00
|
|
|
ratio: f32,
|
2020-05-22 22:15:44 +02:00
|
|
|
|
|
|
|
|
/// The left/top [`Node`] of the split.
|
|
|
|
|
///
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-03-14 05:26:59 +01:00
|
|
|
a: Box<Node>,
|
2020-05-22 22:15:44 +02:00
|
|
|
|
|
|
|
|
/// The right/bottom [`Node`] of the split.
|
|
|
|
|
///
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-03-14 05:26:59 +01:00
|
|
|
b: Box<Node>,
|
|
|
|
|
},
|
2020-05-22 22:15:44 +02:00
|
|
|
/// The region of this [`Node`] is taken by a [`Pane`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Pane`]: struct.Pane.html
|
2020-03-14 05:26:59 +01:00
|
|
|
Pane(Pane),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Node {
|
2020-05-22 22:15:44 +02:00
|
|
|
/// Returns the rectangular region for each [`Pane`] in the [`Node`] given
|
|
|
|
|
/// the spacing between panes and the total available space.
|
|
|
|
|
///
|
|
|
|
|
/// [`Pane`]: struct.Pane.html
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-05-09 00:16:07 +02:00
|
|
|
pub fn regions(
|
|
|
|
|
&self,
|
|
|
|
|
spacing: f32,
|
|
|
|
|
size: Size,
|
|
|
|
|
) -> HashMap<Pane, Rectangle> {
|
|
|
|
|
let mut regions = HashMap::new();
|
|
|
|
|
|
|
|
|
|
self.compute_regions(
|
|
|
|
|
spacing / 2.0,
|
|
|
|
|
&Rectangle {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
|
|
|
|
},
|
|
|
|
|
&mut regions,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
regions
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 22:15:44 +02:00
|
|
|
/// Returns the axis, rectangular region, and ratio for each [`Split`] in
|
|
|
|
|
/// the [`Node`] given the spacing between panes and the total available
|
|
|
|
|
/// space.
|
|
|
|
|
///
|
|
|
|
|
/// [`Split`]: struct.Split.html
|
|
|
|
|
/// [`Node`]: enum.Node.html
|
2020-05-09 00:16:07 +02:00
|
|
|
pub fn splits(
|
|
|
|
|
&self,
|
|
|
|
|
spacing: f32,
|
|
|
|
|
size: Size,
|
|
|
|
|
) -> HashMap<Split, (Axis, Rectangle, f32)> {
|
|
|
|
|
let mut splits = HashMap::new();
|
|
|
|
|
|
|
|
|
|
self.compute_splits(
|
|
|
|
|
spacing / 2.0,
|
|
|
|
|
&Rectangle {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
|
|
|
|
},
|
|
|
|
|
&mut splits,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
splits
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
|
2020-03-14 05:26:59 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split { a, b, .. } => {
|
|
|
|
|
a.find(pane).or_else(move || b.find(pane))
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(p) => {
|
|
|
|
|
if p == pane {
|
|
|
|
|
Some(self)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
pub(crate) fn split(&mut self, id: Split, axis: Axis, new_pane: Pane) {
|
2020-03-14 05:26:59 +01:00
|
|
|
*self = Node::Split {
|
2020-03-14 06:26:09 +01:00
|
|
|
id,
|
2020-03-14 06:32:56 +01:00
|
|
|
axis,
|
2020-05-09 00:16:07 +02:00
|
|
|
ratio: 0.5,
|
2020-03-14 05:26:59 +01:00
|
|
|
a: Box::new(self.clone()),
|
|
|
|
|
b: Box::new(Node::Pane(new_pane)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
pub(crate) fn update(&mut self, f: &impl Fn(&mut Node)) {
|
2020-03-14 05:26:59 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split { a, b, .. } => {
|
|
|
|
|
a.update(f);
|
|
|
|
|
b.update(f);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f(self);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
pub(crate) fn resize(&mut self, split: &Split, percentage: f32) -> bool {
|
2020-03-14 08:10:50 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split {
|
|
|
|
|
id, ratio, a, b, ..
|
|
|
|
|
} => {
|
|
|
|
|
if id == split {
|
2020-05-09 00:16:07 +02:00
|
|
|
*ratio = percentage;
|
2020-03-14 08:10:50 +01:00
|
|
|
|
|
|
|
|
true
|
|
|
|
|
} else if a.resize(split, percentage) {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
b.resize(split, percentage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(_) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
pub(crate) fn remove(&mut self, pane: &Pane) -> Option<Pane> {
|
2020-03-14 05:26:59 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split { a, b, .. } => {
|
|
|
|
|
if a.pane() == Some(*pane) {
|
|
|
|
|
*self = *b.clone();
|
|
|
|
|
Some(self.first_pane())
|
|
|
|
|
} else if b.pane() == Some(*pane) {
|
|
|
|
|
*self = *a.clone();
|
|
|
|
|
Some(self.first_pane())
|
|
|
|
|
} else {
|
|
|
|
|
a.remove(pane).or_else(|| b.remove(pane))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
fn pane(&self) -> Option<Pane> {
|
2020-03-14 05:26:59 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split { .. } => None,
|
|
|
|
|
Node::Pane(pane) => Some(*pane),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
fn first_pane(&self) -> Pane {
|
2020-03-14 05:26:59 +01:00
|
|
|
match self {
|
|
|
|
|
Node::Split { a, .. } => a.first_pane(),
|
|
|
|
|
Node::Pane(pane) => *pane,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compute_regions(
|
|
|
|
|
&self,
|
|
|
|
|
halved_spacing: f32,
|
|
|
|
|
current: &Rectangle,
|
|
|
|
|
regions: &mut HashMap<Pane, Rectangle>,
|
|
|
|
|
) {
|
|
|
|
|
match self {
|
2020-03-14 06:26:09 +01:00
|
|
|
Node::Split {
|
2020-03-14 06:32:56 +01:00
|
|
|
axis, ratio, a, b, ..
|
2020-03-14 06:26:09 +01:00
|
|
|
} => {
|
2020-03-14 05:26:59 +01:00
|
|
|
let (region_a, region_b) =
|
2020-05-09 00:16:07 +02:00
|
|
|
axis.split(current, *ratio, halved_spacing);
|
2020-03-14 05:26:59 +01:00
|
|
|
|
|
|
|
|
a.compute_regions(halved_spacing, ®ion_a, regions);
|
|
|
|
|
b.compute_regions(halved_spacing, ®ion_b, regions);
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(pane) => {
|
|
|
|
|
let _ = regions.insert(*pane, *current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-14 08:10:50 +01:00
|
|
|
|
|
|
|
|
fn compute_splits(
|
|
|
|
|
&self,
|
|
|
|
|
halved_spacing: f32,
|
|
|
|
|
current: &Rectangle,
|
|
|
|
|
splits: &mut HashMap<Split, (Axis, Rectangle, f32)>,
|
|
|
|
|
) {
|
|
|
|
|
match self {
|
|
|
|
|
Node::Split {
|
|
|
|
|
axis,
|
|
|
|
|
ratio,
|
|
|
|
|
a,
|
|
|
|
|
b,
|
|
|
|
|
id,
|
|
|
|
|
} => {
|
|
|
|
|
let (region_a, region_b) =
|
2020-05-09 00:16:07 +02:00
|
|
|
axis.split(current, *ratio, halved_spacing);
|
2020-03-14 08:10:50 +01:00
|
|
|
|
2020-05-09 00:16:07 +02:00
|
|
|
let _ = splits.insert(*id, (*axis, *current, *ratio));
|
2020-03-14 08:10:50 +01:00
|
|
|
|
|
|
|
|
a.compute_splits(halved_spacing, ®ion_a, splits);
|
|
|
|
|
b.compute_splits(halved_spacing, ®ion_b, splits);
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(_) => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-14 05:26:59 +01:00
|
|
|
}
|
2020-05-09 00:16:07 +02:00
|
|
|
|
|
|
|
|
impl std::hash::Hash for Node {
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
match self {
|
|
|
|
|
Node::Split {
|
|
|
|
|
id,
|
|
|
|
|
axis,
|
|
|
|
|
ratio,
|
|
|
|
|
a,
|
|
|
|
|
b,
|
|
|
|
|
} => {
|
|
|
|
|
id.hash(state);
|
|
|
|
|
axis.hash(state);
|
|
|
|
|
((ratio * 100_000.0) as u32).hash(state);
|
|
|
|
|
a.hash(state);
|
|
|
|
|
b.hash(state);
|
|
|
|
|
}
|
|
|
|
|
Node::Pane(pane) => {
|
|
|
|
|
pane.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|