From 5ddd68ad9f32ae4be4a6ec5f15f6724d7aa8a2a5 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 15 Sep 2019 09:41:52 +0300 Subject: [PATCH] ui: add statusbar change with tab switch and updates --- ui/src/components.rs | 4 +++ ui/src/components/mail/listing.rs | 57 +++++++++++++++++++++++++++++++ ui/src/components/utilities.rs | 35 +++++++------------ ui/src/types.rs | 1 + 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/ui/src/components.rs b/ui/src/components.rs index fd8faaf80..bc56f14ac 100644 --- a/ui/src/components.rs +++ b/ui/src/components.rs @@ -100,6 +100,10 @@ pub trait Component: Display + Debug + Send { fn get_shortcuts(&self, _context: &Context) -> ShortcutMaps { Default::default() } + + fn get_status(&self, _context: &Context) -> Option { + None + } } /* diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index 9533b94bc..2feb7259c 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -295,6 +295,11 @@ impl Component for Listing { context .replies .push_back(UIEvent::RefreshMailbox((self.cursor_pos.0, folder_hash))); + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.get_status(context).unwrap(), + ))); return true; } UIEvent::Input(ref k) @@ -335,6 +340,11 @@ impl Component for Listing { context .replies .push_back(UIEvent::RefreshMailbox((self.cursor_pos.0, folder_hash))); + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.get_status(context).unwrap(), + ))); return true; } UIEvent::Action(ref action) => match action { @@ -365,6 +375,11 @@ impl Component for Listing { .position(|&h| h == folder_hash) .unwrap_or(0), ); + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.get_status(context).unwrap(), + ))); self.dirty = true; } UIEvent::ChangeMode(UIMode::Normal) => { @@ -385,9 +400,19 @@ impl Component for Listing { } UIEvent::StartupCheck(_) => { self.dirty = true; + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.get_status(context).unwrap(), + ))); } UIEvent::MailboxUpdate(_) => { self.dirty = true; + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.get_status(context).unwrap(), + ))); } _ => {} } @@ -489,6 +514,38 @@ impl Component for Listing { Conversations(ref mut l) => l.set_id(id), } } + + fn get_status(&self, context: &Context) -> Option { + Some({ + let folder_hash = if let Some(h) = context.accounts[self.cursor_pos.0] + .folders_order + .get(self.cursor_pos.1) + { + *h + } else { + return Some(String::new()); + }; + if !context.accounts[self.cursor_pos.0].folders[&folder_hash].is_available() { + return Some(String::new()); + } + let account = &context.accounts[self.cursor_pos.0]; + let m = if account[self.cursor_pos.1].is_available() { + account[self.cursor_pos.1].unwrap() + } else { + return Some(String::new()); + }; + format!( + "Mailbox: {}, Messages: {}, New: {}", + m.folder.name(), + m.envelopes.len(), + m.envelopes + .iter() + .map(|h| &account.collection[&h]) + .filter(|e| !e.is_seen()) + .count() + ) + }) + } } impl From for ListingComponent { diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index 5c66c8955..4b6763858 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -929,29 +929,7 @@ impl Component for StatusBar { return true; } - match &event { - UIEvent::RefreshMailbox((ref idx_a, ref idx_f)) => { - match context.accounts[*idx_a].status(*idx_f) { - Ok(_) => {} - Err(_) => { - return false; - } - } - let account = &context.accounts[*idx_a]; - let m = &account[*idx_f].unwrap(); - self.status = format!( - "{} | Mailbox: {}, Messages: {}, New: {}", - self.mode, - m.folder.name(), - m.envelopes.len(), - m.envelopes - .iter() - .map(|h| &account.collection[&h]) - .filter(|e| !e.is_seen()) - .count() - ); - self.dirty = true; - } + match event { UIEvent::ChangeMode(m) => { let offset = self.status.find('|').unwrap_or_else(|| self.status.len()); self.status.replace_range(..offset, &format!("{} ", m)); @@ -1038,6 +1016,10 @@ impl Component for StatusBar { self.display_buffer = s.clone(); self.dirty = true; } + UIEvent::StatusEvent(StatusEvent::UpdateStatus(ref mut s)) => { + self.status = format!("{} | {}", self.mode, std::mem::replace(s, String::new())); + self.dirty = true; + } _ => {} } false @@ -1321,6 +1303,13 @@ impl Component for Tabbed { match *event { UIEvent::Input(Key::Char('T')) => { self.cursor_pos = (self.cursor_pos + 1) % self.children.len(); + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus( + self.children[self.cursor_pos] + .get_status(context) + .unwrap_or_default(), + ))); self.set_dirty(); return true; } diff --git a/ui/src/types.rs b/ui/src/types.rs index 174fa9ad4..24e5ba252 100644 --- a/ui/src/types.rs +++ b/ui/src/types.rs @@ -38,6 +38,7 @@ pub enum StatusEvent { DisplayMessage(String), BufClear, BufSet(String), + UpdateStatus(String), } /// `ThreadEvent` encapsulates all of the possible values we need to transfer between our threads