From 7d626d9dfd237c2065493163afc166ab1f196b1d Mon Sep 17 00:00:00 2001 From: John Nunley Date: Mon, 16 Jan 2023 01:14:09 +0000 Subject: [PATCH] Add a function for waiting on a `Duration` --- CHANGELOG.md | 1 + src/event_loop.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 125e367b..d4695817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ And please only add new entries to the top of this list, right below the `# Unre - **Breaking:** On Android, switched to using [`android-activity`](https://github.com/rib/android-activity) crate as a glue layer instead of [`ndk-glue`](https://github.com/rust-windowing/android-ndk-rs/tree/master/ndk-glue). See [README.md#Android](https://github.com/rust-windowing/winit#Android) for more details. ([#2444](https://github.com/rust-windowing/winit/pull/2444)) - **Breaking:** Removed support for `raw-window-handle` version `0.4` - On Wayland, `RedrawRequested` not emitted during resize. +- Add a `set_wait_timeout` function to `ControlFlow` to allow waiting for a `Duration`. - **Breaking:** Remove the unstable `xlib_xconnection()` function from the private interface. - Added Orbital support for Redox OS - On X11, added `drag_resize_window` method. diff --git a/src/event_loop.rs b/src/event_loop.rs index 0b0c4ecf..abdd47ec 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -11,7 +11,7 @@ use std::marker::PhantomData; use std::ops::Deref; use std::{error, fmt}; -use instant::Instant; +use instant::{Duration, Instant}; use once_cell::sync::OnceCell; use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle}; @@ -163,8 +163,10 @@ pub enum ControlFlow { /// example when the scaling of the page has changed. This should be treated as an implementation /// detail which should not be relied on. Poll, + /// When the current loop iteration finishes, suspend the thread until another event arrives. Wait, + /// When the current loop iteration finishes, suspend the thread until either another event /// arrives or the given time is reached. /// @@ -174,6 +176,7 @@ pub enum ControlFlow { /// /// [`Poll`]: Self::Poll WaitUntil(Instant), + /// Send a [`LoopDestroyed`] event and stop the event loop. This variant is *sticky* - once set, /// `control_flow` cannot be changed from `ExitWithCode`, and any future attempts to do so will /// result in the `control_flow` parameter being reset to `ExitWithCode`. @@ -221,6 +224,20 @@ impl ControlFlow { *self = Self::WaitUntil(instant); } + /// Sets this to wait until a timeout has expired. + /// + /// In most cases, this is set to [`WaitUntil`]. However, if the timeout overflows, it is + /// instead set to [`Wait`]. + /// + /// [`WaitUntil`]: Self::WaitUntil + /// [`Wait`]: Self::Wait + pub fn set_wait_timeout(&mut self, timeout: Duration) { + match Instant::now().checked_add(timeout) { + Some(instant) => self.set_wait_until(instant), + None => self.set_wait(), + } + } + /// Sets this to [`ExitWithCode`]`(code)`. /// /// [`ExitWithCode`]: Self::ExitWithCode