blob: 57ca0820e1c4bdd7ad87858b574b0181ff6df93e [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>
Avi Drissman4155d51d2022-01-10 20:40:138#include <tuple>
dchengcbf0d9d2015-12-27 22:49:239#include <utility>
10
Addison Luh9b0f193c2022-02-24 20:56:1411#include "ash/host/ash_window_tree_host_delegate.h"
oshima55ef9212015-04-27 23:27:0312#include "ash/host/root_window_transformer.h"
Hans Wennborg37696452020-04-24 18:28:4913#include "base/check.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4714#include "base/containers/contains.h"
Hans Wennborg37696452020-04-24 18:28:4915#include "base/notreached.h"
oshima55ef9212015-04-27 23:27:0316#include "ui/aura/window.h"
17#include "ui/aura/window_event_dispatcher.h"
18#include "ui/aura/window_targeter.h"
19#include "ui/compositor/compositor.h"
oshima55ef9212015-04-27 23:27:0320#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2421#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0322
23namespace ash {
24
25class UnifiedEventTargeter : public aura::WindowTargeter {
26 public:
Ahmed Fakhry4f8e3722017-10-31 21:01:5827 UnifiedEventTargeter(aura::Window* src_root,
28 aura::Window* dst_root,
Addison Luh9b0f193c2022-02-24 20:56:1429 AshWindowTreeHostDelegate* delegate)
Ahmed Fakhry4f8e3722017-10-31 21:01:5830 : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
31 DCHECK(delegate);
32 }
oshima55ef9212015-04-27 23:27:0333
Peter Boström5fc1d312021-09-24 02:32:1534 UnifiedEventTargeter(const UnifiedEventTargeter&) = delete;
35 UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete;
36
oshima55ef9212015-04-27 23:27:0337 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
38 ui::Event* event) override {
39 if (root == src_root_ && !event->target()) {
Ahmed Fakhry4f8e3722017-10-31 21:01:5840 delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost());
41
oshima55ef9212015-04-27 23:27:0342 if (event->IsLocatedEvent()) {
43 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
44 located_event->ConvertLocationToTarget(
45 static_cast<aura::Window*>(nullptr), dst_root_);
oshima55ef9212015-04-27 23:27:0346 }
Avi Drissman4155d51d2022-01-10 20:40:1347 std::ignore =
48 dst_root_->GetHost()->GetEventSink()->OnEventFromSource(event);
Ahmed Fakhry4f8e3722017-10-31 21:01:5849
50 // Reset the source host.
51 delegate_->SetCurrentEventTargeterSourceHost(nullptr);
52
oshima55ef9212015-04-27 23:27:0353 return nullptr;
54 } else {
oshimaf8b27692015-04-30 07:23:0055 NOTREACHED() << "event type:" << event->type();
oshima55ef9212015-04-27 23:27:0356 return aura::WindowTargeter::FindTargetForEvent(root, event);
57 }
58 }
59
Ahmed Fakhry4f8e3722017-10-31 21:01:5860 private:
oshima55ef9212015-04-27 23:27:0361 aura::Window* src_root_;
62 aura::Window* dst_root_;
Addison Luh9b0f193c2022-02-24 20:56:1463 AshWindowTreeHostDelegate* delegate_; // Not owned.
oshima55ef9212015-04-27 23:27:0364};
65
66AshWindowTreeHostUnified::AshWindowTreeHostUnified(
Ahmed Fakhry4f8e3722017-10-31 21:01:5867 const gfx::Rect& initial_bounds,
Addison Luh9b0f193c2022-02-24 20:56:1468 AshWindowTreeHostDelegate* delegate)
69 : AshWindowTreeHostPlatform(delegate) {
Maksim Sisov08b20ec12019-11-25 13:20:5870 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
sadrul4f923692016-02-05 05:21:2471 window->SetBounds(initial_bounds);
72 SetPlatformWindow(std::move(window));
oshima55ef9212015-04-27 23:27:0373}
74
75AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5476 for (auto* ash_host : mirroring_hosts_)
77 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0378}
79
80void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2481 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1082
vmpstreb900a12016-06-29 02:22:2883 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0384 host->PrepareForShutdown();
85}
86
87void AshWindowTreeHostUnified::RegisterMirroringHost(
88 AshWindowTreeHost* mirroring_ash_host) {
89 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
90 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:5891 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:3492 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:0393 mirroring_hosts_.push_back(mirroring_ash_host);
Addison Luh9b0f193c2022-02-24 20:56:1494 src_root->AddObserver(this);
95 mirroring_ash_host->UpdateCursorConfig();
96}
97
98// Do nothing, since mirroring hosts had their cursor config updated when they
99// were registered.
100void AshWindowTreeHostUnified::UpdateCursorConfig() {}
101
102void AshWindowTreeHostUnified::ClearCursorConfig() {
103 for (auto* host : mirroring_hosts_)
104 host->ClearCursorConfig();
oshima55ef9212015-04-27 23:27:03105}
106
oshima55ef9212015-04-27 23:27:03107void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:28108 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03109 host->AsWindowTreeHost()->SetCursor(cursor);
110}
111
oshima55ef9212015-04-27 23:27:03112void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:28113 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03114 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
115}
116
Kevin Marshall05a716e2021-03-25 02:11:28117void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& bounds) {
sadrul4f923692016-02-05 05:21:24118 if (platform_window())
Kevin Marshall05a716e2021-03-25 02:11:28119 OnHostResizedInPixels(bounds.bounds.size());
sadrul4f923692016-02-05 05:21:24120}
121
oshima55ef9212015-04-27 23:27:03122void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
123 auto iter =
124 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
125 [window](AshWindowTreeHost* ash_host) {
126 return ash_host->AsWindowTreeHost()->window() == window;
127 });
128 DCHECK(iter != mirroring_hosts_.end());
129 window->RemoveObserver(this);
130 mirroring_hosts_.erase(iter);
131}
132
oshima55ef9212015-04-27 23:27:03133} // namespace ash