From a7fac65d62c1cd51b5c7d8d56dd5dc2505898bbd Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 6 Nov 2025 18:33:09 +0100 Subject: [PATCH] gpui: Remove unneeded weak Rc cycle in `WindowsWindowInner` (#42119) Release Notes: - N/A *or* Added/Fixed/Improved ... --- crates/gpui/src/gpui.rs | 4 +- crates/gpui/src/platform/windows/window.rs | 79 +++++++++++----------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index 2e391b6e44..098c0780b2 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 BorrowAppContext for C where - C: BorrowMut, + C: std::borrow::BorrowMut, { fn set_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.rs index ea1027f826..f0e0beb7ca 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, drop_target_helper: IDropTargetHelper, pub(crate) state: RefCell, pub(crate) system_settings: RefCell, @@ -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) { + 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) -> Result<()> { let Some(open_status) = self.state.borrow_mut().initial_placement.take() else { return Ok(()); };