diff --git a/CHANGELOG.md b/CHANGELOG.md index 41b94f7418..92387756cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ And please only add new entries to the top of this list, right below the `# Unre - Fix a bug where Wayland would be chosen on Linux even if the user specified `with_x11`. (#3058) - **Breaking:** Moved `ControlFlow` to `EventLoopWindowTarget::set_control_flow()` and `EventLoopWindowTarget::control_flow()`. - **Breaking:** Moved `ControlFlow::Exit` to `EventLoopWindowTarget::exit()` and `EventLoopWindowTarget::exiting()` and removed `ControlFlow::ExitWithCode(_)` entirely. -- On Web, add `EventLoopWindowTargetExtWebSys` and `PollType`, which allows to set different strategies for `ControlFlow::Poll`. By default the Prioritized Task Scheduling API is used, but an option to use `Window.requestIdleCallback` is available as well. Both use `setTimeout()`, with a trick to circumvent throttling to 4ms, as a fallback. +- On Web, add `EventLoopWindowTargetExtWebSys` and `PollStrategy`, which allows to set different strategies for `ControlFlow::Poll`. By default the Prioritized Task Scheduling API is used, but an option to use `Window.requestIdleCallback` is available as well. Both use `setTimeout()`, with a trick to circumvent throttling to 4ms, as a fallback. # 0.29.1-beta diff --git a/src/platform/web.rs b/src/platform/web.rs index 8772a7900d..3b721af441 100644 --- a/src/platform/web.rs +++ b/src/platform/web.rs @@ -138,34 +138,34 @@ impl EventLoopExtWebSys for EventLoop { pub trait EventLoopWindowTargetExtWebSys { /// Sets the strategy for [`ControlFlow::Poll`]. /// - /// See [`PollType`]. + /// See [`PollStrategy`]. /// /// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll - fn set_poll_type(&self, poll_type: PollType); + fn set_poll_strategy(&self, strategy: PollStrategy); /// Gets the strategy for [`ControlFlow::Poll`]. /// - /// See [`PollType`]. + /// See [`PollStrategy`]. /// /// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll - fn poll_type(&self) -> PollType; + fn poll_strategy(&self) -> PollStrategy; } impl EventLoopWindowTargetExtWebSys for EventLoopWindowTarget { #[inline] - fn set_poll_type(&self, poll_type: PollType) { - self.p.set_poll_type(poll_type); + fn set_poll_strategy(&self, strategy: PollStrategy) { + self.p.set_poll_strategy(strategy); } #[inline] - fn poll_type(&self) -> PollType { - self.p.poll_type() + fn poll_strategy(&self) -> PollStrategy { + self.p.poll_strategy() } } /// Strategy used for [`ControlFlow::Poll`](crate::event_loop::ControlFlow::Poll). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum PollType { +pub enum PollStrategy { /// Uses [`Window.requestIdleCallback()`] to queue the next event loop. If not available /// this will fallback to [`setTimeout()`]. /// diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index 7cd0d3e5f5..d82eafbb1a 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -6,7 +6,7 @@ use crate::event::{ WindowEvent, }; use crate::event_loop::{ControlFlow, DeviceEvents}; -use crate::platform::web::PollType; +use crate::platform::web::PollStrategy; use crate::platform_impl::platform::backend::EventListenerHandle; use crate::window::WindowId; @@ -37,7 +37,7 @@ type OnEventHandle = RefCell>>; pub struct Execution { control_flow: Cell, - poll_type: Cell, + poll_strategy: Cell, exit: Cell, runner: RefCell, suspended: Cell, @@ -142,7 +142,7 @@ impl Shared { Shared(Rc::new(Execution { control_flow: Cell::new(ControlFlow::default()), - poll_type: Cell::new(PollType::default()), + poll_strategy: Cell::new(PollStrategy::default()), exit: Cell::new(false), runner: RefCell::new(RunnerEnum::Pending), suspended: Cell::new(false), @@ -638,7 +638,7 @@ impl Shared { let cloned = self.clone(); State::Poll { request: backend::Schedule::new( - self.poll_type(), + self.poll_strategy(), self.window(), move || cloned.poll(), ), @@ -773,12 +773,12 @@ impl Shared { self.0.exit.get() } - pub(crate) fn set_poll_type(&self, poll_type: PollType) { - self.0.poll_type.set(poll_type) + pub(crate) fn set_poll_strategy(&self, strategy: PollStrategy) { + self.0.poll_strategy.set(strategy) } - pub(crate) fn poll_type(&self) -> PollType { - self.0.poll_type.get() + pub(crate) fn poll_strategy(&self) -> PollStrategy { + self.0.poll_strategy.get() } } diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 7af42255ee..b0bfd4755b 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -21,7 +21,7 @@ use crate::event::{ }; use crate::event_loop::{ControlFlow, DeviceEvents}; use crate::keyboard::ModifiersState; -use crate::platform::web::PollType; +use crate::platform::web::PollStrategy; use crate::window::{Theme, WindowId as RootWindowId}; #[derive(Default)] @@ -696,11 +696,11 @@ impl EventLoopWindowTarget { self.runner.exiting() } - pub(crate) fn set_poll_type(&self, poll_type: PollType) { - self.runner.set_poll_type(poll_type) + pub(crate) fn set_poll_strategy(&self, strategy: PollStrategy) { + self.runner.set_poll_strategy(strategy) } - pub(crate) fn poll_type(&self) -> PollType { - self.runner.poll_type() + pub(crate) fn poll_strategy(&self) -> PollStrategy { + self.runner.poll_strategy() } } diff --git a/src/platform_impl/web/web_sys/schedule.rs b/src/platform_impl/web/web_sys/schedule.rs index eec2ab6bda..7810b3fcd5 100644 --- a/src/platform_impl/web/web_sys/schedule.rs +++ b/src/platform_impl/web/web_sys/schedule.rs @@ -6,7 +6,7 @@ use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{AbortController, AbortSignal, MessageChannel, MessagePort}; -use crate::platform::web::PollType; +use crate::platform::web::PollStrategy; #[derive(Debug)] pub struct Schedule { @@ -32,13 +32,13 @@ enum Inner { } impl Schedule { - pub fn new(poll_type: PollType, window: &web_sys::Window, f: F) -> Schedule + pub fn new(strategy: PollStrategy, window: &web_sys::Window, f: F) -> Schedule where F: 'static + FnMut(), { - if poll_type == PollType::Scheduler && has_scheduler_support(window) { + if strategy == PollStrategy::Scheduler && has_scheduler_support(window) { Self::new_scheduler(window, f, None) - } else if poll_type == PollType::IdleCallback && has_idle_callback_support(window) { + } else if strategy == PollStrategy::IdleCallback && has_idle_callback_support(window) { Self::new_idle_callback(window.clone(), f) } else { Self::new_timeout(window.clone(), f, None)