blob: f0ab67611be268fc5c5b1f49a94d61c9f97eabbc [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"
oshima55ef9212015-04-27 23:27:0310#include "base/logging.h"
dchenga94547472016-04-08 08:41:1111#include "base/memory/ptr_util.h"
oshima55ef9212015-04-27 23:27:0312#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(
penghuang17134c6c2017-03-23 00:01:0735 dst_root_->GetHost()->event_sink()->OnEventFromSource(event));
oshima55ef9212015-04-27 23:27:0336 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() {
sadrul70394d02016-05-25 02:13:1152 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
sadrul4f923692016-02-05 05:21:2453 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
vmpstreb900a12016-06-29 02:22:2865 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0366 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(
riceac9462d42016-08-22 02:40:2873 base::MakeUnique<UnifiedEventTargeter>(src_root, window()));
oshima55ef9212015-04-27 23:27:0374 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
riajiangdfdfc99e2016-11-29 05:37:4780void AshWindowTreeHostUnified::SetBoundsInPixels(const gfx::Rect& bounds) {
81 AshWindowTreeHostPlatform::SetBoundsInPixels(bounds);
riajiang8077b132016-11-29 23:35:3682 OnHostResizedInPixels(bounds.size());
oshima55ef9212015-04-27 23:27:0383}
84
85void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:2886 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0387 host->AsWindowTreeHost()->SetCursor(cursor);
88}
89
oshima55ef9212015-04-27 23:27:0390void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:2891 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0392 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
93}
94
sadrul4f923692016-02-05 05:21:2495void AshWindowTreeHostUnified::OnBoundsChanged(const gfx::Rect& bounds) {
96 if (platform_window())
riajiang8077b132016-11-29 23:35:3697 OnHostResizedInPixels(bounds.size());
sadrul4f923692016-02-05 05:21:2498}
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