frame_window_tree_host.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_window_tree_host.h"
  5. #include "base/fuchsia/fuchsia_logging.h"
  6. #include "content/public/browser/render_widget_host_view.h"
  7. #include "content/public/browser/web_contents.h"
  8. #include "fuchsia_web/webengine/features.h"
  9. #include "ui/aura/client/focus_client.h"
  10. #include "ui/aura/client/window_parenting_client.h"
  11. #include "ui/base/ime/input_method.h"
  12. #include "ui/platform_window/platform_window_init_properties.h"
  13. namespace {
  14. fuchsia::ui::views::ViewRef DupViewRef(
  15. const fuchsia::ui::views::ViewRef& view_ref) {
  16. fuchsia::ui::views::ViewRef dup;
  17. zx_status_t status =
  18. view_ref.reference.duplicate(ZX_RIGHT_SAME_RIGHTS, &dup.reference);
  19. ZX_CHECK(status == ZX_OK, status) << "zx_object_duplicate";
  20. return dup;
  21. }
  22. ui::PlatformWindowInitProperties CreatePlatformWindowInitProperties(
  23. scenic::ViewRefPair view_ref_pair,
  24. ui::ScenicWindowDelegate* scenic_window_delegate) {
  25. ui::PlatformWindowInitProperties properties;
  26. properties.view_ref_pair = std::move(view_ref_pair);
  27. properties.enable_keyboard =
  28. base::FeatureList::IsEnabled(features::kKeyboardInput);
  29. properties.enable_virtual_keyboard =
  30. base::FeatureList::IsEnabled(features::kVirtualKeyboard);
  31. properties.scenic_window_delegate = scenic_window_delegate;
  32. return properties;
  33. }
  34. } // namespace
  35. class FrameWindowTreeHost::WindowParentingClientImpl
  36. : public aura::client::WindowParentingClient {
  37. public:
  38. explicit WindowParentingClientImpl(aura::Window* root_window)
  39. : root_window_(root_window) {
  40. aura::client::SetWindowParentingClient(root_window_, this);
  41. }
  42. ~WindowParentingClientImpl() override {
  43. aura::client::SetWindowParentingClient(root_window_, nullptr);
  44. }
  45. WindowParentingClientImpl(const WindowParentingClientImpl&) = delete;
  46. WindowParentingClientImpl& operator=(const WindowParentingClientImpl&) =
  47. delete;
  48. // WindowParentingClient implementation.
  49. aura::Window* GetDefaultParent(aura::Window* window,
  50. const gfx::Rect& bounds) override {
  51. return root_window_;
  52. }
  53. private:
  54. aura::Window* root_window_;
  55. };
  56. FrameWindowTreeHost::FrameWindowTreeHost(
  57. fuchsia::ui::views::ViewToken view_token,
  58. scenic::ViewRefPair view_ref_pair,
  59. content::WebContents* web_contents,
  60. OnPixelScaleUpdateCallback on_pixel_scale_update)
  61. : view_ref_(DupViewRef(view_ref_pair.view_ref)),
  62. web_contents_(web_contents),
  63. on_pixel_scale_update_(std::move(on_pixel_scale_update)) {
  64. CreateCompositor();
  65. ui::PlatformWindowInitProperties properties =
  66. CreatePlatformWindowInitProperties(std::move(view_ref_pair), this);
  67. properties.view_token = std::move(view_token);
  68. CreateAndSetPlatformWindow(std::move(properties));
  69. window_parenting_client_ =
  70. std::make_unique<WindowParentingClientImpl>(window());
  71. }
  72. FrameWindowTreeHost::FrameWindowTreeHost(
  73. fuchsia::ui::views::ViewCreationToken view_creation_token,
  74. scenic::ViewRefPair view_ref_pair,
  75. content::WebContents* web_contents,
  76. OnPixelScaleUpdateCallback on_pixel_scale_update)
  77. : view_ref_(DupViewRef(view_ref_pair.view_ref)),
  78. web_contents_(web_contents),
  79. on_pixel_scale_update_(std::move(on_pixel_scale_update)) {
  80. CreateCompositor();
  81. ui::PlatformWindowInitProperties properties =
  82. CreatePlatformWindowInitProperties(std::move(view_ref_pair), this);
  83. properties.view_creation_token = std::move(view_creation_token);
  84. CreateAndSetPlatformWindow(std::move(properties));
  85. window_parenting_client_ =
  86. std::make_unique<WindowParentingClientImpl>(window());
  87. }
  88. FrameWindowTreeHost::~FrameWindowTreeHost() = default;
  89. fuchsia::ui::views::ViewRef FrameWindowTreeHost::CreateViewRef() {
  90. return DupViewRef(view_ref_);
  91. }
  92. void FrameWindowTreeHost::OnActivationChanged(bool active) {
  93. // Route focus & blur events to the window's focus observer and its
  94. // InputMethod.
  95. if (active) {
  96. aura::client::GetFocusClient(window())->FocusWindow(window());
  97. GetInputMethod()->OnFocus();
  98. } else {
  99. aura::client::GetFocusClient(window())->FocusWindow(nullptr);
  100. GetInputMethod()->OnBlur();
  101. }
  102. }
  103. void FrameWindowTreeHost::OnWindowStateChanged(
  104. ui::PlatformWindowState old_state,
  105. ui::PlatformWindowState new_state) {
  106. // Tell the root aura::Window whether it is shown or hidden.
  107. if (new_state == ui::PlatformWindowState::kMinimized) {
  108. Hide();
  109. web_contents_->WasOccluded();
  110. } else {
  111. Show();
  112. web_contents_->WasShown();
  113. }
  114. }
  115. void FrameWindowTreeHost::OnWindowBoundsChanged(const BoundsChange& bounds) {
  116. aura::WindowTreeHostPlatform::OnBoundsChanged(bounds);
  117. if (web_contents_->GetPrimaryMainFrame()->IsRenderFrameLive()) {
  118. web_contents_->GetPrimaryMainFrame()->GetView()->SetInsets(
  119. bounds.system_ui_overlap);
  120. }
  121. }
  122. void FrameWindowTreeHost::OnScenicPixelScale(ui::PlatformWindow* window,
  123. float scale) {
  124. scenic_pixel_scale_ = scale;
  125. if (on_pixel_scale_update_)
  126. on_pixel_scale_update_.Run(scenic_pixel_scale_);
  127. }