pub struct Tree<Tab> { /* private fields */ }Expand description
Binary tree representing the relationships between Nodes.
§Implementation details
The binary tree is stored in a Vec indexed by NodeIndex.
The root is always at index 0.
For a given node n:
- left child of n will be at index n * 2 + 1.
- right child of n will be at index n * 2 + 2.
For “Horizontal” nodes:
- left child contains Left node.
- right child contains Right node.
For “Vertical” nodes:
- left child contains Top node.
- right child contains Bottom node.
Implementations§
Source§impl<Tab> Tree<Tab>
impl<Tab> Tree<Tab>
Sourcepub fn find_active(&mut self) -> Option<(Rect, &mut Tab)>
pub fn find_active(&mut self) -> Option<(Rect, &mut Tab)>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the number of nodes in the tree is 0, otherwise false.
Sourcepub fn num_tabs(&self) -> usize
pub fn num_tabs(&self) -> usize
Counts and returns the number of tabs in the whole tree.
§Examples
let mut dock_state = DockState::new(vec!["node 1", "node 2", "node 3"]);
assert_eq!(dock_state.main_surface().num_tabs(), 3);
let [a, b] = dock_state.main_surface_mut().split_left(NodeIndex::root(), 0.5, vec!["tab 4", "tab 5"]);
assert_eq!(dock_state.main_surface().num_tabs(), 5);
dock_state.main_surface_mut().remove_leaf(a);
assert_eq!(dock_state.main_surface().num_tabs(), 2);Sourcepub fn root_node_mut(&mut self) -> Option<&mut Node<Tab>>
pub fn root_node_mut(&mut self) -> Option<&mut Node<Tab>>
Acquire a mutable borrow to the Node at the root of the tree.
Returns None if the tree is empty.
§Examples
let mut dock_state = DockState::new(vec!["single tab"]);
let root_node = dock_state.main_surface_mut().root_node_mut().unwrap();
let root_as_leaf = root_node.get_leaf_mut().unwrap();
root_as_leaf.tabs.push("partner tab");
assert_eq!(root_node.tabs(), Some(["single tab", "partner tab"].as_slice()));Sourcepub fn split_tabs(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2]
pub fn split_tabs( &mut self, parent: NodeIndex, split: Split, fraction: f32, tabs: Vec<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) gets the tabs.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed relatively to the old node, in the direction specified by split.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
§Example
let mut dock_state = DockState::new(vec!["tab 1", "tab 2"]);
// At this point, the main surface only contains the leaf with tab 1 and 2.
assert!(dock_state.main_surface().root_node().unwrap().is_leaf());
// Split the node, giving 50% of the space to the new nodes and 50% to the old ones.
let [old, new] = dock_state.main_surface_mut()
.split_tabs(NodeIndex::root(), Split::Below, 0.5, vec!["tab 3"]);
assert!(dock_state.main_surface().root_node().unwrap().is_parent());
assert!(dock_state[SurfaceIndex::main()][old].is_leaf());
assert!(dock_state[SurfaceIndex::main()][new].is_leaf());Sourcepub fn split_above(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2]
pub fn split_above( &mut self, parent: NodeIndex, fraction: f32, tabs: Vec<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) gets the tabs.
This is a shorthand for using split_tabs with Split::Above.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed above the old node.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
Sourcepub fn split_below(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2]
pub fn split_below( &mut self, parent: NodeIndex, fraction: f32, tabs: Vec<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) gets the tabs.
This is a shorthand for using split_tabs with Split::Below.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed below the old node.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
Sourcepub fn split_left(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2]
pub fn split_left( &mut self, parent: NodeIndex, fraction: f32, tabs: Vec<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) gets the tabs.
This is a shorthand for using split_tabs with Split::Left.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed to the left of the old node.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
Sourcepub fn split_right(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2]
pub fn split_right( &mut self, parent: NodeIndex, fraction: f32, tabs: Vec<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) gets the tabs.
This is a shorthand for using split_tabs with Split::Right.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed to the right of the old node.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
Sourcepub fn split(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
new: Node<Tab>,
) -> [NodeIndex; 2]
pub fn split( &mut self, parent: NodeIndex, split: Split, fraction: f32, new: Node<Tab>, ) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent node and assigns them as its children. The first (old) node
inherits content of the parent from before the split, and the second (new) uses new.
fraction (in range 0..=1) specifies how much of the parent node’s area the old node will attempt to occupy
after the split.
The new node is placed relatively to the old node, in the direction specified by split.
Returns the indices of the old node and the new node.
§Panics
If fraction isn’t in range 0..=1.
If new is an Empty, Horizontal or Vertical node.
If new is a Leaf node without any tabs.
If parent points to an Empty node.
§Example
let mut dock_state = DockState::new(vec!["tab 1", "tab 2"]);
// At this point, the main surface only contains the leaf with tab 1 and 2.
assert!(dock_state.main_surface().root_node().unwrap().is_leaf());
// Splits the node, giving 50% of the space to the new nodes and 50% to the old ones.
let [old, new] = dock_state.main_surface_mut()
.split(NodeIndex::root(), Split::Below, 0.5, Node::leaf_with(vec!["tab 3"]));
assert!(dock_state.main_surface().root_node().unwrap().is_parent());
assert!(dock_state[SurfaceIndex::main()][old].is_leaf());
assert!(dock_state[SurfaceIndex::main()][new].is_leaf());Sourcepub fn leaf(&self, node: NodeIndex) -> Result<&LeafNode<Tab>>
pub fn leaf(&self, node: NodeIndex) -> Result<&LeafNode<Tab>>
Immutably borrows a leaf node at the given path.
Returns Err if the path is invalid or the node at the path is not a leaf.
Sourcepub fn leaf_mut(&mut self, node: NodeIndex) -> Result<&mut LeafNode<Tab>>
pub fn leaf_mut(&mut self, node: NodeIndex) -> Result<&mut LeafNode<Tab>>
Mutably borrows a leaf node at the given index.
Returns Err if the index is invalid or if the node is not a leaf.
Sourcepub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
pub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
Sourcepub fn focused_leaf(&self) -> Option<NodeIndex>
pub fn focused_leaf(&self) -> Option<NodeIndex>
Gets the node index of currently focused leaf node; returns None when no leaf is focused.
Sourcepub fn set_focused_node(&mut self, node_index: NodeIndex)
pub fn set_focused_node(&mut self, node_index: NodeIndex)
Sets the currently focused leaf to node_index if the node at node_index is a leaf.
This method will not never panic and instead removes focus from all nodes when given an invalid index.
Sourcepub fn remove_leaf(&mut self, node: NodeIndex)
pub fn remove_leaf(&mut self, node: NodeIndex)
Sourcepub fn push_to_first_leaf(&mut self, tab: Tab)
pub fn push_to_first_leaf(&mut self, tab: Tab)
Pushes a tab to the first Leaf it finds or create a new leaf if an Empty node is encountered.
Sourcepub fn set_active_tab(
&mut self,
node_index: impl Into<NodeIndex>,
tab_index: impl Into<TabIndex>,
) -> Result
pub fn set_active_tab( &mut self, node_index: impl Into<NodeIndex>, tab_index: impl Into<TabIndex>, ) -> Result
Sets which is the active tab within a specific node.
§Errors
If the node is invalid, not a leaf or if the tab index is out of bounds.
Sourcepub fn push_to_focused_leaf(&mut self, tab: Tab)
pub fn push_to_focused_leaf(&mut self, tab: Tab)
Pushes tab to the currently focused leaf.
If no leaf is focused it will be pushed to the first available leaf.
If no leaf is available then a new leaf will be created.
Sourcepub fn remove_tab(
&mut self,
(node_index, tab_index): (NodeIndex, TabIndex),
) -> Option<Tab>
pub fn remove_tab( &mut self, (node_index, tab_index): (NodeIndex, TabIndex), ) -> Option<Tab>
Sourcepub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
Sourcepub fn map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
pub fn map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
Returns a new Tree while mapping the tab type.
Sourcepub fn filter_tabs<F>(&self, predicate: F) -> Tree<Tab>
pub fn filter_tabs<F>(&self, predicate: F) -> Tree<Tab>
Sourcepub fn retain_tabs<F>(&mut self, predicate: F)
pub fn retain_tabs<F>(&mut self, predicate: F)
Removes all tabs for which predicate returns false.
Any remaining empty Nodes are also removed.
Sourcepub fn find_tab_from(
&self,
predicate: impl Fn(&Tab) -> bool,
) -> Option<(NodeIndex, TabIndex)>
pub fn find_tab_from( &self, predicate: impl Fn(&Tab) -> bool, ) -> Option<(NodeIndex, TabIndex)>
Find a given tab based on predicate.
Returns the indices in where that node and tab is in this surface.
The returned NodeIndex will always point to a Node::Leaf.
In case there are several hits, only the first is returned.
Trait Implementations§
Auto Trait Implementations§
impl<Tab> Freeze for Tree<Tab>
impl<Tab> RefUnwindSafe for Tree<Tab>
impl<Tab> Send for Tree<Tab>
impl<Tab> Sync for Tree<Tab>
impl<Tab> Unpin for Tree<Tab>
impl<Tab> UnsafeUnpin for Tree<Tab>
impl<Tab> UnwindSafe for Tree<Tab>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more