From b07db29a19fdd3f28134f9b56b7675cf8f1410e0 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 22 Sep 2019 11:00:05 +0300 Subject: [PATCH] ui: add timer tick every 300ms Check for pending events in the main process by receiving a timer event every 300ms. This way loaded folders or received emails will get recognized even if the appropriate informing signals got lost. --- src/bin.rs | 35 ++++++++++++++++++++++++++--------- ui/src/state.rs | 5 +++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index b00be789..35acf63f 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -45,12 +45,24 @@ use xdg; fn notify( signals: &[c_int], + sender: crossbeam::channel::Sender, ) -> std::result::Result, std::io::Error> { let (s, r) = crossbeam::channel::bounded(100); let signals = signal_hook::iterator::Signals::new(signals)?; std::thread::spawn(move || { - for signal in signals.forever() { - s.send(signal).unwrap(); + let mut ctr = 0; + loop { + ctr %= 3; + if ctr == 0 { + sender.send(ThreadEvent::Pulse).unwrap(); + } + + for signal in signals.pending() { + s.send(signal).unwrap(); + } + + std::thread::sleep(std::time::Duration::from_millis(100)); + ctr += 1; } }); Ok(r) @@ -175,18 +187,19 @@ fn main() -> std::result::Result<(), std::io::Error> { std::env::set_var("MELI_CONFIG", config_location); } + /* Create the application State. */ + let mut state = State::new(); + + let receiver = state.receiver(); + let sender = state.sender(); + /* Catch SIGWINCH to handle terminal resizing */ let signals = &[ /* Catch SIGWINCH to handle terminal resizing */ signal_hook::SIGWINCH, ]; - let signal_recvr = notify(signals)?; - - /* Create the application State. This is the 'System' part of an ECS architecture */ - let mut state = State::new(); - - let receiver = state.receiver(); + let signal_recvr = notify(signals, sender)?; /* Register some reasonably useful interfaces */ let window = Box::new(Tabbed::new(vec![ @@ -219,7 +232,11 @@ fn main() -> std::result::Result<(), std::io::Error> { /* Poll on all channels. Currently we have the input channel for stdin, watching events and the signal watcher. */ crossbeam::select! { recv(receiver) -> r => { - match debug!(r.unwrap()) { + match r { + Ok(ThreadEvent::Pulse) => {}, + _ => {debug!(&r);} + } + match r.unwrap() { ThreadEvent::Input(Key::Ctrl('z')) => { state.switch_to_main_screen(); //_thread_handler.join().expect("Couldn't join on the associated thread"); diff --git a/ui/src/state.rs b/ui/src/state.rs index 63a82285..27bcdfe2 100644 --- a/ui/src/state.rs +++ b/ui/src/state.rs @@ -365,6 +365,11 @@ impl State { pub fn receiver(&self) -> Receiver { self.context.receiver.clone() } + + pub fn sender(&self) -> Sender { + self.context.sender.clone() + } + pub fn restore_input(&mut self) { self.context.restore_input(); }