Skip to main content

cursive_split_panel/
actions.rs

1use {
2    cursive::{direction::*, event::*},
3    std::collections::*,
4};
5
6/// Split panel action map.
7pub type Actions = HashMap<Event, Action>;
8
9//
10// Action
11//
12
13/// Split panel action.
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub enum Action {
16    /// Move divider towards front.
17    MoveDividerToFront,
18
19    /// Move divider towards back.
20    MoveDividerToBack,
21}
22
23impl Action {
24    /// Default split panel action map.
25    pub fn defaults(orientation: Orientation) -> Actions {
26        match orientation {
27            Orientation::Horizontal => [
28                (Event::Shift(Key::Left), Self::MoveDividerToFront),
29                (Event::Shift(Key::Right), Self::MoveDividerToBack),
30            ],
31            Orientation::Vertical => {
32                [(Event::Shift(Key::Up), Self::MoveDividerToFront), (Event::Shift(Key::Down), Self::MoveDividerToBack)]
33            }
34        }
35        .into()
36    }
37
38    /// Remove from action map.
39    ///
40    /// Return true if removed.
41    pub fn remove(&self, actions: &mut Actions) -> bool {
42        let events = self.events(actions);
43        for event in &events {
44            actions.remove(event);
45        }
46        !events.is_empty()
47    }
48
49    /// Events for the action.
50    pub fn events(&self, actions: &Actions) -> Vec<Event> {
51        actions
52            .into_iter()
53            .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
54            .collect()
55    }
56}