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> |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 10 | #include "ash/host/ash_window_tree_host_mirroring_delegate.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 11 | #include "ash/host/root_window_transformer.h" |
| 12 | #include "base/logging.h" |
Ganesh Borle | 0113f1b | 2018-03-01 08:44:02 | [diff] [blame] | 13 | #include "base/stl_util.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 14 | #include "ui/aura/window.h" |
| 15 | #include "ui/aura/window_event_dispatcher.h" |
| 16 | #include "ui/aura/window_targeter.h" |
| 17 | #include "ui/compositor/compositor.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 18 | #include "ui/gfx/geometry/insets.h" |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 19 | #include "ui/platform_window/stub/stub_window.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 20 | |
| 21 | namespace ash { |
| 22 | |
| 23 | class UnifiedEventTargeter : public aura::WindowTargeter { |
| 24 | public: |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 25 | UnifiedEventTargeter(aura::Window* src_root, |
| 26 | aura::Window* dst_root, |
| 27 | AshWindowTreeHostMirroringDelegate* delegate) |
| 28 | : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) { |
| 29 | DCHECK(delegate); |
| 30 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 31 | |
| 32 | ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 33 | ui::Event* event) override { |
| 34 | if (root == src_root_ && !event->target()) { |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 35 | delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost()); |
| 36 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 37 | if (event->IsLocatedEvent()) { |
| 38 | ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); |
| 39 | located_event->ConvertLocationToTarget( |
| 40 | static_cast<aura::Window*>(nullptr), dst_root_); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 41 | } |
| 42 | ignore_result( |
penghuang | 17134c6c | 2017-03-23 00:01:07 | [diff] [blame] | 43 | dst_root_->GetHost()->event_sink()->OnEventFromSource(event)); |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 44 | |
| 45 | // Reset the source host. |
| 46 | delegate_->SetCurrentEventTargeterSourceHost(nullptr); |
| 47 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 48 | return nullptr; |
| 49 | } else { |
oshima | f8b2769 | 2015-04-30 07:23:00 | [diff] [blame] | 50 | NOTREACHED() << "event type:" << event->type(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 51 | return aura::WindowTargeter::FindTargetForEvent(root, event); |
| 52 | } |
| 53 | } |
| 54 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 55 | private: |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 56 | aura::Window* src_root_; |
| 57 | aura::Window* dst_root_; |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 58 | AshWindowTreeHostMirroringDelegate* delegate_; // Not owned. |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter); |
| 61 | }; |
| 62 | |
| 63 | AshWindowTreeHostUnified::AshWindowTreeHostUnified( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 64 | const gfx::Rect& initial_bounds, |
| 65 | AshWindowTreeHostMirroringDelegate* delegate) |
| 66 | : AshWindowTreeHostPlatform(), delegate_(delegate) { |
| 67 | DCHECK(delegate); |
Maksim Sisov | 08b20ec1 | 2019-11-25 13:20:58 | [diff] [blame^] | 68 | std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this)); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 69 | window->SetBounds(initial_bounds); |
| 70 | SetPlatformWindow(std::move(window)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 74 | for (auto* ash_host : mirroring_hosts_) |
| 75 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 79 | AshWindowTreeHostPlatform::PrepareForShutdown(); |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 80 | |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 81 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 82 | host->PrepareForShutdown(); |
| 83 | } |
| 84 | |
| 85 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 86 | AshWindowTreeHost* mirroring_ash_host) { |
| 87 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 88 | src_root->SetEventTargeter( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 89 | std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_)); |
Jan Wilken Dörrie | 58b0c4b | 2019-06-06 18:44:34 | [diff] [blame] | 90 | DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 91 | mirroring_hosts_.push_back(mirroring_ash_host); |
| 92 | mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this); |
| 93 | } |
| 94 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 95 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 96 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 97 | host->AsWindowTreeHost()->SetCursor(cursor); |
| 98 | } |
| 99 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 100 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
vmpstr | eb900a1 | 2016-06-29 02:22:28 | [diff] [blame] | 101 | for (auto* host : mirroring_hosts_) |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 102 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
| 103 | } |
| 104 | |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 105 | void AshWindowTreeHostUnified::OnBoundsChanged(const gfx::Rect& bounds) { |
| 106 | if (platform_window()) |
riajiang | 8077b13 | 2016-11-29 23:35:36 | [diff] [blame] | 107 | OnHostResizedInPixels(bounds.size()); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 108 | } |
| 109 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 110 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
| 111 | auto iter = |
| 112 | std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 113 | [window](AshWindowTreeHost* ash_host) { |
| 114 | return ash_host->AsWindowTreeHost()->window() == window; |
| 115 | }); |
| 116 | DCHECK(iter != mirroring_hosts_.end()); |
| 117 | window->RemoveObserver(this); |
| 118 | mirroring_hosts_.erase(iter); |
| 119 | } |
| 120 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 121 | } // namespace ash |