blob: bf35a0a5395184ca11a895f3027aeaab59eada1b [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
Ahmed Fakhry4f8e3722017-10-31 21:01:5811#include "ash/host/ash_window_tree_host_mirroring_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,
29 AshWindowTreeHostMirroringDelegate* delegate)
30 : 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_;
Ahmed Fakhry4f8e3722017-10-31 21:01:5863 AshWindowTreeHostMirroringDelegate* delegate_; // Not owned.
oshima55ef9212015-04-27 23:27:0364};
65
66AshWindowTreeHostUnified::AshWindowTreeHostUnified(
Ahmed Fakhry4f8e3722017-10-31 21:01:5867 const gfx::Rect& initial_bounds,
68 AshWindowTreeHostMirroringDelegate* delegate)
69 : AshWindowTreeHostPlatform(), delegate_(delegate) {
70 DCHECK(delegate);
Maksim Sisov08b20ec12019-11-25 13:20:5871 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
sadrul4f923692016-02-05 05:21:2472 window->SetBounds(initial_bounds);
73 SetPlatformWindow(std::move(window));
oshima55ef9212015-04-27 23:27:0374}
75
76AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5477 for (auto* ash_host : mirroring_hosts_)
78 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0379}
80
81void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2482 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1083
vmpstreb900a12016-06-29 02:22:2884 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0385 host->PrepareForShutdown();
86}
87
88void AshWindowTreeHostUnified::RegisterMirroringHost(
89 AshWindowTreeHost* mirroring_ash_host) {
90 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
91 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:5892 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:3493 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:0394 mirroring_hosts_.push_back(mirroring_ash_host);
95 mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this);
96}
97
oshima55ef9212015-04-27 23:27:0398void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:2899 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03100 host->AsWindowTreeHost()->SetCursor(cursor);
101}
102
oshima55ef9212015-04-27 23:27:03103void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:28104 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03105 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
106}
107
Kevin Marshall05a716e2021-03-25 02:11:28108void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& bounds) {
sadrul4f923692016-02-05 05:21:24109 if (platform_window())
Kevin Marshall05a716e2021-03-25 02:11:28110 OnHostResizedInPixels(bounds.bounds.size());
sadrul4f923692016-02-05 05:21:24111}
112
oshima55ef9212015-04-27 23:27:03113void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
114 auto iter =
115 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
116 [window](AshWindowTreeHost* ash_host) {
117 return ash_host->AsWindowTreeHost()->window() == window;
118 });
119 DCHECK(iter != mirroring_hosts_.end());
120 window->RemoveObserver(this);
121 mirroring_hosts_.erase(iter);
122}
123
oshima55ef9212015-04-27 23:27:03124} // namespace ash