views: Implement NativeWindow::SetSizeConstraints
authorCheng Zhao <zcbenz@gmail.com>
Mon, 5 Oct 2015 11:05:59 +0000 (19:05 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 5 Oct 2015 12:07:20 +0000 (20:07 +0800)
atom/browser/native_window_views.cc
atom/browser/native_window_views.h
atom/browser/ui/views/frameless_view.cc
atom/browser/ui/views/native_frame_view.cc
atom/browser/ui/views/native_frame_view.h
atom/browser/ui/views/win_frame_view.cc

index 1c7546854ee2da36f47fba799aff75ad74941c1c..fdcbe42205bf9bbcb945343d54a4ba3785263fe9 100644 (file)
@@ -186,7 +186,8 @@ NativeWindowViews::NativeWindowViews(
     // will not allow us to resize the window larger than scree.
     // Setting directly to INT_MAX somehow doesn't work, so we just devide
     // by 10, which should still be large enough.
-    maximum_size_.SetSize(INT_MAX / 10, INT_MAX / 10);
+    SetContentSizeConstraints(extensions::SizeConstraints(
+        gfx::Size(), gfx::Size(INT_MAX / 10, INT_MAX / 10)));
 
   int width = 800, height = 600;
   options.Get(switches::kWidth, &width);
@@ -271,11 +272,6 @@ NativeWindowViews::NativeWindowViews(
   set_background(views::Background::CreateStandardPanelBackground());
   AddChildView(web_view_);
 
-  if (has_frame() &&
-      options.Get(switches::kUseContentSize, &use_content_size_) &&
-      use_content_size_)
-    bounds = ContentBoundsToWindowBounds(bounds);
-
 #if defined(OS_WIN)
   // Save initial window state.
   if (fullscreen)
@@ -316,8 +312,14 @@ NativeWindowViews::NativeWindowViews(
   if (transparent() && !has_frame())
     wm::SetShadowType(GetNativeWindow(), wm::SHADOW_TYPE_NONE);
 
+  gfx::Size size = bounds.size();
+  if (has_frame() &&
+      options.Get(switches::kUseContentSize, &use_content_size_) &&
+      use_content_size_)
+    size = ContentSizeToWindowSize(size);
+
   window_->UpdateWindowIcon();
-  window_->CenterWindow(bounds.size());
+  window_->CenterWindow(size);
   Layout();
 }
 
@@ -440,60 +442,6 @@ gfx::Rect NativeWindowViews::GetBounds() {
   return window_->GetWindowBoundsInScreen();
 }
 
-void NativeWindowViews::SetSizeConstraints(
-    const extensions::SizeConstraints& size_constraints) {
-}
-
-extensions::SizeConstraints NativeWindowViews::GetSizeConstraints() {
-  return extensions::SizeConstraints();
-}
-
-void NativeWindowViews::SetContentSizeConstraints(
-    const extensions::SizeConstraints& size_constraints) {
-}
-
-extensions::SizeConstraints NativeWindowViews::GetContentSizeConstraints() {
-  return extensions::SizeConstraints();
-}
-
-void NativeWindowViews::SetContentSize(const gfx::Size& size) {
-  if (!has_frame()) {
-    NativeWindow::SetSize(size);
-    return;
-  }
-
-  gfx::Rect bounds = window_->GetWindowBoundsInScreen();
-  bounds.set_size(size);
-  SetBounds(ContentBoundsToWindowBounds(bounds));
-}
-
-gfx::Size NativeWindowViews::GetContentSize() {
-  if (!has_frame())
-    return GetSize();
-
-  gfx::Size content_size =
-      window_->non_client_view()->frame_view()->GetBoundsForClientView().size();
-  if (menu_bar_ && menu_bar_visible_)
-    content_size.set_height(content_size.height() - kMenuBarHeight);
-  return content_size;
-}
-
-void NativeWindowViews::SetMinimumSize(const gfx::Size& size) {
-  minimum_size_ = size;
-}
-
-gfx::Size NativeWindowViews::GetMinimumSize() {
-  return minimum_size_;
-}
-
-void NativeWindowViews::SetMaximumSize(const gfx::Size& size) {
-  maximum_size_ = size;
-}
-
-gfx::Size NativeWindowViews::GetMaximumSize() {
-  return maximum_size_;
-}
-
 void NativeWindowViews::SetResizable(bool resizable) {
 #if defined(OS_WIN)
   // WS_MAXIMIZEBOX => Maximize button
@@ -929,6 +877,47 @@ gfx::Size NativeWindowViews::WindowSizeToFramelessSize(
   return window_bounds.size();
 }
 
+gfx::Size NativeWindowViews::ContentSizeToWindowSize(const gfx::Size& size) {
+  if (!has_frame())
+    return size;
+
+  gfx::Size window_size(size);
+#if defined(OS_WIN)
+  gfx::Rect dpi_bounds =
+      gfx::Rect(gfx::Point(), gfx::win::DIPToScreenSize(size));
+  gfx::Rect window_bounds = gfx::win::ScreenToDIPRect(
+      window_->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
+  window_size = window_bounds.size();
+#endif
+
+  if (menu_bar_ && menu_bar_visible_)
+    window_size.set_height(window_size.height() + kMenuBarHeight);
+  return window_size;
+}
+
+gfx::Size NativeWindowViews::WindowSizeToContentSize(const gfx::Size& size) {
+  if (!has_frame())
+    return size;
+
+  gfx::Size content_size(size);
+#if defined(OS_WIN)
+  content_size = gfx::win::DIPToScreenSize(content_size);
+  RECT rect;
+  SetRectEmpty(&rect);
+  HWND hwnd = GetAcceleratedWidget();
+  DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
+  DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
+  AdjustWindowRectEx(&rect, style, FALSE, ex_style);
+  content_size.set_width(content_size.width() - (rect.right - rect.left));
+  content_size.set_height(content_size.height() - (rect.bottom - rect.top));
+  content_size = gfx::win::ScreenToDIPSize(content_size);
+#endif
+
+  if (menu_bar_ && menu_bar_visible_)
+    content_size.set_height(content_size.height() - kMenuBarHeight);
+  return content_size;
+}
+
 void NativeWindowViews::HandleKeyboardEvent(
     content::WebContents*,
     const content::NativeWebKeyboardEvent& event) {
@@ -970,6 +959,14 @@ void NativeWindowViews::HandleKeyboardEvent(
   }
 }
 
+gfx::Size NativeWindowViews::GetMinimumSize() {
+  return NativeWindow::GetMinimumSize();
+}
+
+gfx::Size NativeWindowViews::GetMaximumSize() {
+  return NativeWindow::GetMaximumSize();
+}
+
 bool NativeWindowViews::AcceleratorPressed(const ui::Accelerator& accelerator) {
   return accelerator_util::TriggerAcceleratorTableCommand(
       &accelerator_table_, accelerator);
@@ -992,26 +989,6 @@ void NativeWindowViews::RegisterAccelerators(ui::MenuModel* menu_model) {
   }
 }
 
-gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
-    const gfx::Rect& bounds) {
-  gfx::Point origin = bounds.origin();
-#if defined(OS_WIN)
-  gfx::Rect dpi_bounds = gfx::win::DIPToScreenRect(bounds);
-  gfx::Rect window_bounds = gfx::win::ScreenToDIPRect(
-      window_->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
-#else
-  gfx::Rect window_bounds =
-      window_->non_client_view()->GetWindowBoundsForClientBounds(bounds);
-#endif
-  // The window's position would also be changed, but we only want to change
-  // the size.
-  window_bounds.set_origin(origin);
-
-  if (menu_bar_ && menu_bar_visible_)
-    window_bounds.set_height(window_bounds.height() + kMenuBarHeight);
-  return window_bounds;
-}
-
 ui::WindowShowState NativeWindowViews::GetRestoredState() {
   if (IsMaximized())
     return ui::SHOW_STATE_MAXIMIZED;
index 5a90e6a221398c7bea657f26e3912803b71ad626..0e74410868e1d1eba0c23482f74954bd96e33a0d 100644 (file)
@@ -63,18 +63,6 @@ class NativeWindowViews : public NativeWindow,
   bool IsFullscreen() const override;
   void SetBounds(const gfx::Rect& bounds) override;
   gfx::Rect GetBounds() override;
-  void SetSizeConstraints(
-      const extensions::SizeConstraints& size_constraints) override;
-  extensions::SizeConstraints GetSizeConstraints() override;
-  void SetContentSizeConstraints(
-      const extensions::SizeConstraints& size_constraints) override;
-  extensions::SizeConstraints GetContentSizeConstraints() override;
-  void SetContentSize(const gfx::Size& size) override;
-  gfx::Size GetContentSize() override;
-  void SetMinimumSize(const gfx::Size& size) override;
-  gfx::Size GetMinimumSize() override;
-  void SetMaximumSize(const gfx::Size& size) override;
-  gfx::Size GetMaximumSize() override;
   void SetResizable(bool resizable) override;
   bool IsResizable() override;
   void SetAlwaysOnTop(bool top) override;
@@ -148,20 +136,20 @@ class NativeWindowViews : public NativeWindow,
 #endif
 
   // NativeWindow:
+  gfx::Size ContentSizeToWindowSize(const gfx::Size& size) override;
+  gfx::Size WindowSizeToContentSize(const gfx::Size& size) override;
   void HandleKeyboardEvent(
       content::WebContents*,
       const content::NativeWebKeyboardEvent& event) override;
 
   // views::View:
+  gfx::Size GetMinimumSize() override;
+  gfx::Size GetMaximumSize() override;
   bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
 
   // Register accelerators supported by the menu model.
   void RegisterAccelerators(ui::MenuModel* menu_model);
 
-  // Converts between client area and window area, since we include the menu bar
-  // in client area we need to substract/add menu bar's height in convertions.
-  gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& content_bounds);
-
   // Returns the restore state for the window.
   ui::WindowShowState GetRestoredState();
 
@@ -203,8 +191,6 @@ class NativeWindowViews : public NativeWindow,
   bool use_content_size_;
   bool resizable_;
   std::string title_;
-  gfx::Size minimum_size_;
-  gfx::Size maximum_size_;
   gfx::Size widget_size_;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
index 03a31e0828742b9bd0f76dfe34344c616c10a714..f5e28025e9bfd828f344f22dec4897975cc8e880 100644 (file)
@@ -104,11 +104,11 @@ gfx::Size FramelessView::GetPreferredSize() const {
 }
 
 gfx::Size FramelessView::GetMinimumSize() const {
-  return window_->GetMinimumSize();
+  return static_cast<NativeWindow*>(window_)->GetMinimumSize();
 }
 
 gfx::Size FramelessView::GetMaximumSize() const {
-  return window_->GetMaximumSize();
+  return static_cast<NativeWindow*>(window_)->GetMaximumSize();
 }
 
 const char* FramelessView::GetClassName() const {
index a434fb434961f06797211132b737d838f6b72899..134255f48458c86a7993e5924c26c64eeaf3aada 100644 (file)
@@ -4,7 +4,7 @@
 
 #include "atom/browser/ui/views/native_frame_view.h"
 
-#include "atom/browser/native_window_views.h"
+#include "atom/browser/native_window.h"
 
 namespace atom {
 
@@ -14,8 +14,7 @@ const char kViewClassName[] = "AtomNativeFrameView";
 
 }  // namespace
 
-NativeFrameView::NativeFrameView(NativeWindowViews* window,
-                                 views::Widget* widget)
+NativeFrameView::NativeFrameView(NativeWindow* window, views::Widget* widget)
     : views::NativeFrameView(widget),
       window_(window) {
 }
index acbe9cddc8dc0f59c6e35bbe67be561da6f1ebea..670459f1cbd0a42fef1f6ec31ad8705092fc73a6 100644 (file)
@@ -9,13 +9,13 @@
 
 namespace atom {
 
-class NativeWindowViews;
+class NativeWindow;
 
 // Like the views::NativeFrameView, but returns the min/max size from the
 // NativeWindowViews.
 class NativeFrameView : public views::NativeFrameView {
  public:
-  NativeFrameView(NativeWindowViews* window, views::Widget* widget);
+  NativeFrameView(NativeWindow* window, views::Widget* widget);
 
  protected:
   // views::View:
@@ -24,7 +24,7 @@ class NativeFrameView : public views::NativeFrameView {
   const char* GetClassName() const override;
 
  private:
-  NativeWindowViews* window_;  // weak ref.
+  NativeWindow* window_;  // weak ref.
 
   DISALLOW_COPY_AND_ASSIGN(NativeFrameView);
 };
index d0338af19d9a003856d3a1f51841003687d9443d..7106d8f759cfddd896b78b00041b213e1c99e8c6 100644 (file)
@@ -41,13 +41,13 @@ int WinFrameView::NonClientHitTest(const gfx::Point& point) {
 
 gfx::Size WinFrameView::GetMinimumSize() const {
   gfx::Size size = window_->WindowSizeToFramelessSize(
-      window_->GetMinimumSize());
+      FramelessView::GetMinimumSize());
   return gfx::win::DIPToScreenSize(size);
 }
 
 gfx::Size WinFrameView::GetMaximumSize() const {
   gfx::Size size = window_->WindowSizeToFramelessSize(
-      window_->GetMaximumSize());
+      FramelessView::GetMaximumSize());
   return gfx::win::DIPToScreenSize(size);
 }