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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
ash / system / notification_center / notification_center_controller.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/notification_center/notification_center_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/system/notification_center/message_center_constants.h"
#include "ash/system/notification_center/message_center_utils.h"
#include "ash/system/notification_center/message_view_factory.h"
#include "ash/system/notification_center/views/message_view_container.h"
#include "ash/system/notification_center/views/notification_center_view.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/views/message_view.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/view.h"
namespace ash {
namespace {
constexpr gfx::Insets NotificationListDefaultMargins =
gfx::Insets::VH(kMessageListNotificationSpacing, 0);
} // namespace
NotificationCenterController::NotificationCenterController() {
CHECK(features::IsNotificationCenterControllerEnabled());
auto* message_center = message_center::MessageCenter::Get();
CHECK(message_center);
message_center->AddObserver(this);
}
NotificationCenterController::~NotificationCenterController() {
auto* message_center = message_center::MessageCenter::Get();
if (message_center) {
message_center->RemoveObserver(this);
}
}
std::unique_ptr<views::View>
NotificationCenterController::CreateNotificationCenterView() {
auto notification_center_view = std::make_unique<NotificationCenterView>();
notification_center_view_ = notification_center_view.get();
notification_center_view_tracker_.SetView(notification_center_view_);
notification_center_view_tracker_.SetIsDeletingCallback(base::BindOnce(
[](raw_ptr<NotificationCenterView>& notification_center_view) {
notification_center_view = nullptr;
},
std::ref(notification_center_view_)));
return std::move(notification_center_view);
}
void NotificationCenterController::InitNotificationCenterView() {
CHECK(notification_center_view_);
auto notifications =
message_center_utils::GetSortedNotificationsWithOwnView();
if (!features::AreOngoingProcessesEnabled()) {
notification_center_view_->Init(notifications);
return;
}
// Ongoing processes include pinned system notifications exclusively.
// Any other type of notifications, including non-pinned or non-system
// (sourced from Web, ARC, etc.) will exist in the regular notification list.
std::vector<message_center::Notification*> regular_notifications,
ongoing_processes;
for (auto* notification : notifications) {
if (notification->notifier_id().type !=
message_center::NotifierType::SYSTEM_COMPONENT) {
regular_notifications.push_back(notification);
continue;
}
if (notification->pinned()) {
ongoing_processes.push_back(notification);
} else {
regular_notifications.push_back(notification);
}
}
auto ongoing_process_list_view =
views::Builder<views::FlexLayoutView>()
.SetID(VIEW_ID_NOTIFICATION_BUBBLE_ONGOING_PROCESS_LIST)
.SetOrientation(views::LayoutOrientation::kVertical)
.CustomConfigure(base::BindOnce([](views::FlexLayoutView* layout) {
layout->SetDefault(views::kMarginsKey,
NotificationListDefaultMargins);
}))
.Build();
ongoing_process_list_view_ = ongoing_process_list_view.get();
ongoing_process_list_view_tracker_.SetView(ongoing_process_list_view_);
ongoing_process_list_view_tracker_.SetIsDeletingCallback(base::BindOnce(
[](raw_ptr<views::View>& ongoing_process_list_view) {
ongoing_process_list_view = nullptr;
},
std::ref(ongoing_process_list_view_)));
// TODO(b/322835713): Also create the regular notification list view from the
// controller instead of from `NotificationCenterView`.
for (auto* notification : ongoing_processes) {
AddNotificationChildView(notification);
}
UpdateListViewBorders(/*is_ongoing_process=*/true, /*force_update=*/true);
notification_center_view_->Init(regular_notifications,
std::move(ongoing_process_list_view));
}
MessageViewContainer*
NotificationCenterController::GetOngoingProcessMessageViewContainerById(
const std::string& id) {
// TODO(b/322835713): This function should search both regular notification
// and ongoing process lists when they're all created by this controller.
auto* list_view = ongoing_process_list_view_.get();
const auto i = base::ranges::find(
list_view->children(), id,
[](const views::View* v) { return AsMVC(v)->GetNotificationId(); });
return (i == list_view->children().cend()) ? nullptr : AsMVC(*i);
}
void NotificationCenterController::OnNotificationAdded(const std::string& id) {
if (!features::AreOngoingProcessesEnabled() || !notification_center_view_) {
return;
}
auto* notification =
message_center::MessageCenter::Get()->FindVisibleNotificationById(id);
if (!notification) {
return;
}
bool is_ongoing_process = notification->pinned() &&
notification->notifier_id().type ==
message_center::NotifierType::SYSTEM_COMPONENT;
if (!is_ongoing_process) {
// TODO(b/322835713): Also create and manage other notification views from
// this controller instead of from `NotificationListView`.
notification_center_view_->OnNotificationAdded(id);
return;
}
if (!ongoing_process_list_view_) {
return;
}
if (GetOngoingProcessMessageViewContainerById(id)) {
OnNotificationUpdated(id);
return;
}
AddNotificationChildView(notification);
UpdateListViewBorders(is_ongoing_process);
notification_center_view_->ListPreferredSizeChanged();
}
void NotificationCenterController::OnNotificationRemoved(const std::string& id,
bool by_user) {
if (!notification_center_view_) {
return;
}
notification_center_view_->OnNotificationRemoved(id, by_user);
if (!features::AreOngoingProcessesEnabled()) {
return;
}
// TODO(b/322835713): Also create and manage other notification views from
// this controller instead of from `NotificationListView`.
if (!ongoing_process_list_view_) {
return;
}
auto* message_view_container = GetOngoingProcessMessageViewContainerById(id);
if (!message_view_container) {
return;
}
ongoing_process_list_view_->RemoveChildView(message_view_container);
UpdateListViewBorders(/*is_ongoing_process=*/true);
notification_center_view_->ListPreferredSizeChanged();
}
void NotificationCenterController::OnNotificationUpdated(
const std::string& id) {
if (!features::AreOngoingProcessesEnabled() || !notification_center_view_) {
return;
}
auto* notification =
message_center::MessageCenter::Get()->FindVisibleNotificationById(id);
if (!notification) {
return;
}
bool is_ongoing_process = notification->pinned() &&
notification->notifier_id().type ==
message_center::NotifierType::SYSTEM_COMPONENT;
if (!is_ongoing_process) {
// TODO(b/322835713): Also manage other notification views from this
// controller instead of from `NotificationListView`.
notification_center_view_->OnNotificationUpdated(id);
return;
}
if (!ongoing_process_list_view_) {
return;
}
auto* message_view_container = GetOngoingProcessMessageViewContainerById(id);
if (!message_view_container) {
return;
}
auto previous_height = message_view_container->CalculateHeight();
message_view_container->UpdateWithNotification(*notification);
if (previous_height != message_view_container->CalculateHeight()) {
notification_center_view_->ListPreferredSizeChanged();
}
}
void NotificationCenterController::UpdateListViewBorders(
const bool is_ongoing_process,
const bool force_update) {
auto list_view = is_ongoing_process ? ongoing_process_list_view_ : nullptr;
if (!list_view) {
return;
}
auto children = list_view->children();
for (views::View* child : children) {
AsMVC(child)->UpdateBorder(/*is_top=*/child == children.front(),
/*is_bottom=*/child == children.back(),
force_update);
}
}
std::unique_ptr<message_center::MessageView>
NotificationCenterController::CreateMessageView(
const message_center::Notification& notification) {
CHECK(features::AreOngoingProcessesEnabled());
auto message_view =
MessageViewFactory::Create(notification, /*shown_in_popup=*/false);
if (!notification.group_child()) {
message_view->SetIsNested();
}
notification_center_view_->ConfigureMessageView(message_view.get());
return message_view;
}
void NotificationCenterController::AddNotificationChildView(
message_center::Notification* notification) {
// TODO(b/322835713): Currently only handles ongoing processes. Also create
// other notifications from this controller.
auto list_view = ongoing_process_list_view_;
auto message_view_container = std::make_unique<MessageViewContainer>(
/*message_view=*/CreateMessageView(*notification));
// The insertion order for notifications is reversed.
list_view->AddChildViewAt(std::move(message_view_container),
/*index=*/list_view->children().size());
message_center::MessageCenter::Get()->DisplayedNotification(
notification->id(), message_center::DISPLAY_SOURCE_MESSAGE_CENTER);
}
const MessageViewContainer* NotificationCenterController::AsMVC(
const views::View* v) {
return static_cast<const MessageViewContainer*>(v);
}
MessageViewContainer* NotificationCenterController::AsMVC(views::View* v) {
return static_cast<MessageViewContainer*>(v);
}
} // namespace ash