From 2225cc96083ec028299cd9a2821de88897b5819b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 25 Apr 2018 16:05:43 +0900 Subject: [PATCH] refactor: manage widget_ in NativeWindow --- atom/browser/native_window.cc | 12 +++++++++++- atom/browser/native_window.h | 8 ++++++++ atom/browser/native_window_mac.h | 3 --- atom/browser/native_window_mac.mm | 15 +++------------ atom/browser/native_window_views.cc | 9 --------- atom/browser/native_window_views.h | 4 ---- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index e1468b02a9c8..0fbb96aa3a14 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -13,6 +13,7 @@ #include "atom/common/color_util.h" #include "atom/common/options_switches.h" #include "native_mate/dictionary.h" +#include "ui/views/widget/widget.h" DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay); @@ -20,7 +21,8 @@ namespace atom { NativeWindow::NativeWindow(const mate::Dictionary& options, NativeWindow* parent) - : has_frame_(true), + : widget_(new views::Widget), + has_frame_(true), transparent_(false), enable_larger_than_screen_(false), is_closed_(false), @@ -509,6 +511,14 @@ void NativeWindow::NotifyWindowMessage(UINT message, } #endif +views::Widget* NativeWindow::GetWidget() { + return widget(); +} + +const views::Widget* NativeWindow::GetWidget() const { + return widget(); +} + NativeWindowRelay::NativeWindowRelay(base::WeakPtr window) : key(UserDataKey()), window(window) {} diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index a3e3364cf6e2..4f3b5719abf6 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -261,6 +261,8 @@ class NativeWindow : public base::SupportsUserData, observers_.RemoveObserver(obs); } + views::Widget* widget() const { return widget_.get(); } + bool has_frame() const { return has_frame_; } void set_has_frame(bool has_frame) { has_frame_ = has_frame; } @@ -274,11 +276,17 @@ class NativeWindow : public base::SupportsUserData, protected: NativeWindow(const mate::Dictionary& options, NativeWindow* parent); + // views::WidgetDelegate: + views::Widget* GetWidget() override; + const views::Widget* GetWidget() const override; + void set_browser_view(NativeBrowserView* browser_view) { browser_view_ = browser_view; } private: + std::unique_ptr widget_; + // Whether window has standard frame. bool has_frame_; diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 2b067af0094d..53f774dacf51 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -144,8 +144,6 @@ class NativeWindowMac : public NativeWindow { protected: // views::WidgetDelegate: - views::Widget* GetWidget() override; - const views::Widget* GetWidget() const override; bool CanResize() const override; private: @@ -154,7 +152,6 @@ class NativeWindowMac : public NativeWindow { void SetForwardMouseMessages(bool forward); - std::unique_ptr widget_; AtomNSWindow* window_; // Weak ref, managed by widget_. base::scoped_nsobject window_delegate_; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 182e3d79134c..feb2f8c6f88d 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -308,15 +308,14 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options, // Create views::Widget and assign window_ with it. // TODO(zcbenz): Get rid of the window_ in future. - widget_.reset(new views::Widget()); views::Widget::InitParams params; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; params.bounds = bounds; params.delegate = this; params.type = views::Widget::InitParams::TYPE_WINDOW; - params.native_widget = new AtomNativeWidgetMac(styleMask, widget_.get()); - widget_->Init(params); - window_ = static_cast(widget_->GetNativeWindow()); + params.native_widget = new AtomNativeWidgetMac(styleMask, widget()); + widget()->Init(params); + window_ = static_cast(widget()->GetNativeWindow()); [window_ setShell:this]; [window_ setEnableLargerThanScreen:enable_larger_than_screen()]; @@ -1305,14 +1304,6 @@ gfx::Rect NativeWindowMac::WindowBoundsToContentBounds( } } -views::Widget* NativeWindowMac::GetWidget() { - return widget_.get(); -} - -const views::Widget* NativeWindowMac::GetWidget() const { - return widget_.get(); -} - bool NativeWindowMac::CanResize() const { return resizable_; } diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 503fdefa8018..ae9bf4e49563 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -120,7 +120,6 @@ class NativeWindowClientView : public views::ClientView { NativeWindowViews::NativeWindowViews(const mate::Dictionary& options, NativeWindow* parent) : NativeWindow(options, parent), - widget_(new views::Widget), content_view_(nullptr), focused_view_(nullptr), menu_bar_autohide_(false), @@ -1264,14 +1263,6 @@ bool NativeWindowViews::ShouldHandleSystemCommands() const { return true; } -views::Widget* NativeWindowViews::GetWidget() { - return widget(); -} - -const views::Widget* NativeWindowViews::GetWidget() const { - return widget(); -} - views::View* NativeWindowViews::GetContentsView() { return this; } diff --git a/atom/browser/native_window_views.h b/atom/browser/native_window_views.h index 6e4525b7f30c..691748e71fff 100644 --- a/atom/browser/native_window_views.h +++ b/atom/browser/native_window_views.h @@ -138,7 +138,6 @@ class NativeWindowViews : public NativeWindow, void SetIcon(const gfx::ImageSkia& icon); #endif - views::Widget* widget() const { return widget_.get(); } views::View* content_view() const { return content_view_; } SkRegion* draggable_region() const { return draggable_region_.get(); } @@ -160,8 +159,6 @@ class NativeWindowViews : public NativeWindow, bool CanMinimize() const override; base::string16 GetWindowTitle() const override; bool ShouldHandleSystemCommands() const override; - views::Widget* GetWidget() override; - const views::Widget* GetWidget() const override; views::View* GetContentsView() override; bool ShouldDescendIntoChildForEventHandling( gfx::NativeView child, @@ -210,7 +207,6 @@ class NativeWindowViews : public NativeWindow, // Returns the restore state for the window. ui::WindowShowState GetRestoredState(); - std::unique_ptr widget_; views::View* content_view_; // Weak ref. views::View* focused_view_; // The view should be focused by default.