Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
feat: Expose timeout parameter to the main event loop, such that co…
Browse files Browse the repository at this point in the history
…ntrol flow

is passed back to an external caller. This provides more flexibility to the API
user.

Related #43 #38
  • Loading branch information
wngr committed Dec 7, 2020
1 parent cbddc6e commit 365d004
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/systray-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() -> Result<(), systray::Error> {
})?;

println!("Waiting on message!");
app.wait_for_message()?;
app.wait_for_message(None)?;
Ok(())
}

Expand Down
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -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<Duration>) -> 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;
}
Expand Down

0 comments on commit 365d004

Please sign in to comment.