unified_system_tray_controller.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_CONTROLLER_H_
  5. #define ASH_SYSTEM_UNIFIED_UNIFIED_SYSTEM_TRAY_CONTROLLER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "ash/system/audio/unified_volume_slider_controller.h"
  11. #include "ash/system/media/unified_media_controls_controller.h"
  12. #include "ash/system/time/calendar_metrics.h"
  13. #include "ash/system/time/calendar_model.h"
  14. #include "ash/system/unified/unified_system_tray_model.h"
  15. #include "base/memory/scoped_refptr.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. #include "ui/compositor/throughput_tracker.h"
  18. #include "ui/gfx/geometry/point.h"
  19. #include "ui/views/animation/animation_delegate_views.h"
  20. class PrefRegistrySimple;
  21. class PrefService;
  22. namespace gfx {
  23. class SlideAnimation;
  24. } // namespace gfx
  25. namespace ash {
  26. class DetailedViewController;
  27. class FeaturePodControllerBase;
  28. class PaginationController;
  29. class UnifiedMediaControlsController;
  30. class UnifiedBrightnessSliderController;
  31. class UnifiedVolumeSliderController;
  32. class UnifiedSystemTrayBubble;
  33. class UnifiedSystemTrayModel;
  34. class UnifiedSystemTrayView;
  35. // Controller class of UnifiedSystemTrayView. Handles events of the view.
  36. class ASH_EXPORT UnifiedSystemTrayController
  37. : public views::AnimationDelegateViews,
  38. public SessionObserver,
  39. public UnifiedVolumeSliderController::Delegate,
  40. public UnifiedMediaControlsController::Delegate {
  41. public:
  42. class Observer : public base::CheckedObserver {
  43. public:
  44. // Gets called when `ShowCalendarView`, right as animations starts.
  45. virtual void OnOpeningCalendarView() {}
  46. // Gets called when leaving from the calendar view to main view.
  47. virtual void OnTransitioningFromCalendarToMainView() {}
  48. };
  49. explicit UnifiedSystemTrayController(
  50. scoped_refptr<UnifiedSystemTrayModel> model,
  51. UnifiedSystemTrayBubble* bubble = nullptr,
  52. views::View* owner_view = nullptr);
  53. UnifiedSystemTrayController(const UnifiedSystemTrayController&) = delete;
  54. UnifiedSystemTrayController& operator=(const UnifiedSystemTrayController&) =
  55. delete;
  56. ~UnifiedSystemTrayController() override;
  57. void AddObserver(Observer* observer);
  58. void RemoveObserver(Observer* observer);
  59. // Registers pref to preserve tray expanded state between reboots.
  60. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  61. // Create the view. The created view is unowned.
  62. UnifiedSystemTrayView* CreateView();
  63. // Sign out from the current user. Called from the view.
  64. void HandleSignOutAction();
  65. // Show lock screen which asks the user password. Called from the view.
  66. void HandleLockAction();
  67. // Show WebUI settings. Called from the view.
  68. void HandleSettingsAction();
  69. // Shutdown the computer. Called from the view.
  70. void HandlePowerAction();
  71. // Switch to page represented by it's button. Called from the view.
  72. void HandlePageSwitchAction(int page);
  73. // Show date and time settings. Called from the view.
  74. void HandleOpenDateTimeSettingsAction();
  75. // Show power settings. Called from the view.
  76. void HandleOpenPowerSettingsAction();
  77. // Show enterprise managed device info. Called from the view.
  78. void HandleEnterpriseInfoAction();
  79. // Toggle expanded state of UnifiedSystemTrayView. Called from the view.
  80. void ToggleExpanded();
  81. // Handle finger dragging and expand/collapse the view. Called from view.
  82. void BeginDrag(const gfx::PointF& location);
  83. void UpdateDrag(const gfx::PointF& location);
  84. void EndDrag(const gfx::PointF& location);
  85. void Fling(int velocity);
  86. // Show user selector view. Called from the view.
  87. void ShowUserChooserView();
  88. // Show the detailed view of network. If |force| is true, it shows the
  89. // detailed view even if it's collapsed. Called from the view.
  90. void ShowNetworkDetailedView(bool force);
  91. // Show the detailed view of bluetooth. If collapsed, it doesn't show the
  92. // detailed view. Called from the view.
  93. void ShowBluetoothDetailedView();
  94. // Show the detailed view of cast. Called from the view.
  95. void ShowCastDetailedView();
  96. // Show the detailed view of accessibility. Called from the view.
  97. void ShowAccessibilityDetailedView();
  98. // Show the detailed view of VPN. Called from the view.
  99. void ShowVPNDetailedView();
  100. // Show the detailed view of IME. Called from the view.
  101. void ShowIMEDetailedView();
  102. // Show the detailed view of locale. Called from the view.
  103. void ShowLocaleDetailedView();
  104. // Show the detailed view of audio. Called from the view.
  105. void ShowAudioDetailedView();
  106. // Show the detailed view of notifier settings. Called from the view.
  107. void ShowNotifierSettingsView();
  108. // Show the detailed view of media controls. Called from the view.
  109. void ShowMediaControlsDetailedView();
  110. // Show the detailed view of Calendar. Called from the view.
  111. void ShowCalendarView(calendar_metrics::CalendarViewShowSource show_source,
  112. calendar_metrics::CalendarEventSource event_source);
  113. // If you want to add a new detailed view, add here.
  114. // Show the main view back from a detailed view. If |restore_focus| is true,
  115. // it restores previous keyboard focus in the main view. Called from a
  116. // detailed view controller.
  117. void TransitionToMainView(bool restore_focus);
  118. // Close the bubble. Called from a detailed view controller.
  119. void CloseBubble();
  120. // Inform UnifiedSystemTrayBubble that UnifiedSystemTrayView is requesting to
  121. // relinquish focus.
  122. bool FocusOut(bool reverse);
  123. // Ensure the main view is collapsed. Called from the slider bubble
  124. // controller.
  125. void EnsureCollapsed();
  126. // Ensure the main view is expanded. Called from the slider bubble controller.
  127. void EnsureExpanded();
  128. // Collapse the tray without animating if there isn't sufficient space for the
  129. // notifications area.
  130. void ResetToCollapsedIfRequired();
  131. // Collapse the tray without animating.
  132. void CollapseWithoutAnimating();
  133. // SessionObserver:
  134. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  135. // views::AnimationDelegateViews:
  136. void AnimationEnded(const gfx::Animation* animation) override;
  137. void AnimationProgressed(const gfx::Animation* animation) override;
  138. void AnimationCanceled(const gfx::Animation* animation) override;
  139. // UnifiedVolumeSliderController::Delegate:
  140. void OnAudioSettingsButtonClicked() override;
  141. // UnifedMediaControlsController::Delegate;
  142. void ShowMediaControls() override;
  143. void OnMediaControlsViewClicked() override;
  144. // Return true if UnifiedSystemTray is expanded.
  145. bool IsExpanded() const;
  146. scoped_refptr<UnifiedSystemTrayModel> model() { return model_; }
  147. PaginationController* pagination_controller() {
  148. return pagination_controller_.get();
  149. }
  150. DetailedViewController* detailed_view_controller() {
  151. return detailed_view_controller_.get();
  152. }
  153. bool showing_audio_detailed_view() const {
  154. return showing_audio_detailed_view_;
  155. }
  156. bool showing_calendar_view() const { return showing_calendar_view_; }
  157. private:
  158. friend class SystemTrayTestApi;
  159. friend class UnifiedSystemTrayControllerTest;
  160. friend class UnifiedMessageCenterBubbleTest;
  161. // How the expanded state is toggled. The enum is used to back an UMA
  162. // histogram and should be treated as append-only.
  163. enum ToggleExpandedType {
  164. TOGGLE_EXPANDED_TYPE_BY_BUTTON = 0,
  165. TOGGLE_EXPANDED_TYPE_BY_GESTURE,
  166. TOGGLE_EXPANDED_TYPE_COUNT
  167. };
  168. // Type of a help page opened by the "Managed" indicator in the bubble. The
  169. // enum is used to back an UMA histogram and should be treated as append-only.
  170. enum ManagedType { MANAGED_TYPE_ENTERPRISE = 0, MANAGED_TYPE_COUNT };
  171. // Loads the `kSystemTrayExpanded` pref to the model.
  172. void LoadIsExpandedPref();
  173. // Initialize feature pod controllers and their views.
  174. // If you want to add a new feature pod item, you have to add here.
  175. void InitFeaturePods();
  176. // Add the feature pod controller and its view.
  177. void AddFeaturePodItem(std::unique_ptr<FeaturePodControllerBase> controller);
  178. // Show the detailed view.
  179. void ShowDetailedView(std::unique_ptr<DetailedViewController> controller);
  180. // Update how much the view is expanded based on |animation_|.
  181. void UpdateExpandedAmount();
  182. // Update the gesture distance by using the tray's collapsed and expanded
  183. // height.
  184. void UpdateDragThreshold();
  185. // Return touch drag amount between 0.0 and 1.0. If expanding, it increases
  186. // towards 1.0. If collapsing, it decreases towards 0.0. If the view is
  187. // dragged to the same direction as the current state, it does not change the
  188. // value. For example, if the view is expanded and it's dragged to the top, it
  189. // keeps returning 1.0.
  190. double GetDragExpandedAmount(const gfx::PointF& location) const;
  191. // Return true if message center needs to be collapsed due to limited
  192. // screen height.
  193. bool IsMessageCenterCollapseRequired() const;
  194. // Starts animation to expand or collapse the bubble.
  195. void StartAnimation(bool expand);
  196. // views::AnimationDelegateViews:
  197. base::TimeDelta GetAnimationDurationForReporting() const override;
  198. bool ShouldShowDeferredUpdateDialog() const;
  199. // Model that stores UI specific variables. Unowned.
  200. scoped_refptr<UnifiedSystemTrayModel> model_;
  201. // Unowned. Owned by Views hierarchy.
  202. UnifiedSystemTrayView* unified_view_ = nullptr;
  203. // Unowned.
  204. UnifiedSystemTrayBubble* bubble_ = nullptr;
  205. // The pref service of the currently active user. Can be null in tests.
  206. PrefService* active_user_prefs_ = nullptr;
  207. // The controller of the current detailed view. If the main view is shown,
  208. // it's null. Owned.
  209. std::unique_ptr<DetailedViewController> detailed_view_controller_;
  210. // Controllers of feature pod buttons. Owned by this.
  211. std::vector<std::unique_ptr<FeaturePodControllerBase>>
  212. feature_pod_controllers_;
  213. std::unique_ptr<PaginationController> pagination_controller_;
  214. std::unique_ptr<UnifiedMediaControlsController> media_controls_controller_;
  215. // Controller of volume slider. Owned.
  216. std::unique_ptr<UnifiedVolumeSliderController> volume_slider_controller_;
  217. // Controller of brightness slider. Owned.
  218. std::unique_ptr<UnifiedBrightnessSliderController>
  219. brightness_slider_controller_;
  220. // If the previous state is expanded or not. Only valid during dragging (from
  221. // BeginDrag to EndDrag).
  222. bool was_expanded_ = true;
  223. // The last |location| passed to BeginDrag(). Only valid during dragging.
  224. gfx::PointF drag_init_point_;
  225. // Threshold in pixel that fully collapses / expands the view through gesture.
  226. // Used to calculate the expanded amount that corresponds to gesture location
  227. // during drag.
  228. double drag_threshold_ = 0;
  229. // Animation between expanded and collapsed states.
  230. std::unique_ptr<gfx::SlideAnimation> animation_;
  231. // Tracks the smoothness of collapse and expand animation.
  232. absl::optional<ui::ThroughputTracker> animation_tracker_;
  233. bool showing_audio_detailed_view_ = false;
  234. bool showing_calendar_view_ = false;
  235. base::ObserverList<Observer> observers_;
  236. };
  237. } // namespace ash
  238. #endif // ASH_SYSTEM_UNIFIED_UNIFIED_SYSTEM_TRAY_CONTROLLER_H_