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.
5 #include "ash/host/ash_window_tree_host_unified.h"
6 #include "ash/host/root_window_transformer.h"
7 #include "base/logging.h"
8 #include "ui/aura/window.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_targeter.h"
11 #include "ui/compositor/compositor.h"
12 #include "ui/events/event_processor.h"
13 #include "ui/gfx/geometry/insets.h"
17 class UnifiedEventTargeter
: public aura::WindowTargeter
{
19 UnifiedEventTargeter(aura::Window
* src_root
, aura::Window
* dst_root
)
20 : src_root_(src_root
), dst_root_(dst_root
) {}
22 ui::EventTarget
* FindTargetForEvent(ui::EventTarget
* root
,
23 ui::Event
* event
) override
{
24 if (root
== src_root_
&& !event
->target()) {
25 if (event
->IsLocatedEvent()) {
26 ui::LocatedEvent
* located_event
= static_cast<ui::LocatedEvent
*>(event
);
27 located_event
->ConvertLocationToTarget(
28 static_cast<aura::Window
*>(nullptr), dst_root_
);
29 located_event
->UpdateForRootTransform(
30 dst_root_
->GetHost()->GetRootTransform());
33 dst_root_
->GetHost()->event_processor()->OnEventFromSource(event
));
36 LOG(ERROR
) << "Handling Event:" << event
->type();
37 return aura::WindowTargeter::FindTargetForEvent(root
, event
);
41 aura::Window
* src_root_
;
42 aura::Window
* dst_root_
;
44 DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter
);
47 AshWindowTreeHostUnified::AshWindowTreeHostUnified(
48 const gfx::Rect
& initial_bounds
)
49 : bounds_(gfx::Rect(initial_bounds
.size())) {
50 CreateCompositor(GetAcceleratedWidget());
53 AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
58 void AshWindowTreeHostUnified::PrepareForShutdown() {
59 for (auto host
: mirroring_hosts_
)
60 host
->PrepareForShutdown();
63 void AshWindowTreeHostUnified::RegisterMirroringHost(
64 AshWindowTreeHost
* mirroring_ash_host
) {
65 aura::Window
* src_root
= mirroring_ash_host
->AsWindowTreeHost()->window();
66 src_root
->SetEventTargeter(
67 make_scoped_ptr(new UnifiedEventTargeter(src_root
, window())));
68 DCHECK(std::find(mirroring_hosts_
.begin(), mirroring_hosts_
.end(),
69 mirroring_ash_host
) == mirroring_hosts_
.end());
70 mirroring_hosts_
.push_back(mirroring_ash_host
);
71 mirroring_ash_host
->AsWindowTreeHost()->window()->AddObserver(this);
74 void AshWindowTreeHostUnified::ToggleFullScreen() {
77 bool AshWindowTreeHostUnified::ConfineCursorToRootWindow() {
81 void AshWindowTreeHostUnified::UnConfineCursor() {
84 void AshWindowTreeHostUnified::SetRootWindowTransformer(
85 scoped_ptr
<RootWindowTransformer
> transformer
) {
86 // TODO(oshima): Find out if this is neceessary.
90 gfx::Insets
AshWindowTreeHostUnified::GetHostInsets() const {
94 aura::WindowTreeHost
* AshWindowTreeHostUnified::AsWindowTreeHost() {
98 ui::EventSource
* AshWindowTreeHostUnified::GetEventSource() {
102 gfx::AcceleratedWidget
AshWindowTreeHostUnified::GetAcceleratedWidget() {
103 // TODO(oshima): Enable offscreen compositor.
104 return gfx::kNullAcceleratedWidget
;
107 void AshWindowTreeHostUnified::Show() {
110 void AshWindowTreeHostUnified::Hide() {
113 gfx::Rect
AshWindowTreeHostUnified::GetBounds() const {
117 void AshWindowTreeHostUnified::SetBounds(const gfx::Rect
& bounds
) {
118 if (bounds_
.size() == bounds
.size())
120 bounds_
.set_size(bounds
.size());
121 OnHostResized(bounds_
.size());
124 void AshWindowTreeHostUnified::SetCapture() {
127 void AshWindowTreeHostUnified::ReleaseCapture() {
130 gfx::Point
AshWindowTreeHostUnified::GetLocationOnNativeScreen() const {
134 void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor
) {
135 for (auto host
: mirroring_hosts_
)
136 host
->AsWindowTreeHost()->SetCursor(cursor
);
139 void AshWindowTreeHostUnified::MoveCursorToNative(const gfx::Point
& location
) {
140 // TODO(oshima): Find out if this is neceessary.
144 void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show
) {
145 for (auto host
: mirroring_hosts_
)
146 host
->AsWindowTreeHost()->OnCursorVisibilityChanged(show
);
149 void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window
* window
) {
151 std::find_if(mirroring_hosts_
.begin(), mirroring_hosts_
.end(),
152 [window
](AshWindowTreeHost
* ash_host
) {
153 return ash_host
->AsWindowTreeHost()->window() == window
;
155 DCHECK(iter
!= mirroring_hosts_
.end());
156 window
->RemoveObserver(this);
157 mirroring_hosts_
.erase(iter
);
160 ui::EventProcessor
* AshWindowTreeHostUnified::GetEventProcessor() {