Make canvas in WindowBuilder safe (#3320)

This commit is contained in:
daxpedda 2023-12-26 01:22:10 +01:00 committed by GitHub
parent 843d7904d6
commit e0fea25b06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 214 additions and 169 deletions

View file

@ -1,3 +1,4 @@
use super::super::main_thread::MainThreadMarker;
use super::super::DeviceId;
use super::{backend, state::State};
use crate::dpi::PhysicalSize;
@ -37,6 +38,7 @@ impl Clone for Shared {
type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;
pub struct Execution {
main_thread: MainThreadMarker,
proxy_spawner: WakerSpawner<Weak<Self>>,
control_flow: Cell<ControlFlow>,
poll_strategy: Cell<PollStrategy>,
@ -143,13 +145,14 @@ impl Runner {
impl Shared {
pub fn new() -> Self {
let main_thread = MainThreadMarker::new().expect("only callable from inside the `Window`");
#[allow(clippy::disallowed_methods)]
let window = web_sys::window().expect("only callable from inside the `Window`");
#[allow(clippy::disallowed_methods)]
let document = window.document().expect("Failed to obtain document");
Shared(Rc::<Execution>::new_cyclic(|weak| {
let proxy_spawner = WakerSpawner::new(weak.clone(), |runner, count| {
let proxy_spawner = WakerSpawner::new(main_thread, weak.clone(), |runner, count| {
if let Some(runner) = runner.upgrade() {
Shared(runner).send_events(iter::repeat(Event::UserEvent(())).take(count))
}
@ -157,6 +160,7 @@ impl Shared {
.expect("`EventLoop` has to be created in the main thread");
Execution {
main_thread,
proxy_spawner,
control_flow: Cell::new(ControlFlow::default()),
poll_strategy: Cell::new(PollStrategy::default()),
@ -184,6 +188,10 @@ impl Shared {
}))
}
pub fn main_thread(&self) -> MainThreadMarker {
self.0.main_thread
}
pub fn window(&self) -> &web_sys::Window {
&self.0.window
}