native_browser_view_views.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/native_browser_view_views.h"
  5. #include "atom/browser/ui/inspectable_web_contents_view.h"
  6. #include "ui/gfx/geometry/rect.h"
  7. #include "ui/views/background.h"
  8. #include "ui/views/view.h"
  9. namespace atom {
  10. NativeBrowserViewViews::NativeBrowserViewViews(
  11. InspectableWebContents* inspectable_web_contents)
  12. : NativeBrowserView(inspectable_web_contents) {}
  13. NativeBrowserViewViews::~NativeBrowserViewViews() {}
  14. void NativeBrowserViewViews::SetAutoResizeFlags(uint8_t flags) {
  15. auto_resize_flags_ = flags;
  16. ResetAutoResizeProportions();
  17. }
  18. void NativeBrowserViewViews::SetAutoResizeProportions(
  19. const gfx::Size& window_size) {
  20. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) &&
  21. !auto_horizontal_proportion_set_) {
  22. auto* view = GetInspectableWebContentsView()->GetView();
  23. auto view_bounds = view->bounds();
  24. auto_horizontal_proportion_width_ =
  25. static_cast<float>(window_size.width()) /
  26. static_cast<float>(view_bounds.width());
  27. auto_horizontal_proportion_left_ = static_cast<float>(window_size.width()) /
  28. static_cast<float>(view_bounds.x());
  29. auto_horizontal_proportion_set_ = true;
  30. }
  31. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) &&
  32. !auto_vertical_proportion_set_) {
  33. auto* view = GetInspectableWebContentsView()->GetView();
  34. auto view_bounds = view->bounds();
  35. auto_vertical_proportion_height_ =
  36. static_cast<float>(window_size.height()) /
  37. static_cast<float>(view_bounds.height());
  38. auto_vertical_proportion_top_ = static_cast<float>(window_size.height()) /
  39. static_cast<float>(view_bounds.y());
  40. auto_vertical_proportion_set_ = true;
  41. }
  42. }
  43. void NativeBrowserViewViews::AutoResize(const gfx::Rect& new_window,
  44. int width_delta,
  45. int height_delta) {
  46. auto* view = GetInspectableWebContentsView()->GetView();
  47. const auto flags = GetAutoResizeFlags();
  48. if (!(flags & kAutoResizeWidth)) {
  49. width_delta = 0;
  50. }
  51. if (!(flags & kAutoResizeHeight)) {
  52. height_delta = 0;
  53. }
  54. if (height_delta || width_delta) {
  55. auto new_view_size = view->size();
  56. new_view_size.set_width(new_view_size.width() + width_delta);
  57. new_view_size.set_height(new_view_size.height() + height_delta);
  58. view->SetSize(new_view_size);
  59. }
  60. auto new_view_bounds = view->bounds();
  61. if (flags & kAutoResizeHorizontal) {
  62. new_view_bounds.set_width(new_window.width() /
  63. auto_horizontal_proportion_width_);
  64. new_view_bounds.set_x(new_window.width() /
  65. auto_horizontal_proportion_left_);
  66. }
  67. if (flags & kAutoResizeVertical) {
  68. new_view_bounds.set_height(new_window.height() /
  69. auto_vertical_proportion_height_);
  70. new_view_bounds.set_y(new_window.height() / auto_vertical_proportion_top_);
  71. }
  72. if ((flags & kAutoResizeHorizontal) || (flags & kAutoResizeVertical)) {
  73. view->SetBoundsRect(new_view_bounds);
  74. }
  75. }
  76. void NativeBrowserViewViews::ResetAutoResizeProportions() {
  77. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) {
  78. auto_horizontal_proportion_set_ = false;
  79. }
  80. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) {
  81. auto_vertical_proportion_set_ = false;
  82. }
  83. }
  84. void NativeBrowserViewViews::SetBounds(const gfx::Rect& bounds) {
  85. auto* view = GetInspectableWebContentsView()->GetView();
  86. view->SetBoundsRect(bounds);
  87. ResetAutoResizeProportions();
  88. }
  89. void NativeBrowserViewViews::SetBackgroundColor(SkColor color) {
  90. auto* view = GetInspectableWebContentsView()->GetView();
  91. view->SetBackground(views::CreateSolidBackground(color));
  92. view->SchedulePaint();
  93. }
  94. // static
  95. NativeBrowserView* NativeBrowserView::Create(
  96. InspectableWebContents* inspectable_web_contents) {
  97. return new NativeBrowserViewViews(inspectable_web_contents);
  98. }
  99. } // namespace atom