blob: 4b98f729520bd03e4e5169d226364a55b3fd1e2d [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"
6#include "ash/host/root_window_transformer.h"
oshima5e0fff82015-06-27 01:31:307#include "ash/ime/input_method_event_handler.h"
oshima55ef9212015-04-27 23:27:038#include "base/logging.h"
9#include "ui/aura/window.h"
10#include "ui/aura/window_event_dispatcher.h"
11#include "ui/aura/window_targeter.h"
12#include "ui/compositor/compositor.h"
13#include "ui/events/event_processor.h"
oshimab8fb0b72015-04-28 18:25:1014#include "ui/events/null_event_targeter.h"
oshima55ef9212015-04-27 23:27:0315#include "ui/gfx/geometry/insets.h"
16
17namespace ash {
18
19class UnifiedEventTargeter : public aura::WindowTargeter {
20 public:
21 UnifiedEventTargeter(aura::Window* src_root, aura::Window* dst_root)
22 : src_root_(src_root), dst_root_(dst_root) {}
23
24 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
25 ui::Event* event) override {
26 if (root == src_root_ && !event->target()) {
27 if (event->IsLocatedEvent()) {
28 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
29 located_event->ConvertLocationToTarget(
30 static_cast<aura::Window*>(nullptr), dst_root_);
31 located_event->UpdateForRootTransform(
32 dst_root_->GetHost()->GetRootTransform());
33 }
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)
oshima38dffad2015-05-05 17:22:5651 : bounds_(gfx::Rect(initial_bounds.size())), transformer_helper_(this) {
oshima55ef9212015-04-27 23:27:0352 CreateCompositor(GetAcceleratedWidget());
oshima38dffad2015-05-05 17:22:5653 transformer_helper_.Init();
oshima55ef9212015-04-27 23:27:0354}
55
56AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5457 for (auto* ash_host : mirroring_hosts_)
58 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
59
oshima55ef9212015-04-27 23:27:0360 DestroyCompositor();
61 DestroyDispatcher();
62}
63
64void AshWindowTreeHostUnified::PrepareForShutdown() {
oshimab8fb0b72015-04-28 18:25:1065 window()->SetEventTargeter(
66 scoped_ptr<ui::EventTargeter>(new ui::NullEventTargeter));
67
oshima55ef9212015-04-27 23:27:0368 for (auto host : mirroring_hosts_)
69 host->PrepareForShutdown();
70}
71
72void AshWindowTreeHostUnified::RegisterMirroringHost(
73 AshWindowTreeHost* mirroring_ash_host) {
74 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
75 src_root->SetEventTargeter(
76 make_scoped_ptr(new UnifiedEventTargeter(src_root, window())));
77 DCHECK(std::find(mirroring_hosts_.begin(), mirroring_hosts_.end(),
78 mirroring_ash_host) == mirroring_hosts_.end());
79 mirroring_hosts_.push_back(mirroring_ash_host);
80 mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this);
81}
82
83void AshWindowTreeHostUnified::ToggleFullScreen() {
84}
85
86bool AshWindowTreeHostUnified::ConfineCursorToRootWindow() {
87 return true;
88}
89
90void AshWindowTreeHostUnified::UnConfineCursor() {
91}
92
93void AshWindowTreeHostUnified::SetRootWindowTransformer(
94 scoped_ptr<RootWindowTransformer> transformer) {
oshima38dffad2015-05-05 17:22:5695 transformer_helper_.SetRootWindowTransformer(transformer.Pass());
oshima55ef9212015-04-27 23:27:0396}
97
98gfx::Insets AshWindowTreeHostUnified::GetHostInsets() const {
oshima38dffad2015-05-05 17:22:5699 return transformer_helper_.GetHostInsets();
oshima55ef9212015-04-27 23:27:03100}
101
102aura::WindowTreeHost* AshWindowTreeHostUnified::AsWindowTreeHost() {
103 return this;
104}
105
106ui::EventSource* AshWindowTreeHostUnified::GetEventSource() {
107 return this;
108}
109
110gfx::AcceleratedWidget AshWindowTreeHostUnified::GetAcceleratedWidget() {
oshima55ef9212015-04-27 23:27:03111 return gfx::kNullAcceleratedWidget;
112}
113
jbauman8da589a2015-06-04 02:23:12114void AshWindowTreeHostUnified::ShowImpl() {
oshima55ef9212015-04-27 23:27:03115}
116
jbauman8da589a2015-06-04 02:23:12117void AshWindowTreeHostUnified::HideImpl() {
oshima55ef9212015-04-27 23:27:03118}
119
120gfx::Rect AshWindowTreeHostUnified::GetBounds() const {
121 return bounds_;
122}
123
124void AshWindowTreeHostUnified::SetBounds(const gfx::Rect& bounds) {
125 if (bounds_.size() == bounds.size())
126 return;
127 bounds_.set_size(bounds.size());
128 OnHostResized(bounds_.size());
129}
130
oshima38dffad2015-05-05 17:22:56131gfx::Transform AshWindowTreeHostUnified::GetRootTransform() const {
132 return transformer_helper_.GetTransform();
133}
134
135void AshWindowTreeHostUnified::SetRootTransform(
136 const gfx::Transform& transform) {
137 transformer_helper_.SetTransform(transform);
138}
139
140gfx::Transform AshWindowTreeHostUnified::GetInverseRootTransform() const {
141 return transformer_helper_.GetInverseTransform();
142}
143
144void AshWindowTreeHostUnified::UpdateRootWindowSize(
145 const gfx::Size& host_size) {
146 transformer_helper_.UpdateWindowSize(host_size);
147}
148
oshima55ef9212015-04-27 23:27:03149void AshWindowTreeHostUnified::SetCapture() {
150}
151
152void AshWindowTreeHostUnified::ReleaseCapture() {
153}
154
155gfx::Point AshWindowTreeHostUnified::GetLocationOnNativeScreen() const {
156 return gfx::Point();
157}
158
159void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
160 for (auto host : mirroring_hosts_)
161 host->AsWindowTreeHost()->SetCursor(cursor);
162}
163
164void AshWindowTreeHostUnified::MoveCursorToNative(const gfx::Point& location) {
165 // TODO(oshima): Find out if this is neceessary.
166 NOTIMPLEMENTED();
167}
168
169void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
170 for (auto host : mirroring_hosts_)
171 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
172}
173
174void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
175 auto iter =
176 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
177 [window](AshWindowTreeHost* ash_host) {
178 return ash_host->AsWindowTreeHost()->window() == window;
179 });
180 DCHECK(iter != mirroring_hosts_.end());
181 window->RemoveObserver(this);
182 mirroring_hosts_.erase(iter);
183}
184
oshima5e0fff82015-06-27 01:31:30185bool AshWindowTreeHostUnified::DispatchKeyEventPostIME(
186 const ui::KeyEvent& event) {
187 ui::KeyEvent event_copy(event);
188 input_method_handler()->SetPostIME(true);
189 ui::EventSource::DeliverEventToProcessor(&event_copy);
190 input_method_handler()->SetPostIME(false);
191 return event_copy.handled();
192}
193
194ui::EventDispatchDetails AshWindowTreeHostUnified::DeliverEventToProcessor(
195 ui::Event* event) {
196 return ui::EventSource::DeliverEventToProcessor(event);
197}
198
oshima55ef9212015-04-27 23:27:03199} // namespace ash