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 | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame] | 10 | #include "ash/ime/input_method_event_handler.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 11 | #include "base/logging.h" |
| 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( |
| 35 | dst_root_->GetHost()->event_processor()->OnEventFromSource(event)); |
| 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() { |
| 52 | scoped_ptr<ui::PlatformWindow> window(new ui::StubWindow(this)); |
| 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 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 65 | for (auto host : mirroring_hosts_) |
| 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( |
| 73 | make_scoped_ptr(new UnifiedEventTargeter(src_root, window()))); |
| 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 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 80 | void AshWindowTreeHostUnified::SetBounds(const gfx::Rect& bounds) { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame^] | 81 | AshWindowTreeHostPlatform::SetBounds(bounds); |
| 82 | OnHostResized(bounds.size()); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
| 86 | for (auto host : mirroring_hosts_) |
| 87 | host->AsWindowTreeHost()->SetCursor(cursor); |
| 88 | } |
| 89 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 90 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
| 91 | for (auto host : mirroring_hosts_) |
| 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()) |
| 97 | OnHostResized(bounds.size()); |
| 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 |