summaryrefslogtreecommitdiffstats
path: root/src/main_view.rs
blob: 7dc7a54e0f2d95b381f419d606a416e6862e99c3 (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
use std::path::PathBuf;
use anyhow::Result;
use cursive::Cursive;
use cursive::Printer;
use cursive::Rect;
use cursive::View;
use cursive::XY;
use cursive::direction::Direction;
use cursive::event::Callback;
use cursive::event::Event;
use cursive::event::EventResult;
use cursive::view::Nameable;
use cursive::view::Selector;
use cursive::views::Dialog;
use cursive::views::EditView;
use cursive::views::ListView;
use cursive::views::LinearLayout;
use cursive::views::NamedView;
use cursive::views::ResizedView;
use cursive::views::TextView;
use cursive_table_view::TableView;
use cursive_table_view::TableViewItem;
use cursive_table_view::TableColumn;
use cursive_tabs::TabPanel;

use crate::configuration::Configuration;
use crate::maillist_view::MaillistView;

pub const MAIN_VIEW_NAME: &'static str = "main_view";
pub const MAIN_MAIL_LIST_NAME: &'static str = "main_mail_list";

pub struct MainView {
    config: Configuration,
    tabs: cursive_tabs::TabPanel<String>,
}

impl View for MainView {
    fn draw(&self, printer: &Printer) {
        self.tabs.draw(printer)
    }

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

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

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

    fn on_event(&mut self, e: Event) -> EventResult {
        match e {
            Event::Char('q') => {
                if self.tabs.tab_order().len() == 1 {
                    EventResult::Ignored
                } else {
                    if let Some(key) = self.tabs.active_tab().cloned() {
                        self.tabs.remove_tab(&key);
                        debug!("Remove tab");
                        EventResult::Consumed(None)
                    } else {
                        debug!("No tab to remove found.");
                        EventResult::Ignored
                    }
                }
            },

            Event::Char('o') => {
                EventResult::Consumed(Some(Callback::from_fn(MainView::add_notmuch_query_layer)))
            },

            other => self.tabs.on_event(other),
        }
    }

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

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

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

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

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

}

impl MainView {
    pub fn new(config: Configuration) -> Result<NamedView<Self>> {
        let tabs = cursive_tabs::TabPanel::default()
            .with_bar_alignment(cursive_tabs::Align::Start)
            .with_bar_placement(cursive_tabs::Placement::HorizontalTop)
            .with_tab(config.notmuch_default_query().clone(), {
                ResizedView::new(cursive::view::SizeConstraint::Full,
                                 cursive::view::SizeConstraint::Full,
                                 MaillistView::create_for(config.notmuch_database_path(), config.notmuch_default_query(), MAIN_MAIL_LIST_NAME.to_string())?
                                     .with_name(MAIN_MAIL_LIST_NAME))
            });

        Ok(MainView { config, tabs }.with_name(MAIN_VIEW_NAME))
    }

    pub fn add_tab<T: View>(&mut self, id: String, view: T) {
        self.tabs.add_tab(id, view)
    }

    pub fn config(&self) -> &Configuration {
        &self.config
    }

    fn add_notmuch_query_layer(siv: &mut Cursive) {
        let edit_view = EditView::new()
            .on_submit(move |siv: &mut Cursive, query: &str| {
                let database_path = siv.call_on_name(MAIN_VIEW_NAME, move |main_view: &mut MainView| {
                    main_view.config().notmuch_database_path().clone()
                });

                let database_path = database_path.unwrap(); // TODO: Fixme

                let tab_name = format!("{}-view", query);
                let tab = MaillistView::create_for(&database_path, query, query.to_string())
                    .unwrap() // TODO: FIXME
                    .with_name(tab_name);

                let tab = ResizedView::new(cursive::view::SizeConstraint::Full,
                                 cursive::view::SizeConstraint::Full,
                                 tab);

                siv.call_on_name(MAIN_VIEW_NAME, move |main_view: &mut MainView| {
                    main_view.add_tab(query.to_string(), tab);
                });

                siv.pop_layer();
            })
            .with_name("query");

        siv.add_layer({
            ResizedView::new(cursive::view::SizeConstraint::AtLeast(80),
                             cursive::view::SizeConstraint::Free,
                             Dialog::around(edit_view).title("Query"))
        })
    }

}