native_browser_view_views.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "shell/browser/native_browser_view_views.h"
  5. #include "shell/browser/ui/views/inspectable_web_contents_view_views.h"
  6. #include "ui/gfx/geometry/rect.h"
  7. #include "ui/views/background.h"
  8. #include "ui/views/view.h"
  9. namespace electron {
  10. NativeBrowserViewViews::NativeBrowserViewViews(
  11. InspectableWebContents* inspectable_web_contents)
  12. : NativeBrowserView(inspectable_web_contents) {}
  13. NativeBrowserViewViews::~NativeBrowserViewViews() = default;
  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. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  23. if (!iwc_view)
  24. return;
  25. auto* view = iwc_view->GetView();
  26. auto view_bounds = view->bounds();
  27. auto_horizontal_proportion_width_ =
  28. static_cast<float>(window_size.width()) /
  29. static_cast<float>(view_bounds.width());
  30. auto_horizontal_proportion_left_ = static_cast<float>(window_size.width()) /
  31. static_cast<float>(view_bounds.x());
  32. auto_horizontal_proportion_set_ = true;
  33. }
  34. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) &&
  35. !auto_vertical_proportion_set_) {
  36. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  37. if (!iwc_view)
  38. return;
  39. auto* view = iwc_view->GetView();
  40. auto view_bounds = view->bounds();
  41. auto_vertical_proportion_height_ =
  42. static_cast<float>(window_size.height()) /
  43. static_cast<float>(view_bounds.height());
  44. auto_vertical_proportion_top_ = static_cast<float>(window_size.height()) /
  45. static_cast<float>(view_bounds.y());
  46. auto_vertical_proportion_set_ = true;
  47. }
  48. }
  49. void NativeBrowserViewViews::AutoResize(const gfx::Rect& new_window,
  50. int width_delta,
  51. int height_delta) {
  52. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  53. if (!iwc_view)
  54. return;
  55. auto* view = iwc_view->GetView();
  56. const auto flags = GetAutoResizeFlags();
  57. if (!(flags & kAutoResizeWidth)) {
  58. width_delta = 0;
  59. }
  60. if (!(flags & kAutoResizeHeight)) {
  61. height_delta = 0;
  62. }
  63. if (height_delta || width_delta) {
  64. auto new_view_size = view->size();
  65. new_view_size.set_width(new_view_size.width() + width_delta);
  66. new_view_size.set_height(new_view_size.height() + height_delta);
  67. view->SetSize(new_view_size);
  68. }
  69. auto new_view_bounds = view->bounds();
  70. if (flags & kAutoResizeHorizontal) {
  71. new_view_bounds.set_width(new_window.width() /
  72. auto_horizontal_proportion_width_);
  73. new_view_bounds.set_x(new_window.width() /
  74. auto_horizontal_proportion_left_);
  75. }
  76. if (flags & kAutoResizeVertical) {
  77. new_view_bounds.set_height(new_window.height() /
  78. auto_vertical_proportion_height_);
  79. new_view_bounds.set_y(new_window.height() / auto_vertical_proportion_top_);
  80. }
  81. if ((flags & kAutoResizeHorizontal) || (flags & kAutoResizeVertical)) {
  82. view->SetBoundsRect(new_view_bounds);
  83. }
  84. }
  85. void NativeBrowserViewViews::ResetAutoResizeProportions() {
  86. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) {
  87. auto_horizontal_proportion_set_ = false;
  88. }
  89. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) {
  90. auto_vertical_proportion_set_ = false;
  91. }
  92. }
  93. void NativeBrowserViewViews::SetBounds(const gfx::Rect& bounds) {
  94. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  95. if (!iwc_view)
  96. return;
  97. auto* view = iwc_view->GetView();
  98. view->SetBoundsRect(bounds);
  99. ResetAutoResizeProportions();
  100. view->InvalidateLayout();
  101. view->SchedulePaint();
  102. }
  103. gfx::Rect NativeBrowserViewViews::GetBounds() {
  104. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  105. if (!iwc_view)
  106. return gfx::Rect();
  107. return iwc_view->GetView()->bounds();
  108. }
  109. void NativeBrowserViewViews::RenderViewReady() {
  110. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  111. if (iwc_view)
  112. iwc_view->GetView()->Layout();
  113. }
  114. void NativeBrowserViewViews::SetBackgroundColor(SkColor color) {
  115. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  116. if (!iwc_view)
  117. return;
  118. auto* view = iwc_view->GetView();
  119. view->SetBackground(views::CreateSolidBackground(color));
  120. view->SchedulePaint();
  121. }
  122. // static
  123. NativeBrowserView* NativeBrowserView::Create(
  124. InspectableWebContents* inspectable_web_contents) {
  125. return new NativeBrowserViewViews(inspectable_web_contents);
  126. }
  127. } // namespace electron