native_browser_view_views.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <vector>
  6. #include "shell/browser/ui/drag_util.h"
  7. #include "shell/browser/ui/views/inspectable_web_contents_view_views.h"
  8. #include "ui/gfx/geometry/rect.h"
  9. #include "ui/views/background.h"
  10. #include "ui/views/view.h"
  11. namespace electron {
  12. NativeBrowserViewViews::NativeBrowserViewViews(
  13. InspectableWebContents* inspectable_web_contents)
  14. : NativeBrowserView(inspectable_web_contents) {}
  15. NativeBrowserViewViews::~NativeBrowserViewViews() = default;
  16. void NativeBrowserViewViews::SetAutoResizeFlags(uint8_t flags) {
  17. auto_resize_flags_ = flags;
  18. ResetAutoResizeProportions();
  19. }
  20. void NativeBrowserViewViews::UpdateDraggableRegions(
  21. const std::vector<mojom::DraggableRegionPtr>& regions) {
  22. if (&draggable_regions_ != &regions)
  23. draggable_regions_ = mojo::Clone(regions);
  24. // We need to snap the regions to the bounds of the current BrowserView.
  25. // For example, if an attached BrowserView is draggable but its bounds are
  26. // { x: 200, y: 100, width: 300, height: 300 }
  27. // then we need to add 200 to the x-value and 100 to the
  28. // y-value of each of the passed regions or it will be incorrectly
  29. // assumed that the regions begin in the top left corner as they
  30. // would for the main client window.
  31. auto const offset = GetBounds().OffsetFromOrigin();
  32. for (auto& snapped_region : draggable_regions_) {
  33. snapped_region->bounds.Offset(offset);
  34. }
  35. draggable_region_ = DraggableRegionsToSkRegion(draggable_regions_);
  36. }
  37. void NativeBrowserViewViews::SetAutoResizeProportions(
  38. const gfx::Size& window_size) {
  39. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) &&
  40. !auto_horizontal_proportion_set_) {
  41. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  42. if (!iwc_view)
  43. return;
  44. auto* view = iwc_view->GetView();
  45. auto view_bounds = view->bounds();
  46. auto_horizontal_proportion_width_ =
  47. static_cast<float>(window_size.width()) /
  48. static_cast<float>(view_bounds.width());
  49. auto_horizontal_proportion_left_ = static_cast<float>(window_size.width()) /
  50. static_cast<float>(view_bounds.x());
  51. auto_horizontal_proportion_set_ = true;
  52. }
  53. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) &&
  54. !auto_vertical_proportion_set_) {
  55. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  56. if (!iwc_view)
  57. return;
  58. auto* view = iwc_view->GetView();
  59. auto view_bounds = view->bounds();
  60. auto_vertical_proportion_height_ =
  61. static_cast<float>(window_size.height()) /
  62. static_cast<float>(view_bounds.height());
  63. auto_vertical_proportion_top_ = static_cast<float>(window_size.height()) /
  64. static_cast<float>(view_bounds.y());
  65. auto_vertical_proportion_set_ = true;
  66. }
  67. }
  68. void NativeBrowserViewViews::AutoResize(const gfx::Rect& new_window,
  69. int width_delta,
  70. int height_delta) {
  71. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  72. if (!iwc_view)
  73. return;
  74. auto* view = iwc_view->GetView();
  75. const auto flags = GetAutoResizeFlags();
  76. if (!(flags & kAutoResizeWidth)) {
  77. width_delta = 0;
  78. }
  79. if (!(flags & kAutoResizeHeight)) {
  80. height_delta = 0;
  81. }
  82. if (height_delta || width_delta) {
  83. auto new_view_size = view->size();
  84. new_view_size.set_width(new_view_size.width() + width_delta);
  85. new_view_size.set_height(new_view_size.height() + height_delta);
  86. view->SetSize(new_view_size);
  87. }
  88. auto new_view_bounds = view->bounds();
  89. if (flags & kAutoResizeHorizontal) {
  90. new_view_bounds.set_width(new_window.width() /
  91. auto_horizontal_proportion_width_);
  92. new_view_bounds.set_x(new_window.width() /
  93. auto_horizontal_proportion_left_);
  94. }
  95. if (flags & kAutoResizeVertical) {
  96. new_view_bounds.set_height(new_window.height() /
  97. auto_vertical_proportion_height_);
  98. new_view_bounds.set_y(new_window.height() / auto_vertical_proportion_top_);
  99. }
  100. if ((flags & kAutoResizeHorizontal) || (flags & kAutoResizeVertical)) {
  101. view->SetBoundsRect(new_view_bounds);
  102. }
  103. }
  104. void NativeBrowserViewViews::ResetAutoResizeProportions() {
  105. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) {
  106. auto_horizontal_proportion_set_ = false;
  107. }
  108. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) {
  109. auto_vertical_proportion_set_ = false;
  110. }
  111. }
  112. void NativeBrowserViewViews::SetBounds(const gfx::Rect& bounds) {
  113. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  114. if (!iwc_view)
  115. return;
  116. auto* view = iwc_view->GetView();
  117. view->SetBoundsRect(bounds);
  118. ResetAutoResizeProportions();
  119. view->InvalidateLayout();
  120. view->SchedulePaint();
  121. // Ensure draggable regions are properly updated to reflect new bounds.
  122. UpdateDraggableRegions(draggable_regions_);
  123. }
  124. gfx::Rect NativeBrowserViewViews::GetBounds() {
  125. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  126. if (!iwc_view)
  127. return gfx::Rect();
  128. return iwc_view->GetView()->bounds();
  129. }
  130. void NativeBrowserViewViews::RenderViewReady() {
  131. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  132. if (iwc_view)
  133. iwc_view->GetView()->Layout();
  134. }
  135. void NativeBrowserViewViews::SetBackgroundColor(SkColor color) {
  136. InspectableWebContentsView* iwc_view = GetInspectableWebContentsView();
  137. if (!iwc_view)
  138. return;
  139. auto* view = iwc_view->GetView();
  140. view->SetBackground(views::CreateSolidBackground(color));
  141. view->SchedulePaint();
  142. }
  143. // static
  144. NativeBrowserView* NativeBrowserView::Create(
  145. InspectableWebContents* inspectable_web_contents) {
  146. return new NativeBrowserViewViews(inspectable_web_contents);
  147. }
  148. } // namespace electron