notification_menu_header_view.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2018 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. #include "ash/app_menu/notification_menu_header_view.h"
  5. #include "ash/public/cpp/app_menu_constants.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "ui/base/l10n/l10n_util.h"
  8. #include "ui/strings/grit/ui_strings.h"
  9. #include "ui/views/border.h"
  10. #include "ui/views/controls/label.h"
  11. #include "ui/views/controls/menu/menu_config.h"
  12. namespace ash {
  13. namespace {
  14. // Color of text in NotificationMenuHeaderView.
  15. constexpr SkColor kNotificationHeaderTextColor =
  16. SkColorSetRGB(0X1A, 0x73, 0xE8);
  17. // Line height of all text in the NotificationMenuHeaderView in dips.
  18. constexpr int kNotificationHeaderLineHeight = 20;
  19. } // namespace
  20. NotificationMenuHeaderView::NotificationMenuHeaderView() {
  21. SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(
  22. kNotificationVerticalPadding, kNotificationHorizontalPadding)));
  23. notification_title_ = new views::Label(
  24. std::u16string(l10n_util::GetStringUTF16(
  25. IDS_MESSAGE_CENTER_NOTIFICATION_ACCESSIBLE_NAME_PLURAL)),
  26. {views::Label::GetDefaultFontList().DeriveWithSizeDelta(1)});
  27. notification_title_->SetEnabledColor(kNotificationHeaderTextColor);
  28. notification_title_->SetLineHeight(kNotificationHeaderLineHeight);
  29. AddChildView(notification_title_);
  30. counter_ = new views::Label(
  31. std::u16string(),
  32. {views::Label::GetDefaultFontList().DeriveWithSizeDelta(1)});
  33. counter_->SetEnabledColor(kNotificationHeaderTextColor);
  34. counter_->SetLineHeight(kNotificationHeaderLineHeight);
  35. AddChildView(counter_);
  36. }
  37. NotificationMenuHeaderView::~NotificationMenuHeaderView() = default;
  38. void NotificationMenuHeaderView::UpdateCounter(int number_of_notifications) {
  39. if (number_of_notifications_ == number_of_notifications)
  40. return;
  41. number_of_notifications_ = number_of_notifications;
  42. counter_->SetText(base::NumberToString16(number_of_notifications_));
  43. }
  44. gfx::Size NotificationMenuHeaderView::CalculatePreferredSize() const {
  45. return gfx::Size(
  46. views::MenuConfig::instance().touchable_menu_min_width,
  47. GetInsets().height() + notification_title_->GetPreferredSize().height());
  48. }
  49. void NotificationMenuHeaderView::Layout() {
  50. const gfx::Insets insets = GetInsets();
  51. const gfx::Size notification_title_preferred_size =
  52. notification_title_->GetPreferredSize();
  53. notification_title_->SetBounds(insets.left(), insets.top(),
  54. notification_title_preferred_size.width(),
  55. notification_title_preferred_size.height());
  56. const gfx::Size counter_preferred_size = counter_->GetPreferredSize();
  57. counter_->SetBounds(width() - counter_preferred_size.width() - insets.right(),
  58. insets.top(), counter_preferred_size.width(),
  59. counter_preferred_size.height());
  60. }
  61. } // namespace ash