oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 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 | |
| 5 | #include "ash/host/ash_window_tree_host_unified.h" |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 6 | |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 7 | #include <memory> |
Avi Drissman | 4155d51d | 2022-01-10 20:40:13 | [diff] [blame] | 8 | #include <tuple> |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 11 | #include "ash/host/ash_window_tree_host_delegate.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 12 | #include "ash/host/root_window_transformer.h" |
Hans Wennborg | 3769645 | 2020-04-24 18:28:49 | [diff] [blame] | 13 | #include "base/check.h" |
Jan Wilken Dörrie | b5a41c3 | 2020-12-09 18:55:47 | [diff] [blame] | 14 | #include "base/containers/contains.h" |
Hans Wennborg | 3769645 | 2020-04-24 18:28:49 | [diff] [blame] | 15 | #include "base/notreached.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 16 | #include "ui/aura/window.h" |
| 17 | #include "ui/aura/window_event_dispatcher.h" |
| 18 | #include "ui/aura/window_targeter.h" |
| 19 | #include "ui/compositor/compositor.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 20 | #include "ui/gfx/geometry/insets.h" |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 21 | #include "ui/platform_window/stub/stub_window.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 22 | |
| 23 | namespace ash { |
| 24 | |
| 25 | class UnifiedEventTargeter : public aura::WindowTargeter { |
| 26 | public: |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 27 | UnifiedEventTargeter(aura::Window* src_root, |
| 28 | aura::Window* dst_root, |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 29 | AshWindowTreeHostDelegate* delegate) |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 30 | : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) { |
| 31 | DCHECK(delegate); |
| 32 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 33 | |
Peter Boström | 5fc1d31 | 2021-09-24 02:32:15 | [diff] [blame] | 34 | UnifiedEventTargeter(const UnifiedEventTargeter&) = delete; |
| 35 | UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete; |
| 36 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 37 | ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 38 | ui::Event* event) override { |
| 39 | if (root == src_root_ && !event->target()) { |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 40 | delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost()); |
| 41 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 42 | if (event->IsLocatedEvent()) { |
| 43 | ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); |
| 44 | located_event->ConvertLocationToTarget( |
| 45 | static_cast<aura::Window*>(nullptr), dst_root_); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 46 | } |
Avi Drissman | 4155d51d | 2022-01-10 20:40:13 | [diff] [blame] | 47 | std::ignore = |
| 48 | dst_root_->GetHost()->GetEventSink()->OnEventFromSource(event); |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 49 | |
| 50 | // Reset the source host. |
| 51 | delegate_->SetCurrentEventTargeterSourceHost(nullptr); |
| 52 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 53 | return nullptr; |
| 54 | } else { |
oshima | f8b2769 | 2015-04-30 07:23:00 | [diff] [blame] | 55 | NOTREACHED() << "event type:" << event->type(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 56 | return aura::WindowTargeter::FindTargetForEvent(root, event); |
| 57 | } |
| 58 | } |
| 59 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 60 | private: |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 61 | aura::Window* src_root_; |
| 62 | aura::Window* dst_root_; |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 63 | AshWindowTreeHostDelegate* delegate_; // Not owned. |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | AshWindowTreeHostUnified::AshWindowTreeHostUnified( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 67 | const gfx::Rect& initial_bounds, |
Addison Luh | 592ab5a | 2022-03-10 22:54:50 | [diff] [blame] | 68 | AshWindowTreeHostDelegate* delegate, |
| 69 | size_t compositor_memory_limit_mb) |
Mitsuru Oshima | 90cba8d | 2022-04-01 20:25:00 | [diff] [blame] | 70 | : AshWindowTreeHostPlatform( |
| 71 | std::make_unique<ui::StubWindow>(initial_bounds), |
| 72 | delegate, |
| 73 | compositor_memory_limit_mb) { |
| 74 | ui::StubWindow* stub_window = static_cast<ui::StubWindow*>(platform_window()); |
| 75 | stub_window->InitDelegate(this, true); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 79 | for (auto* ash_host : mirroring_hosts_) |
| 80 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 84 | AshWindowTreeHostPlatform::PrepareForShutdown(); |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 85 | |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 86 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 87 | host->PrepareForShutdown(); |
| 88 | } |
| 89 | |
| 90 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 91 | AshWindowTreeHost* mirroring_ash_host) { |
| 92 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 93 | src_root->SetEventTargeter( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 94 | std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_)); |
Jan Wilken Dörrie | 58b0c4b | 2019-06-06 18:44:34 | [diff] [blame] | 95 | DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 96 | mirroring_hosts_.push_back(mirroring_ash_host); |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 97 | src_root->AddObserver(this); |
| 98 | mirroring_ash_host->UpdateCursorConfig(); |
| 99 | } |
| 100 | |
| 101 | // Do nothing, since mirroring hosts had their cursor config updated when they |
| 102 | // were registered. |
| 103 | void AshWindowTreeHostUnified::UpdateCursorConfig() {} |
| 104 | |
| 105 | void AshWindowTreeHostUnified::ClearCursorConfig() { |
| 106 | for (auto* host : mirroring_hosts_) |
| 107 | host->ClearCursorConfig(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 108 | } |
| 109 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 110 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 111 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 112 | host->AsWindowTreeHost()->SetCursor(cursor); |
| 113 | } |
| 114 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 115 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 116 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 117 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
| 118 | } |
| 119 | |
Mitsuru Oshima | 410e5dc3 | 2022-07-26 20:01:43 | [diff] [blame^] | 120 | void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 121 | if (platform_window()) |
Mitsuru Oshima | 410e5dc3 | 2022-07-26 20:01:43 | [diff] [blame^] | 122 | OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size()); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 123 | } |
| 124 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 125 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
| 126 | auto iter = |
| 127 | std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 128 | [window](AshWindowTreeHost* ash_host) { |
| 129 | return ash_host->AsWindowTreeHost()->window() == window; |
| 130 | }); |
| 131 | DCHECK(iter != mirroring_hosts_.end()); |
| 132 | window->RemoveObserver(this); |
| 133 | mirroring_hosts_.erase(iter); |
| 134 | } |
| 135 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 136 | } // namespace ash |