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    /// Toggle border.
23    ///
24    /// Intended for debugging.
25    ToggleBorder,
26
27    /// Toggle visible divider.
28    ///
29    /// Intended for debugging.
30    ToggleVisibleDivider,
31
32    /// Toggle movable divider.
33    ///
34    /// Intended for debugging.
35    ToggleMovableDivider,
36
37    /// Toggle orientation.
38    ///
39    /// Will set the default action events for the orientation.
40    ///
41    /// Intended for debugging.
42    ToggleOrientation,
43}
44
45impl Action {
46    /// Default split panel action map.
47    pub fn defaults(orientation: Orientation) -> Actions {
48        match orientation {
49            Orientation::Horizontal => [
50                (Event::Shift(Key::Left), Self::MoveDividerToFront),
51                (Event::Shift(Key::Right), Self::MoveDividerToBack),
52            ],
53            Orientation::Vertical => {
54                [(Event::Shift(Key::Up), Self::MoveDividerToFront), (Event::Shift(Key::Down), Self::MoveDividerToBack)]
55            }
56        }
57        .into()
58    }
59
60    /// Remove from action map.
61    ///
62    /// Return true if removed.
63    pub fn remove(&self, actions: &mut Actions) -> bool {
64        let events = self.events(actions);
65        for event in &events {
66            actions.remove(event);
67        }
68        !events.is_empty()
69    }
70
71    /// Events for the action.
72    pub fn events(&self, actions: &Actions) -> Vec<Event> {
73        actions
74            .into_iter()
75            .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
76            .collect()
77    }
78}