unified_system_tray.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #ifndef ASH_SYSTEM_UNIFIED_UNIFIED_SYSTEM_TRAY_H_
  5. #define ASH_SYSTEM_UNIFIED_UNIFIED_SYSTEM_TRAY_H_
  6. #include <list>
  7. #include <memory>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/accelerators.h"
  10. #include "ash/public/cpp/shelf_config.h"
  11. #include "ash/shell_observer.h"
  12. #include "ash/system/status_area_widget.h"
  13. #include "ash/system/time/time_view.h"
  14. #include "ash/system/tray/tray_background_view.h"
  15. #include "ash/system/unified/unified_system_tray_controller.h"
  16. #include "ash/system/unified/unified_system_tray_model.h"
  17. #include "base/memory/scoped_refptr.h"
  18. #include "base/memory/weak_ptr.h"
  19. #include "base/scoped_multi_source_observation.h"
  20. #include "base/time/time.h"
  21. #include "base/timer/timer.h"
  22. namespace message_center {
  23. class MessagePopupView;
  24. } // namespace message_center
  25. namespace ash {
  26. class AutozoomToastController;
  27. class AshMessagePopupCollection;
  28. class CameraMicTrayItemView;
  29. class ChannelIndicatorView;
  30. class CurrentLocaleView;
  31. class ImeModeView;
  32. class ManagedDeviceTrayItemView;
  33. class NetworkTrayView;
  34. class NotificationGroupingController;
  35. class NotificationIconsController;
  36. class PrivacyIndicatorsTrayItemView;
  37. class PrivacyScreenToastController;
  38. class ScreenCaptureTrayItemView;
  39. class SnoopingProtectionView;
  40. class TimeTrayItemView;
  41. class TrayItemView;
  42. class UnifiedSliderBubbleController;
  43. class UnifiedSystemTrayBubble;
  44. class UnifiedMessageCenterBubble;
  45. // The UnifiedSystemTray is the system menu of Chromium OS, which is a clickable
  46. // rounded rectangle typically located on the bottom right corner of the screen,
  47. // (called the Status Area). The system tray shows multiple icons on it to
  48. // indicate system status (e.g. time, power, etc.).
  49. //
  50. // Note that the Status Area refers to the parent container of the
  51. // UnifiedSystemTray, which also includes other "trays" such as the ImeMenuTray,
  52. // SelectToSpeakTray, VirtualKeyboardTray, etc.
  53. //
  54. // UnifiedSystemTrayBubble is the actual menu bubble shown above the system tray
  55. // after the user clicks on it. The UnifiedSystemTrayBubble is created and owned
  56. // by this class.
  57. class ASH_EXPORT UnifiedSystemTray
  58. : public TrayBackgroundView,
  59. public ShelfConfig::Observer,
  60. public ShellObserver,
  61. public UnifiedSystemTrayController::Observer {
  62. public:
  63. class Observer : public base::CheckedObserver {
  64. public:
  65. // Gets called when showing calendar view.
  66. virtual void OnOpeningCalendarView() {}
  67. // Gets called when leaving from the calendar view.
  68. virtual void OnLeavingCalendarView() {}
  69. };
  70. explicit UnifiedSystemTray(Shelf* shelf);
  71. UnifiedSystemTray(const UnifiedSystemTray&) = delete;
  72. UnifiedSystemTray& operator=(const UnifiedSystemTray&) = delete;
  73. ~UnifiedSystemTray() override;
  74. void AddObserver(Observer* observer);
  75. void RemoveObserver(Observer* observer);
  76. // Adds a padding on top of the vertical clock if there are other visible
  77. // icons in the tray, removes it if the clock is the only visible icon.
  78. void MaybeUpdateVerticalClockPadding();
  79. // Update `privacy_indicators_view_` according to camera/microphone access.
  80. void UpdatePrivacyIndicatorsTrayItem(bool camera_is_used,
  81. bool microphone_is_used);
  82. // views::ViewObserver:
  83. void OnViewVisibilityChanged(views::View* observed_view,
  84. views::View* starting_view) override;
  85. // True if the bubble is shown. It does not include slider bubbles, and when
  86. // they're shown it still returns false.
  87. bool IsBubbleShown() const;
  88. // True if a slider bubble e.g. volume slider triggered by keyboard
  89. // accelerator is shown.
  90. bool IsSliderBubbleShown() const;
  91. // True if the bubble containing notifications is visible..
  92. bool IsMessageCenterBubbleShown() const;
  93. // True if the bubble is active.
  94. bool IsBubbleActive() const;
  95. // Closes all non-system tray bubbles (e.g. volume/brightness, and toasts) if
  96. // any are shown.
  97. void CloseSecondaryBubbles();
  98. // Activates the system tray bubble.
  99. void ActivateBubble();
  100. // Collapse the message center bubble.
  101. void CollapseMessageCenter();
  102. // Expand the message center bubble.
  103. void ExpandMessageCenter();
  104. // Ensure the quick settings bubble is collapsed.
  105. void EnsureQuickSettingsCollapsed(bool animate);
  106. // Ensure the system tray bubble is expanded.
  107. void EnsureBubbleExpanded();
  108. // Shows volume slider bubble shown at the right bottom of screen. The bubble
  109. // is same as one shown when volume buttons on keyboard are pressed.
  110. void ShowVolumeSliderBubble();
  111. // Shows main bubble with audio settings detailed view.
  112. void ShowAudioDetailedViewBubble();
  113. // Shows main bubble with network settings detailed view.
  114. void ShowNetworkDetailedViewBubble();
  115. // Return the bounds of the bubble in the screen.
  116. gfx::Rect GetBubbleBoundsInScreen() const;
  117. // Enable / disable UnifiedSystemTray button in status area. If the bubble is
  118. // open when disabling, also close it.
  119. void SetTrayEnabled(bool enabled);
  120. // Set the target notification, which is visible in the viewport when the
  121. // message center opens.
  122. void SetTargetNotification(const std::string& notification_id);
  123. // Sets the height of the system tray bubble from the edge of the work area
  124. // so that the notification popups don't overlap with the tray. Pass 0 if no
  125. // bubble is shown.
  126. void SetTrayBubbleHeight(int height);
  127. // Transfer focus to the message center bubble. Will focus only on the message
  128. // center if vox is enabled. Otherwise, will focus on the first element in the
  129. // message center while honoring the `reverse` attribute that is passed in.
  130. bool FocusMessageCenter(bool reverse, bool collapse_quick_settings = false);
  131. // Transfer focus to the quick settings bubble. Will focus only on the quick
  132. // settings bubble if vox is enabled. Otherwise, will focus on the first
  133. // element in the quick settings while honoring the `reverse` attribute that
  134. // is passed in.
  135. bool FocusQuickSettings(bool reverse);
  136. // Called by `UnifiedSystemTrayBubble` when it is destroyed with the calendar
  137. // view in the foreground.
  138. void NotifyLeavingCalendarView();
  139. // Returns true if the user manually expanded the quick settings.
  140. bool IsQuickSettingsExplicitlyExpanded() const;
  141. // This enum is for the ChromeOS.SystemTray.FirstInteraction UMA histogram and
  142. // should be kept in sync.
  143. enum class FirstInteractionType {
  144. kQuickSettings = 0,
  145. kMessageCenter = 1,
  146. kMaxValue = kMessageCenter,
  147. };
  148. // Records a metric of the first interaction with the tray bubble, i.e.
  149. // whether it was a click/tap on the message center or quick settings.
  150. void MaybeRecordFirstInteraction(FirstInteractionType type);
  151. // TrayBackgroundView:
  152. bool PerformAction(const ui::Event& event) override;
  153. void ShowBubble() override;
  154. void CloseBubble() override;
  155. std::u16string GetAccessibleNameForBubble() override;
  156. std::u16string GetAccessibleNameForTray() override;
  157. void HandleLocaleChange() override;
  158. void HideBubble(const TrayBubbleView* bubble_view) override;
  159. void HideBubbleWithView(const TrayBubbleView* bubble_view) override;
  160. void ClickedOutsideBubble() override;
  161. void UpdateLayout() override;
  162. void UpdateAfterLoginStatusChange() override;
  163. bool ShouldEnableExtraKeyboardAccessibility() override;
  164. views::Widget* GetBubbleWidget() const override;
  165. const char* GetClassName() const override;
  166. absl::optional<AcceleratorAction> GetAcceleratorAction() const override;
  167. // ShellObserver:
  168. void OnShelfAlignmentChanged(aura::Window* root_window,
  169. ShelfAlignment old_alignment) override;
  170. // ShelfConfig::Observer:
  171. void OnShelfConfigUpdated() override;
  172. // UnifiedSystemTrayController::Observer:
  173. void OnOpeningCalendarView() override;
  174. void OnTransitioningFromCalendarToMainView() override;
  175. // Gets called when an action is performed on the `DateTray`.
  176. void OnDateTrayActionPerformed(const ui::Event& event);
  177. // Whether the bubble is currently showing the calendar view.
  178. bool IsShowingCalendarView() const;
  179. // Returns whether the channel indicator should be shown.
  180. bool ShouldChannelIndicatorBeShown() const;
  181. std::u16string GetAccessibleNameForQuickSettingsBubble();
  182. AshMessagePopupCollection* GetMessagePopupCollection();
  183. NotificationGroupingController* GetNotificationGroupingController();
  184. scoped_refptr<UnifiedSystemTrayModel> model() { return model_; }
  185. UnifiedSystemTrayBubble* bubble() { return bubble_.get(); }
  186. UnifiedMessageCenterBubble* message_center_bubble() {
  187. return message_center_bubble_.get();
  188. }
  189. PrivacyIndicatorsTrayItemView* privacy_indicators_view() {
  190. return privacy_indicators_view_;
  191. }
  192. ChannelIndicatorView* channel_indicator_view() {
  193. return channel_indicator_view_;
  194. }
  195. private:
  196. static const base::TimeDelta kNotificationCountUpdateDelay;
  197. friend class NotificationGroupingControllerTest;
  198. friend class SystemTrayTestApi;
  199. friend class UnifiedSystemTrayTest;
  200. // Private class implements MessageCenterUiDelegate.
  201. class UiDelegate;
  202. // Forwarded from UiDelegate.
  203. void ShowBubbleInternal();
  204. void HideBubbleInternal();
  205. void UpdateNotificationInternal();
  206. void UpdateNotificationAfterDelay();
  207. // Forwarded to UiDelegate.
  208. message_center::MessagePopupView* GetPopupViewForNotificationID(
  209. const std::string& notification_id);
  210. // Adds the tray item to the the unified system tray container.
  211. // The container takes the ownership of |tray_item|.
  212. void AddTrayItemToContainer(TrayItemView* tray_item);
  213. // Returns true if there is two or more tray items that are visible.
  214. bool MoreThanOneVisibleTrayItem() const;
  215. // Add observed tray item views.
  216. void AddObservedTrayItem(TrayItemView* tray_item);
  217. // Destroys the `bubble_` and the `message_center_bubble_`, also handles
  218. // removing bubble related observers.
  219. void DestroyBubbles();
  220. std::unique_ptr<UiDelegate> ui_delegate_;
  221. std::unique_ptr<UnifiedSystemTrayBubble> bubble_;
  222. std::unique_ptr<UnifiedMessageCenterBubble> message_center_bubble_;
  223. // Model class that stores UnifiedSystemTray's UI specific variables.
  224. scoped_refptr<UnifiedSystemTrayModel> model_;
  225. const std::unique_ptr<UnifiedSliderBubbleController>
  226. slider_bubble_controller_;
  227. const std::unique_ptr<PrivacyScreenToastController>
  228. privacy_screen_toast_controller_;
  229. std::unique_ptr<AutozoomToastController> autozoom_toast_controller_;
  230. // Manages showing notification icons in the tray.
  231. const std::unique_ptr<NotificationIconsController>
  232. notification_icons_controller_;
  233. SnoopingProtectionView* const snooping_protection_view_;
  234. CurrentLocaleView* const current_locale_view_;
  235. ImeModeView* const ime_mode_view_;
  236. ManagedDeviceTrayItemView* const managed_device_view_;
  237. CameraMicTrayItemView* const camera_view_;
  238. CameraMicTrayItemView* const mic_view_;
  239. TimeTrayItemView* const time_view_;
  240. PrivacyIndicatorsTrayItemView* const privacy_indicators_view_;
  241. ScreenCaptureTrayItemView* const screen_capture_view_;
  242. NetworkTrayView* network_tray_view_ = nullptr;
  243. ChannelIndicatorView* channel_indicator_view_ = nullptr;
  244. // Contains all tray items views added to tray_container().
  245. std::list<TrayItemView*> tray_items_;
  246. base::ScopedMultiSourceObservation<views::View, views::ViewObserver>
  247. tray_items_observations_{this};
  248. // Padding owned by the view hierarchy used to separate vertical
  249. // clock from other tray icons.
  250. views::View* vertical_clock_padding_ = nullptr;
  251. base::OneShotTimer timer_;
  252. bool first_interaction_recorded_ = false;
  253. base::ObserverList<Observer> observers_;
  254. // Records time the QS bubble was shown. Used for metrics.
  255. base::TimeTicks time_opened_;
  256. base::WeakPtrFactory<UnifiedSystemTray> weak_factory_{this};
  257. };
  258. } // namespace ash
  259. #endif // ASH_SYSTEM_UNIFIED_UNIFIED_SYSTEM_TRAY_H_