From ed3242adc1ac2cd1e549a9a8a528b59add6764af Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 31 Jul 2024 01:21:20 +0200 Subject: [PATCH] fix: vibrant view is inserted into Views API hierarchy (#43078) * fix: vibrant view is inserted into Views API hierarchy (#42263) * Update shell/browser/native_window_mac.mm Co-authored-by: Charles Kerr --------- Co-authored-by: Hans Halverson Co-authored-by: Charles Kerr --- shell/browser/native_window_mac.h | 4 ++++ shell/browser/native_window_mac.mm | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 363dfecb9ad2..d3c541ba688f 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -12,6 +12,7 @@ #include #include +#include "base/memory/raw_ptr.h" #include "electron/shell/common/api/api.mojom.h" #include "shell/browser/native_window.h" #include "third_party/skia/include/core/SkRegion.h" @@ -300,6 +301,9 @@ class NativeWindowMac : public NativeWindow, std::string vibrancy_type_; + // A views::NativeViewHost wrapping the vibrant view. Owned by the root view. + raw_ptr vibrant_native_view_host_ = nullptr; + // The presentation options before entering simple fullscreen mode. NSApplicationPresentationOptions simple_fullscreen_options_; }; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 7f278788f956..06b4600245f1 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1391,13 +1391,19 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { NativeWindow::SetVibrancy(type); NSVisualEffectView* vibrantView = [window_ vibrantView]; + views::View* rootView = GetContentsView(); if (type.empty()) { - if (vibrantView == nil) - return; + if (vibrant_native_view_host_ != nullptr) { + // Transfers ownership back to caller in the form of a unique_ptr which is + // subsequently deleted. + rootView->RemoveChildViewT(vibrant_native_view_host_); + vibrant_native_view_host_ = nullptr; + } - [vibrantView removeFromSuperview]; - [window_ setVibrantView:nil]; + if (vibrantView != nil) { + [window_ setVibrantView:nil]; + } return; } @@ -1453,9 +1459,13 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { [vibrantView setState:NSVisualEffectStateFollowsWindowActiveState]; } - [[window_ contentView] addSubview:vibrantView - positioned:NSWindowBelow - relativeTo:nil]; + // Vibrant view is inserted into the root view hierarchy underneath all + // other views. + vibrant_native_view_host_ = rootView->AddChildViewAt( + std::make_unique(), 0); + vibrant_native_view_host_->Attach(vibrantView); + + rootView->DeprecatedLayoutImmediately(); UpdateVibrancyRadii(IsFullscreen()); }