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 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 11 | #include "ash/host/ash_window_tree_host_mirroring_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, |
| 29 | AshWindowTreeHostMirroringDelegate* delegate) |
| 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_; |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 63 | AshWindowTreeHostMirroringDelegate* 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, |
| 68 | AshWindowTreeHostMirroringDelegate* delegate) |
| 69 | : AshWindowTreeHostPlatform(), delegate_(delegate) { |
| 70 | DCHECK(delegate); |
Maksim Sisov | 08b20ec1 | 2019-11-25 13:20:58 | [diff] [blame] | 71 | std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this)); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 72 | window->SetBounds(initial_bounds); |
| 73 | SetPlatformWindow(std::move(window)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 77 | for (auto* ash_host : mirroring_hosts_) |
| 78 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 82 | AshWindowTreeHostPlatform::PrepareForShutdown(); |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 83 | |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 84 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 85 | host->PrepareForShutdown(); |
| 86 | } |
| 87 | |
| 88 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 89 | AshWindowTreeHost* mirroring_ash_host) { |
| 90 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 91 | src_root->SetEventTargeter( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 92 | std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_)); |
Jan Wilken Dörrie | 58b0c4b | 2019-06-06 18:44:34 | [diff] [blame] | 93 | DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 94 | mirroring_hosts_.push_back(mirroring_ash_host); |
| 95 | mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this); |
| 96 | } |
| 97 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 98 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 99 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 100 | host->AsWindowTreeHost()->SetCursor(cursor); |
| 101 | } |
| 102 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 103 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 104 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 105 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
| 106 | } |
| 107 | |
Kevin Marshall | 05a716e | 2021-03-25 02:11:28 | [diff] [blame] | 108 | void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& bounds) { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 109 | if (platform_window()) |
Kevin Marshall | 05a716e | 2021-03-25 02:11:28 | [diff] [blame] | 110 | OnHostResizedInPixels(bounds.bounds.size()); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 111 | } |
| 112 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 113 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
| 114 | auto iter = |
| 115 | std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 116 | [window](AshWindowTreeHost* ash_host) { |
| 117 | return ash_host->AsWindowTreeHost()->window() == window; |
| 118 | }); |
| 119 | DCHECK(iter != mirroring_hosts_.end()); |
| 120 | window->RemoveObserver(this); |
| 121 | mirroring_hosts_.erase(iter); |
| 122 | } |
| 123 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 124 | } // namespace ash |