blob: 2c5a45235bdfd70ada4905f6a65578b881cb94ec [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 Luh592ab5a2022-03-10 22:54:5068 AshWindowTreeHostDelegate* delegate,
69 size_t compositor_memory_limit_mb)
Mitsuru Oshima90cba8d2022-04-01 20:25:0070 : AshWindowTreeHostPlatform(
71 std::make_unique<ui::StubWindow>(initial_bounds),
72 delegate,
73 compositor_memory_limit_mb) {
74 ui::StubWindow* stub_window = static_cast<ui::StubWindow*>(platform_window());
75 stub_window->InitDelegate(this, true);
oshima55ef9212015-04-27 23:27:0376}
77
78AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5479 for (auto* ash_host : mirroring_hosts_)
80 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0381}
82
83void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2484 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1085
vmpstreb900a12016-06-29 02:22:2886 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0387 host->PrepareForShutdown();
88}
89
90void AshWindowTreeHostUnified::RegisterMirroringHost(
91 AshWindowTreeHost* mirroring_ash_host) {
92 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
93 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:5894 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:3495 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:0396 mirroring_hosts_.push_back(mirroring_ash_host);
Addison Luh9b0f193c2022-02-24 20:56:1497 src_root->AddObserver(this);
98 mirroring_ash_host->UpdateCursorConfig();
99}
100
101// Do nothing, since mirroring hosts had their cursor config updated when they
102// were registered.
103void AshWindowTreeHostUnified::UpdateCursorConfig() {}
104
105void AshWindowTreeHostUnified::ClearCursorConfig() {
106 for (auto* host : mirroring_hosts_)
107 host->ClearCursorConfig();
oshima55ef9212015-04-27 23:27:03108}
109
oshima55ef9212015-04-27 23:27:03110void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:28111 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03112 host->AsWindowTreeHost()->SetCursor(cursor);
113}
114
oshima55ef9212015-04-27 23:27:03115void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:28116 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03117 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
118}
119
Mitsuru Oshima410e5dc32022-07-26 20:01:43120void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) {
sadrul4f923692016-02-05 05:21:24121 if (platform_window())
Mitsuru Oshima410e5dc32022-07-26 20:01:43122 OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size());
sadrul4f923692016-02-05 05:21:24123}
124
oshima55ef9212015-04-27 23:27:03125void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
126 auto iter =
127 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
128 [window](AshWindowTreeHost* ash_host) {
129 return ash_host->AsWindowTreeHost()->window() == window;
130 });
131 DCHECK(iter != mirroring_hosts_.end());
132 window->RemoveObserver(this);
133 mirroring_hosts_.erase(iter);
134}
135
oshima55ef9212015-04-27 23:27:03136} // namespace ash