diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-01-30 12:43:15 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-01-30 13:01:35 +0100 |
commit | e5e72a6d36c9bacc61efd3ed3453867c7f4d970f (patch) | |
tree | 942a0968c05f9e6e14f9c9194da06712b9adead0 | |
parent | 37b555faa39c0bee96ce40f0cff7e76dd5ef783b (diff) |
Change implementation to use menubar for commands
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/tabs.rs | 8 | ||||
-rw-r--r-- | src/views/main.rs | 4 | ||||
-rw-r--r-- | src/views/menu.rs | 70 | ||||
-rw-r--r-- | src/views/mod.rs | 1 |
5 files changed, 84 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 5d847d5..0dce57a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,8 @@ fn main() -> Result<()> { debug!("Adding mainview"); siv.add_fullscreen_layer(crate::views::main::MainView::new(Rc::new(runtime))?); + crate::views::menu::init(&mut siv); + siv.add_global_callback(cursive::event::Event::Char(':') , |s| s.select_menubar()); siv.add_global_callback('~', |siv: &mut Cursive| siv.add_layer(FlexiLoggerView::scrollable())); debug!("Starting cursive"); diff --git a/src/tabs.rs b/src/tabs.rs index e0963fe..8132f1c 100644 --- a/src/tabs.rs +++ b/src/tabs.rs @@ -18,7 +18,7 @@ use crate::views::maillist::MaillistView; #[derive(Hash, Eq, PartialEq, Clone, parse_display::Display)] pub enum TabPanelName { - #[display("Query: {}")] + #[display("Query: {0}")] NotmuchQuery(String), #[display("Other")] @@ -51,6 +51,12 @@ impl Tabs { 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 { diff --git a/src/views/main.rs b/src/views/main.rs index c42e417..98b8849 100644 --- a/src/views/main.rs +++ b/src/views/main.rs @@ -39,6 +39,10 @@ impl MainView { Ok(MainView { rt, tabs }.with_name(MAIN_VIEW_NAME)) } + pub fn add_tab_for_query(&mut self, query: &str) -> Result<()> { + self.tabs.add_tab_for_query(self.rt.clone(), query) + } + 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>() diff --git a/src/views/menu.rs b/src/views/menu.rs new file mode 100644 index 0000000..04324ec --- /dev/null +++ b/src/views/menu.rs @@ -0,0 +1,70 @@ +use cursive::Cursive; +use cursive::views::Menubar; +use cursive::views::Dialog; +use cursive::views::EditView; +use cursive::view::Nameable; +use cursive::view::Resizable; +use cursive::menu::MenuTree; +use cursive::menu::MenuItem; + +use crate::views::maillist::MaillistView; +use crate::views::main::MainView; +use crate::views::main::MAIN_VIEW_NAME; + +/// build the menu bar +pub fn init(siv: &mut Cursive) { + let menubar = siv.menubar(); + add_main(menubar); +} + +fn add_main(bar: &mut Menubar) { + bar.add_subtree( + "Muar", + MenuTree::new() + .leaf("Query", menu_command_query) + .delimiter() + .leaf("Quit", menu_command_quit) + ); +} + +fn menu_command_quit(siv: &mut Cursive) { + siv.quit() +} + +fn menu_command_query(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| { + let res = siv.call_on_name(MAIN_VIEW_NAME, move |main_view: &mut MainView| { + main_view.add_tab_for_query(query) + }); + + match res { + Some(Ok(())) => { + siv.pop_layer(); + info!("Querying went fine: {}", query); + }, + + Some(Err(e)) => { + siv.pop_layer(); + error!("Error: {:?}", e); + siv.add_layer(error_dialog_for(e)); + }, + + None => { + siv.pop_layer(); + error!("Failed to get main view"); + siv.add_layer(dialog_for("Failed to get database connection set up")); + }, + } + }) + .with_name("query"); + + siv.add_layer({ + Dialog::around(edit_view) + .title("Query") + .min_width(80) + }) +} diff --git a/src/views/mod.rs b/src/views/mod.rs index 9053bf1..681e5db 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -1,3 +1,4 @@ +pub mod menu; pub mod maillist; pub mod mail; pub mod main; |