Struct tui_tree_widget::TreeState
source · pub struct TreeState<Identifier> { /* private fields */ }
Expand description
Keeps the state of what is currently selected and what was opened in a Tree
.
The generic argument Identifier
is used to keep the state like the currently selected or opened TreeItem
s in the TreeState
.
For more information see TreeItem
.
§Example
type Identifier = usize;
let mut state = TreeState::<Identifier>::default();
Implementations§
source§impl<Identifier> State<Identifier>
impl<Identifier> State<Identifier>
pub const fn get_offset(&self) -> usize
pub fn get_all_opened(&self) -> Vec<Vec<Identifier>>
sourcepub fn flatten<'a>(
&self,
items: &'a [Item<'a, Identifier>]
) -> Vec<Flattened<'a, Identifier>>
pub fn flatten<'a>( &self, items: &'a [Item<'a, Identifier>] ) -> Vec<Flattened<'a, Identifier>>
Get a flat list of all visible (= below open) TreeItem
s with this TreeState
.
pub fn selected(&self) -> Vec<Identifier>
sourcepub fn select(&mut self, identifier: Vec<Identifier>) -> bool
pub fn select(&mut self, identifier: Vec<Identifier>) -> bool
Selects the given identifier.
Returns true
when the selection changed.
Clear the selection by passing an empty identifier vector:
state.select(Vec::new());
sourcepub fn open(&mut self, identifier: Vec<Identifier>) -> bool
pub fn open(&mut self, identifier: Vec<Identifier>) -> bool
Open a tree node.
Returns true
if the node was closed and has been opened.
Returns false
if the node was already open.
sourcepub fn close(&mut self, identifier: &[Identifier]) -> bool
pub fn close(&mut self, identifier: &[Identifier]) -> bool
Close a tree node.
Returns true
if the node was open and has been closed.
Returns false
if the node was already closed.
sourcepub fn toggle_selected(&mut self) -> bool
pub fn toggle_selected(&mut self) -> bool
Toggles the currently selected tree node.
See also toggle
Returns true
when a node is opened / closed.
As toggle always changes something, this only returns false
when nothing is selected.
sourcepub fn close_all(&mut self) -> bool
pub fn close_all(&mut self) -> bool
Closes all open nodes.
Returns true
when any node was closed.
sourcepub fn select_first(&mut self, items: &[Item<'_, Identifier>]) -> bool
pub fn select_first(&mut self, items: &[Item<'_, Identifier>]) -> bool
Select the first node.
Returns true
when the selection changed.
sourcepub fn select_last(&mut self, items: &[Item<'_, Identifier>]) -> bool
pub fn select_last(&mut self, items: &[Item<'_, Identifier>]) -> bool
Select the last visible node.
Returns true
when the selection changed.
sourcepub fn select_visible_index(
&mut self,
items: &[Item<'_, Identifier>],
new_index: usize
) -> bool
pub fn select_visible_index( &mut self, items: &[Item<'_, Identifier>], new_index: usize ) -> bool
Select the node visible on the given index.
Returns true
when the selection changed.
This can be useful for mouse clicks.
sourcepub fn select_visible_relative<F>(
&mut self,
items: &[Item<'_, Identifier>],
change_function: F
) -> bool
pub fn select_visible_relative<F>( &mut self, items: &[Item<'_, Identifier>], change_function: F ) -> bool
Move the current selection with the direction/amount by the given function.
Returns true
when the selection changed.
§Example
// Move the selection one down
state.select_visible_relative(&items, |current| {
current.map_or(0, |current| current.saturating_add(1))
});
For more examples take a look into the source code of key_up
or key_down
.
They are implemented with this method.
sourcepub fn scroll_selected_into_view(&mut self)
pub fn scroll_selected_into_view(&mut self)
Ensure the selected TreeItem
is visible on next render
sourcepub fn scroll_up(&mut self, lines: usize) -> bool
pub fn scroll_up(&mut self, lines: usize) -> bool
Scroll the specified amount of lines up
Returns true
when the scroll position changed.
Returns false
when the scrolling has reached the top.
sourcepub fn scroll_down(&mut self, lines: usize) -> bool
pub fn scroll_down(&mut self, lines: usize) -> bool
Scroll the specified amount of lines down
In contrast to scroll_up()
this can not return whether the view position changed or not as the actual change is determined on render.
Always returns true
.
sourcepub fn key_up(&mut self, items: &[Item<'_, Identifier>]) -> bool
pub fn key_up(&mut self, items: &[Item<'_, Identifier>]) -> bool
Handles the up arrow key. Moves up in the current depth or to its parent.
Returns true
when the selection changed.
sourcepub fn key_down(&mut self, items: &[Item<'_, Identifier>]) -> bool
pub fn key_down(&mut self, items: &[Item<'_, Identifier>]) -> bool
Handles the down arrow key. Moves down in the current depth or into a child node.
Returns true
when the selection changed.