message_center_utils.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2021 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. #include "ash/system/message_center/message_center_utils.h"
  5. #include "ash/public/cpp/metrics_util.h"
  6. #include "ash/public/cpp/vm_camera_mic_constants.h"
  7. #include "ash/root_window_controller.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/message_center/notification_grouping_controller.h"
  10. #include "ash/system/status_area_widget.h"
  11. #include "ash/system/unified/unified_system_tray.h"
  12. #include "base/metrics/histogram_functions.h"
  13. #include "ui/compositor/animation_throughput_reporter.h"
  14. #include "ui/compositor/layer.h"
  15. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  16. #include "ui/gfx/geometry/vector2d_f.h"
  17. #include "ui/message_center/message_center.h"
  18. #include "ui/views/animation/animation_builder.h"
  19. #include "ui/views/view.h"
  20. #include "ui/views/widget/widget.h"
  21. namespace {
  22. void ReportAnimationSmoothness(const std::string& animation_histogram_name,
  23. int smoothness) {
  24. // Record animation smoothness if `animation_histogram_name` is given.
  25. if (!animation_histogram_name.empty())
  26. base::UmaHistogramPercentage(animation_histogram_name, smoothness);
  27. }
  28. } // namespace
  29. namespace ash {
  30. namespace message_center_utils {
  31. bool CompareNotifications(message_center::Notification* n1,
  32. message_center::Notification* n2) {
  33. if (n1->pinned() && !n2->pinned())
  34. return true;
  35. if (!n1->pinned() && n2->pinned())
  36. return false;
  37. return message_center::CompareTimestampSerial()(n1, n2);
  38. }
  39. std::vector<message_center::Notification*> GetSortedNotificationsWithOwnView() {
  40. auto visible_notifications =
  41. message_center::MessageCenter::Get()->GetVisibleNotifications();
  42. std::vector<message_center::Notification*> sorted_notifications;
  43. std::copy_if(visible_notifications.begin(), visible_notifications.end(),
  44. std::back_inserter(sorted_notifications),
  45. [](message_center::Notification* notification) {
  46. return !notification->group_child();
  47. });
  48. std::sort(sorted_notifications.begin(), sorted_notifications.end(),
  49. CompareNotifications);
  50. return sorted_notifications;
  51. }
  52. size_t GetNotificationCount() {
  53. size_t count = 0;
  54. for (message_center::Notification* notification :
  55. message_center::MessageCenter::Get()->GetVisibleNotifications()) {
  56. const std::string& notifier = notification->notifier_id().id;
  57. // Don't count these notifications since we have `CameraMicTrayItemView` to
  58. // show indicators on the systray.
  59. if (notifier == kVmCameraMicNotifierId)
  60. continue;
  61. // Don't count group child notifications since they're contained in a single
  62. // parent view.
  63. if (notification->group_child())
  64. continue;
  65. ++count;
  66. }
  67. return count;
  68. }
  69. message_center::NotificationViewController*
  70. GetActiveNotificationViewControllerForDisplay(int64_t display_id) {
  71. RootWindowController* root_window_controller =
  72. Shell::GetRootWindowControllerWithDisplayId(display_id);
  73. if (!root_window_controller || !root_window_controller->GetStatusAreaWidget())
  74. return nullptr;
  75. return root_window_controller->GetStatusAreaWidget()
  76. ->unified_system_tray()
  77. ->GetNotificationGroupingController()
  78. ->GetActiveNotificationViewController();
  79. }
  80. message_center::NotificationViewController*
  81. GetActiveNotificationViewControllerForNotificationView(
  82. views::View* notification_view) {
  83. aura::Window* window = notification_view->GetWidget()->GetNativeWindow();
  84. auto display_id =
  85. display::Screen::GetScreen()->GetDisplayNearestWindow(window).id();
  86. return GetActiveNotificationViewControllerForDisplay(display_id);
  87. }
  88. void InitLayerForAnimations(views::View* view) {
  89. view->SetPaintToLayer();
  90. view->layer()->SetFillsBoundsOpaquely(false);
  91. }
  92. void FadeInView(views::View* view,
  93. int delay_in_ms,
  94. int duration_in_ms,
  95. gfx::Tween::Type tween_type,
  96. const std::string& animation_histogram_name) {
  97. // If we are in testing with animation (non zero duration), we shouldn't have
  98. // delays so that we can properly track when animation is completed in test.
  99. if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
  100. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION) {
  101. delay_in_ms = 0;
  102. }
  103. // The view must have a layer to perform animation.
  104. DCHECK(view->layer());
  105. ui::AnimationThroughputReporter reporter(
  106. view->layer()->GetAnimator(),
  107. metrics_util::ForSmoothness(base::BindRepeating(
  108. &ReportAnimationSmoothness, animation_histogram_name)));
  109. views::AnimationBuilder()
  110. .SetPreemptionStrategy(
  111. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  112. .Once()
  113. .SetDuration(base::TimeDelta())
  114. .SetOpacity(view, 0.0f)
  115. .At(base::Milliseconds(delay_in_ms))
  116. .SetDuration(base::Milliseconds(duration_in_ms))
  117. .SetOpacity(view, 1.0f, tween_type);
  118. }
  119. void FadeOutView(views::View* view,
  120. base::OnceClosure on_animation_ended,
  121. int delay_in_ms,
  122. int duration_in_ms,
  123. gfx::Tween::Type tween_type,
  124. const std::string& animation_histogram_name) {
  125. // If we are in testing with animation (non zero duration), we shouldn't have
  126. // delays so that we can properly track when animation is completed in test.
  127. if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
  128. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION) {
  129. delay_in_ms = 0;
  130. }
  131. std::pair<base::OnceClosure, base::OnceClosure> split =
  132. base::SplitOnceCallback(std::move(on_animation_ended));
  133. // The view must have a layer to perform animation.
  134. DCHECK(view->layer());
  135. ui::AnimationThroughputReporter reporter(
  136. view->layer()->GetAnimator(),
  137. metrics_util::ForSmoothness(base::BindRepeating(
  138. &ReportAnimationSmoothness, animation_histogram_name)));
  139. view->SetVisible(true);
  140. views::AnimationBuilder()
  141. .SetPreemptionStrategy(
  142. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  143. .OnEnded(std::move(split.first))
  144. .OnAborted(std::move(split.second))
  145. .Once()
  146. .At(base::Milliseconds(delay_in_ms))
  147. .SetDuration(base::Milliseconds(duration_in_ms))
  148. .SetVisibility(view, false)
  149. .SetOpacity(view, 0.0f, tween_type);
  150. }
  151. void SlideOutView(views::View* view,
  152. base::OnceClosure on_animation_ended,
  153. base::OnceClosure on_animation_aborted,
  154. int delay_in_ms,
  155. int duration_in_ms,
  156. gfx::Tween::Type tween_type,
  157. const std::string& animation_histogram_name) {
  158. // If we are in testing with animation (non zero duration), we shouldn't have
  159. // delays so that we can properly track when animation is completed in test.
  160. if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
  161. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION) {
  162. delay_in_ms = 0;
  163. }
  164. // The view must have a layer to perform animation.
  165. DCHECK(view->layer());
  166. ui::AnimationThroughputReporter reporter(
  167. view->layer()->GetAnimator(),
  168. metrics_util::ForSmoothness(base::BindRepeating(
  169. &ReportAnimationSmoothness, animation_histogram_name)));
  170. gfx::Transform transform;
  171. transform.Translate(gfx::Vector2dF(view->bounds().width(), 0));
  172. views::AnimationBuilder()
  173. .SetPreemptionStrategy(
  174. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  175. .OnEnded(std::move(on_animation_ended))
  176. .OnAborted(std::move(on_animation_aborted))
  177. .Once()
  178. .At(base::Milliseconds(delay_in_ms))
  179. .SetDuration(base::Milliseconds(duration_in_ms))
  180. .SetTransform(view->layer(), transform);
  181. }
  182. } // namespace message_center_utils
  183. } // namespace ash