refactor: add NativeWindow::FromWidget() helper (#46917)

refactor: add NativeWindow::FromWidet() helper

refactor: make kElectronNativeWindowKey a protected field
This commit is contained in:
Charles Kerr
2025-05-05 09:27:41 -05:00
committed by GitHub
parent cb445b3bbd
commit 1f4f1b4afc
5 changed files with 20 additions and 12 deletions

View File

@@ -99,12 +99,11 @@ void WebContentsView::WebContentsDestroyed() {
void WebContentsView::OnViewAddedToWidget(views::View* observed_view) {
DCHECK_EQ(observed_view, view());
views::Widget* widget = view()->GetWidget();
auto* native_window =
static_cast<NativeWindow*>(widget->GetNativeWindowProperty(
electron::kElectronNativeWindowKey.c_str()));
NativeWindow* native_window = NativeWindow::FromWidget(view()->GetWidget());
if (!native_window)
return;
// We don't need to call SetOwnerWindow(nullptr) in OnViewRemovedFromWidget
// because that's handled in the WebContents dtor called prior.
api_web_contents_->SetOwnerWindow(native_window);
@@ -114,11 +113,11 @@ void WebContentsView::OnViewAddedToWidget(views::View* observed_view) {
void WebContentsView::OnViewRemovedFromWidget(views::View* observed_view) {
DCHECK_EQ(observed_view, view());
views::Widget* widget = view()->GetWidget();
auto* native_window = static_cast<NativeWindow*>(
widget->GetNativeWindowProperty(kElectronNativeWindowKey.c_str()));
NativeWindow* native_window = NativeWindow::FromWidget(view()->GetWidget());
if (!native_window)
return;
native_window->RemoveDraggableRegionProvider(this);
}

View File

@@ -284,6 +284,13 @@ void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
Show();
}
// static
NativeWindow* NativeWindow::FromWidget(const views::Widget* widget) {
DCHECK(widget);
return static_cast<NativeWindow*>(
widget->GetNativeWindowProperty(kNativeWindowKey.c_str()));
}
void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
SetBounds(gfx::Rect(GetPosition(), size), animate);
}

View File

@@ -47,9 +47,6 @@ class PersistentDictionary;
namespace electron {
inline constexpr base::cstring_view kElectronNativeWindowKey =
"__ELECTRON_NATIVE_WINDOW__";
class ElectronMenuModel;
class BackgroundThrottlingSource;
@@ -78,6 +75,8 @@ class NativeWindow : public base::SupportsUserData,
const gin_helper::Dictionary& options,
NativeWindow* parent = nullptr);
[[nodiscard]] static NativeWindow* FromWidget(const views::Widget* widget);
void InitFromOptions(const gin_helper::Dictionary& options);
virtual void SetContentView(views::View* view) = 0;
@@ -453,6 +452,9 @@ class NativeWindow : public base::SupportsUserData,
virtual void CloseImpl() = 0;
virtual void CloseImmediatelyImpl() = 0;
static inline constexpr base::cstring_view kNativeWindowKey =
"__ELECTRON_NATIVE_WINDOW__";
// The boolean parsing of the "titleBarOverlay" option
bool titlebar_overlay_ = false;

View File

@@ -203,7 +203,7 @@ NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options,
params.native_widget =
new ElectronNativeWidgetMac(this, windowType, styleMask, widget());
widget()->Init(std::move(params));
widget()->SetNativeWindowProperty(kElectronNativeWindowKey.c_str(), this);
widget()->SetNativeWindowProperty(kNativeWindowKey.c_str(), this);
SetCanResize(resizable);
window_ = static_cast<ElectronNSWindow*>(
widget()->GetNativeWindow().GetNativeNSWindow());

View File

@@ -307,7 +307,7 @@ NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
#endif
widget()->Init(std::move(params));
widget()->SetNativeWindowProperty(kElectronNativeWindowKey.c_str(), this);
widget()->SetNativeWindowProperty(kNativeWindowKey.c_str(), this);
SetCanResize(resizable_);
bool fullscreen = false;