mirror of
https://github.com/electron/electron.git
synced 2026-02-19 03:14:51 -05:00
refactor: simplify NativeWindow-to-BaseWindow lookup (#49520)
refactor: simplify native window to base window lookup
This commit is contained in:
@@ -111,8 +111,8 @@ BaseWindow::BaseWindow(v8::Isolate* isolate,
|
||||
}
|
||||
|
||||
// Creates NativeWindow.
|
||||
window_ = NativeWindow::Create(
|
||||
options, parent.IsEmpty() ? nullptr : parent->window_.get());
|
||||
NativeWindow* parent_native = parent.IsEmpty() ? nullptr : parent->window();
|
||||
window_ = NativeWindow::Create(GetID(), options, parent_native);
|
||||
window_->AddObserver(this);
|
||||
|
||||
SetContentView(View::Create(isolate));
|
||||
@@ -146,7 +146,6 @@ BaseWindow::~BaseWindow() {
|
||||
}
|
||||
|
||||
void BaseWindow::InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
||||
AttachAsUserData(window_.get());
|
||||
gin_helper::TrackableObject<BaseWindow>::InitWith(isolate, wrapper);
|
||||
|
||||
// We can only append this window to parent window's child windows after this
|
||||
|
||||
@@ -336,11 +336,12 @@ void BrowserWindow::BuildPrototype(v8::Isolate* isolate,
|
||||
// static
|
||||
v8::Local<v8::Value> BrowserWindow::From(v8::Isolate* isolate,
|
||||
NativeWindow* native_window) {
|
||||
auto* existing = TrackableObject::FromWrappedClass(isolate, native_window);
|
||||
if (existing)
|
||||
return existing->GetWrapper();
|
||||
else
|
||||
return v8::Null(isolate);
|
||||
if (native_window != nullptr) {
|
||||
if (auto* base = FromWeakMapID(isolate, native_window->base_window_id()))
|
||||
return base->GetWrapper();
|
||||
}
|
||||
|
||||
return v8::Null(isolate);
|
||||
}
|
||||
|
||||
} // namespace electron::api
|
||||
|
||||
@@ -96,9 +96,11 @@ gfx::Size GetExpandedWindowSize(const NativeWindow* window,
|
||||
|
||||
} // namespace
|
||||
|
||||
NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
|
||||
NativeWindow::NativeWindow(const int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent)
|
||||
: title_bar_style_{options.ValueOrDefault(options::kTitleBarStyle,
|
||||
: base_window_id_{base_window_id},
|
||||
title_bar_style_{options.ValueOrDefault(options::kTitleBarStyle,
|
||||
TitleBarStyle::kNormal)},
|
||||
transparent_{options.ValueOrDefault(options::kTransparent, false)},
|
||||
enable_larger_than_screen_{
|
||||
@@ -108,6 +110,8 @@ NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
|
||||
has_frame_{options.ValueOrDefault(options::kFrame, true) &&
|
||||
title_bar_style_ == TitleBarStyle::kNormal},
|
||||
parent_{parent} {
|
||||
DCHECK_NE(base_window_id_, 0);
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
options.Get(options::kBackgroundMaterial, &background_material_);
|
||||
#elif BUILDFLAG(IS_MAC)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/observer_list.h"
|
||||
#include "base/strings/cstring_view.h"
|
||||
#include "base/supports_user_data.h"
|
||||
#include "content/public/browser/desktop_media_id.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "extensions/browser/app_window/size_constraints.h"
|
||||
@@ -56,8 +55,7 @@ using NativeWindowHandle = gfx::NativeView;
|
||||
using NativeWindowHandle = gfx::AcceleratedWidget;
|
||||
#endif
|
||||
|
||||
class NativeWindow : public base::SupportsUserData,
|
||||
public views::WidgetDelegate {
|
||||
class NativeWindow : public views::WidgetDelegate {
|
||||
public:
|
||||
~NativeWindow() override;
|
||||
|
||||
@@ -68,6 +66,7 @@ class NativeWindow : public base::SupportsUserData,
|
||||
// Create window with existing WebContents, the caller is responsible for
|
||||
// managing the window's live.
|
||||
static std::unique_ptr<NativeWindow> Create(
|
||||
int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent = nullptr);
|
||||
|
||||
@@ -429,8 +428,12 @@ class NativeWindow : public base::SupportsUserData,
|
||||
// throttling, then throttling in the `ui::Compositor` will be disabled.
|
||||
void UpdateBackgroundThrottlingState();
|
||||
|
||||
[[nodiscard]] auto base_window_id() const { return base_window_id_; }
|
||||
|
||||
protected:
|
||||
NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent);
|
||||
NativeWindow(int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent);
|
||||
|
||||
void set_titlebar_overlay_height(int height) {
|
||||
titlebar_overlay_height_ = height;
|
||||
@@ -488,6 +491,9 @@ class NativeWindow : public base::SupportsUserData,
|
||||
static inline int32_t next_id_ = 0;
|
||||
const int32_t window_id_ = ++next_id_;
|
||||
|
||||
// ID of the api::BaseWindow that owns this NativeWindow.
|
||||
const int32_t base_window_id_;
|
||||
|
||||
// The "titleBarStyle" option.
|
||||
const TitleBarStyle title_bar_style_;
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@ class NativeWindowMac : public NativeWindow,
|
||||
public ui::NativeThemeObserver,
|
||||
public display::DisplayObserver {
|
||||
public:
|
||||
NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
|
||||
NativeWindowMac(int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent);
|
||||
~NativeWindowMac() override;
|
||||
|
||||
// NativeWindow:
|
||||
|
||||
@@ -151,9 +151,11 @@ class NativeAppWindowFrameViewMacClient
|
||||
|
||||
NativeWindowMac::~NativeWindowMac() = default;
|
||||
|
||||
NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options,
|
||||
NativeWindowMac::NativeWindowMac(const int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent)
|
||||
: NativeWindow(options, parent), root_view_(new RootViewMac(this)) {
|
||||
: NativeWindow{base_window_id, options, parent},
|
||||
root_view_(new RootViewMac(this)) {
|
||||
ui::NativeTheme::GetInstanceForNativeUi()->AddObserver(this);
|
||||
display::Screen::Get()->AddObserver(this);
|
||||
|
||||
@@ -1876,9 +1878,10 @@ void NativeWindowMac::OnWidgetInitialized() {
|
||||
|
||||
// static
|
||||
std::unique_ptr<NativeWindow> NativeWindow::Create(
|
||||
const int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent) {
|
||||
return std::make_unique<NativeWindowMac>(options, parent);
|
||||
return std::make_unique<NativeWindowMac>(base_window_id, options, parent);
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
|
||||
@@ -193,9 +193,10 @@ class NativeWindowClientView : public views::ClientView {
|
||||
|
||||
} // namespace
|
||||
|
||||
NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
|
||||
NativeWindowViews::NativeWindowViews(const int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent)
|
||||
: NativeWindow(options, parent) {
|
||||
: NativeWindow{base_window_id, options, parent} {
|
||||
if (std::string val; options.Get(options::kTitle, &val))
|
||||
SetTitle(val);
|
||||
|
||||
@@ -1952,9 +1953,10 @@ void NativeWindowViews::MoveBehindTaskBarIfNeeded() {
|
||||
|
||||
// static
|
||||
std::unique_ptr<NativeWindow> NativeWindow::Create(
|
||||
const int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent) {
|
||||
return std::make_unique<NativeWindowViews>(options, parent);
|
||||
return std::make_unique<NativeWindowViews>(base_window_id, options, parent);
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
|
||||
@@ -48,7 +48,8 @@ class NativeWindowViews : public NativeWindow,
|
||||
private views::WidgetObserver,
|
||||
private ui::EventHandler {
|
||||
public:
|
||||
NativeWindowViews(const gin_helper::Dictionary& options,
|
||||
NativeWindowViews(int32_t base_window_id,
|
||||
const gin_helper::Dictionary& options,
|
||||
NativeWindow* parent);
|
||||
~NativeWindowViews() override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user