Struct egui_dock::dock_state::tree::Tree
source · pub struct Tree<Tab> { /* private fields */ }
Expand description
Binary tree representing the relationships between Node
s.
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)>
Returns the viewport [Rect
] and the Tab
inside the first leaf node,
or None
if no leaf exists in the Tree
.
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();
if let Node::Leaf { tabs, ..} = root_node {
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 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 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 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 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 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 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 find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
pub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
Returns the viewport [Rect
] and the Tab
inside the focused leaf node or None
if it does not exist.
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: NodeIndex, tab_index: TabIndex)
pub fn set_active_tab(&mut self, node_index: NodeIndex, tab_index: TabIndex)
Sets which is the active tab within a specific node.
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.