From 365d004e1ff60e3f6d718db77c848128673db1e6 Mon Sep 17 00:00:00 2001 From: wngr Date: Mon, 7 Dec 2020 17:08:31 +0100 Subject: [PATCH 1/2] feat: Expose `timeout` parameter to the main event loop, such that control flow is passed back to an external caller. This provides more flexibility to the API user. Related #43 #38 --- examples/systray-example.rs | 2 +- src/lib.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/systray-example.rs b/examples/systray-example.rs index 85c1a6e..b976b0a 100644 --- a/examples/systray-example.rs +++ b/examples/systray-example.rs @@ -33,7 +33,7 @@ fn main() -> Result<(), systray::Error> { })?; println!("Waiting on message!"); - app.wait_for_message()?; + app.wait_for_message(None)?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 36c6dbd..7e83a33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,8 @@ pub mod api; use std::{ collections::HashMap, error, fmt, - sync::mpsc::{channel, Receiver}, + sync::mpsc::{channel, Receiver, RecvTimeoutError}, + time::Duration, }; type BoxedError = Box; @@ -133,12 +134,17 @@ impl Application { self.window.quit() } - pub fn wait_for_message(&mut self) -> Result<(), Error> { + pub fn wait_for_message(&mut self, timeout: Option) -> Result<(), Error> { loop { let msg; - match self.rx.recv() { + match self + .rx + .recv_timeout(timeout.unwrap_or_else(|| Duration::new(u64::MAX, 0))) + { Ok(m) => msg = m, - Err(_) => { + // Yield and wait for the next poll + Err(RecvTimeoutError::Timeout) => break, + Err(RecvTimeoutError::Disconnected) => { self.quit(); break; } From a2dc79dc2e03460065dcd8e4db8d6c4491fa2d9d Mon Sep 17 00:00:00 2001 From: wngr Date: Tue, 8 Dec 2020 12:14:12 +0100 Subject: [PATCH 2/2] add dedicated `try_wait` method --- examples/systray-example.rs | 2 +- src/lib.rs | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/systray-example.rs b/examples/systray-example.rs index b976b0a..85c1a6e 100644 --- a/examples/systray-example.rs +++ b/examples/systray-example.rs @@ -33,7 +33,7 @@ fn main() -> Result<(), systray::Error> { })?; println!("Waiting on message!"); - app.wait_for_message(None)?; + app.wait_for_message()?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 7e83a33..3a2641d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub enum Error { NotImplementedError, UnknownError, Error(BoxedError), + TimeoutError, } impl From for Error { @@ -39,6 +40,7 @@ impl fmt::Display for Error { NotImplementedError => write!(f, "Functionality is not implemented yet"), UnknownError => write!(f, "Unknown error occurrred"), Error(ref e) => write!(f, "Error: {}", e), + TimeoutError => write!(f, "Timeout"), } } } @@ -134,16 +136,17 @@ impl Application { self.window.quit() } - pub fn wait_for_message(&mut self, timeout: Option) -> Result<(), Error> { + pub fn wait_for_message(&mut self) -> Result<(), Error> { + self.try_wait(Duration::new(u64::MAX, 0)) + } + + pub fn try_wait(&mut self, timeout: Duration) -> Result<(), Error> { loop { let msg; - match self - .rx - .recv_timeout(timeout.unwrap_or_else(|| Duration::new(u64::MAX, 0))) - { + match self.rx.recv_timeout(timeout) { Ok(m) => msg = m, // Yield and wait for the next poll - Err(RecvTimeoutError::Timeout) => break, + Err(RecvTimeoutError::Timeout) => return Err(Error::TimeoutError), Err(RecvTimeoutError::Disconnected) => { self.quit(); break;