summaryrefslogtreecommitdiffstats
path: root/src/tabs.rs
blob: 3f8f97d5a41f1a156a021a5ec86454bdb022eec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::ops::Deref;
use std::ops::DerefMut;
use std::rc::Rc;

use anyhow::Result;
use cursive::Printer;
use cursive::Rect;
use cursive::View;
use cursive::XY;
use cursive::direction::Direction;
use cursive::event::Event;
use cursive::event::EventResult;
use cursive::view::Selector;
use cursive::view::ViewNotFound;

use crate::runtime::Runtime;
use crate::views::maillist::MaillistView;

#[derive(Hash, Eq, PartialEq, Clone, parse_display::Display)]
pub enum TabPanelName {
    #[display("Query: {0}")]
    NotmuchQuery(String),

    #[display("Other")]
    Other,
}

pub struct Tabs(cursive_tabs::TabPanel<TabPanelName>);

impl Deref for Tabs {
    type Target = cursive_tabs::TabPanel<TabPanelName>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Tabs {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Tabs {
    pub fn new(query: &str, rt: Rc<Runtime>) -> Result<Self> {
        let tab_ident = TabPanelName::NotmuchQuery(query.to_string());
        let tp = cursive_tabs::TabPanel::new()
            .with_bar_alignment(cursive_tabs::Align::Start)
            .with_bar_placement(cursive_tabs::Placement::VerticalLeft)
            .with_tab(tab_ident, Tab::for_query(rt, query)?);

        Ok(Tabs(tp))
    }

    pub fn add_tab_for_query(&mut self, rt: Rc<Runtime>, query: &str) -> Result<()> {
        let ident = TabPanelName::NotmuchQuery(query.to_string());
        self.0.add_tab(ident, Tab::for_query(rt, query)?);
        Ok(())
    }
}

impl View for Tabs {
    fn draw(&self, printer: &Printer) {
        self.0.draw(printer);
    }

    fn layout(&mut self, xy: XY<usize>) {
        self.0.layout(xy)
    }

    fn needs_relayout(&self) -> bool {
        self.0.needs_relayout()
    }

    fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> {
        self.0.required_size(constraint)
    }

    fn on_event(&mut self, e: Event) -> EventResult {
        self.0.on_event(e)
    }

    fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) {
        self.0.call_on_any(s, tpl);
    }

    fn focus_view(&mut self, s: &Selector) -> Result<(), ViewNotFound> {
        self.0.focus_view(s)
    }

    fn take_focus(&mut self, source: Direction) -> bool {
        self.0.take_focus(source)
    }

    fn important_area(&self, view_size: XY<usize>) -> Rect {
        self.0.important_area(view_size)
    }

    fn type_name(&self) -> &'static str {
        self.0.type_name()
    }

}

#[derive(getset::Getters, getset::MutGetters)]
pub struct Tab {
    #[getset(get = "pub", get_mut = "pub")]
    mux: cursive_multiplex::Mux,
    nodes: Vec<cursive_multiplex::Id>,
    rt: Rc<Runtime>,
}

impl Tab {
    fn new(rt: Rc<Runtime>) -> Self {
        Tab {
            mux: cursive_multiplex::Mux::new(),
            nodes: vec![],
            rt,
        }
    }

    fn for_query(rt: Rc<Runtime>, query: &str) -> Result<Self> {
        use failure::Fail;

        let mut this = Self::new(rt.clone());
        let root = this.mux.root().build().unwrap();
        let list = MaillistView::for_query(rt, query)?;
        this.nodes.push(this.mux.add_right_of(list, root).map_err(|e| e.compat())?);
        Ok(this)
    }
}

impl View for Tab {
    fn draw(&self, printer: &Printer) {
        self.mux.draw(printer);
    }

    fn layout(&mut self, xy: XY<usize>) {
        self.mux.layout(xy)
    }

    fn needs_relayout(&self) -> bool {
        self.mux.needs_relayout()
    }

    fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> {
        self.mux.required_size(constraint)
    }

    fn on_event(&mut self, e: Event) -> EventResult {
        self.mux.on_event(e)
    }

    fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) {
        self.mux.call_on_any(s, tpl);
    }

    fn focus_view(&mut self, s: &Selector) -> Result<(), ViewNotFound> {
        self.mux.focus_view(s)
    }

    fn take_focus(&mut self, source: Direction) -> bool {
        self.mux.take_focus(source)
    }

    fn important_area(&self, view_size: XY<usize>) -> Rect {
        self.mux.important_area(view_size)
    }

    fn type_name(&self) -> &'static str {
        self.mux.type_name()
    }
}