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