cursive-tree 0.0.9

Tree view for the Cursive TUI library
Documentation
use super::{
    super::{backend::*, model::*},
    tree::*,
};

use cursive::event::*;

impl<BackendT> TreeView<BackendT>
where
    BackendT: TreeBackend,
    BackendT::Context: 'static + Clone + Send + Sync,
    BackendT::Error: 'static + Send + Sync,
{
    /// Handle toggle branch state event.
    pub(crate) fn on_toggle_branch_state(node: &mut Node<BackendT>, context: BackendT::Context) -> EventResult {
        EventResult::Consumed(match node.toggle_branch_state(context.clone()) {
            Ok(_) => None,
            Err(error) => Some(Self::error_callback(context, error)),
        })
    }

    /// Handle expand branch event.
    pub(crate) fn on_expand_branch(
        node: &mut Node<BackendT>,
        depth: Option<usize>,
        context: BackendT::Context,
    ) -> EventResult {
        EventResult::Consumed(match node.expand(depth, context.clone()) {
            Ok(_) => None,
            Err(error) => Some(Self::error_callback(context, error)),
        })
    }

    /// Handle expand all event.
    pub(crate) fn on_expand_all(&mut self) -> EventResult {
        EventResult::Consumed(match self.model.expand(None) {
            Ok(_) => None,
            Err(error) => Some(Self::error_callback(self.model.context.clone(), error)),
        })
    }

    /// Handle selection changed.
    pub(crate) fn on_selection_changed(&self, last_selected_row: Option<usize>) -> EventResult {
        EventResult::Consumed(if self.selected_row != last_selected_row {
            let context = self.model.context.clone();
            Some(Callback::from_fn_once(|cursive| BackendT::handle_selection_changed(cursive, context)))
        } else {
            None
        })
    }

    // Error callback.
    fn error_callback(context: BackendT::Context, error: BackendT::Error) -> Callback {
        Callback::from_fn_once(|cursive| BackendT::handle_error(cursive, context, error))
    }
}