diff options
Diffstat (limited to 'src/views/main.rs')
-rw-r--r-- | src/views/main.rs | 146 |
1 files changed, 54 insertions, 92 deletions
diff --git a/src/views/main.rs b/src/views/main.rs index af9dcea..2cf54c3 100644 --- a/src/views/main.rs +++ b/src/views/main.rs @@ -1,48 +1,84 @@ +use std::rc::Rc; + 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::event::Key; +use cursive::traits::Resizable; use cursive::view::Nameable; use cursive::view::Selector; -use cursive::views::Dialog; -use cursive::views::EditView; +use cursive::view::SizeConstraint; +use cursive::view::ViewNotFound; use cursive::views::NamedView; use cursive::views::ResizedView; -use cursive::traits::Resizable; -use cursive_multiplex::Mux; -use cursive::view::SizeConstraint; -use cursive::event::Key; use getset::{Getters, MutGetters}; use crate::bindings::BindingCaller; use crate::bindings::Bindings; -use crate::configuration::Configuration; -use crate::views::mail::MailView; -use crate::views::maillist::MailListingData; -use crate::views::maillist::MaillistView; +use crate::tabs::*; +use crate::runtime::Runtime; pub const MAIN_VIEW_NAME: &'static str = "main_view"; -pub const MAIN_MUX_NAME: &'static str = "main_mux"; -pub const MAIN_MAIL_LIST_NAME: &'static str = "main_mail_list"; #[derive(Getters, MutGetters)] pub struct MainView { - config: Configuration, - muxroot: cursive_multiplex::Id, + rt: Rc<Runtime>, #[getset(get = "pub", get_mut = "pub")] - tabs: cursive_tabs::TabPanel<String>, + tabs: crate::tabs::Tabs, bindings: Bindings, bindings_caller: Option<ResizedView<NamedView<BindingCaller>>>, } +impl MainView { + pub fn new(rt: Rc<Runtime>) -> Result<NamedView<Self>> { + let default_query = rt.config().notmuch_default_query(); + let tabs = Tabs::new(default_query, rt.clone())?; + let bindings = crate::bindings::get_bindings(); + + Ok(MainView { rt, tabs, bindings, bindings_caller: None }.with_name(MAIN_VIEW_NAME)) + } + + pub fn get_current_tab(&self) -> Option<&cursive_multiplex::Mux> { + if let Some(mux) = self.tabs.get_active_tab() { + mux.downcast_ref::<cursive_multiplex::Mux>() + } else { + None + } + } + + pub fn get_current_mux(&self) -> Option<&Box<dyn cursive::View>> { + if let Some(mux) = self.get_current_tab() { + return mux.get_current_view() + } + + None + } + + pub fn get_current_tab_mut(&mut self) -> Option<&mut cursive_multiplex::Mux> { + if let Some(mux) = self.tabs.get_active_tab_mut() { + mux.downcast_mut::<cursive_multiplex::Mux>() + } else { + None + } + } + + pub fn get_current_mux_mut(&mut self) -> Option<&mut Box<dyn cursive::View>> { + if let Some(mut mux) = self.get_current_tab_mut() { + return mux.get_current_view_mut() + } + + None + } + +} + impl View for MainView { fn draw(&self, printer: &Printer) { self.tabs.draw(printer); @@ -99,7 +135,7 @@ impl View for MainView { self.tabs.call_on_any(s, tpl); } - fn focus_view(&mut self, s: &Selector) -> Result<(), ()> { + fn focus_view(&mut self, s: &Selector) -> Result<(), ViewNotFound> { self.tabs.focus_view(s) } @@ -116,77 +152,3 @@ impl View for MainView { } } - -impl MainView { - pub fn new(config: Configuration) -> Result<NamedView<Self>> { - let mut tab = cursive_multiplex::Mux::new(); - let muxroot = tab.root().build().unwrap(); - - { - let dbpath = config.notmuch_database_path(); - let dbquery = config.notmuch_default_query(); - let mlname = MAIN_MAIL_LIST_NAME.to_string(); - let view = MaillistView::create_for(dbpath.to_path_buf(), dbquery, mlname)?; - - let _ = tab.add_right_of(view, muxroot); - } - - 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(), tab.with_name(MAIN_MUX_NAME)); - - let bindings = crate::bindings::get_bindings(); - - Ok(MainView { config, muxroot, tabs, bindings, bindings_caller: None }.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 - } - - pub fn add_notmuch_query_layer(siv: &mut Cursive) { - use crate::util::dialog_for; - use crate::util::error_dialog_for; - - let edit_view = EditView::new() - .on_submit(move |siv: &mut Cursive, query: &str| { - siv.call_on_name(MAIN_VIEW_NAME, move |main_view: &mut MainView| { - main_view.config().notmuch_database_path().clone() - }) - .map(|dbpath| { - let t = MaillistView::create_for(dbpath.to_path_buf(), query, query.to_string())? - .full_screen() - .with_name(format!("{}-view", query)); - - siv.call_on_name(MAIN_VIEW_NAME, move |main_view: &mut MainView| { - main_view.add_tab(query.to_string(), t); - }); - - siv.pop_layer(); - Ok(()) - }) - .unwrap_or_else(|| { - siv.pop_layer(); - siv.add_layer(dialog_for("Failed to get database connection set up")); - Ok(()) - }) - .unwrap_or_else(|e: anyhow::Error| { - siv.pop_layer(); - siv.add_layer(error_dialog_for(e)) - }); - }) - .with_name("query"); - - siv.add_layer({ - Dialog::around(edit_view) - .title("Query") - .min_width(80) - }) - } - -} |