notification_icons_controller.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2020 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. #ifndef ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_
  5. #define ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/session/session_observer.h"
  8. #include "ash/system/tray/tray_item_view.h"
  9. #include "ash/system/unified/unified_system_tray_model.h"
  10. #include "base/scoped_observation.h"
  11. #include "ui/message_center/message_center_observer.h"
  12. namespace message_center {
  13. class Notification;
  14. } // namespace message_center
  15. namespace ash {
  16. class UnifiedSystemTray;
  17. class TrayContainer;
  18. class TrayItemView;
  19. class NotificationCounterView;
  20. class NotificationIconsController;
  21. class QuietModeView;
  22. class SeparatorTrayItemView;
  23. // Tray item view for notification icon shown in the tray.
  24. class ASH_EXPORT NotificationIconTrayItemView : public TrayItemView {
  25. public:
  26. NotificationIconTrayItemView(Shelf* shelf,
  27. NotificationIconsController* controller_);
  28. ~NotificationIconTrayItemView() override;
  29. NotificationIconTrayItemView(const NotificationIconTrayItemView&) = delete;
  30. NotificationIconTrayItemView& operator=(const NotificationIconTrayItemView&) =
  31. delete;
  32. // Set the image and tooltip for the view according to the notification.
  33. void SetNotification(message_center::Notification* notification);
  34. // Reset notification pointer, id, image and tooltip text.
  35. void Reset();
  36. // Returns a string describing the current state for accessibility.
  37. const std::u16string& GetAccessibleNameString() const;
  38. const std::string& GetNotificationId() const;
  39. // TrayItemView:
  40. void HandleLocaleChange() override;
  41. const char* GetClassName() const override;
  42. void OnThemeChanged() override;
  43. private:
  44. // Store the id to make sure we still have it when notification is removed and
  45. // goes out of scope.
  46. std::string notification_id_;
  47. NotificationIconsController* const controller_;
  48. };
  49. // Controller for notification icons in UnifiedSystemTray button. The icons will
  50. // be displayed in medium or large screen size and only for important
  51. // notifications.
  52. class ASH_EXPORT NotificationIconsController
  53. : public UnifiedSystemTrayModel::Observer,
  54. public message_center::MessageCenterObserver,
  55. public SessionObserver {
  56. public:
  57. explicit NotificationIconsController(UnifiedSystemTray* tray);
  58. ~NotificationIconsController() override;
  59. NotificationIconsController(const NotificationIconsController&) = delete;
  60. NotificationIconsController& operator=(const NotificationIconsController&) =
  61. delete;
  62. // Initialize the view by adding items to the container of the tray.
  63. void AddNotificationTrayItems(TrayContainer* tray_container);
  64. // Returns true if any item in `tray_items_` is containing a notification.
  65. bool TrayItemHasNotification() const;
  66. // Returns the number of notification icons showing in |tray_items_|.
  67. size_t TrayNotificationIconsCount() const;
  68. // Returns a string describing the current state for accessibility.
  69. std::u16string GetAccessibleNameString() const;
  70. // Update notification indicators, including counters and quiet mode view.
  71. void UpdateNotificationIndicators();
  72. // UnifiedSystemTrayModel::Observer:
  73. void OnSystemTrayButtonSizeChanged(
  74. UnifiedSystemTrayModel::SystemTrayButtonSize system_tray_size) override;
  75. // message_center::MessageCenterObserver:
  76. void OnNotificationAdded(const std::string& id) override;
  77. void OnNotificationRemoved(const std::string& id, bool by_user) override;
  78. void OnNotificationUpdated(const std::string& id) override;
  79. // SessionObserver:
  80. void OnSessionStateChanged(session_manager::SessionState state) override;
  81. std::vector<NotificationIconTrayItemView*> tray_items() {
  82. return tray_items_;
  83. }
  84. NotificationCounterView* notification_counter_view() {
  85. return notification_counter_view_;
  86. }
  87. QuietModeView* quiet_mode_view() { return quiet_mode_view_; }
  88. bool icons_view_visible() const { return icons_view_visible_; }
  89. // Iterate through the notifications in message center and update the icons
  90. // shown accordingly.
  91. void UpdateNotificationIcons();
  92. private:
  93. friend class NotificationIconsControllerTest;
  94. // If the notification with given id is currently shown in tray, returns the
  95. // pointer to that tray item. Otherwise, returns a null pointer.
  96. NotificationIconTrayItemView* GetNotificationIconShownInTray(
  97. const std::string& id);
  98. // Contains notification icon tray items that are added to tray container. All
  99. // items are owned by views hierarchy.
  100. std::vector<NotificationIconTrayItemView*> tray_items_;
  101. // Points to the first item that is available to use among the notification
  102. // icons tray item. All the items in previous index are used and visible.
  103. size_t first_unused_item_index_ = 0;
  104. // Indicates if the notification icons view is set to be shown. Currently, we
  105. // show the icon view in medium or large screen size.
  106. bool icons_view_visible_ = false;
  107. UnifiedSystemTray* tray_;
  108. NotificationCounterView* notification_counter_view_ = nullptr;
  109. QuietModeView* quiet_mode_view_ = nullptr;
  110. SeparatorTrayItemView* separator_ = nullptr;
  111. base::ScopedObservation<UnifiedSystemTrayModel,
  112. UnifiedSystemTrayModel::Observer>
  113. system_tray_model_observation_{this};
  114. };
  115. } // namespace ash
  116. #endif // ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_