Web: fix crash InnerSizeWriter::request_inner_size() (#3727)

This commit is contained in:
daxpedda 2024-06-12 00:22:03 +02:00 committed by GitHub
parent 39bc139500
commit 3b4e064a07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 28 deletions

View file

@ -50,6 +50,7 @@ changelog entry.
- On Web, fix `EventLoopProxy::send_event()` triggering event loop immediately - On Web, fix `EventLoopProxy::send_event()` triggering event loop immediately
when not called from inside the event loop. Now queues a microtask instead. when not called from inside the event loop. Now queues a microtask instead.
- On Web, stop overwriting default cursor with `CursorIcon::Default`. - On Web, stop overwriting default cursor with `CursorIcon::Default`.
- On Web, prevent crash when using `InnerSizeWriter::request_inner_size()`.
### Removed ### Removed

View file

@ -427,8 +427,8 @@ impl Canvas {
pub(crate) fn on_resize_scale<S, R>(&mut self, scale_handler: S, size_handler: R) pub(crate) fn on_resize_scale<S, R>(&mut self, scale_handler: S, size_handler: R)
where where
S: 'static + FnMut(PhysicalSize<u32>, f64), S: 'static + Fn(PhysicalSize<u32>, f64),
R: 'static + FnMut(PhysicalSize<u32>), R: 'static + Fn(PhysicalSize<u32>),
{ {
self.on_resize_scale = Some(ResizeScaleHandle::new( self.on_resize_scale = Some(ResizeScaleHandle::new(
self.window().clone(), self.window().clone(),

View file

@ -16,7 +16,7 @@ use super::media_query_handle::MediaQueryListHandle;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::rc::Rc; use std::rc::Rc;
pub struct ResizeScaleHandle(Rc<RefCell<ResizeScaleInternal>>); pub struct ResizeScaleHandle(Rc<ResizeScaleInternal>);
impl ResizeScaleHandle { impl ResizeScaleHandle {
pub(crate) fn new<S, R>( pub(crate) fn new<S, R>(
@ -28,8 +28,8 @@ impl ResizeScaleHandle {
resize_handler: R, resize_handler: R,
) -> Self ) -> Self
where where
S: 'static + FnMut(PhysicalSize<u32>, f64), S: 'static + Fn(PhysicalSize<u32>, f64),
R: 'static + FnMut(PhysicalSize<u32>), R: 'static + Fn(PhysicalSize<u32>),
{ {
Self(ResizeScaleInternal::new( Self(ResizeScaleInternal::new(
window, window,
@ -42,7 +42,7 @@ impl ResizeScaleHandle {
} }
pub(crate) fn notify_resize(&self) { pub(crate) fn notify_resize(&self) {
self.0.borrow_mut().notify() self.0.notify()
} }
} }
@ -53,11 +53,11 @@ struct ResizeScaleInternal {
document: Document, document: Document,
canvas: HtmlCanvasElement, canvas: HtmlCanvasElement,
style: Style, style: Style,
mql: MediaQueryListHandle, mql: RefCell<MediaQueryListHandle>,
observer: ResizeObserver, observer: ResizeObserver,
_observer_closure: Closure<dyn FnMut(Array, ResizeObserver)>, _observer_closure: Closure<dyn FnMut(Array, ResizeObserver)>,
scale_handler: Box<dyn FnMut(PhysicalSize<u32>, f64)>, scale_handler: Box<dyn Fn(PhysicalSize<u32>, f64)>,
resize_handler: Box<dyn FnMut(PhysicalSize<u32>)>, resize_handler: Box<dyn Fn(PhysicalSize<u32>)>,
notify_scale: Cell<bool>, notify_scale: Cell<bool>,
} }
@ -69,12 +69,12 @@ impl ResizeScaleInternal {
style: Style, style: Style,
scale_handler: S, scale_handler: S,
resize_handler: R, resize_handler: R,
) -> Rc<RefCell<Self>> ) -> Rc<Self>
where where
S: 'static + FnMut(PhysicalSize<u32>, f64), S: 'static + Fn(PhysicalSize<u32>, f64),
R: 'static + FnMut(PhysicalSize<u32>), R: 'static + Fn(PhysicalSize<u32>),
{ {
Rc::<RefCell<ResizeScaleInternal>>::new_cyclic(|weak_self| { Rc::<ResizeScaleInternal>::new_cyclic(|weak_self| {
let mql = Self::create_mql(&window, { let mql = Self::create_mql(&window, {
let weak_self = weak_self.clone(); let weak_self = weak_self.clone();
move |mql| { move |mql| {
@ -86,9 +86,7 @@ impl ResizeScaleInternal {
let weak_self = weak_self.clone(); let weak_self = weak_self.clone();
let observer_closure = Closure::new(move |entries: Array, _| { let observer_closure = Closure::new(move |entries: Array, _| {
if let Some(rc_self) = weak_self.upgrade() { if let Some(this) = weak_self.upgrade() {
let mut this = rc_self.borrow_mut();
let size = this.process_entry(entries); let size = this.process_entry(entries);
if this.notify_scale.replace(false) { if this.notify_scale.replace(false) {
@ -101,18 +99,18 @@ impl ResizeScaleInternal {
}); });
let observer = Self::create_observer(&canvas, observer_closure.as_ref()); let observer = Self::create_observer(&canvas, observer_closure.as_ref());
RefCell::new(Self { Self {
window, window,
document, document,
canvas, canvas,
style, style,
mql, mql: RefCell::new(mql),
observer, observer,
_observer_closure: observer_closure, _observer_closure: observer_closure,
scale_handler: Box::new(scale_handler), scale_handler: Box::new(scale_handler),
resize_handler: Box::new(resize_handler), resize_handler: Box::new(resize_handler),
notify_scale: Cell::new(false), notify_scale: Cell::new(false),
}) }
}) })
} }
@ -152,7 +150,7 @@ impl ResizeScaleInternal {
observer observer
} }
fn notify(&mut self) { fn notify(&self) {
if !self.document.contains(Some(&self.canvas)) || self.style.get("display") == "none" { if !self.document.contains(Some(&self.canvas)) || self.style.get("display") == "none" {
let size = PhysicalSize::new(0, 0); let size = PhysicalSize::new(0, 0);
@ -200,10 +198,9 @@ impl ResizeScaleInternal {
} }
} }
fn handle_scale(this: Rc<RefCell<Self>>, mql: &MediaQueryList) { fn handle_scale(self: Rc<Self>, mql: &MediaQueryList) {
let weak_self = Rc::downgrade(&this); let weak_self = Rc::downgrade(&self);
let mut this = this.borrow_mut(); let scale = super::scale_factor(&self.window);
let scale = super::scale_factor(&this.window);
// TODO: confirm/reproduce this problem, see: // TODO: confirm/reproduce this problem, see:
// <https://github.com/rust-windowing/winit/issues/2597>. // <https://github.com/rust-windowing/winit/issues/2597>.
@ -217,15 +214,15 @@ impl ResizeScaleInternal {
return; return;
} }
let new_mql = Self::create_mql(&this.window, move |mql| { let new_mql = Self::create_mql(&self.window, move |mql| {
if let Some(rc_self) = weak_self.upgrade() { if let Some(rc_self) = weak_self.upgrade() {
Self::handle_scale(rc_self, mql); Self::handle_scale(rc_self, mql);
} }
}); });
this.mql = new_mql; self.mql.replace(new_mql);
this.notify_scale.set(true); self.notify_scale.set(true);
this.notify(); self.notify();
} }
fn process_entry(&self, entries: Array) -> PhysicalSize<u32> { fn process_entry(&self, entries: Array) -> PhysicalSize<u32> {