From 54eb30a642c254bb247be67fb0c1bd6ef628764a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 22 Apr 2026 05:34:07 -0500 Subject: [PATCH] chore: remove calls to DeprecatedLayoutImmediately (#51183) * chore: remove calls to DeprecatedLayoutImmediately Replace the calls to `DeprecatedLayoutImmediately` that have test coverage. - The docked DevTools test added last week covers the three calls in IWCV. - api-web-contents-view-spec covers the calls from NativeWindow{Views,Mac}. There are a couple of remaining calls that don't have test coverage yet. I'll get to them in a followup. * test: handle both sync or microtask layout * refactor: add a FlushPendingRootLayout() helper --- shell/browser/native_window.cc | 7 +++++++ shell/browser/native_window.h | 1 + shell/browser/native_window_mac.mm | 2 +- shell/browser/native_window_views.cc | 2 +- .../browser/ui/inspectable_web_contents_view.cc | 16 +++++++++++++--- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index c4608a8da1..25d39035b4 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -240,6 +240,13 @@ void NativeWindow::SetShape(const std::vector& rects) { widget()->SetShape(std::make_unique>(rects)); } +void NativeWindow::FlushPendingRootLayout(views::View* view) { + view->InvalidateLayout(); + + if (views::Widget* widget = view->GetWidget()) + widget->LayoutRootViewIfNecessary(); +} + bool NativeWindow::IsClosed() const { return is_closed_; } diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 22e159cbec..02a34be972 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -462,6 +462,7 @@ class NativeWindow : public views::WidgetDelegate { const views::Widget* GetWidget() const override; void set_content_view(views::View* view) { content_view_ = view; } + void FlushPendingRootLayout(views::View* view); static inline constexpr base::cstring_view kNativeWindowKey = "__ELECTRON_NATIVE_WINDOW__"; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 8244fe1a2d..4af794261f 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -381,7 +381,7 @@ void NativeWindowMac::SetContentView(views::View* view) { root_view->AddChildViewRaw(content_view()); - root_view->DeprecatedLayoutImmediately(); + FlushPendingRootLayout(root_view); } void NativeWindowMac::Close() { diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index a4c127e60b..fef6661ae7 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -533,7 +533,7 @@ void NativeWindowViews::SetContentView(views::View* view) { set_content_view(view); focused_view_ = view; root_view_.GetMainView()->AddChildViewRaw(content_view()); - root_view_.GetMainView()->DeprecatedLayoutImmediately(); + FlushPendingRootLayout(root_view_.GetMainView()); } void NativeWindowViews::Close() { diff --git a/shell/browser/ui/inspectable_web_contents_view.cc b/shell/browser/ui/inspectable_web_contents_view.cc index e9216e22dc..a8841b0a2e 100644 --- a/shell/browser/ui/inspectable_web_contents_view.cc +++ b/shell/browser/ui/inspectable_web_contents_view.cc @@ -23,6 +23,16 @@ #include "ui/views/window/client_view.h" namespace electron { +namespace { + +void FlushPendingRootLayout(views::View* view) { + view->InvalidateLayout(); + + if (views::Widget* widget = view->GetWidget()) + widget->LayoutRootViewIfNecessary(); +} + +} // namespace class DevToolsWindowDelegate : public views::ClientView, public views::WidgetDelegate { @@ -135,7 +145,7 @@ void InspectableWebContentsView::ShowDevTools(bool activate) { devtools_web_view_->SetWebContents( inspectable_web_contents_->GetDevToolsWebContents()); devtools_web_view_->RequestFocus(); - DeprecatedLayoutImmediately(); + FlushPendingRootLayout(this); } } @@ -173,7 +183,7 @@ void InspectableWebContentsView::CloseDevTools() { } else { devtools_web_view_->SetVisible(false); devtools_web_view_->SetWebContents(nullptr); - DeprecatedLayoutImmediately(); + FlushPendingRootLayout(this); } } @@ -223,7 +233,7 @@ void InspectableWebContentsView::SetIsDocked(bool docked, bool activate) { void InspectableWebContentsView::SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) { strategy_.CopyFrom(strategy); - DeprecatedLayoutImmediately(); + FlushPendingRootLayout(this); } void InspectableWebContentsView::SetTitle(const std::u16string& title) {