native_browser_view_views.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "shell/browser/ui/drag_util.h"
  9. #include "shell/browser/ui/inspectable_web_contents_view.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/views/background.h"
  12. #include "ui/views/view.h"
  13. namespace electron {
  14. NativeBrowserViewViews::NativeBrowserViewViews(
  15. InspectableWebContents* inspectable_web_contents)
  16. : NativeBrowserView(inspectable_web_contents) {}
  17. NativeBrowserViewViews::~NativeBrowserViewViews() = default;
  18. void NativeBrowserViewViews::SetAutoResizeFlags(uint8_t flags) {
  19. auto_resize_flags_ = flags;
  20. ResetAutoResizeProportions();
  21. }
  22. void NativeBrowserViewViews::UpdateDraggableRegions(
  23. const std::vector<mojom::DraggableRegionPtr>& 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. auto snapped_regions = mojo::Clone(regions);
  33. for (auto& snapped_region : snapped_regions) {
  34. snapped_region->bounds.Offset(offset);
  35. }
  36. draggable_region_ = DraggableRegionsToSkRegion(snapped_regions);
  37. }
  38. void NativeBrowserViewViews::SetAutoResizeProportions(
  39. const gfx::Size& window_size) {
  40. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) &&
  41. !auto_horizontal_proportion_set_) {
  42. auto* iwc_view = GetInspectableWebContentsView();
  43. if (!iwc_view)
  44. return;
  45. auto* view = iwc_view->GetView();
  46. auto view_bounds = view->bounds();
  47. auto_horizontal_proportion_width_ =
  48. static_cast<float>(window_size.width()) /
  49. static_cast<float>(view_bounds.width());
  50. auto_horizontal_proportion_left_ = static_cast<float>(window_size.width()) /
  51. static_cast<float>(view_bounds.x());
  52. auto_horizontal_proportion_set_ = true;
  53. }
  54. if ((auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) &&
  55. !auto_vertical_proportion_set_) {
  56. auto* iwc_view = GetInspectableWebContentsView();
  57. if (!iwc_view)
  58. return;
  59. auto* view = iwc_view->GetView();
  60. auto view_bounds = view->bounds();
  61. auto_vertical_proportion_height_ =
  62. static_cast<float>(window_size.height()) /
  63. static_cast<float>(view_bounds.height());
  64. auto_vertical_proportion_top_ = static_cast<float>(window_size.height()) /
  65. static_cast<float>(view_bounds.y());
  66. auto_vertical_proportion_set_ = true;
  67. }
  68. }
  69. void NativeBrowserViewViews::AutoResize(const gfx::Rect& new_window,
  70. int width_delta,
  71. int height_delta) {
  72. auto* iwc_view = GetInspectableWebContentsView();
  73. if (!iwc_view)
  74. return;
  75. auto* view = iwc_view->GetView();
  76. const auto flags = GetAutoResizeFlags();
  77. if (!(flags & kAutoResizeWidth)) {
  78. width_delta = 0;
  79. }
  80. if (!(flags & kAutoResizeHeight)) {
  81. height_delta = 0;
  82. }
  83. if (height_delta || width_delta) {
  84. auto new_view_size = view->size();
  85. new_view_size.set_width(new_view_size.width() + width_delta);
  86. new_view_size.set_height(new_view_size.height() + height_delta);
  87. view->SetSize(new_view_size);
  88. }
  89. auto new_view_bounds = view->bounds();
  90. if (flags & kAutoResizeHorizontal) {
  91. new_view_bounds.set_width(new_window.width() /
  92. auto_horizontal_proportion_width_);
  93. new_view_bounds.set_x(new_window.width() /
  94. auto_horizontal_proportion_left_);
  95. }
  96. if (flags & kAutoResizeVertical) {
  97. new_view_bounds.set_height(new_window.height() /
  98. auto_vertical_proportion_height_);
  99. new_view_bounds.set_y(new_window.height() / auto_vertical_proportion_top_);
  100. }
  101. if ((flags & kAutoResizeHorizontal) || (flags & kAutoResizeVertical)) {
  102. view->SetBoundsRect(new_view_bounds);
  103. }
  104. }
  105. void NativeBrowserViewViews::ResetAutoResizeProportions() {
  106. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeHorizontal) {
  107. auto_horizontal_proportion_set_ = false;
  108. }
  109. if (auto_resize_flags_ & AutoResizeFlags::kAutoResizeVertical) {
  110. auto_vertical_proportion_set_ = false;
  111. }
  112. }
  113. void NativeBrowserViewViews::SetBounds(const gfx::Rect& bounds) {
  114. auto* iwc_view = GetInspectableWebContentsView();
  115. if (!iwc_view)
  116. return;
  117. auto* view = iwc_view->GetView();
  118. view->SetBoundsRect(bounds);
  119. ResetAutoResizeProportions();
  120. }
  121. gfx::Rect NativeBrowserViewViews::GetBounds() {
  122. auto* iwc_view = GetInspectableWebContentsView();
  123. if (!iwc_view)
  124. return gfx::Rect();
  125. return iwc_view->GetView()->bounds();
  126. }
  127. void NativeBrowserViewViews::SetBackgroundColor(SkColor color) {
  128. auto* iwc_view = GetInspectableWebContentsView();
  129. if (!iwc_view)
  130. return;
  131. auto* view = iwc_view->GetView();
  132. view->SetBackground(views::CreateSolidBackground(color));
  133. view->SchedulePaint();
  134. }
  135. // static
  136. NativeBrowserView* NativeBrowserView::Create(
  137. InspectableWebContents* inspectable_web_contents) {
  138. return new NativeBrowserViewViews(inspectable_web_contents);
  139. }
  140. } // namespace electron