mirror of
https://github.com/electron/electron.git
synced 2026-01-08 23:18:06 -05:00
refactor: add NativeWindow::FromWidget() helper (#46917)
refactor: add NativeWindow::FromWidet() helper refactor: make kElectronNativeWindowKey a protected field
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user