Add Window.requestIdleCallback() support (#3084)

This commit is contained in:
daxpedda 2023-09-07 12:12:35 +02:00 committed by GitHub
parent b99403b1b9
commit 83950acd5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 162 additions and 28 deletions

View file

@ -6,6 +6,7 @@ use crate::event::{
WindowEvent,
};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::platform::web::PollType;
use crate::platform_impl::platform::backend::EventListenerHandle;
use crate::window::WindowId;
@ -36,6 +37,7 @@ type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;
pub struct Execution {
control_flow: Cell<ControlFlow>,
poll_type: Cell<PollType>,
exit: Cell<bool>,
runner: RefCell<RunnerEnum>,
suspended: Cell<bool>,
@ -140,6 +142,7 @@ impl Shared {
Shared(Rc::new(Execution {
control_flow: Cell::new(ControlFlow::default()),
poll_type: Cell::new(PollType::default()),
exit: Cell::new(false),
runner: RefCell::new(RunnerEnum::Pending),
suspended: Cell::new(false),
@ -635,9 +638,9 @@ impl Shared {
let cloned = self.clone();
State::Poll {
request: backend::Schedule::new(
self.window().clone(),
self.poll_type(),
self.window(),
move || cloned.poll(),
None,
),
}
}
@ -658,10 +661,10 @@ impl Shared {
State::WaitUntil {
start,
end,
timeout: backend::Schedule::new(
self.window().clone(),
timeout: backend::Schedule::new_with_duration(
self.window(),
move || cloned.resume_time_reached(start, end),
Some(delay),
delay,
),
}
}
@ -769,6 +772,14 @@ impl Shared {
pub(crate) fn exiting(&self) -> bool {
self.0.exit.get()
}
pub(crate) fn set_poll_type(&self, poll_type: PollType) {
self.0.poll_type.set(poll_type)
}
pub(crate) fn poll_type(&self) -> PollType {
self.0.poll_type.get()
}
}
pub(crate) enum EventWrapper {

View file

@ -21,6 +21,7 @@ use crate::event::{
};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::keyboard::ModifiersState;
use crate::platform::web::PollType;
use crate::window::{Theme, WindowId as RootWindowId};
#[derive(Default)]
@ -694,4 +695,12 @@ impl<T> EventLoopWindowTarget<T> {
pub(crate) fn exiting(&self) -> bool {
self.runner.exiting()
}
pub(crate) fn set_poll_type(&self, poll_type: PollType) {
self.runner.set_poll_type(poll_type)
}
pub(crate) fn poll_type(&self) -> PollType {
self.runner.poll_type()
}
}