Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Remove unneeded weak Rc cycle in `WindowsWindowInner` (#42119)
Lukas Wirtha7fac65d62c1
diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rsindex 2e391b6e442..098c0780b2c 100644--- a/crates/gpui/src/gpui.rs+++ b/crates/gpui/src/gpui.rs@@ -107,7 +107,7 @@ pub use util::{FutureExt, Timeout, arc_cow::ArcCow};pub use view::*;pub use window::*;-use std::{any::Any, borrow::BorrowMut, future::Future};+use std::{any::Any, future::Future};use taffy::TaffyLayoutEngine;/// The context trait, allows the different contexts in GPUI to be used@@ -253,7 +253,7 @@ pub trait BorrowAppContext {impl<C> BorrowAppContext for Cwhere- C: BorrowMut<App>,+ C: std::borrow::BorrowMut<App>,{fn set_global<G: Global>(&mut self, global: G) {self.borrow_mut().set_global(global)diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rsindex ea1027f826a..f0e0beb7ca0 100644--- a/crates/gpui/src/platform/windows/window.rs+++ b/crates/gpui/src/platform/windows/window.rs@@ -61,7 +61,6 @@ pub struct WindowsWindowState {pub(crate) struct WindowsWindowInner {hwnd: HWND,- pub(super) this: Weak<Self>,drop_target_helper: IDropTargetHelper,pub(crate) state: RefCell<WindowsWindowState>,pub(crate) system_settings: RefCell<WindowsSystemSettings>,@@ -215,9 +214,8 @@ impl WindowsWindowInner {context.disable_direct_composition,)?);- Ok(Rc::new_cyclic(|this| Self {+ Ok(Rc::new(Self {hwnd,- this: this.clone(),drop_target_helper: context.drop_target_helper.clone(),state,handle: context.handle,@@ -232,11 +230,8 @@ impl WindowsWindowInner {}))}- fn toggle_fullscreen(&self) {- let Some(this) = self.this.upgrade() else {- log::error!("Unable to toggle fullscreen: window has been dropped");- return;- };+ fn toggle_fullscreen(self: &Rc<Self>) {+ let this = self.clone();self.executor.spawn(async move {let mut lock = this.state.borrow_mut();@@ -246,36 +241,42 @@ impl WindowsWindowInner {y,cx,cy,- } = if let Some(state) = lock.fullscreen.take() {- state- } else {- let (window_bounds, _) = lock.calculate_window_bounds();- lock.fullscreen_restore_bounds = window_bounds;- let style = WINDOW_STYLE(unsafe { get_window_long(this.hwnd, GWL_STYLE) } as _);- let mut rc = RECT::default();- unsafe { GetWindowRect(this.hwnd, &mut rc) }- .context("failed to get window rect")- .log_err();- let _ = lock.fullscreen.insert(StyleAndBounds {- style,- x: rc.left,- y: rc.top,- cx: rc.right - rc.left,- cy: rc.bottom - rc.top,- });- let style = style- & !(WS_THICKFRAME- | WS_SYSMENU- | WS_MAXIMIZEBOX- | WS_MINIMIZEBOX- | WS_CAPTION);- let physical_bounds = lock.display.physical_bounds();- StyleAndBounds {- style,- x: physical_bounds.left().0,- y: physical_bounds.top().0,- cx: physical_bounds.size.width.0,- cy: physical_bounds.size.height.0,+ } = match lock.fullscreen.take() {+ Some(state) => state,+ None => {+ let (window_bounds, _) = lock.calculate_window_bounds();+ lock.fullscreen_restore_bounds = window_bounds;+ drop(lock);++ let style =+ WINDOW_STYLE(unsafe { get_window_long(this.hwnd, GWL_STYLE) } as _);+ let mut rc = RECT::default();+ unsafe { GetWindowRect(this.hwnd, &mut rc) }+ .context("failed to get window rect")+ .log_err();++ lock = this.state.borrow_mut();+ let _ = lock.fullscreen.insert(StyleAndBounds {+ style,+ x: rc.left,+ y: rc.top,+ cx: rc.right - rc.left,+ cy: rc.bottom - rc.top,+ });+ let style = style+ & !(WS_THICKFRAME+ | WS_SYSMENU+ | WS_MAXIMIZEBOX+ | WS_MINIMIZEBOX+ | WS_CAPTION);+ let physical_bounds = lock.display.physical_bounds();+ StyleAndBounds {+ style,+ x: physical_bounds.left().0,+ y: physical_bounds.top().0,+ cx: physical_bounds.size.width.0,+ cy: physical_bounds.size.height.0,+ }}};drop(lock);@@ -296,7 +297,7 @@ impl WindowsWindowInner {.detach();}- fn set_window_placement(&self) -> Result<()> {+ fn set_window_placement(self: &Rc<Self>) -> Result<()> {let Some(open_status) = self.state.borrow_mut().initial_placement.take() else {return Ok(());};