Struct egui_dock::dock_state::tree::Tree

source ·
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>

source

pub fn new(tabs: Vec<Tab>) -> Self

Creates a new Tree with given Vec of Tabs in its root node.

source

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.

source

pub fn len(&self) -> usize

Returns the number of nodes in the Tree.

This includes Empty nodes.

source

pub fn is_empty(&self) -> bool

Returns true if the number of nodes in the tree is 0, otherwise false.

source

pub fn iter(&self) -> Iter<'_, Node<Tab>>

Returns an Iterator of the underlying collection of nodes.

This includes Empty nodes.

source

pub fn iter_mut(&mut self) -> IterMut<'_, Node<Tab>>

Returns IterMut of the underlying collection of nodes.

This includes Empty nodes.

source

pub fn tabs(&self) -> TabIter<'_, Tab>

Returns an iterator over all tabs in arbitrary order.

source

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);
source

pub fn root_node(&self) -> Option<&Node<Tab>>

Acquire a immutable 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().root_node().unwrap();

assert_eq!(root_node.tabs(), Some(["single tab"].as_slice()));
source

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()));
source

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());
source

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.

source

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.

source

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.

source

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.

source

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());
source

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.

source

pub fn focused_leaf(&self) -> Option<NodeIndex>

Gets the node index of currently focused leaf node; returns None when no leaf is focused.

source

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.

source

pub fn remove_leaf(&mut self, node: NodeIndex)

Removes the given node from the Tree.

§Panics
  • If the tree is empty.
  • If the node at index node is not a Leaf.
source

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.

source

pub fn set_active_tab(&mut self, node_index: NodeIndex, tab_index: TabIndex)

Sets which is the active tab within a specific node.

source

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.

source

pub fn remove_tab( &mut self, (node_index, tab_index): (NodeIndex, TabIndex) ) -> Option<Tab>

Removes the tab at the given (NodeIndex, TabIndex) pair.

If the node is emptied after the tab is removed, the node will also be removed.

Returns the removed tab if it exists, or None otherwise.

source

pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
where F: Clone + FnMut(&Tab) -> Option<NewTab>,

Returns a new Tree while mapping and filtering the tab type. Any remaining empty Nodes are removed.

source

pub fn map_tabs<F, NewTab>(&self, function: F) -> Tree<NewTab>
where F: Clone + FnMut(&Tab) -> NewTab,

Returns a new Tree while mapping the tab type.

source

pub fn filter_tabs<F>(&self, predicate: F) -> Tree<Tab>
where F: Clone + FnMut(&Tab) -> bool, Tab: Clone,

Returns a new Tree while filtering the tab type. Any remaining empty Nodes are removed.

source

pub fn retain_tabs<F>(&mut self, predicate: F)
where F: Clone + FnMut(&mut Tab) -> bool,

Removes all tabs for which predicate returns false. Any remaining empty Nodes are also removed.

source§

impl<Tab> Tree<Tab>
where Tab: PartialEq,

source

pub fn find_tab(&self, needle_tab: &Tab) -> Option<(NodeIndex, TabIndex)>

Find the given tab.

Returns in which node and where in that node the tab is.

The returned NodeIndex will always point to a Node::Leaf.

In case there are several hits, only the first is returned.

Trait Implementations§

source§

impl<Tab: Clone> Clone for Tree<Tab>

source§

fn clone(&self) -> Tree<Tab>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Tab> Debug for Tree<Tab>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Tab> Default for Tree<Tab>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Tab> Index<NodeIndex> for Tree<Tab>

§

type Output = Node<Tab>

The returned type after indexing.
source§

fn index(&self, index: NodeIndex) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<Tab> IndexMut<NodeIndex> for Tree<Tab>

source§

fn index_mut(&mut self, index: NodeIndex) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<Tab> Freeze for Tree<Tab>

§

impl<Tab> RefUnwindSafe for Tree<Tab>
where Tab: RefUnwindSafe,

§

impl<Tab> Send for Tree<Tab>
where Tab: Send,

§

impl<Tab> Sync for Tree<Tab>
where Tab: Sync,

§

impl<Tab> Unpin for Tree<Tab>
where Tab: Unpin,

§

impl<Tab> UnwindSafe for Tree<Tab>
where Tab: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,