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