blob: 821a9a7c403dc89aa99a0a341010f85c11ae9984 [file] [log] [blame]
oshima55ef9212015-04-27 23:27:031// 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"
dchengcbf0d9d2015-12-27 22:49:236
7#include <utility>
8
oshima55ef9212015-04-27 23:27:039#include "ash/host/root_window_transformer.h"
oshima5e0fff82015-06-27 01:31:3010#include "ash/ime/input_method_event_handler.h"
oshima55ef9212015-04-27 23:27:0311#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"
oshima55ef9212015-04-27 23:27:0316#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2417#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0318
19namespace ash {
20
21class 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_);
oshima55ef9212015-04-27 23:27:0333 }
34 ignore_result(
35 dst_root_->GetHost()->event_processor()->OnEventFromSource(event));
36 return nullptr;
37 } else {
oshimaf8b27692015-04-30 07:23:0038 NOTREACHED() << "event type:" << event->type();
oshima55ef9212015-04-27 23:27:0339 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
49AshWindowTreeHostUnified::AshWindowTreeHostUnified(
50 const gfx::Rect& initial_bounds)
sadrul4f923692016-02-05 05:21:2451 : AshWindowTreeHostPlatform() {
52 scoped_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
53 window->SetBounds(initial_bounds);
54 SetPlatformWindow(std::move(window));
oshima55ef9212015-04-27 23:27:0355}
56
57AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5458 for (auto* ash_host : mirroring_hosts_)
59 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0360}
61
62void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2463 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1064
oshima55ef9212015-04-27 23:27:0365 for (auto host : mirroring_hosts_)
66 host->PrepareForShutdown();
67}
68
69void 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
oshima55ef9212015-04-27 23:27:0380void AshWindowTreeHostUnified::SetBounds(const gfx::Rect& bounds) {
sadrul4f923692016-02-05 05:21:2481 AshWindowTreeHostPlatform::SetBounds(bounds);
82 OnHostResized(bounds.size());
oshima55ef9212015-04-27 23:27:0383}
84
85void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
86 for (auto host : mirroring_hosts_)
87 host->AsWindowTreeHost()->SetCursor(cursor);
88}
89
oshima55ef9212015-04-27 23:27:0390void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
91 for (auto host : mirroring_hosts_)
92 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
93}
94
sadrul4f923692016-02-05 05:21:2495void AshWindowTreeHostUnified::OnBoundsChanged(const gfx::Rect& bounds) {
96 if (platform_window())
97 OnHostResized(bounds.size());
98}
99
oshima55ef9212015-04-27 23:27:03100void 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
oshima55ef9212015-04-27 23:27:03111} // namespace ash