status_area_widget.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright (c) 2012 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_STATUS_AREA_WIDGET_H_
  5. #define ASH_SYSTEM_STATUS_AREA_WIDGET_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/login_status.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "ash/public/cpp/shelf_types.h"
  10. #include "ash/shelf/shelf_component.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "ui/views/widget/widget.h"
  13. namespace aura {
  14. class Window;
  15. }
  16. namespace ash {
  17. class DateTray;
  18. class DictationButtonTray;
  19. class HoldingSpaceTray;
  20. class ImeMenuTray;
  21. class LogoutButtonTray;
  22. class MediaTray;
  23. class OverviewButtonTray;
  24. class PaletteTray;
  25. class PhoneHubTray;
  26. class ProjectorAnnotationTray;
  27. class SelectToSpeakTray;
  28. class Shelf;
  29. class StatusAreaOverflowButtonTray;
  30. class StatusAreaWidgetDelegate;
  31. class StopRecordingButtonTray;
  32. class TrayBackgroundView;
  33. class UnifiedSystemTray;
  34. class VirtualKeyboardTray;
  35. class EcheTray;
  36. // Widget showing the system tray, notification tray, and other tray views in
  37. // the bottom-right of the screen. Exists separately from ShelfView/ShelfWidget
  38. // so that it can be shown in cases where the rest of the shelf is hidden (e.g.
  39. // on secondary monitors at the login screen).
  40. class ASH_EXPORT StatusAreaWidget : public SessionObserver,
  41. public ShelfComponent,
  42. public views::Widget {
  43. public:
  44. // Whether the status area is collapsed or expanded. Currently, this is only
  45. // applicable in in-app tablet mode. Otherwise the state is NOT_COLLAPSIBLE.
  46. enum class CollapseState { NOT_COLLAPSIBLE, COLLAPSED, EXPANDED };
  47. // Used to keep track of visible TrayBubbles per display, and notify the shelf
  48. // when 0<->1 bubbles are visible.
  49. class ScopedTrayBubbleCounter {
  50. public:
  51. explicit ScopedTrayBubbleCounter(StatusAreaWidget* status_area_widget);
  52. ~ScopedTrayBubbleCounter();
  53. private:
  54. base::WeakPtr<StatusAreaWidget> status_area_widget_;
  55. };
  56. StatusAreaWidget(aura::Window* status_container, Shelf* shelf);
  57. StatusAreaWidget(const StatusAreaWidget&) = delete;
  58. StatusAreaWidget& operator=(const StatusAreaWidget&) = delete;
  59. ~StatusAreaWidget() override;
  60. // Returns the status area widget for the display that |window| is on.
  61. static StatusAreaWidget* ForWindow(aura::Window* window);
  62. // Creates the child tray views, initializes them, and shows the widget. Not
  63. // part of the constructor because some child views call back into this object
  64. // during construction.
  65. void Initialize();
  66. // Called by the client when the login status changes. Caches login_status
  67. // and calls UpdateAfterLoginStatusChange for the system tray and the web
  68. // notification tray.
  69. void UpdateAfterLoginStatusChange(LoginStatus login_status);
  70. // Updates the collapse state of the status area after the state of the shelf
  71. // changes.
  72. void UpdateCollapseState();
  73. // Logs the number of visible status area item pods. Called after the a pod
  74. // changes visibility.
  75. void LogVisiblePodCountMetric();
  76. // SessionObserver:
  77. void OnSessionStateChanged(session_manager::SessionState state) override;
  78. // ShelfComponent:
  79. void CalculateTargetBounds() override;
  80. gfx::Rect GetTargetBounds() const override;
  81. void UpdateLayout(bool animate) override;
  82. void UpdateTargetBoundsForGesture(int shelf_position) override;
  83. // Called by shelf layout manager when a locale change has been detected.
  84. void HandleLocaleChange();
  85. // It is called when the visibility of any tray bubbles changes.
  86. // Bubbles report their visibility change here and other tray items get
  87. // notified about when their `OnAnyBubbleVisibilityChanged` is called.
  88. void NotifyAnyBubbleVisibilityChanged(views::Widget* bubble_widget,
  89. bool visible);
  90. // Sets system tray visibility. Shows or hides widget if needed.
  91. void SetSystemTrayVisibility(bool visible);
  92. // Get the tray button that the system tray bubble and the notification center
  93. // bubble will be anchored. Usually |unified_system_tray_|, but when the
  94. // overview button is visible (i.e. tablet mode is enabled), it returns
  95. // |overview_button_tray_|.
  96. TrayBackgroundView* GetSystemTrayAnchor() const;
  97. // Called by media tray to calculate anchor rect.
  98. gfx::Rect GetMediaTrayAnchorRect() const;
  99. StatusAreaWidgetDelegate* status_area_widget_delegate() {
  100. return status_area_widget_delegate_;
  101. }
  102. UnifiedSystemTray* unified_system_tray() { return unified_system_tray_; }
  103. DateTray* date_tray() { return date_tray_; }
  104. DictationButtonTray* dictation_button_tray() {
  105. return dictation_button_tray_;
  106. }
  107. MediaTray* media_tray() { return media_tray_; }
  108. StatusAreaOverflowButtonTray* overflow_button_tray() {
  109. return overflow_button_tray_;
  110. }
  111. OverviewButtonTray* overview_button_tray() { return overview_button_tray_; }
  112. PaletteTray* palette_tray() { return palette_tray_; }
  113. StopRecordingButtonTray* stop_recording_button_tray() {
  114. return stop_recording_button_tray_;
  115. }
  116. ProjectorAnnotationTray* projector_annotation_tray() {
  117. return projector_annotation_tray_;
  118. }
  119. ImeMenuTray* ime_menu_tray() { return ime_menu_tray_; }
  120. HoldingSpaceTray* holding_space_tray() { return holding_space_tray_; }
  121. PhoneHubTray* phone_hub_tray() { return phone_hub_tray_; }
  122. EcheTray* eche_tray() { return eche_tray_; }
  123. SelectToSpeakTray* select_to_speak_tray() { return select_to_speak_tray_; }
  124. Shelf* shelf() { return shelf_; }
  125. const std::vector<TrayBackgroundView*>& tray_buttons() const {
  126. return tray_buttons_;
  127. }
  128. LoginStatus login_status() const { return login_status_; }
  129. // Returns true if the shelf should be visible. This is used when the
  130. // shelf is configured to auto-hide and test if the shelf should force
  131. // the shelf to remain visible.
  132. bool ShouldShowShelf() const;
  133. // True if any message bubble is shown.
  134. bool IsMessageBubbleShown() const;
  135. // Notifies child trays, and the |status_area_widget_delegate_| to schedule a
  136. // paint.
  137. void SchedulePaint();
  138. // Overridden from views::Widget:
  139. bool OnNativeWidgetActivationChanged(bool active) override;
  140. // TODO(jamescook): Introduce a test API instead of these methods.
  141. LogoutButtonTray* logout_button_tray_for_testing() {
  142. return logout_button_tray_;
  143. }
  144. VirtualKeyboardTray* virtual_keyboard_tray_for_testing() {
  145. return virtual_keyboard_tray_;
  146. }
  147. CollapseState collapse_state() const { return collapse_state_; }
  148. void set_collapse_state_for_test(CollapseState state) {
  149. collapse_state_ = state;
  150. }
  151. private:
  152. friend class MediaTrayTest;
  153. friend class TrayBackgroundViewTest;
  154. struct LayoutInputs {
  155. gfx::Rect bounds;
  156. CollapseState collapse_state = CollapseState::NOT_COLLAPSIBLE;
  157. float opacity = 0.0f;
  158. // Each bit keep track of one child's visibility.
  159. unsigned int child_visibility_bitmask = 0;
  160. // Indicates whether animation is allowed.
  161. bool should_animate = true;
  162. // |should_animate| does not affect the status area widget's target
  163. // layout. So it is not taken into consideration when comparing LayoutInputs
  164. // instances.
  165. bool operator==(const LayoutInputs& other) const {
  166. return bounds == other.bounds && collapse_state == other.collapse_state &&
  167. opacity == other.opacity &&
  168. child_visibility_bitmask == other.child_visibility_bitmask;
  169. }
  170. };
  171. // Collects the inputs for layout.
  172. LayoutInputs GetLayoutInputs() const;
  173. // The set of inputs that impact this widget's layout. The assumption is that
  174. // this widget needs a relayout if, and only if, one or more of these has
  175. // changed.
  176. absl::optional<LayoutInputs> layout_inputs_;
  177. // views::Widget:
  178. void OnMouseEvent(ui::MouseEvent* event) override;
  179. void OnGestureEvent(ui::GestureEvent* event) override;
  180. void OnScrollEvent(ui::ScrollEvent* event) override;
  181. // Adds a new tray button to the status area. Implementation is in source
  182. // file to avoid recursive includes, and function is not used outside of the
  183. // compilation unit. Template required for a type safe subclass to be
  184. // returned.
  185. // Any references to the method outside of this compilation unit will fail
  186. // linking unless a specialization is declared in status_area_widget.cc.
  187. template <typename TrayButtonT>
  188. TrayButtonT* AddTrayButton(std::unique_ptr<TrayButtonT> tray_button);
  189. // Called when in the collapsed state to calculate and update the visibility
  190. // of each tray button.
  191. void CalculateButtonVisibilityForCollapsedState();
  192. // Move the `stop_recording_button_tray_` to the front so that it's more
  193. // visible.
  194. void EnsureTrayOrder();
  195. // Calculates and returns the appropriate collapse state depending on
  196. // current conditions.
  197. CollapseState CalculateCollapseState() const;
  198. // Gets the collapse available width based on if the date tray is shown.
  199. // If `force_collapsible`, returns a fixed width which is not based on the
  200. // shelf width.
  201. int GetCollapseAvailableWidth(bool force_collapsible) const;
  202. StatusAreaWidgetDelegate* const status_area_widget_delegate_;
  203. StatusAreaOverflowButtonTray* overflow_button_tray_ = nullptr;
  204. OverviewButtonTray* overview_button_tray_ = nullptr;
  205. DictationButtonTray* dictation_button_tray_ = nullptr;
  206. MediaTray* media_tray_ = nullptr;
  207. DateTray* date_tray_ = nullptr;
  208. UnifiedSystemTray* unified_system_tray_ = nullptr;
  209. LogoutButtonTray* logout_button_tray_ = nullptr;
  210. PaletteTray* palette_tray_ = nullptr;
  211. PhoneHubTray* phone_hub_tray_ = nullptr;
  212. EcheTray* eche_tray_ = nullptr;
  213. StopRecordingButtonTray* stop_recording_button_tray_ = nullptr;
  214. ProjectorAnnotationTray* projector_annotation_tray_ = nullptr;
  215. VirtualKeyboardTray* virtual_keyboard_tray_ = nullptr;
  216. ImeMenuTray* ime_menu_tray_ = nullptr;
  217. SelectToSpeakTray* select_to_speak_tray_ = nullptr;
  218. HoldingSpaceTray* holding_space_tray_ = nullptr;
  219. // Vector of the tray buttons above. The ordering is used to determine which
  220. // tray buttons are hidden when they overflow the available width.
  221. std::vector<TrayBackgroundView*> tray_buttons_;
  222. LoginStatus login_status_ = LoginStatus::NOT_LOGGED_IN;
  223. CollapseState collapse_state_ = CollapseState::NOT_COLLAPSIBLE;
  224. gfx::Rect target_bounds_;
  225. Shelf* shelf_;
  226. bool initialized_ = false;
  227. // Number of active tray bubbles on the display where status area widget
  228. // lives.
  229. int tray_bubble_count_ = 0;
  230. base::WeakPtrFactory<StatusAreaWidget> weak_ptr_factory_{this};
  231. };
  232. } // namespace ash
  233. #endif // ASH_SYSTEM_STATUS_AREA_WIDGET_H_