Avoid double borrow panic when resizing windows

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-08-27 15:03:37 -07:00
parent bc63fca8d7
commit 936af9bc5e
4 changed files with 40 additions and 28 deletions

View File

@ -1217,16 +1217,8 @@ impl MutableAppContext {
{
let mut app = self.upgrade();
let presenter = presenter.clone();
window.on_resize(Box::new(move |window| {
app.update(|cx| {
let scene = presenter.borrow_mut().build_scene(
window.size(),
window.scale_factor(),
cx,
);
window.present_scene(scene);
})
window.on_resize(Box::new(move || {
app.update(|cx| cx.resize_window(window_id))
}));
}
@ -1391,6 +1383,13 @@ impl MutableAppContext {
Effect::Focus { window_id, view_id } => {
self.focus(window_id, view_id);
}
Effect::ResizeWindow { window_id } => {
if let Some(window) = self.cx.windows.get_mut(&window_id) {
window
.invalidation
.get_or_insert(WindowInvalidation::default());
}
}
Effect::RefreshWindows => {
refreshing = true;
}
@ -1439,6 +1438,11 @@ impl MutableAppContext {
}
}
fn resize_window(&mut self, window_id: usize) {
self.pending_effects
.push_back(Effect::ResizeWindow { window_id });
}
pub fn refresh_windows(&mut self) {
self.pending_effects.push_back(Effect::RefreshWindows);
}
@ -1794,6 +1798,9 @@ pub enum Effect {
window_id: usize,
view_id: usize,
},
ResizeWindow {
window_id: usize,
},
RefreshWindows,
}
@ -1818,6 +1825,10 @@ impl Debug for Effect {
.field("window_id", window_id)
.field("view_id", view_id)
.finish(),
Effect::ResizeWindow { window_id } => f
.debug_struct("Effect::RefreshWindow")
.field("window_id", window_id)
.finish(),
Effect::RefreshWindows => f.debug_struct("Effect::FullViewRefresh").finish(),
}
}

View File

@ -80,7 +80,7 @@ pub trait Dispatcher: Send + Sync {
pub trait Window: WindowContext {
fn as_any_mut(&mut self) -> &mut dyn Any;
fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
fn on_resize(&mut self, callback: Box<dyn FnMut()>);
fn on_close(&mut self, callback: Box<dyn FnOnce()>);
fn prompt(
&self,

View File

@ -130,7 +130,7 @@ struct WindowState {
id: usize,
native_window: id,
event_callback: Option<Box<dyn FnMut(Event)>>,
resize_callback: Option<Box<dyn FnMut(&mut dyn platform::WindowContext)>>,
resize_callback: Option<Box<dyn FnMut()>>,
close_callback: Option<Box<dyn FnOnce()>>,
synthetic_drag_counter: usize,
executor: Rc<executor::Foreground>,
@ -280,7 +280,7 @@ impl platform::Window for Window {
self.0.as_ref().borrow_mut().event_callback = Some(callback);
}
fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn platform::WindowContext)>) {
fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
self.0.as_ref().borrow_mut().resize_callback = Some(callback);
}
@ -489,24 +489,24 @@ extern "C" fn make_backing_layer(this: &Object, _: Sel) -> id {
extern "C" fn view_did_change_backing_properties(this: &Object, _: Sel) {
let window_state = unsafe { get_window_state(this) };
let mut window_state = window_state.as_ref().borrow_mut();
let mut window_state_borrow = window_state.as_ref().borrow_mut();
unsafe {
let _: () =
msg_send![window_state.layer, setContentsScale: window_state.scale_factor() as f64];
let _: () = msg_send![window_state_borrow.layer, setContentsScale: window_state_borrow.scale_factor() as f64];
}
if let Some(mut callback) = window_state.resize_callback.take() {
callback(&mut *window_state);
window_state.resize_callback = Some(callback);
if let Some(mut callback) = window_state_borrow.resize_callback.take() {
drop(window_state_borrow);
callback();
window_state.as_ref().borrow_mut().resize_callback = Some(callback);
};
}
extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) {
let window_state = unsafe { get_window_state(this) };
let mut window_state = window_state.as_ref().borrow_mut();
let mut window_state_borrow = window_state.as_ref().borrow_mut();
if window_state.size() == vec2f(size.width as f32, size.height as f32) {
if window_state_borrow.size() == vec2f(size.width as f32, size.height as f32) {
return;
}
@ -514,19 +514,20 @@ extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) {
let _: () = msg_send![super(this, class!(NSView)), setFrameSize: size];
}
let scale_factor = window_state.scale_factor() as f64;
let scale_factor = window_state_borrow.scale_factor() as f64;
let drawable_size: NSSize = NSSize {
width: size.width * scale_factor,
height: size.height * scale_factor,
};
unsafe {
let _: () = msg_send![window_state.layer, setDrawableSize: drawable_size];
let _: () = msg_send![window_state_borrow.layer, setDrawableSize: drawable_size];
}
if let Some(mut callback) = window_state.resize_callback.take() {
callback(&mut *window_state);
window_state.resize_callback = Some(callback);
if let Some(mut callback) = window_state_borrow.resize_callback.take() {
drop(window_state_borrow);
callback();
window_state.borrow_mut().resize_callback = Some(callback);
};
}

View File

@ -29,7 +29,7 @@ pub struct Window {
scale_factor: f32,
current_scene: Option<crate::Scene>,
event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
resize_handlers: Vec<Box<dyn FnMut(&mut dyn super::WindowContext)>>,
resize_handlers: Vec<Box<dyn FnMut()>>,
close_handlers: Vec<Box<dyn FnOnce()>>,
pub(crate) last_prompt: RefCell<Option<Box<dyn FnOnce(usize)>>>,
}
@ -189,7 +189,7 @@ impl super::Window for Window {
self.event_handlers.push(callback);
}
fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
self.resize_handlers.push(callback);
}