From 874a252394f8210aae723941d9c95309271c7f13 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 23 Nov 2019 18:00:00 +0200 Subject: [PATCH] ui: add periodic account connectivity check 1. spawn thread to send ThreadPulses to the main event loop that "parks" until unparked from State 2. State unparks thread if there are accounts that are offline 3. thread sends ThreadPulse and parks again 4. State checks accounts again and so on. --- src/bin.rs | 1 + ui/src/components/mail/listing.rs | 4 ++-- ui/src/components/utilities.rs | 4 ++-- ui/src/state.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index b5786013..1e37dd9e 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -376,6 +376,7 @@ fn run_app() -> Result<()> { state.render(); }, ThreadEvent::Pulse => { + state.check_accounts(); state.redraw(); }, ThreadEvent::ThreadJoin(id) => { diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index 8b4c3471..ab28aba6 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -222,7 +222,6 @@ impl Component for Listing { .dirty_areas .push_back(((mid, get_y(upper_left)), (mid, get_y(bottom_right)))); } - self.dirty = false; if right_component_width == total_cols { if !context.is_online(self.cursor_pos.0) { @@ -278,6 +277,7 @@ impl Component for Listing { } } } + self.dirty = false; } fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool { if match self.component { @@ -468,7 +468,7 @@ impl Component for Listing { self.dirty = true; } UIEvent::Resize => { - self.dirty = true; + self.set_dirty(); } UIEvent::Input(Key::Up) => { let amount = if self.cmd_buf.is_empty() { diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs index d712341b..f3d656c4 100644 --- a/ui/src/components/utilities.rs +++ b/ui/src/components/utilities.rs @@ -1423,9 +1423,9 @@ impl Component for Tabbed { create_box(grid, area); let area = ( pos_inc(upper_left!(area), (3, 2)), - set_x( - bottom_right!(area), + ( get_x(bottom_right!(area)).saturating_sub(3), + get_y(bottom_right!(area)).saturating_sub(1), ), ); let mut children_maps = self.children[self.cursor_pos].get_shortcuts(context); diff --git a/ui/src/state.rs b/ui/src/state.rs index 359eeae8..dd788181 100644 --- a/ui/src/state.rs +++ b/ui/src/state.rs @@ -183,6 +183,7 @@ pub struct State { pub mode: UIMode, components: Vec>, pub context: Context, + timer: thread::JoinHandle<()>, threads: FnvHashMap, thread::JoinHandle<()>)>, } @@ -241,6 +242,22 @@ impl State { .collect::>>()?; accounts.sort_by(|a, b| a.name().cmp(&b.name())); + let timer = { + let sender = sender.clone(); + thread::Builder::new().spawn(move || { + let sender = sender; + loop { + thread::park(); + debug!("unparked"); + + sender.send(ThreadEvent::Pulse).unwrap(); + thread::sleep(std::time::Duration::from_millis(100)); + } + }) + }?; + + timer.thread().unpark(); + let mut s = State { cols, rows, @@ -249,6 +266,7 @@ impl State { child: None, mode: UIMode::Normal, components: Vec::with_capacity(1), + timer, context: Context { accounts, @@ -716,4 +734,17 @@ impl State { fn stdout(&mut self) -> &mut StateStdout { self.stdout.as_mut().unwrap() } + + pub fn check_accounts(&mut self) { + let mut ctr = 0; + for i in 0..self.context.accounts.len() { + if self.context.is_online(i) { + ctr += 1; + } + } + if ctr != self.context.accounts.len() { + debug!("unparking"); + self.timer.thread().unpark(); + } + } }