Implement panes::State::close
This commit is contained in:
parent
a6531c840b
commit
15fad17f37
2 changed files with 54 additions and 3 deletions
|
|
@ -199,6 +199,10 @@ impl<T> State<T> {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.panes.len()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, pane: &Pane) -> Option<&mut T> {
|
||||
self.panes.get_mut(pane)
|
||||
}
|
||||
|
|
@ -252,6 +256,15 @@ impl<T> State<T> {
|
|||
|
||||
Some(new_pane)
|
||||
}
|
||||
|
||||
pub fn close(&mut self, pane: &Pane) -> Option<T> {
|
||||
if let Some(sibling) = self.internal.layout.remove(pane) {
|
||||
self.internal.focused_pane = Some(sibling);
|
||||
self.panes.remove(pane)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
|
|
@ -266,7 +279,7 @@ enum Node {
|
|||
}
|
||||
|
||||
impl Node {
|
||||
pub fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
|
||||
fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
if let Some(node) = a.find(pane) {
|
||||
|
|
@ -285,7 +298,7 @@ impl Node {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn split(&mut self, kind: Split, new_pane: Pane) {
|
||||
fn split(&mut self, kind: Split, new_pane: Pane) {
|
||||
*self = Node::Split {
|
||||
kind,
|
||||
ratio: 500_000,
|
||||
|
|
@ -294,6 +307,23 @@ impl Node {
|
|||
};
|
||||
}
|
||||
|
||||
fn remove(&mut self, pane: &Pane) -> Option<Pane> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn regions(&self, size: Size) -> HashMap<Pane, Rectangle> {
|
||||
let mut regions = HashMap::new();
|
||||
|
||||
|
|
@ -310,6 +340,20 @@ impl Node {
|
|||
regions
|
||||
}
|
||||
|
||||
fn pane(&self) -> Option<Pane> {
|
||||
match self {
|
||||
Node::Split { .. } => None,
|
||||
Node::Pane(pane) => Some(*pane),
|
||||
}
|
||||
}
|
||||
|
||||
fn first_pane(&self) -> Pane {
|
||||
match self {
|
||||
Node::Split { a, .. } => a.first_pane(),
|
||||
Node::Pane(pane) => *pane,
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_regions(
|
||||
&self,
|
||||
current: &Rectangle,
|
||||
|
|
@ -330,7 +374,7 @@ impl Node {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash)]
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||
pub enum Split {
|
||||
Horizontal,
|
||||
Vertical,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue