ash_window_tree_host_unified.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2015 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 "ash/host/ash_window_tree_host_unified.h"
  5. #include <memory>
  6. #include <tuple>
  7. #include <utility>
  8. #include "ash/host/ash_window_tree_host_delegate.h"
  9. #include "ash/host/root_window_transformer.h"
  10. #include "base/check.h"
  11. #include "base/containers/contains.h"
  12. #include "base/notreached.h"
  13. #include "ui/aura/window.h"
  14. #include "ui/aura/window_event_dispatcher.h"
  15. #include "ui/aura/window_targeter.h"
  16. #include "ui/compositor/compositor.h"
  17. #include "ui/gfx/geometry/insets.h"
  18. #include "ui/platform_window/stub/stub_window.h"
  19. namespace ash {
  20. class UnifiedEventTargeter : public aura::WindowTargeter {
  21. public:
  22. UnifiedEventTargeter(aura::Window* src_root,
  23. aura::Window* dst_root,
  24. AshWindowTreeHostDelegate* delegate)
  25. : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
  26. DCHECK(delegate);
  27. }
  28. UnifiedEventTargeter(const UnifiedEventTargeter&) = delete;
  29. UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete;
  30. ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
  31. ui::Event* event) override {
  32. if (root == src_root_ && !event->target()) {
  33. delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost());
  34. if (event->IsLocatedEvent()) {
  35. ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
  36. located_event->ConvertLocationToTarget(
  37. static_cast<aura::Window*>(nullptr), dst_root_);
  38. }
  39. std::ignore =
  40. dst_root_->GetHost()->GetEventSink()->OnEventFromSource(event);
  41. // Reset the source host.
  42. delegate_->SetCurrentEventTargeterSourceHost(nullptr);
  43. return nullptr;
  44. } else {
  45. NOTREACHED() << "event type:" << event->type();
  46. return aura::WindowTargeter::FindTargetForEvent(root, event);
  47. }
  48. }
  49. private:
  50. aura::Window* src_root_;
  51. aura::Window* dst_root_;
  52. AshWindowTreeHostDelegate* delegate_; // Not owned.
  53. };
  54. AshWindowTreeHostUnified::AshWindowTreeHostUnified(
  55. const gfx::Rect& initial_bounds,
  56. AshWindowTreeHostDelegate* delegate,
  57. size_t compositor_memory_limit_mb)
  58. : AshWindowTreeHostPlatform(
  59. std::make_unique<ui::StubWindow>(initial_bounds),
  60. delegate,
  61. compositor_memory_limit_mb) {
  62. ui::StubWindow* stub_window = static_cast<ui::StubWindow*>(platform_window());
  63. stub_window->InitDelegate(this, true);
  64. }
  65. AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
  66. for (auto* ash_host : mirroring_hosts_)
  67. ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
  68. }
  69. void AshWindowTreeHostUnified::PrepareForShutdown() {
  70. AshWindowTreeHostPlatform::PrepareForShutdown();
  71. for (auto* host : mirroring_hosts_)
  72. host->PrepareForShutdown();
  73. }
  74. void AshWindowTreeHostUnified::RegisterMirroringHost(
  75. AshWindowTreeHost* mirroring_ash_host) {
  76. aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
  77. src_root->SetEventTargeter(
  78. std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
  79. DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
  80. mirroring_hosts_.push_back(mirroring_ash_host);
  81. src_root->AddObserver(this);
  82. mirroring_ash_host->UpdateCursorConfig();
  83. }
  84. // Do nothing, since mirroring hosts had their cursor config updated when they
  85. // were registered.
  86. void AshWindowTreeHostUnified::UpdateCursorConfig() {}
  87. void AshWindowTreeHostUnified::ClearCursorConfig() {
  88. for (auto* host : mirroring_hosts_)
  89. host->ClearCursorConfig();
  90. }
  91. void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
  92. for (auto* host : mirroring_hosts_)
  93. host->AsWindowTreeHost()->SetCursor(cursor);
  94. }
  95. void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
  96. for (auto* host : mirroring_hosts_)
  97. host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
  98. }
  99. void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) {
  100. if (platform_window())
  101. OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size());
  102. }
  103. void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
  104. auto iter =
  105. std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
  106. [window](AshWindowTreeHost* ash_host) {
  107. return ash_host->AsWindowTreeHost()->window() == window;
  108. });
  109. DCHECK(iter != mirroring_hosts_.end());
  110. window->RemoveObserver(this);
  111. mirroring_hosts_.erase(iter);
  112. }
  113. } // namespace ash