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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/views/message_view.h"
#include "base/observer_list.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/shadow_util.h"
#include "ui/gfx/shadow_value.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/views/notification_background_painter.h"
#include "ui/message_center/views/notification_control_buttons_view.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/widget/widget.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/time/time.h"
#endif
namespace message_center {
namespace {
bool ShouldShowAeroShadowBorder() {
#if BUILDFLAG(IS_WIN)
return true;
#else
return false;
#endif
}
// Helper function to setup focus ring shapes for `MessageView`.
void InstallHighlightPathGenerator(views::View* view,
float top_radius,
float bottom_radius) {
auto corners = gfx::RoundedCornersF{top_radius, top_radius, bottom_radius,
bottom_radius};
// Shrink focus ring size by -`kDefaultHaloInset` on each side to draw
// them on top of the notifications. We need to do this because
// `TrayBubbleView` and `MessagePopupView` has a layer that masks to bounds
// due to which the focus ring can not extend outside the view.
views::HighlightPathGenerator::Install(
view, std::make_unique<views::RoundRectHighlightPathGenerator>(
gfx::Insets(-views::FocusRing::kDefaultHaloInset), corners));
}
} // namespace
MessageView::MessageView(const Notification& notification)
: notification_id_(notification.id()),
notifier_id_(notification.notifier_id()),
timestamp_(notification.timestamp()),
slide_out_controller_(this, this) {
SetNotifyEnterExitOnChild(true);
slide_out_controller_.set_trackpad_gestures_enabled(true);
SetFocusBehavior(FocusBehavior::ALWAYS);
views::FocusRing::Install(this);
views::FocusRing::Get(this)->SetOutsetFocusRingDisabled(true);
// Paint to a dedicated layer to make the layer non-opaque.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
GetViewAccessibility().SetRole(ax::mojom::Role::kGenericContainer);
GetViewAccessibility().SetRoleDescription(
l10n_util::GetStringUTF8(IDS_MESSAGE_NOTIFICATION_ACCESSIBLE_NAME));
UpdateWithNotification(notification);
UpdateCornerRadius(0, 0);
// If Aero is enabled, set shadow border.
if (ShouldShowAeroShadowBorder()) {
const auto& shadow = gfx::ShadowDetails::Get(2, 0);
gfx::Insets ninebox_insets = gfx::ShadowValue::GetBlurRegion(shadow.values);
SetBorder(views::CreateBorderPainter(
views::Painter::CreateImagePainter(shadow.nine_patch_image,
ninebox_insets),
-gfx::ShadowValue::GetMargin(shadow.values)));
}
}
MessageView::~MessageView() {
RemovedFromWidget();
}
views::View* MessageView::FindGroupNotificationView(
const std::string& notification_id) {
// Not implemented by default.
return nullptr;
}
// Creates text for spoken feedback from the data contained in the
// notification.
std::u16string MessageView::CreateAccessibleName(
const Notification& notification) {
if (!notification.accessible_name().empty())
return notification.accessible_name();
// Fall back to text constructed from the notification.
// Add non-empty elements.
std::vector<std::u16string> accessible_lines;
if (!notification.title().empty())
accessible_lines.push_back(notification.title());
if (!notification.message().empty())
accessible_lines.push_back(notification.message());
if (!notification.context_message().empty())
accessible_lines.push_back(notification.context_message());
std::vector<NotificationItem> items = notification.items();
for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) {
accessible_lines.push_back(items[i].title() + u" " + items[i].message());
}
return base::JoinString(accessible_lines, u"\n");
}
void MessageView::UpdateWithNotification(const Notification& notification) {
pinned_ = notification.pinned();
std::u16string name = CreateAccessibleName(notification);
if (name.empty()) {
GetViewAccessibility().SetName(
std::u16string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
} else {
GetViewAccessibility().SetName(name);
}
if (updated_name_callback_) {
updated_name_callback_.Run(
notification.rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
}
slide_out_controller_.set_slide_mode(CalculateSlideMode());
}
void MessageView::SetIsNested() {
DCHECK(!is_nested_) << "MessageView::SetIsNested() is called twice wrongly.";
is_nested_ = true;
// Update enability since it might be changed by "is_nested" flag.
slide_out_controller_.set_slide_mode(CalculateSlideMode());
slide_out_controller_.set_update_opacity(false);
UpdateNestedBorder();
if (GetControlButtonsView())
GetControlButtonsView()->ShowCloseButton(GetMode() != Mode::PINNED);
}
void MessageView::CloseSwipeControl() {
slide_out_controller_.CloseSwipeControl();
}
void MessageView::SlideOutAndClose(int direction) {
// Do not process events once the message view is animating out.
// crbug.com/940719
SetEnabled(false);
slide_out_controller_.SlideOutAndClose(direction);
}
void MessageView::SetExpanded(bool expanded) {
MessageCenter::Get()->OnSetExpanded(notification_id_, expanded);
}
bool MessageView::IsExpanded() const {
// Not implemented by default.
return false;
}
bool MessageView::IsAutoExpandingAllowed() const {
// Allowed by default.
return true;
}
bool MessageView::IsManuallyExpandedOrCollapsed() const {
// Not implemented by default.
return false;
}
void MessageView::SetManuallyExpandedOrCollapsed(ExpandState state) {
// Not implemented by default.
}
void MessageView::ToggleInlineSettings(const ui::Event& event) {
// Not implemented by default.
}
void MessageView::ToggleSnoozeSettings(const ui::Event& event) {
// Not implemented by default.
}
void MessageView::UpdateCornerRadius(int top_radius, int bottom_radius) {
SetCornerRadius(top_radius, bottom_radius);
if (!GetWidget()) {
return;
}
UpdateBackgroundPainter();
SchedulePaint();
}
void MessageView::OnContainerAnimationStarted() {
// Not implemented by default.
}
void MessageView::OnContainerAnimationEnded() {
// Not implemented by default.
}
bool MessageView::OnMousePressed(const ui::MouseEvent& event) {
return true;
}
bool MessageView::OnMouseDragged(const ui::MouseEvent& event) {
return true;
}
void MessageView::OnMouseReleased(const ui::MouseEvent& event) {
if (!event.IsOnlyLeftMouseButton()) {
return;
}
MessageCenter::Get()->ClickOnNotification(notification_id_);
}
void MessageView::OnMouseEntered(const ui::MouseEvent& event) {
UpdateControlButtonsVisibility();
MessageCenter::Get()->OnMessageViewHovered(notification_id_);
}
void MessageView::OnMouseExited(const ui::MouseEvent& event) {
UpdateControlButtonsVisibility();
}
bool MessageView::OnKeyPressed(const ui::KeyEvent& event) {
if (event.flags() != ui::EF_NONE) {
return false;
}
if (event.key_code() == ui::VKEY_RETURN) {
MessageCenter::Get()->ClickOnNotification(notification_id_);
return true;
} else if ((event.key_code() == ui::VKEY_DELETE ||
event.key_code() == ui::VKEY_BACK)) {
MessageCenter::Get()->RemoveNotification(notification_id_,
true /* by_user */);
return true;
}
return false;
}
bool MessageView::OnKeyReleased(const ui::KeyEvent& event) {
// Space key handling is triggered at key-release timing. See
// ui/views/controls/buttons/button.cc for why.
if (event.flags() != ui::EF_NONE || event.key_code() != ui::VKEY_SPACE)
return false;
MessageCenter::Get()->ClickOnNotification(notification_id_);
return true;
}
void MessageView::OnPaint(gfx::Canvas* canvas) {
if (ShouldShowAeroShadowBorder()) {
// If the border is shadow, paint border first.
OnPaintBorder(canvas);
// Clip at the border so we don't paint over it.
canvas->ClipRect(GetContentsBounds());
OnPaintBackground(canvas);
} else {
views::View::OnPaint(canvas);
}
}
void MessageView::OnBlur() {
views::View::OnBlur();
// We paint a focus indicator.
SchedulePaint();
}
void MessageView::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() == ui::EventType::kGestureTap) {
MessageCenter::Get()->ClickOnNotification(notification_id_);
event->SetHandled();
return;
}
if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent()) {
return;
}
if (scroller_) {
scroller_->OnGestureEvent(event);
}
event->SetHandled();
}
void MessageView::RemovedFromWidget() {
if (!focus_manager_) {
return;
}
focus_manager_->RemoveFocusChangeListener(this);
focus_manager_ = nullptr;
}
void MessageView::AddedToWidget() {
focus_manager_ = GetFocusManager();
if (focus_manager_) {
focus_manager_->AddFocusChangeListener(this);
}
}
void MessageView::OnThemeChanged() {
View::OnThemeChanged();
UpdateNestedBorder();
UpdateBackgroundPainter();
}
ui::Layer* MessageView::GetSlideOutLayer() {
// If a message view is contained in a parent message view it should give up
// slide behavior to the parent message view when the parent view is
// collapsed.
auto* nested_layer = (ShouldParentHandleSlide() && parent_message_view_)
? parent_message_view_->layer()
: layer();
bool is_nested = (ShouldParentHandleSlide() && parent_message_view_)
? parent_message_view_->is_nested()
: is_nested_;
return is_nested ? nested_layer : GetWidget()->GetLayer();
}
void MessageView::OnSlideStarted() {
observers_.Notify(&Observer::OnSlideStarted, notification_id_);
}
void MessageView::OnSlideChanged(bool in_progress) {
// crbug/1333664: We need to make sure to disable scrolling while a
// notification view is sliding. This is to ensure the notification view can
// only move horizontally or vertically at one time.
if (scroller_ && !is_sliding_ && slide_out_controller_.GetGestureAmount()) {
is_sliding_ = true;
scroller_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
}
if (scroller_ && !in_progress) {
is_sliding_ = false;
scroller_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kEnabled);
}
if (in_progress) {
observers_.Notify(&Observer::OnSlideChanged, notification_id_);
} else {
observers_.Notify(&Observer::OnSlideEnded, notification_id_);
}
}
void MessageView::AddObserver(MessageView::Observer* observer) {
observers_.AddObserver(observer);
}
void MessageView::RemoveObserver(MessageView::Observer* observer) {
observers_.RemoveObserver(observer);
}
void MessageView::OnSlideOut() {
if (ShouldParentHandleSlide() && parent_message_view_)
return parent_message_view_->OnSlideOut();
// The notification may be deleted after slide out, so give observers a
// chance to handle the notification before fulling sliding out.
observers_.Notify(&Observer::OnPreSlideOut, notification_id_);
// Copy the |notification_id| here as calling OnSlideOut() might destroy
// |this| but we still want to call RemoveNotification(). Note that the
// iteration over |observers_| is still safe and will simply stop.
std::string notification_id_copy = notification_id_;
observers_.Notify(&Observer::OnSlideOut, notification_id_);
auto* message_center = MessageCenter::Get();
if (message_center->FindPopupNotificationById(notification_id_copy)) {
message_center->MarkSinglePopupAsShown(notification_id_copy,
/*mark_notification_as_read=*/true);
return;
}
message_center->RemoveNotification(notification_id_copy, /*by_user=*/true);
}
void MessageView::OnDidChangeFocus(views::View* before, views::View* now) {
if (Contains(before) || Contains(now) ||
(GetControlButtonsView() && (GetControlButtonsView()->Contains(before) ||
GetControlButtonsView()->Contains(now)))) {
UpdateControlButtonsVisibility();
}
}
views::SlideOutController::SlideMode MessageView::CalculateSlideMode() const {
if (disable_slide_) {
return views::SlideOutController::SlideMode::kNone;
}
switch (GetMode()) {
case Mode::SETTING:
return views::SlideOutController::SlideMode::kNone;
case Mode::PINNED:
return views::SlideOutController::SlideMode::kPartial;
case Mode::NORMAL:
return views::SlideOutController::SlideMode::kFull;
}
NOTREACHED();
}
MessageView::Mode MessageView::GetMode() const {
if (setting_mode_) {
return Mode::SETTING;
}
// Only nested notifications can be pinned. Standalones (i.e. popups) can't
// be.
if (pinned_ && is_nested_) {
return Mode::PINNED;
}
return Mode::NORMAL;
}
float MessageView::GetSlideAmount() const {
if (slide_out_controller_.mode() ==
views::SlideOutController::SlideMode::kNone) {
// The return value of this method is used by NotificationSwipeControlView
// to determine visibility of the setting button. Returning 0 not to show
// the setting button with SlideMode::kNone.
return 0.f;
}
return slide_out_controller_.gesture_amount();
}
void MessageView::SetSettingMode(bool setting_mode) {
setting_mode_ = setting_mode;
slide_out_controller_.set_slide_mode(CalculateSlideMode());
UpdateControlButtonsVisibility();
}
void MessageView::DisableNotification() {
MessageCenter::Get()->DisableNotification(notification_id());
}
void MessageView::DisableSlideForcibly(bool disable) {
disable_slide_ = disable;
slide_out_controller_.set_slide_mode(CalculateSlideMode());
}
void MessageView::SetSlideButtonWidth(int control_button_width) {
slide_out_controller_.SetSwipeControlWidth(control_button_width);
}
void MessageView::SetCornerRadius(int top_radius, int bottom_radius) {
top_radius_ = top_radius;
bottom_radius_ = bottom_radius;
InstallHighlightPathGenerator(this, top_radius, bottom_radius);
}
void MessageView::OnCloseButtonPressed() {
observers_.Notify(&Observer::OnCloseButtonPressed, notification_id_);
MessageCenter::Get()->RemoveNotification(notification_id_,
true /* by_user */);
}
void MessageView::OnSettingsButtonPressed(const ui::Event& event) {
observers_.Notify(&Observer::OnSettingsButtonPressed, notification_id_);
if (inline_settings_enabled_) {
ToggleInlineSettings(event);
} else {
MessageCenter::Get()->ClickOnSettingsButton(notification_id_);
}
}
void MessageView::OnSnoozeButtonPressed(const ui::Event& event) {
observers_.Notify(&Observer::OnSnoozeButtonPressed, notification_id_);
if (snooze_settings_enabled_) {
ToggleSnoozeSettings(event);
} else {
MessageCenter::Get()->ClickOnSnoozeButton(notification_id());
}
}
#if BUILDFLAG(IS_CHROMEOS)
base::TimeDelta MessageView::GetBoundsAnimationDuration(
const Notification& notification) const {
return base::Milliseconds(0);
}
#endif
bool MessageView::ShouldShowControlButtons() const {
#if BUILDFLAG(IS_CHROMEOS)
// Users on ChromeOS are used to the Settings and Close buttons not being
// visible at all times, but users on other platforms expect them to be
// visible.
auto* control_buttons_view = GetControlButtonsView();
return control_buttons_view &&
(control_buttons_view->IsAnyButtonFocused() ||
(GetMode() != Mode::SETTING && IsMouseHovered()) ||
MessageCenter::Get()->IsSpokenFeedbackEnabled());
#else
return true;
#endif
}
bool MessageView::ShouldParentHandleSlide() const {
if (!parent_message_view_) {
return false;
}
return !parent_message_view_->IsExpanded();
}
void MessageView::UpdateBackgroundPainter() {
const auto* color_provider = GetColorProvider();
SkColor background_color =
color_provider->GetColor(ui::kColorNotificationBackgroundActive);
SetBackground(views::CreateBackgroundFromPainter(
std::make_unique<NotificationBackgroundPainter>(
top_radius_, bottom_radius_, background_color)));
}
void MessageView::UpdateNestedBorder() {
if (!is_nested_ || !GetWidget()) {
return;
}
SkColor border_color;
#if BUILDFLAG(IS_CHROMEOS)
border_color = SK_ColorTRANSPARENT;
#else
border_color =
GetColorProvider()->GetColor(ui::kColorFocusableBorderUnfocused);
#endif
SetBorder(views::CreateRoundedRectBorder(
kNotificationBorderThickness, kNotificationCornerRadius, border_color));
}
void MessageView::UpdateControlButtonsVisibility() {
auto* control_buttons_view = GetControlButtonsView();
if (control_buttons_view)
control_buttons_view->ShowButtons(ShouldShowControlButtons());
}
void MessageView::UpdateControlButtonsVisibilityWithNotification(
const Notification& notification) {
auto* control_buttons_view = GetControlButtonsView();
if (control_buttons_view) {
control_buttons_view->ShowButtons(ShouldShowControlButtons());
control_buttons_view->ShowSettingsButton(
notification.should_show_settings_button());
control_buttons_view->ShowSnoozeButton(
notification.should_show_snooze_button());
control_buttons_view->ShowCloseButton(GetMode() != Mode::PINNED);
}
UpdateControlButtonsVisibility();
}
void MessageView::SetUpdatedNameCallback(UpdatedNameCallback callback) {
CHECK(callback);
updated_name_callback_ = std::move(callback);
}
BEGIN_METADATA(MessageView)
END_METADATA
} // namespace message_center
|