Skip to main content

cursive_tree/view/
state.rs

1use super::{
2    super::{backend::*, model::*},
3    tree::*,
4};
5
6use cursive::event::*;
7
8impl<BackendT> TreeView<BackendT>
9where
10    BackendT: TreeBackend,
11    BackendT::Context: 'static + Clone + Send + Sync,
12    BackendT::Error: 'static + Send + Sync,
13{
14    /// Handle toggle branch state event.
15    pub(crate) fn on_toggle_branch_state(node: &mut Node<BackendT>, context: BackendT::Context) -> EventResult {
16        EventResult::Consumed(match node.toggle_branch_state(context.clone()) {
17            Ok(_) => None,
18            Err(error) => Some(Self::error_callback(context, error)),
19        })
20    }
21
22    /// Handle expand branch event.
23    pub(crate) fn on_expand_branch(
24        node: &mut Node<BackendT>,
25        depth: Option<usize>,
26        context: BackendT::Context,
27    ) -> EventResult {
28        EventResult::Consumed(match node.expand(depth, context.clone()) {
29            Ok(_) => None,
30            Err(error) => Some(Self::error_callback(context, error)),
31        })
32    }
33
34    /// Handle expand all event.
35    pub(crate) fn on_expand_all(&mut self) -> EventResult {
36        EventResult::Consumed(match self.model.expand(None) {
37            Ok(_) => None,
38            Err(error) => Some(Self::error_callback(self.model.context.clone(), error)),
39        })
40    }
41
42    /// Handle selection changed.
43    pub(crate) fn on_selection_changed(&self, last_selected_row: Option<usize>) -> EventResult {
44        EventResult::Consumed(if self.selected_row != last_selected_row {
45            let context = self.model.context.clone();
46            Some(Callback::from_fn_once(|cursive| BackendT::handle_selection_changed(cursive, context)))
47        } else {
48            None
49        })
50    }
51
52    // Error callback.
53    fn error_callback(context: BackendT::Context, error: BackendT::Error) -> Callback {
54        Callback::from_fn_once(|cursive| BackendT::handle_error(cursive, context, error))
55    }
56}