Browse Source

Add NativeWindow::SetContentView

Cheng Zhao 7 years ago
parent
commit
56735d4ff5

+ 1 - 1
atom/browser/api/atom_api_browser_window.cc

@@ -150,9 +150,9 @@ void BrowserWindow::Init(v8::Isolate* isolate,
 
   // Creates BrowserWindow.
   window_.reset(NativeWindow::Create(
-      web_contents->managed_web_contents(),
       options,
       parent.IsEmpty() ? nullptr : parent->window_.get()));
+  window_->SetContentView(web_contents->managed_web_contents());
   web_contents->SetOwnerWindow(window_.get());
 
   // Tell the content module to initialize renderer widget with transparent

+ 5 - 4
atom/browser/native_window.h

@@ -53,13 +53,14 @@ class NativeWindow : public base::SupportsUserData {
 
   // Create window with existing WebContents, the caller is responsible for
   // managing the window's live.
-  static NativeWindow* Create(
-      brightray::InspectableWebContents* inspectable_web_contents,
-      const mate::Dictionary& options,
-      NativeWindow* parent = nullptr);
+  static NativeWindow* Create(const mate::Dictionary& options,
+                              NativeWindow* parent = nullptr);
 
   void InitFromOptions(const mate::Dictionary& options);
 
+  virtual void SetContentView(
+      brightray::InspectableWebContents* web_contents) = 0;
+
   virtual void Close() = 0;
   virtual void CloseImmediately() = 0;
   virtual bool IsClosed() const { return is_closed_; }

+ 2 - 2
atom/browser/native_window_mac.h

@@ -21,12 +21,12 @@ namespace atom {
 
 class NativeWindowMac : public NativeWindow {
  public:
-  NativeWindowMac(brightray::InspectableWebContents* inspectable_web_contents,
-                  const mate::Dictionary& options,
+  NativeWindowMac(const mate::Dictionary& options,
                   NativeWindow* parent);
   ~NativeWindowMac() override;
 
   // NativeWindow:
+  void SetContentView(brightray::InspectableWebContents* web_contents) override;
   void Close() override;
   void CloseImmediately() override;
   void Focus(bool focus) override;

+ 15 - 14
atom/browser/native_window_mac.mm

@@ -776,10 +776,8 @@ struct Converter<atom::NativeWindowMac::TitleBarStyle> {
 
 namespace atom {
 
-NativeWindowMac::NativeWindowMac(
-    brightray::InspectableWebContents* web_contents,
-    const mate::Dictionary& options,
-    NativeWindow* parent)
+NativeWindowMac::NativeWindowMac(const mate::Dictionary& options,
+                                 NativeWindow* parent)
     : NativeWindow(options, parent),
       is_kiosk_(false),
       was_fullscreen_(false),
@@ -952,9 +950,6 @@ NativeWindowMac::NativeWindowMac(
   options.Get(options::kDisableAutoHideCursor, &disableAutoHideCursor);
   [window_ setDisableAutoHideCursor:disableAutoHideCursor];
 
-  NSView* view = web_contents->GetView()->GetNativeView();
-  [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
-
   // Use an NSEvent monitor to listen for the wheel event.
   BOOL __block began = NO;
   wheel_event_monitor_ = [NSEvent
@@ -975,8 +970,6 @@ NativeWindowMac::NativeWindowMac(
       return event;
   }];
 
-  InstallView(web_contents->GetView()->GetNativeView());
-
   std::string type;
   if (options.Get(options::kVibrancyType, &type)) {
     SetVibrancy(type);
@@ -991,6 +984,16 @@ NativeWindowMac::~NativeWindowMac() {
   [NSEvent removeMonitor:wheel_event_monitor_];
 }
 
+void NativeWindowMac::SetContentView(
+    brightray::InspectableWebContents* web_contents) {
+  // TODO(zcbenz): Uninstall view first.
+  // TODO(zcbenz): Handle vibrancy.
+  // TODO(zcbenz): Handle draggable regions.
+  NSView* view = web_contents->GetView()->GetNativeView();
+  [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
+  InstallView(web_contents->GetView()->GetNativeView());
+}
+
 void NativeWindowMac::Close() {
   // When this is a sheet showing, performClose won't work.
   if (is_modal() && parent() && IsVisible()) {
@@ -1860,11 +1863,9 @@ void NativeWindowMac::SetCollectionBehavior(bool on, NSUInteger flag) {
 }
 
 // static
-NativeWindow* NativeWindow::Create(
-    brightray::InspectableWebContents* inspectable_web_contents,
-    const mate::Dictionary& options,
-    NativeWindow* parent) {
-  return new NativeWindowMac(inspectable_web_contents, options, parent);
+NativeWindow* NativeWindow::Create(const mate::Dictionary& options,
+                                   NativeWindow* parent) {
+  return new NativeWindowMac(options, parent);
 }
 
 }  // namespace atom

+ 55 - 43
atom/browser/native_window_views.cc

@@ -118,14 +118,12 @@ class NativeWindowClientView : public views::ClientView {
 
 }  // namespace
 
-NativeWindowViews::NativeWindowViews(
-    brightray::InspectableWebContents* web_contents,
-    const mate::Dictionary& options,
-    NativeWindow* parent)
+NativeWindowViews::NativeWindowViews(const mate::Dictionary& options,
+                                     NativeWindow* parent)
     : NativeWindow(options, parent),
       window_(new views::Widget),
-      content_view_(web_contents->GetView()->GetView()),
-      focused_view_(web_contents->GetView()->GetWebView()),
+      content_view_(nullptr),
+      focused_view_(nullptr),
       menu_bar_autohide_(false),
       menu_bar_visible_(false),
       menu_bar_alt_pressed_(false),
@@ -266,8 +264,6 @@ NativeWindowViews::NativeWindowViews(
     SetWindowType(GetAcceleratedWidget(), window_type);
 #endif
 
-  AddChildView(content_view_);
-
 #if defined(OS_WIN)
   if (!has_frame()) {
     // Set Window style so that we get a minimize and maximize animation when
@@ -310,7 +306,6 @@ NativeWindowViews::NativeWindowViews(
     size = ContentBoundsToWindowBounds(gfx::Rect(size)).size();
 
   window_->CenterWindow(size);
-  Layout();
 
 #if defined(OS_WIN)
   // Save initial window state.
@@ -331,6 +326,22 @@ NativeWindowViews::~NativeWindowViews() {
 #endif
 }
 
+void NativeWindowViews::SetContentView(
+    brightray::InspectableWebContents* web_contents) {
+  if (content_view_) {
+    RemoveChildView(content_view_);
+    if (browser_view()) {
+      content_view_->RemoveChildView(
+          browser_view()->GetInspectableWebContentsView()->GetView());
+      set_browser_view(nullptr);
+    }
+  }
+  content_view_ = web_contents->GetView()->GetView();
+  focused_view_ = web_contents->GetView()->GetWebView();
+  AddChildView(content_view_);
+  Layout();
+}
+
 void NativeWindowViews::Close() {
   if (!IsClosable()) {
     WindowList::WindowCloseCancelled(this);
@@ -412,6 +423,33 @@ bool NativeWindowViews::IsEnabled() {
 #endif
 }
 
+void NativeWindowViews::SetEnabled(bool enable) {
+  // Handle multiple calls of SetEnabled correctly.
+  if (enable) {
+    --disable_count_;
+    if (disable_count_ != 0)
+      return;
+  } else {
+    ++disable_count_;
+    if (disable_count_ != 1)
+      return;
+  }
+
+#if defined(OS_WIN)
+  ::EnableWindow(GetAcceleratedWidget(), enable);
+#elif defined(USE_X11)
+  views::DesktopWindowTreeHostX11* tree_host =
+      views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
+  if (enable) {
+    tree_host->RemoveEventRewriter(event_disabler_.get());
+    event_disabler_.reset();
+  } else {
+    event_disabler_.reset(new EventDisabler);
+    tree_host->AddEventRewriter(event_disabler_.get());
+  }
+#endif
+}
+
 void NativeWindowViews::Maximize() {
 #if defined(OS_WIN)
   // For window without WS_THICKFRAME style, we can not call Maximize().
@@ -544,7 +582,7 @@ gfx::Rect NativeWindowViews::GetBounds() {
 }
 
 gfx::Rect NativeWindowViews::GetContentBounds() {
-  return content_view_->GetBoundsInScreen();
+  return content_view_ ? content_view_->GetBoundsInScreen() : gfx::Rect();
 }
 
 gfx::Size NativeWindowViews::GetContentSize() {
@@ -553,7 +591,7 @@ gfx::Size NativeWindowViews::GetContentSize() {
     return NativeWindow::GetContentSize();
 #endif
 
-  return content_view_->size();
+  return content_view_ ? content_view_->size() : gfx::Size();
 }
 
 void NativeWindowViews::SetContentSizeConstraints(
@@ -933,6 +971,9 @@ void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
 }
 
 void NativeWindowViews::SetBrowserView(NativeBrowserView* view) {
+  if (!content_view_)
+    return;
+
   if (browser_view()) {
     content_view_->RemoveChildView(
         browser_view()->GetInspectableWebContentsView()->GetView());
@@ -1129,33 +1170,6 @@ void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
 }
 #endif
 
-void NativeWindowViews::SetEnabled(bool enable) {
-  // Handle multiple calls of SetEnabled correctly.
-  if (enable) {
-    --disable_count_;
-    if (disable_count_ != 0)
-      return;
-  } else {
-    ++disable_count_;
-    if (disable_count_ != 1)
-      return;
-  }
-
-#if defined(OS_WIN)
-  ::EnableWindow(GetAcceleratedWidget(), enable);
-#elif defined(USE_X11)
-  views::DesktopWindowTreeHostX11* tree_host =
-      views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
-  if (enable) {
-    tree_host->RemoveEventRewriter(event_disabler_.get());
-    event_disabler_.reset();
-  } else {
-    event_disabler_.reset(new EventDisabler);
-    tree_host->AddEventRewriter(event_disabler_.get());
-  }
-#endif
-}
-
 void NativeWindowViews::OnWidgetActivationChanged(
     views::Widget* widget, bool active) {
   if (widget != window_.get())
@@ -1399,11 +1413,9 @@ ui::WindowShowState NativeWindowViews::GetRestoredState() {
 }
 
 // static
-NativeWindow* NativeWindow::Create(
-    brightray::InspectableWebContents* inspectable_web_contents,
-    const mate::Dictionary& options,
-    NativeWindow* parent) {
-  return new NativeWindowViews(inspectable_web_contents, options, parent);
+NativeWindow* NativeWindow::Create(const mate::Dictionary& options,
+                                   NativeWindow* parent) {
+  return new NativeWindowViews(options, parent);
 }
 
 }  // namespace atom

+ 3 - 4
atom/browser/native_window_views.h

@@ -46,12 +46,12 @@ class NativeWindowViews : public NativeWindow,
                           public views::WidgetDelegateView,
                           public views::WidgetObserver {
  public:
-  NativeWindowViews(brightray::InspectableWebContents* inspectable_web_contents,
-                    const mate::Dictionary& options,
+  NativeWindowViews(const mate::Dictionary& options,
                     NativeWindow* parent);
   ~NativeWindowViews() override;
 
   // NativeWindow:
+  void SetContentView(brightray::InspectableWebContents* web_contents) override;
   void Close() override;
   void CloseImmediately() override;
   void Focus(bool focus) override;
@@ -61,6 +61,7 @@ class NativeWindowViews : public NativeWindow,
   void Hide() override;
   bool IsVisible() override;
   bool IsEnabled() override;
+  void SetEnabled(bool enable) override;
   void Maximize() override;
   void Unmaximize() override;
   bool IsMaximized() override;
@@ -139,8 +140,6 @@ class NativeWindowViews : public NativeWindow,
   void SetIcon(const gfx::ImageSkia& icon);
 #endif
 
-  void SetEnabled(bool enable) override;
-
   views::Widget* widget() const { return window_.get(); }
   views::View* content_view() const { return content_view_; }
   SkRegion* draggable_region() const { return draggable_region_.get(); }