Skip to content

Commit

Permalink
perf(state): less clones on handling the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed May 15, 2024
1 parent bba94dc commit 2811a79
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/tree_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ where
}

#[must_use]
#[deprecated = "use self.get_selected"]
pub fn selected(&self) -> Vec<Identifier> {
self.selected.clone()
}

#[must_use]
pub fn get_selected(&self) -> &[Identifier] {
&self.selected
}

/// Get a flat list of all visible (= below open) [`TreeItem`]s with this `TreeState`.
#[must_use]
pub fn flatten<'a>(
Expand Down Expand Up @@ -117,8 +123,19 @@ where
/// Returns `true` when a node is opened / closed.
/// As toggle always changes something, this only returns `false` when nothing is selected.
pub fn toggle_selected(&mut self) -> bool {
if self.selected.is_empty() {
return false;
}

self.ensure_selected_in_view_on_next_render = true;
self.toggle(self.selected())

// Reimplement self.close because of multiple different borrows
let was_open = self.open.remove(&self.selected);
if was_open {
return true;
}

self.open(self.selected.clone())
}

/// Closes all open nodes.
Expand Down Expand Up @@ -272,9 +289,13 @@ where
/// Opens the currently selected.
///
/// Returns `true` when it was closed and has been opened.
/// Returns `false` when it was already open.
/// Returns `false` when it was already open or nothing being selected.
pub fn key_right(&mut self) -> bool {
self.ensure_selected_in_view_on_next_render = true;
self.open(self.selected())
if self.selected.is_empty() {
false
} else {
self.ensure_selected_in_view_on_next_render = true;
self.open(self.selected.clone())
}
}
}

0 comments on commit 2811a79

Please sign in to comment.