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 TreeItems in the TreeState.
For more information see TreeItem.
§Example
type Identifier = usize;
let mut state = TreeState::<Identifier>::default();Implementations§
Source§impl<Identifier> TreeState<Identifier>
impl<Identifier> TreeState<Identifier>
pub const fn get_offset(&self) -> usize
pub fn get_all_opened(&self) -> Vec<Vec<Identifier>>
pub const fn opened(&self) -> &HashSet<Vec<Identifier>>
pub fn selected(&self) -> &[Identifier]
Sourcepub fn flatten<'text>(
&self,
items: &'text [TreeItem<'text, Identifier>],
) -> Vec<Flattened<'text, Identifier>>
pub fn flatten<'text>( &self, items: &'text [TreeItem<'text, Identifier>], ) -> Vec<Flattened<'text, Identifier>>
Get a flat list of all currently viewable (including by scrolling) TreeItems with this TreeState.
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 when it was closed and has been opened.
Returns false when it 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 when it was open and has been closed.
Returns false when it was already closed.
Sourcepub fn toggle_selected(&mut self) -> bool
pub fn toggle_selected(&mut self) -> bool
Toggles the currently selected tree node open/close state.
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) -> bool
pub fn select_first(&mut self) -> bool
Select the first node.
Returns true when the selection changed.
Sourcepub fn select_last(&mut self) -> bool
pub fn select_last(&mut self) -> bool
Select the last node.
Returns true when the selection changed.
Sourcepub fn select_visible_index(&mut self, new_index: usize) -> bool
👎Deprecated: Prefer self.click_at or self.rendered_at as visible index is hard to predict with height != 1
pub fn select_visible_index(&mut self, new_index: usize) -> bool
Select the node 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, change_function: F) -> bool
👎Deprecated: renamed to select_relative
pub fn select_visible_relative<F>(&mut self, 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(|current| {
// When nothing is currently selected, select index 0
// Otherwise select current + 1 (without panicking)
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 select_relative<F>(&mut self, change_function: F) -> bool
pub fn select_relative<F>(&mut self, 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_relative(|current| {
// When nothing is currently selected, select index 0
// Otherwise select current + 1 (without panicking)
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 rendered_at(&self, position: Position) -> Option<&[Identifier]>
pub fn rendered_at(&self, position: Position) -> Option<&[Identifier]>
Get the identifier that was rendered for the given position on last render.
Sourcepub fn click_at(&mut self, position: Position) -> bool
pub fn click_at(&mut self, position: Position) -> bool
Select what was rendered at the given position on last render. When it is already selected, toggle it.
Returns true when the state changed.
Returns false when there was nothing at the given position.
Sourcepub fn scroll_selected_into_view(&mut self)
pub fn scroll_selected_into_view(&mut self)
Ensure the selected TreeItem is in view 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
Returns true when the scroll position changed.
Returns false when the scrolling has reached the last TreeItem.
Sourcepub fn key_up(&mut self) -> bool
pub fn key_up(&mut self) -> 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) -> bool
pub fn key_down(&mut self) -> bool
Handles the down arrow key. Moves down in the current depth or into a child node.
Returns true when the selection changed.
Trait Implementations§
Auto Trait Implementations§
impl<Identifier> Freeze for TreeState<Identifier>
impl<Identifier> RefUnwindSafe for TreeState<Identifier>where
Identifier: RefUnwindSafe,
impl<Identifier> Send for TreeState<Identifier>where
Identifier: Send,
impl<Identifier> Sync for TreeState<Identifier>where
Identifier: Sync,
impl<Identifier> Unpin for TreeState<Identifier>where
Identifier: Unpin,
impl<Identifier> UnwindSafe for TreeState<Identifier>where
Identifier: UnwindSafe,
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
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