File: notification_icons_controller.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (198 lines) | stat: -rw-r--r-- 7,475 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_
#define ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_

#include <cstdint>
#include <optional>

#include "ash/ash_export.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/system/tray/tray_item_view.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/display/display_observer.h"
#include "ui/message_center/message_center_observer.h"
#include "ui/message_center/message_center_types.h"

namespace message_center {
class Notification;
}  // namespace message_center

namespace ash {

class NotificationCenterTray;
class NotificationCounterView;
class NotificationIconsController;
class QuietModeView;
class TrayContainer;
class TrayItemView;
class UnifiedSystemTray;

// NotificationIconTrayItemView ------------------------------------------------

// Tray item view for notification icon shown in the tray.
class ASH_EXPORT NotificationIconTrayItemView : public TrayItemView {
  METADATA_HEADER(NotificationIconTrayItemView, TrayItemView)

 public:
  NotificationIconTrayItemView(Shelf* shelf,
                               NotificationIconsController* controller_);
  ~NotificationIconTrayItemView() override;
  NotificationIconTrayItemView(const NotificationIconTrayItemView&) = delete;
  NotificationIconTrayItemView& operator=(const NotificationIconTrayItemView&) =
      delete;

  // Set the image and tooltip for the view according to the notification.
  void SetNotification(message_center::Notification* notification);

  // Calls `Reset()` immediately or does nothing according to the following
  // flow:
  //   1. If this tray item is currently running its hide animation, or if the
  //      `NotificationCenterTray` it belongs to is running its hide animation,
  //      then this does nothing (because `Reset()` will be called at the end of
  //      the relevant animation).
  //   2. Otherwise, `Reset()` is still called immediately.
  void MaybeReset();

  // Returns a string describing the current state for accessibility.
  const std::u16string& GetAccessibleNameString() const;

  const std::string& GetNotificationId() const;

  // TrayItemView:
  void HandleLocaleChange() override;
  void OnThemeChanged() override;
  void UpdateLabelOrImageViewColor(bool active) override;
  void ImmediatelyUpdateVisibility() override;
  void AnimationEnded(const gfx::Animation* animation) override;

 private:
  void UpdateImageViewColor();

  // Resets notification pointer, id, image and tooltip text.
  void Reset();

  // Store the id to make sure we still have it when notification is removed and
  // goes out of scope.
  std::string notification_id_;

  // Stores the notification to update its icon color based on the active state
  // of this tray.
  std::unique_ptr<message_center::Notification> notification_;

  const raw_ptr<NotificationIconsController, DanglingUntriaged> controller_;
};

// NotificationIconsController -------------------------------------------------

// Controller for notification icons in `UnifiedSystemTray` button. This is used
// in `NotificationCenterTray`, and has the added responsibility of letting the
// `NotificationCenterTray` know when it may need to update its visibility. The
// icons will be displayed in medium or large screen size and only for important
// notifications.
class ASH_EXPORT NotificationIconsController
    : public display::DisplayObserver,
      public message_center::MessageCenterObserver,
      public SessionObserver {
 public:
  explicit NotificationIconsController(
      Shelf* shelf,
      NotificationCenterTray* notification_center_tray = nullptr);
  ~NotificationIconsController() override;
  NotificationIconsController(const NotificationIconsController&) = delete;
  NotificationIconsController& operator=(const NotificationIconsController&) =
      delete;

  // Initialize the view by adding items to the container of the tray.
  void AddNotificationTrayItems(TrayContainer* tray_container);

  // Returns true if any item in `tray_items_` is containing a notification.
  bool TrayItemHasNotification() const;

  // Returns the number of notification icons showing in |tray_items_|.
  size_t TrayNotificationIconsCount() const;

  // Returns a string describing the current state for accessibility, if any.
  std::optional<std::u16string> GetAccessibleNameString() const;

  // Iterate through the notifications in message center and update the icons
  // shown accordingly.
  void UpdateNotificationIcons();

  // Update notification indicators, including counters and quiet mode view.
  void UpdateNotificationIndicators();

  // display::DisplayObserver:
  void OnDisplayMetricsChanged(const display::Display& display,
                               uint32_t changed_metrics) override;

  // message_center::MessageCenterObserver:
  void OnNotificationAdded(const std::string& id) override;
  void OnNotificationRemoved(const std::string& id, bool by_user) override;
  void OnNotificationUpdated(const std::string& id) override;
  void OnNotificationDisplayed(
      const std::string& notification_id,
      const message_center::DisplaySource source) override;
  void OnQuietModeChanged(bool in_quiet_mode) override;

  // SessionObserver:
  void OnSessionStateChanged(session_manager::SessionState state) override;

  std::vector<raw_ptr<NotificationIconTrayItemView, VectorExperimental>>
  tray_items() {
    return tray_items_;
  }

  NotificationCounterView* notification_counter_view() {
    return notification_counter_view_;
  }

  QuietModeView* quiet_mode_view() { return quiet_mode_view_; }

  bool icons_view_visible() const { return icons_view_visible_; }

 private:
  friend class NotificationIconsControllerTest;

  // If the notification with given id is currently shown in tray, returns the
  // pointer to that tray item. Otherwise, returns a null pointer.
  NotificationIconTrayItemView* GetNotificationIconShownInTray(
      const std::string& id);

  // Updates `icons_view_visible_` according to the size of the display
  // associated with `shelf_`.
  void UpdateIconsViewVisibleForDisplaySize();

  // Contains notification icon tray items that are added to tray container. All
  // items are owned by views hierarchy.
  std::vector<raw_ptr<NotificationIconTrayItemView, VectorExperimental>>
      tray_items_;

  // Points to the first item that is available to use among the notification
  // icons tray item. All the items in previous index are used and visible.
  size_t first_unused_item_index_ = 0;

  // Indicates if the notification icons view is set to be shown. Currently, we
  // show the icon view in medium or large screen size.
  bool icons_view_visible_ = false;

  // Owned by `RootWindowController`
  const raw_ptr<Shelf> shelf_;

  // `NotificationCenterTray` owns this `NotificationIconsController`.
  // `NotificationCenterTray` itself is owned by the views hierarchy.
  raw_ptr<NotificationCenterTray> notification_center_tray_ = nullptr;

  raw_ptr<NotificationCounterView> notification_counter_view_ = nullptr;
  raw_ptr<QuietModeView> quiet_mode_view_ = nullptr;

  display::ScopedDisplayObserver display_observer_{this};
};

}  // namespace ash

#endif  // ASH_SYSTEM_UNIFIED_NOTIFICATION_ICONS_CONTROLLER_H_