refactor: add NativeWindow::FromWidget() helper (36-x-y) (#46932)

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-06 02:51:11 -05:00 committed by GitHub
parent 5045ba29a5
commit 5223225fb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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 @@ bool NativeWindow::IsClosed() const {
return is_closed_;
}
// 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;
@ -451,6 +450,9 @@ class NativeWindow : public base::SupportsUserData,
void set_content_view(views::View* view) { content_view_ = view; }
static inline constexpr base::cstring_view kNativeWindowKey =
"__ELECTRON_NATIVE_WINDOW__";
// The boolean parsing of the "titleBarOverlay" option
bool titlebar_overlay_ = false;

View file

@ -204,7 +204,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

@ -312,7 +312,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;