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:
parent
5045ba29a5
commit
5223225fb5
5 changed files with 20 additions and 12 deletions
|
@ -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 @@ 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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue