Struct 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 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>
where Identifier: Clone + PartialEq + Eq + Hash,

Source

pub const fn get_offset(&self) -> usize

Source

pub fn get_all_opened(&self) -> Vec<Vec<Identifier>>

👎Deprecated: Use self.opened()
Source

pub const fn opened(&self) -> &HashSet<Vec<Identifier>>

Source

pub fn selected(&self) -> &[Identifier]

Source

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.

Source

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

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.

Source

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.

Source

pub fn toggle(&mut self, identifier: Vec<Identifier>) -> bool

Toggles a tree node open/close state. When it is currently open, then close is called. Otherwise open.

Returns true when a node is opened / closed. As toggle always changes something, this only returns false when an empty identifier is given.

Source

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.

Source

pub fn close_all(&mut self) -> bool

Closes all open nodes.

Returns true when any node was closed.

Source

pub fn select_first(&mut self) -> bool

Select the first node.

Returns true when the selection changed.

Source

pub fn select_last(&mut self) -> bool

Select the last node.

Returns true when the selection changed.

Source

pub 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

Select the node on the given index.

Returns true when the selection changed.

This can be useful for mouse clicks.

Source

pub fn select_visible_relative<F>(&mut self, change_function: F) -> bool
where F: FnOnce(Option<usize>) -> usize,

👎Deprecated: renamed to select_relative

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.

Source

pub fn select_relative<F>(&mut self, change_function: F) -> bool
where F: FnOnce(Option<usize>) -> usize,

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.

Source

pub fn rendered_at(&self, position: Position) -> Option<&[Identifier]>

Get the identifier that was rendered for the given position on last render.

Source

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.

Source

pub fn scroll_selected_into_view(&mut self)

Ensure the selected TreeItem is in view on next render

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn key_left(&mut self) -> bool

Handles the left arrow key. Closes the currently selected or moves to its parent.

Returns true when the selection or the open state changed.

Source

pub fn key_right(&mut self) -> bool

Handles the right arrow key. Opens the currently selected.

Returns true when it was closed and has been opened. Returns false when it was already open or nothing being selected.

Trait Implementations§

Source§

impl<Identifier: Debug> Debug for TreeState<Identifier>

Source§

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

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

impl<Identifier: Default> Default for TreeState<Identifier>

Source§

fn default() -> TreeState<Identifier>

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

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

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

Source§

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>,

Source§

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.