tray_item_view.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "ash/system/tray/tray_item_view.h"
  5. #include "ash/public/cpp/shelf_types.h"
  6. #include "ash/shelf/shelf.h"
  7. #include "ash/system/tray/tray_constants.h"
  8. #include "base/metrics/histogram_functions.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "ui/accessibility/ax_enums.mojom.h"
  11. #include "ui/accessibility/ax_node_data.h"
  12. #include "ui/compositor/compositor.h"
  13. #include "ui/compositor/layer.h"
  14. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  15. #include "ui/compositor/throughput_tracker.h"
  16. #include "ui/gfx/animation/slide_animation.h"
  17. #include "ui/views/controls/image_view.h"
  18. #include "ui/views/controls/label.h"
  19. #include "ui/views/layout/fill_layout.h"
  20. #include "ui/views/widget/widget.h"
  21. namespace ash {
  22. namespace {
  23. // Animating in will start (after resize stage) when animation value is greater
  24. // than this value.
  25. constexpr double kAnimatingInStartValue = 0.5;
  26. // Animating out will end (before resize stage) when animation value is less
  27. // than this value.
  28. constexpr double kAnimatingOutEndValue = 0.5;
  29. constexpr char kShowAnimationSmoothnessHistogramName[] =
  30. "Ash.StatusArea.TrayItemView.Show";
  31. constexpr char kHideAnimationSmoothnessHistogramName[] =
  32. "Ash.StatusArea.TrayItemView.Hide";
  33. void RecordAnimationSmoothness(const std::string& histogram_name,
  34. int smoothness) {
  35. DCHECK(0 <= smoothness && smoothness <= 100);
  36. base::UmaHistogramPercentage(histogram_name, smoothness);
  37. }
  38. void SetupThroughputTrackerForAnimationSmoothness(
  39. views::Widget* widget,
  40. absl::optional<ui::ThroughputTracker>& tracker,
  41. const char* histogram_name) {
  42. // Return if `tracker` is already running; `widget` may not exist in tests.
  43. if (tracker || !widget)
  44. return;
  45. tracker.emplace(widget->GetCompositor()->RequestNewThroughputTracker());
  46. tracker->Start(ash::metrics_util::ForSmoothness(
  47. base::BindRepeating(&RecordAnimationSmoothness, histogram_name)));
  48. }
  49. } // namespace
  50. void IconizedLabel::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  51. if (custom_accessible_name_.empty())
  52. return Label::GetAccessibleNodeData(node_data);
  53. node_data->role = ax::mojom::Role::kStaticText;
  54. node_data->SetName(custom_accessible_name_);
  55. }
  56. TrayItemView::TrayItemView(Shelf* shelf)
  57. : views::AnimationDelegateViews(this),
  58. shelf_(shelf),
  59. label_(NULL),
  60. image_view_(NULL) {
  61. DCHECK(shelf_);
  62. SetPaintToLayer();
  63. layer()->SetFillsBoundsOpaquely(false);
  64. SetLayoutManager(std::make_unique<views::FillLayout>());
  65. }
  66. TrayItemView::~TrayItemView() = default;
  67. void TrayItemView::CreateLabel() {
  68. label_ = new IconizedLabel;
  69. AddChildView(label_);
  70. PreferredSizeChanged();
  71. }
  72. void TrayItemView::CreateImageView() {
  73. image_view_ = new views::ImageView;
  74. AddChildView(image_view_);
  75. PreferredSizeChanged();
  76. }
  77. void TrayItemView::DestroyLabel() {
  78. if (!label_)
  79. return;
  80. RemoveChildViewT(label_);
  81. label_ = nullptr;
  82. }
  83. void TrayItemView::DestroyImageView() {
  84. if (!image_view_)
  85. return;
  86. RemoveChildViewT(image_view_);
  87. image_view_ = nullptr;
  88. }
  89. void TrayItemView::SetVisible(bool set_visible) {
  90. if (!GetWidget() ||
  91. ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
  92. ui::ScopedAnimationDurationScaleMode::ZERO_DURATION) {
  93. views::View::SetVisible(set_visible);
  94. return;
  95. }
  96. // Do not invoke animation when visibility is not changing.
  97. if (set_visible == GetVisible())
  98. return;
  99. target_visible_ = set_visible;
  100. if (!animation_) {
  101. animation_ = std::make_unique<gfx::SlideAnimation>(this);
  102. animation_->SetTweenType(gfx::Tween::LINEAR);
  103. animation_->Reset(GetVisible() ? 1.0 : 0.0);
  104. }
  105. if (target_visible_) {
  106. SetupThroughputTrackerForAnimationSmoothness(
  107. GetWidget(), throughput_tracker_,
  108. kShowAnimationSmoothnessHistogramName);
  109. animation_->SetSlideDuration(base::Milliseconds(400));
  110. animation_->Show();
  111. AnimationProgressed(animation_.get());
  112. views::View::SetVisible(true);
  113. layer()->SetOpacity(0.f);
  114. } else {
  115. SetupThroughputTrackerForAnimationSmoothness(
  116. GetWidget(), throughput_tracker_,
  117. kHideAnimationSmoothnessHistogramName);
  118. animation_->SetSlideDuration(base::Milliseconds(100));
  119. animation_->Hide();
  120. AnimationProgressed(animation_.get());
  121. }
  122. }
  123. bool TrayItemView::IsHorizontalAlignment() const {
  124. return shelf_->IsHorizontalAlignment();
  125. }
  126. gfx::Size TrayItemView::CalculatePreferredSize() const {
  127. DCHECK_EQ(1u, children().size());
  128. gfx::Size size = views::View::CalculatePreferredSize();
  129. if (image_view_) {
  130. size = gfx::Size(kUnifiedTrayIconSize, kUnifiedTrayIconSize);
  131. }
  132. if (!animation_.get() || !animation_->is_animating() ||
  133. !InResizeAnimation(animation_->GetCurrentValue())) {
  134. return size;
  135. }
  136. double progress = gfx::Tween::CalculateValue(
  137. gfx::Tween::FAST_OUT_SLOW_IN,
  138. GetResizeProgressFromAnimationProgress(animation_->GetCurrentValue()));
  139. if (shelf_->IsHorizontalAlignment()) {
  140. size.set_width(std::max(1, static_cast<int>(size.width() * progress)));
  141. } else {
  142. size.set_height(std::max(1, static_cast<int>(size.height() * progress)));
  143. }
  144. return size;
  145. }
  146. int TrayItemView::GetHeightForWidth(int width) const {
  147. return GetPreferredSize().height();
  148. }
  149. const char* TrayItemView::GetClassName() const {
  150. return "TrayItemView";
  151. }
  152. void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
  153. PreferredSizeChanged();
  154. }
  155. void TrayItemView::AnimationProgressed(const gfx::Animation* animation) {
  156. // Should not animate during resize stage.
  157. if (InResizeAnimation(animation->GetCurrentValue())) {
  158. PreferredSizeChanged();
  159. return;
  160. }
  161. double scale_progress =
  162. GetItemScaleProgressFromAnimationProgress(animation->GetCurrentValue());
  163. layer()->SetOpacity(scale_progress);
  164. // Only scale when animating icon in.
  165. if (target_visible_ && use_scale_in_animation_) {
  166. scale_progress = gfx::Tween::CalculateValue(gfx::Tween::LINEAR_OUT_SLOW_IN,
  167. scale_progress);
  168. gfx::Transform transform;
  169. transform.Translate(
  170. gfx::Tween::DoubleValueBetween(scale_progress,
  171. static_cast<double>(width()) / 2, 0.),
  172. gfx::Tween::DoubleValueBetween(scale_progress,
  173. static_cast<double>(height()) / 2, 0.));
  174. transform.Scale(scale_progress, scale_progress);
  175. layer()->SetTransform(transform);
  176. }
  177. // Container size might not fully transition to full size (the resize progress
  178. // value converted from animation progress might not be 1 after resize
  179. // animation). This call makes sure that it is fully resized.
  180. PreferredSizeChanged();
  181. }
  182. void TrayItemView::AnimationEnded(const gfx::Animation* animation) {
  183. if (animation->GetCurrentValue() < 0.1)
  184. views::View::SetVisible(false);
  185. if (throughput_tracker_) {
  186. // Reset `throughput_tracker_` to reset animation metrics recording.
  187. throughput_tracker_->Stop();
  188. throughput_tracker_.reset();
  189. }
  190. }
  191. void TrayItemView::AnimationCanceled(const gfx::Animation* animation) {
  192. AnimationEnded(animation);
  193. }
  194. bool TrayItemView::InResizeAnimation(double animation_value) const {
  195. // Animation should be delayed for the first part of animating in and last
  196. // part of animating out, allowing item resize happen before item animating in
  197. // and after item animating out.
  198. return ((target_visible_ && animation_value <= kAnimatingInStartValue) ||
  199. (!target_visible_ && animation_value <= kAnimatingOutEndValue));
  200. }
  201. double TrayItemView::GetResizeProgressFromAnimationProgress(
  202. double animation_value) const {
  203. DCHECK(InResizeAnimation(animation_value));
  204. // When animating in, convert value from [0,kAnimatingInStartValue] to [0,1].
  205. if (target_visible_)
  206. return animation_value * (1 / kAnimatingInStartValue);
  207. // When animating out, convert value from [kAnimatingOutEndValue,0] to [1,0].
  208. return animation_value * (1 / kAnimatingOutEndValue);
  209. }
  210. double TrayItemView::GetItemScaleProgressFromAnimationProgress(
  211. double animation_value) const {
  212. DCHECK(!InResizeAnimation(animation_value));
  213. // When animating in, convert value from [kAnimatingInStartValue,1] to [0,1].
  214. if (target_visible_) {
  215. return (animation_value - kAnimatingInStartValue) *
  216. (1 / (1 - kAnimatingInStartValue));
  217. }
  218. // When animating out, convert value from [1,kAnimatingOutEndValue] to [1,0].
  219. return (animation_value - kAnimatingOutEndValue) *
  220. (1 / (1 - kAnimatingOutEndValue));
  221. }
  222. } // namespace ash