frame_layout_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "fuchsia_web/webengine/browser/frame_layout_manager.h"
  5. #include "ui/aura/window_tree_host.h"
  6. #include "ui/compositor/compositor.h"
  7. #include "ui/gfx/geometry/size.h"
  8. #include "ui/gfx/geometry/transform.h"
  9. namespace {
  10. // Returns a scaling factor that will allow |inset| to fit fully inside
  11. // |container| without clipping.
  12. float ProportionalScale(gfx::Size inset, gfx::Size container) {
  13. gfx::SizeF inset_f(inset);
  14. gfx::SizeF container_f(container);
  15. const float container_aspect_ratio =
  16. container_f.width() / container_f.height();
  17. const float inset_aspect_ratio = inset_f.width() / inset_f.height();
  18. if (container_aspect_ratio > inset_aspect_ratio) {
  19. // Height is constraining.
  20. return container_f.height() / inset_f.height();
  21. } else {
  22. // Width is constraining.
  23. return container_f.width() / inset_f.width();
  24. }
  25. }
  26. } // namespace
  27. FrameLayoutManager::FrameLayoutManager() = default;
  28. FrameLayoutManager::~FrameLayoutManager() = default;
  29. void FrameLayoutManager::ForceContentDimensions(gfx::Size size) {
  30. render_size_override_ = size;
  31. UpdateContentBounds();
  32. }
  33. void FrameLayoutManager::OnWindowResized() {
  34. // Resize the child to match the size of the parent.
  35. UpdateContentBounds();
  36. }
  37. void FrameLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
  38. if (child->GetType() == aura::client::WINDOW_TYPE_CONTROL) {
  39. DCHECK(!main_child_);
  40. main_child_ = child;
  41. SetChildBoundsDirect(main_child_,
  42. gfx::Rect(main_child_->parent()->bounds().size()));
  43. UpdateContentBounds();
  44. // Make the compositor's background transparent by default.
  45. main_child_->parent()->GetHost()->compositor()->SetBackgroundColor(
  46. SK_AlphaTRANSPARENT);
  47. main_child_->GetHost()->compositor()->SetBackgroundColor(
  48. SK_AlphaTRANSPARENT);
  49. }
  50. }
  51. void FrameLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
  52. if (child->GetType() == aura::client::WINDOW_TYPE_CONTROL) {
  53. DCHECK_EQ(child, main_child_);
  54. main_child_ = nullptr;
  55. }
  56. }
  57. void FrameLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {}
  58. void FrameLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
  59. bool visible) {}
  60. void FrameLayoutManager::SetChildBounds(aura::Window* child,
  61. const gfx::Rect& requested_bounds) {
  62. if (child != main_child_)
  63. SetChildBoundsDirect(child, requested_bounds);
  64. }
  65. void FrameLayoutManager::UpdateContentBounds() {
  66. if (!main_child_)
  67. return;
  68. const gfx::Size actual_size(main_child_->parent()->bounds().size());
  69. if (render_size_override_.IsEmpty()) {
  70. // Use all of the area available to the View.
  71. SetChildBoundsDirect(main_child_, gfx::Rect(actual_size));
  72. main_child_->SetTransform(gfx::Transform());
  73. return;
  74. }
  75. SetChildBoundsDirect(main_child_, gfx::Rect(render_size_override_));
  76. // Scale the window to fit in the View without clipping.
  77. const float scale = ProportionalScale(render_size_override_, actual_size);
  78. gfx::Transform transform;
  79. transform.Scale(scale, scale);
  80. // Center the window.
  81. const float center_x_offset =
  82. (actual_size.width() - (render_size_override_.width() * scale)) / 2.0;
  83. const float center_y_offset =
  84. (actual_size.height() - (render_size_override_.height() * scale)) / 2.0;
  85. transform.Translate(center_x_offset, center_y_offset);
  86. main_child_->SetTransform(transform);
  87. }