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

ash / system / notification_center / notification_center_tray.cc [blame]

// Copyright 2022 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_tray.h"

#include <memory>
#include <string>

#include "ash/constants/ash_features.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/shelf/shelf.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/notification_center/notification_center_bubble.h"
#include "ash/system/notification_center/notification_metrics_recorder.h"
#include "ash/system/notification_center/views/notification_center_view.h"
#include "ash/system/privacy/privacy_indicators_tray_item_view.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_bubble_view.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/unified/notification_counter_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/display/screen.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace ash {

NotificationCenterTray::NotificationCenterTray(Shelf* shelf)
    : TrayBackgroundView(shelf,
                         TrayBackgroundViewCatalogName::kNotificationCenter,
                         RoundedCornerBehavior::kStartRounded),
      notification_grouping_controller_(
          std::make_unique<NotificationGroupingController>(this)),
      popup_collection_(std::make_unique<AshMessagePopupCollection>(
          display::Screen::GetScreen(),
          shelf)),
      notification_metrics_recorder_(
          std::make_unique<NotificationMetricsRecorder>(this)),
      notification_icons_controller_(
          std::make_unique<NotificationIconsController>(
              shelf,
              /*notification_center_tray=*/this)) {
  SetCallback(base::BindRepeating(&NotificationCenterTray::OnTrayButtonPressed,
                                  base::Unretained(this)));
  SetID(VIEW_ID_SA_NOTIFICATION_TRAY);
  set_use_bounce_in_animation(false);

  tray_container()->SetMargin(
      /*main_axis_margin=*/kUnifiedTrayContentPadding -
          ShelfConfig::Get()->status_area_hit_region_padding(),
      0);

  UpdateAccessibleName();
}

NotificationCenterTray::~NotificationCenterTray() {
  for (views::View* tray_item : tray_container()->children()) {
    static_cast<TrayItemView*>(tray_item)->RemoveObserver(this);
  }
}

void NotificationCenterTray::AddNotificationCenterTrayObserver(
    Observer* observer) {
  observers_.AddObserver(observer);
}

void NotificationCenterTray::RemoveNotificationCenterTrayObserver(
    Observer* observer) {
  observers_.RemoveObserver(observer);
}

void NotificationCenterTray::OnTrayItemVisibilityAboutToChange(
    bool target_visibility) {
  // A change in one of this tray's tray items could have implications for this
  // tray's overall visibility (e.g. if the only visible tray item wants to
  // become hidden, which could happen when dismissing all notifications). We
  // need to update this tray's visibility here, before the tray item gets a
  // chance to start its own visibility change animation, so that this tray does
  // not briefly become empty, for instance.
  //
  // If the tray item's visibility change does not imply a change in visibility
  // for this tray, then `SetVisiblePreferred()` (which is called by
  // `UpdateVisibility()`) will do nothing.
  UpdateVisibility();
}

void NotificationCenterTray::AddTooltipChangedCallbackToNotificationIcon(
    NotificationIconTrayItemView* tray_item) {
  notification_icon_image_tooltip_changed_subscriptions_.push_back(
      tray_item->image_view()->AddTooltipTextChangedCallback(
          base::BindRepeating(&NotificationCenterTray::UpdateAccessibleName,
                              base::Unretained(this))));
}

void NotificationCenterTray::OnSystemTrayVisibilityChanged(
    bool system_tray_visible) {
  system_tray_visible_ = system_tray_visible;
  UpdateVisibility();
}

void NotificationCenterTray::OnTrayButtonPressed() {
  if (GetBubbleWidget()) {
    CloseBubble();
    return;
  }

  ShowBubble();
}

NotificationListView* NotificationCenterTray::GetNotificationListView() {
  if (!bubble_) {
    return nullptr;
  }

  auto* notification_center_view = bubble_->GetNotificationCenterView();
  return notification_center_view
             ? notification_center_view->notification_list_view()
             : nullptr;
}

bool NotificationCenterTray::IsBubbleShown() const {
  return !!bubble_;
}

void NotificationCenterTray::Initialize() {
  TrayBackgroundView::Initialize();

  // Add all child `TrayItemView`s.
  // TODO(b/255986529): Rewrite the `NotificationIconsController` class so that
  // we do not have to add icon views that are owned by the
  // `NotificationCenterTray` from the controller. We should make sure views are
  // only added by host views.
  notification_icons_controller_->AddNotificationTrayItems(tray_container());

  // Privacy indicator is only enabled when Video Conference is disabled.
  if (!features::IsVideoConferenceEnabled()) {
    privacy_indicators_view_ = tray_container()->AddChildView(
        std::make_unique<PrivacyIndicatorsTrayItemView>(shelf()));
  }
  for (views::View* tray_item : tray_container()->children()) {
    static_cast<TrayItemView*>(tray_item)->AddObserver(this);
  }
  for (auto& observer : observers_) {
    observer.OnAllTrayItemsAdded();
  }

  // Now that the NotificationTrayItem objects are created, add callbacks.
  AddCallbacksForAccessibility();

  // Update this tray's visibility as well as the visibility of all of its tray
  // items according to the current state of notifications.
  UpdateVisibility();
  notification_icons_controller_->UpdateNotificationIcons();
  notification_icons_controller_->UpdateNotificationIndicators();
}

std::u16string NotificationCenterTray::GetAccessibleNameForBubble() {
  return l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_ACCESSIBLE_NAME);
}

void NotificationCenterTray::HandleLocaleChange() {}

void NotificationCenterTray::HideBubbleWithView(
    const TrayBubbleView* bubble_view) {
  if (bubble_->GetBubbleView() == bubble_view) {
    CloseBubble();
  }
}

void NotificationCenterTray::HideBubble(const TrayBubbleView* bubble_view) {
  CloseBubble();
}

void NotificationCenterTray::ClickedOutsideBubble(
    const ui::LocatedEvent& event) {
  CloseBubble();
}

void NotificationCenterTray::UpdateTrayItemColor(bool is_active) {
  for (views::View* tray_item : tray_container()->children()) {
    static_cast<TrayItemView*>(tray_item)->UpdateLabelOrImageViewColor(
        is_active);
  }
}

void NotificationCenterTray::CloseBubbleInternal() {
  if (!bubble_) {
    return;
  }

  bubble_.reset();
  SetIsActive(false);

  // Inform the message center that the bubble has closed so that popups are
  // created for new notifications.
  message_center::MessageCenter::Get()->SetVisibility(
      message_center::VISIBILITY_TRANSIENT);
}

void NotificationCenterTray::ShowBubble() {
  if (bubble_) {
    return;
  }

  // Inform the message center that the bubble is showing so that we do not
  // create popups for incoming notifications and dismiss existing popups. This
  // needs to happen before the bubble is created so that the
  // `NotificationListView` is the active `NotificationViewController` when the
  // `NotificationGroupingController` access it. This happens when notifications
  // are added to the `NotificationListView`.
  message_center::MessageCenter::Get()->SetVisibility(
      message_center::VISIBILITY_MESSAGE_CENTER);

  bubble_ = std::make_unique<NotificationCenterBubble>(this);
  bubble_->ShowBubble();

  SetIsActive(true);
}

void NotificationCenterTray::UpdateAfterLoginStatusChange() {
  UpdateVisibility();
}

TrayBubbleView* NotificationCenterTray::GetBubbleView() {
  return bubble_ ? bubble_->GetBubbleView() : nullptr;
}

views::Widget* NotificationCenterTray::GetBubbleWidget() const {
  return bubble_ ? bubble_->GetBubbleWidget() : nullptr;
}

void NotificationCenterTray::UpdateLayout() {
  TrayBackgroundView::UpdateLayout();

  if (privacy_indicators_view_) {
    privacy_indicators_view_->UpdateAlignmentForShelf(shelf());
  }
}

void NotificationCenterTray::UpdateVisibility() {
  // `NotificationIconsController` handles updating this tray's tray items, so
  // no need to do that here.
  const bool new_visibility =
      message_center::MessageCenter::Get()->NotificationCount() > 0 &&
      system_tray_visible_;
  SetVisiblePreferred(new_visibility);
  UpdateTrayItemColor(is_active());

  // We should close the bubble if there are no more notifications to show.
  if (!new_visibility && bubble_) {
    CloseBubble();
  }
}

void NotificationCenterTray::UpdateAccessibleName() {
  std::u16string name =
      notification_icons_controller_->GetAccessibleNameString().value_or(
          l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_ACCESSIBLE_NAME));
  GetViewAccessibility().SetName(name);
}

void NotificationCenterTray::AddCallbacksForAccessibility() {
  notification_counter_image_tooltip_changed_subscription_ =
      notification_icons_controller_->notification_counter_view()
          ->image_view()
          ->AddTooltipTextChangedCallback(
              base::BindRepeating(&NotificationCenterTray::UpdateAccessibleName,
                                  base::Unretained(this)));

  quiet_mode_visibility_changed_subscription_ =
      notification_icons_controller_->quiet_mode_view()
          ->AddVisibleChangedCallback(
              base::BindRepeating(&NotificationCenterTray::UpdateAccessibleName,
                                  base::Unretained(this)));

  notification_counter_visibility_changed_subscription_ =
      notification_icons_controller_->notification_counter_view()
          ->AddVisibleChangedCallback(
              base::BindRepeating(&NotificationCenterTray::UpdateAccessibleName,
                                  base::Unretained(this)));
}

BEGIN_METADATA(NotificationCenterTray)
END_METADATA

}  // namespace ash