Improve media queries take 2

This commit is contained in:
dAxpeDDa 2023-06-09 05:58:58 +02:00 committed by daxpedda
parent 07d39abddd
commit f7a400ddf6

View file

@ -21,7 +21,7 @@ impl ScaleChangeDetector {
struct ScaleChangeDetectorInternal { struct ScaleChangeDetectorInternal {
window: web_sys::Window, window: web_sys::Window,
callback: Box<dyn FnMut(ScaleChangeArgs)>, callback: Box<dyn FnMut(ScaleChangeArgs)>,
mql: Option<MediaQueryListHandle>, mql: MediaQueryListHandle,
last_scale: f64, last_scale: f64,
} }
@ -31,24 +31,21 @@ impl ScaleChangeDetectorInternal {
F: 'static + FnMut(ScaleChangeArgs), F: 'static + FnMut(ScaleChangeArgs),
{ {
let current_scale = super::scale_factor(&window); let current_scale = super::scale_factor(&window);
let new_self = Rc::new(RefCell::new(Self { Rc::new_cyclic(|weak_self| {
window: window.clone(), let weak_self = weak_self.clone();
callback: Box::new(handler),
mql: None,
last_scale: current_scale,
}));
let weak_self = Rc::downgrade(&new_self);
let mql = Self::create_mql(&window, move |mql| { let mql = Self::create_mql(&window, move |mql| {
if let Some(rc_self) = weak_self.upgrade() { if let Some(rc_self) = weak_self.upgrade() {
Self::handler(rc_self, mql); Self::handler(rc_self, mql);
} }
}); });
{
let mut borrowed_self = new_self.borrow_mut(); RefCell::new(Self {
borrowed_self.mql = Some(mql); window,
} callback: Box::new(handler),
new_self mql,
last_scale: current_scale,
})
})
} }
fn create_mql<F>(window: &web_sys::Window, closure: F) -> MediaQueryListHandle fn create_mql<F>(window: &web_sys::Window, closure: F) -> MediaQueryListHandle
@ -65,7 +62,7 @@ impl ScaleChangeDetectorInternal {
assert!( assert!(
mql.mql().matches(), mql.mql().matches(),
"created media query doesn't match, {current_scale} != {}", "created media query doesn't match, {current_scale} != {}",
super::scale_factor(window,) super::scale_factor(window)
); );
mql mql
} }
@ -75,21 +72,31 @@ impl ScaleChangeDetectorInternal {
let mut this = this.borrow_mut(); let mut this = this.borrow_mut();
let old_scale = this.last_scale; let old_scale = this.last_scale;
let new_scale = super::scale_factor(&this.window); let new_scale = super::scale_factor(&this.window);
// TODO: confirm/reproduce this problem, see:
// <https://github.com/rust-windowing/winit/issues/2597>.
// This should never happen, but if it does then apparently the scale factor didn't change.
if mql.matches() {
warn!(
"media query tracking scale factor was triggered without a change:\n\
Media Query: {}\n\
Current Scale: {new_scale}",
mql.media(),
);
return;
}
(this.callback)(ScaleChangeArgs { (this.callback)(ScaleChangeArgs {
old_scale, old_scale,
new_scale, new_scale,
}); });
// If this matches, then the scale factor is back to it's
// old value again, so we won't need to update the query.
if !mql.matches() {
let new_mql = Self::create_mql(&this.window, move |mql| { let new_mql = Self::create_mql(&this.window, move |mql| {
if let Some(rc_self) = weak_self.upgrade() { if let Some(rc_self) = weak_self.upgrade() {
Self::handler(rc_self, mql); Self::handler(rc_self, mql);
} }
}); });
this.mql = Some(new_mql); this.mql = new_mql;
this.last_scale = new_scale; this.last_scale = new_scale;
} }
}
} }