native_browser_view_views.cc 5.7 KB

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