blob: 40369d29af5136c1b6e7ca4e86bfc6c17c58d0c4 [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
Mitsuru Oshima04b54d02017-10-09 14:22:457#include <memory>
dchengcbf0d9d2015-12-27 22:49:238#include <utility>
9
Ahmed Fakhry4f8e3722017-10-31 21:01:5810#include "ash/host/ash_window_tree_host_mirroring_delegate.h"
oshima55ef9212015-04-27 23:27:0311#include "ash/host/root_window_transformer.h"
Hans Wennborg37696452020-04-24 18:28:4912#include "base/check.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4713#include "base/containers/contains.h"
Hans Wennborg37696452020-04-24 18:28:4914#include "base/notreached.h"
oshima55ef9212015-04-27 23:27:0315#include "ui/aura/window.h"
16#include "ui/aura/window_event_dispatcher.h"
17#include "ui/aura/window_targeter.h"
18#include "ui/compositor/compositor.h"
oshima55ef9212015-04-27 23:27:0319#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2420#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0321
22namespace ash {
23
24class UnifiedEventTargeter : public aura::WindowTargeter {
25 public:
Ahmed Fakhry4f8e3722017-10-31 21:01:5826 UnifiedEventTargeter(aura::Window* src_root,
27 aura::Window* dst_root,
28 AshWindowTreeHostMirroringDelegate* delegate)
29 : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
30 DCHECK(delegate);
31 }
oshima55ef9212015-04-27 23:27:0332
33 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
34 ui::Event* event) override {
35 if (root == src_root_ && !event->target()) {
Ahmed Fakhry4f8e3722017-10-31 21:01:5836 delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost());
37
oshima55ef9212015-04-27 23:27:0338 if (event->IsLocatedEvent()) {
39 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
40 located_event->ConvertLocationToTarget(
41 static_cast<aura::Window*>(nullptr), dst_root_);
oshima55ef9212015-04-27 23:27:0342 }
43 ignore_result(
Wei Lib88861342021-02-02 04:34:4644 dst_root_->GetHost()->GetEventSink()->OnEventFromSource(event));
Ahmed Fakhry4f8e3722017-10-31 21:01:5845
46 // Reset the source host.
47 delegate_->SetCurrentEventTargeterSourceHost(nullptr);
48
oshima55ef9212015-04-27 23:27:0349 return nullptr;
50 } else {
oshimaf8b27692015-04-30 07:23:0051 NOTREACHED() << "event type:" << event->type();
oshima55ef9212015-04-27 23:27:0352 return aura::WindowTargeter::FindTargetForEvent(root, event);
53 }
54 }
55
Ahmed Fakhry4f8e3722017-10-31 21:01:5856 private:
oshima55ef9212015-04-27 23:27:0357 aura::Window* src_root_;
58 aura::Window* dst_root_;
Ahmed Fakhry4f8e3722017-10-31 21:01:5859 AshWindowTreeHostMirroringDelegate* delegate_; // Not owned.
oshima55ef9212015-04-27 23:27:0360
61 DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter);
62};
63
64AshWindowTreeHostUnified::AshWindowTreeHostUnified(
Ahmed Fakhry4f8e3722017-10-31 21:01:5865 const gfx::Rect& initial_bounds,
66 AshWindowTreeHostMirroringDelegate* delegate)
67 : AshWindowTreeHostPlatform(), delegate_(delegate) {
68 DCHECK(delegate);
Maksim Sisov08b20ec12019-11-25 13:20:5869 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
sadrul4f923692016-02-05 05:21:2470 window->SetBounds(initial_bounds);
71 SetPlatformWindow(std::move(window));
oshima55ef9212015-04-27 23:27:0372}
73
74AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5475 for (auto* ash_host : mirroring_hosts_)
76 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0377}
78
79void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2480 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1081
vmpstreb900a12016-06-29 02:22:2882 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0383 host->PrepareForShutdown();
84}
85
86void AshWindowTreeHostUnified::RegisterMirroringHost(
87 AshWindowTreeHost* mirroring_ash_host) {
88 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
89 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:5890 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:3491 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:0392 mirroring_hosts_.push_back(mirroring_ash_host);
93 mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this);
94}
95
oshima55ef9212015-04-27 23:27:0396void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:2897 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0398 host->AsWindowTreeHost()->SetCursor(cursor);
99}
100
oshima55ef9212015-04-27 23:27:03101void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:28102 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03103 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
104}
105
Kevin Marshall05a716e2021-03-25 02:11:28106void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& bounds) {
sadrul4f923692016-02-05 05:21:24107 if (platform_window())
Kevin Marshall05a716e2021-03-25 02:11:28108 OnHostResizedInPixels(bounds.bounds.size());
sadrul4f923692016-02-05 05:21:24109}
110
oshima55ef9212015-04-27 23:27:03111void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
112 auto iter =
113 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
114 [window](AshWindowTreeHost* ash_host) {
115 return ash_host->AsWindowTreeHost()->window() == window;
116 });
117 DCHECK(iter != mirroring_hosts_.end());
118 window->RemoveObserver(this);
119 mirroring_hosts_.erase(iter);
120}
121
oshima55ef9212015-04-27 23:27:03122} // namespace ash