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    /// Intended for debugging.
40    ToggleOrientation,
41}
42
43impl Action {
44    /// Default split panel action map.
45    pub fn defaults(orientation: Orientation) -> Actions {
46        match orientation {
47            Orientation::Horizontal => [
48                (Event::Shift(Key::Left), Self::MoveDividerToFront),
49                (Event::Shift(Key::Right), Self::MoveDividerToBack),
50            ],
51            Orientation::Vertical => {
52                [(Event::Shift(Key::Up), Self::MoveDividerToFront), (Event::Shift(Key::Down), Self::MoveDividerToBack)]
53            }
54        }
55        .into()
56    }
57
58    /// Remove from action map.
59    ///
60    /// Return true if removed.
61    pub fn remove(&self, actions: &mut Actions) -> bool {
62        let events = self.events(actions);
63        for event in &events {
64            actions.remove(event);
65        }
66        !events.is_empty()
67    }
68
69    /// Events for the action.
70    pub fn events(&self, actions: &Actions) -> Vec<Event> {
71        actions
72            .into_iter()
73            .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
74            .collect()
75    }
76}