unified_message_center_view.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_MESSAGE_CENTER_UNIFIED_MESSAGE_CENTER_VIEW_H_
  5. #define ASH_SYSTEM_MESSAGE_CENTER_UNIFIED_MESSAGE_CENTER_VIEW_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/system/message_center/message_center_scroll_bar.h"
  8. #include "ash/system/message_center/unified_message_list_view.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "ui/gfx/animation/animation_delegate.h"
  11. #include "ui/views/background.h"
  12. #include "ui/views/controls/button/button.h"
  13. #include "ui/views/controls/label.h"
  14. #include "ui/views/focus/focus_manager.h"
  15. #include "ui/views/view.h"
  16. namespace gfx {
  17. class LinearAnimation;
  18. } // namespace gfx
  19. namespace message_center {
  20. class Notification;
  21. } // namespace message_center
  22. namespace views {
  23. class ScrollView;
  24. class ScrollBar;
  25. } // namespace views
  26. namespace ash {
  27. class StackedNotificationBar;
  28. class UnifiedMessageCenterBubble;
  29. class UnifiedSystemTrayModel;
  30. class UnifiedSystemTrayView;
  31. // Note: This enum represents the current animation state for
  32. // UnifiedMessageCenterView. There is an equivalent animation state enum in
  33. // the child UnifiedMessageListView. The animations for these two views can
  34. // occur simultaneously or independently, so states for both views are tracked
  35. // separately.
  36. enum class UnifiedMessageCenterAnimationState {
  37. // No animation is running.
  38. IDLE,
  39. // Animating hiding the stacking bar. Runs when the user dismisses the
  40. // second to last notification and during the clear all animation.
  41. HIDE_STACKING_BAR,
  42. // Animating collapsing the entire message center. Runs after the user
  43. // dismisses the last notification and during the clear all animation.
  44. // TODO(tengs): This animation is not yet implemented.
  45. COLLAPSE,
  46. };
  47. // Manages scrolling of notification list.
  48. class ASH_EXPORT UnifiedMessageCenterView
  49. : public views::View,
  50. public MessageCenterScrollBar::Observer,
  51. public views::FocusChangeListener,
  52. public gfx::AnimationDelegate {
  53. public:
  54. UnifiedMessageCenterView(UnifiedSystemTrayView* parent,
  55. scoped_refptr<UnifiedSystemTrayModel> model,
  56. UnifiedMessageCenterBubble* bubble);
  57. UnifiedMessageCenterView(const UnifiedMessageCenterView&) = delete;
  58. UnifiedMessageCenterView& operator=(const UnifiedMessageCenterView&) = delete;
  59. ~UnifiedMessageCenterView() override;
  60. // Initializes the `UnifiedMessageListView` with existing notifications.
  61. // Should be called after ctor.
  62. void Init();
  63. // Sets the maximum height that the view can take.
  64. // TODO(tengs): The layout of this view is heavily dependant on this max
  65. // height (equal to the height of the entire tray), but we should refactor and
  66. // consolidate this function with SetAvailableHeight().
  67. void SetMaxHeight(int max_height);
  68. // Sets the height available to the message center view. This is the remaining
  69. // height after counting the system menu, which may be expanded or collapsed.
  70. void SetAvailableHeight(int available_height);
  71. // Called from UnifiedMessageListView when the preferred size is changed.
  72. void ListPreferredSizeChanged();
  73. // Called from the UnifiedMessageListView after a notification is dismissed by
  74. // the user and the slide animation is finished.
  75. void OnNotificationSlidOut();
  76. // Configures MessageView to forward scroll events. Called from
  77. // UnifiedMessageListView.
  78. void ConfigureMessageView(message_center::MessageView* message_view);
  79. // Count number of notifications that are still in the MessageCenter that are
  80. // above visible area. NOTE: views may be in the view hierarchy, but no longer
  81. // in the message center.
  82. std::vector<message_center::Notification*> GetStackedNotifications() const;
  83. // Count the number of notifications that are not visible in the scrollable
  84. // window, but still in the view hierarchy, with no checks for whether they
  85. // are in the message center.
  86. std::vector<std::string> GetNonVisibleNotificationIdsInViewHierarchy() const;
  87. // Relinquish focus and transfer it to the quick settings widget.
  88. void FocusOut(bool reverse);
  89. // Set the first notification view to be focused when focus is acquired.
  90. // This is the oldest notification if `reverse` is `true`. Otherwise, if
  91. // `reverse` is `false`, this is the newest notification.
  92. void FocusEntered(bool reverse);
  93. // Expand message center to show all notifications and stacked notification
  94. // bar if needed.
  95. void SetExpanded();
  96. // Collapse the message center to only show the stacked notification bar.
  97. void SetCollapsed(bool animate);
  98. // Called when user clicks the clear all button.
  99. void ClearAllNotifications();
  100. // Called when user clicks the see all notifications button.
  101. void ExpandMessageCenter();
  102. // Returns true if the notification bar is visible.
  103. bool IsNotificationBarVisible() const;
  104. // Returns true if the scroll bar is visible.
  105. bool IsScrollBarVisible() const;
  106. // views::View:
  107. void AddedToWidget() override;
  108. void RemovedFromWidget() override;
  109. void Layout() override;
  110. gfx::Size CalculatePreferredSize() const override;
  111. const char* GetClassName() const override;
  112. // MessageCenterScrollBar::Observer:
  113. void OnMessageCenterScrolled() override;
  114. // views::FocusChangeListener:
  115. void OnWillChangeFocus(views::View* before, views::View* now) override;
  116. void OnDidChangeFocus(views::View* before, views::View* now) override;
  117. // gfx::AnimationDelegate:
  118. void AnimationEnded(const gfx::Animation* animation) override;
  119. void AnimationProgressed(const gfx::Animation* animation) override;
  120. void AnimationCanceled(const gfx::Animation* animation) override;
  121. UnifiedMessageListView* message_list_view() { return message_list_view_; }
  122. bool collapsed() { return collapsed_; }
  123. private:
  124. friend class UnifiedMessageCenterViewTest;
  125. friend class UnifiedMessageCenterBubbleTest;
  126. // Starts the animation to hide the notification stacking bar.
  127. void StartHideStackingBarAnimation();
  128. // Starts the animation to collapse the message center.
  129. void StartCollapseAnimation();
  130. // Returns the current animation value after tweening.
  131. double GetAnimationValue() const;
  132. // Decides whether the message center should be shown or not based on
  133. // current state.
  134. void UpdateVisibility();
  135. // Scroll the notification list to the target position.
  136. void ScrollToTarget();
  137. // Get first and last focusable child views. These functions are used to
  138. // figure out if we need to focus out or to set the correct focused view
  139. // when focus is acquired from another widget.
  140. View* GetFirstFocusableChild();
  141. View* GetLastFocusableChild();
  142. UnifiedSystemTrayView* const parent_;
  143. scoped_refptr<UnifiedSystemTrayModel> model_;
  144. UnifiedMessageCenterBubble* const message_center_bubble_;
  145. StackedNotificationBar* const notification_bar_;
  146. views::ScrollBar* scroll_bar_;
  147. views::ScrollView* const scroller_;
  148. UnifiedMessageListView* const message_list_view_;
  149. // Position from the bottom of scroll contents in dip.
  150. int last_scroll_position_from_bottom_;
  151. const bool is_notifications_refresh_enabled_;
  152. // The height available to the message center view. This is the remaining
  153. // height of the system tray excluding the system menu (which can be expanded
  154. // or collapsed).
  155. int available_height_ = 0;
  156. // Current state of the message center view.
  157. bool collapsed_ = false;
  158. // Tracks the current animation state.
  159. UnifiedMessageCenterAnimationState animation_state_ =
  160. UnifiedMessageCenterAnimationState::IDLE;
  161. const std::unique_ptr<gfx::LinearAnimation> animation_;
  162. const std::unique_ptr<views::FocusSearch> focus_search_;
  163. views::FocusManager* focus_manager_ = nullptr;
  164. };
  165. } // namespace ash
  166. #endif // ASH_SYSTEM_MESSAGE_CENTER_UNIFIED_MESSAGE_CENTER_VIEW_H_