forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounded_window_corners_manager.cc
181 lines (145 loc) · 5.82 KB
/
rounded_window_corners_manager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/graphics/rounded_window_corners_manager.h"
#include <memory>
#include <string>
#include <unordered_set>
#include "base/containers/contains.h"
#include "base/strings/string_number_conversions.h"
#include "chromecast/graphics/cast_window_manager.h"
#include "components/exo/surface.h"
#include "ui/aura/env.h"
namespace chromecast {
namespace {
aura::Window* FindTopmostVisibleNonCornersWindow(
aura::Window* window,
const std::unordered_set<aura::Window*>& observed_container_windows) {
aura::Window* found_window = nullptr;
if (!window->IsVisible())
return nullptr;
if (base::Contains(observed_container_windows, window))
return window;
for (auto it = window->children().rbegin(); it != window->children().rend();
++it) {
found_window =
FindTopmostVisibleNonCornersWindow(*it, observed_container_windows);
if (found_window)
return found_window;
}
return window->GetId() != CastWindowManager::CORNERS_OVERLAY ? window
: nullptr;
}
bool HasNonAppParent(const aura::Window* window) {
const aura::Window* parent = window->parent();
while (parent && parent->IsVisible()) {
if (parent->GetId() != CastWindowManager::APP)
return true;
else
parent = parent->parent();
}
return false;
}
} // namespace
// Keeps track of the creation and destruction of webview container windows, and
// adds and removes the root window rounded corner decoration accordingly.
// Rounded corners only need to be present when webviews are being displayed.
class RoundedCornersObserver : public aura::WindowObserver,
public CastWindowManager::Observer {
public:
explicit RoundedCornersObserver(CastWindowManager* cast_window_manager)
: cast_window_manager_(cast_window_manager) {
DCHECK(cast_window_manager);
DCHECK(cast_window_manager_->GetRootWindow());
cast_window_manager_->AddObserver(this);
cast_window_manager_->GetRootWindow()->AddObserver(this);
DecideCorners();
}
RoundedCornersObserver(const RoundedCornersObserver&) = delete;
RoundedCornersObserver& operator=(const RoundedCornersObserver&) = delete;
~RoundedCornersObserver() override {
cast_window_manager_->RemoveObserver(this);
cast_window_manager_->GetRootWindow()->RemoveObserver(this);
}
void OnNewWebviewContainerWindow(aura::Window* window, int app_id) {
// Observe the lifecycle of this window so we can add rounded corners
// when it is visible.
window->AddObserver(this);
observed_container_windows_.insert(window);
if (window->IsVisible())
DecideCorners();
}
// aura::WindowObserver:
void OnWindowVisibilityChanged(aura::Window* window, bool visible) override {
DecideCorners();
}
void OnWindowDestroyed(aura::Window* window) override {
if (!base::Contains(observed_container_windows_, window))
return;
observed_container_windows_.erase(window);
DecideCorners();
}
void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override {
DecideCorners();
}
void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override {
DecideCorners();
}
// CastWindowManager::Observer:
void WindowOrderChanged() override { DecideCorners(); }
private:
void DecideCorners() {
aura::Window* root_window = cast_window_manager_->GetRootWindow();
aura::Window* topmost_visible_window = FindTopmostVisibleNonCornersWindow(
root_window, observed_container_windows_);
if (!topmost_visible_window)
return;
int window_id = topmost_visible_window->GetId();
// The window may be a child to a visible non-app window that does not draw
// its own corners, so this needs to be checked for.
bool set_rounded_corners =
(window_id != CastWindowManager::APP) ||
base::Contains(observed_container_windows_, topmost_visible_window) ||
HasNonAppParent(topmost_visible_window);
if (rounded_corners_ == set_rounded_corners)
return;
cast_window_manager_->SetEnableRoundedCorners(set_rounded_corners);
rounded_corners_ = set_rounded_corners;
}
bool rounded_corners_ = false;
CastWindowManager* cast_window_manager_;
std::unordered_set<aura::Window*> observed_container_windows_;
};
RoundedWindowCornersManager::RoundedWindowCornersManager(
CastWindowManager* cast_window_manager)
: rounded_corners_observer_(
std::make_unique<RoundedCornersObserver>(cast_window_manager)) {
aura::Env::GetInstance()->AddObserver(this);
}
RoundedWindowCornersManager::~RoundedWindowCornersManager() {
aura::Env::GetInstance()->RemoveObserver(this);
}
void RoundedWindowCornersManager::OnWindowInitialized(aura::Window* window) {
window->AddObserver(this);
}
void RoundedWindowCornersManager::OnWindowDestroying(aura::Window* window) {
window->RemoveObserver(this);
}
void RoundedWindowCornersManager::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (key != exo::kClientSurfaceIdKey)
return;
// Note: The property was originally an integer, and was switched to be a
// string. For compatibility integer values are converted to a string via
// base::NumberToString before being set as the property value.
std::string* app_id_str = window->GetProperty(exo::kClientSurfaceIdKey);
if (!app_id_str)
return;
int app_id = 0;
if (!base::StringToInt(*app_id_str, &app_id))
return;
LOG(INFO) << "Found window for webview " << app_id;
rounded_corners_observer_->OnNewWebviewContainerWindow(window, app_id);
}
} // namespace chromecast