tray_item_view.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_TRAY_TRAY_ITEM_VIEW_H_
  5. #define ASH_SYSTEM_TRAY_TRAY_ITEM_VIEW_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "ui/compositor/throughput_tracker.h"
  10. #include "ui/views/animation/animation_delegate_views.h"
  11. #include "ui/views/controls/label.h"
  12. #include "ui/views/view.h"
  13. namespace gfx {
  14. class SlideAnimation;
  15. }
  16. namespace views {
  17. class ImageView;
  18. }
  19. namespace ash {
  20. class Shelf;
  21. // Label view which can be given a different data from the visible label.
  22. // IME icons like "US" (US keyboard) or "あ(Google Japanese Input)" are
  23. // rendered as a label, but reading such text literally will not always be
  24. // understandable.
  25. class IconizedLabel : public views::Label {
  26. public:
  27. void SetCustomAccessibleName(const std::u16string& name) {
  28. custom_accessible_name_ = name;
  29. }
  30. std::u16string GetAccessibleNameString() const {
  31. return custom_accessible_name_;
  32. }
  33. // views::Label:
  34. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  35. private:
  36. std::u16string custom_accessible_name_;
  37. };
  38. // Base-class for items in the tray. It makes sure the widget is updated
  39. // correctly when the visibility/size of the tray item changes. It also adds
  40. // animation when showing/hiding the item in the tray.
  41. class ASH_EXPORT TrayItemView : public views::View,
  42. public views::AnimationDelegateViews {
  43. public:
  44. explicit TrayItemView(Shelf* shelf);
  45. TrayItemView(const TrayItemView&) = delete;
  46. TrayItemView& operator=(const TrayItemView&) = delete;
  47. ~TrayItemView() override;
  48. // Convenience function for creating a child Label or ImageView.
  49. // Only one of the two should be called.
  50. void CreateLabel();
  51. void CreateImageView();
  52. // Methods for destroying a child label or ImageView, which a user of
  53. // `TrayItemView` should do if they know a child view is no longer visible and
  54. // is expected to remain as such for longer than ~0.1 seconds.
  55. void DestroyLabel();
  56. void DestroyImageView();
  57. // Called when locale change is detected (which should not happen after the
  58. // user session starts). It should reload any strings the view is using.
  59. virtual void HandleLocaleChange() = 0;
  60. IconizedLabel* label() const { return label_; }
  61. views::ImageView* image_view() const { return image_view_; }
  62. // views::View.
  63. void SetVisible(bool visible) override;
  64. gfx::Size CalculatePreferredSize() const override;
  65. int GetHeightForWidth(int width) const override;
  66. const char* GetClassName() const override;
  67. void set_use_scale_in_animation(bool use_scale_in_animation) {
  68. use_scale_in_animation_ = use_scale_in_animation;
  69. }
  70. protected:
  71. // Returns whether the shelf is horizontal.
  72. bool IsHorizontalAlignment() const;
  73. private:
  74. // views::View.
  75. void ChildPreferredSizeChanged(View* child) override;
  76. // views::AnimationDelegateViews.
  77. void AnimationProgressed(const gfx::Animation* animation) override;
  78. void AnimationEnded(const gfx::Animation* animation) override;
  79. void AnimationCanceled(const gfx::Animation* animation) override;
  80. // Return true if the animation is in resize animation stage, which
  81. // happens before item animating in and after item animating out.
  82. bool InResizeAnimation(double animation_value) const;
  83. // Converts the overall visibility animation progress to the progress for the
  84. // animation stage that resize the tray container.
  85. double GetResizeProgressFromAnimationProgress(double animation_value) const;
  86. // Converts the overall visibility animation progress to the progress for the
  87. // animation stage that fades and scales the tray item.
  88. double GetItemScaleProgressFromAnimationProgress(
  89. double animation_value) const;
  90. Shelf* const shelf_;
  91. // When showing the item in tray, the animation is executed with 2 stages:
  92. // 1. Resize: The size reserved for tray item view gradually increases.
  93. // 2. Item animation: After size has changed to the target size, the actual
  94. // tray item starts appearing.
  95. // The steps reverse when hiding the item (the item disappears, then width
  96. // change animation).
  97. std::unique_ptr<gfx::SlideAnimation> animation_;
  98. // The target visibility for the item when all the animation is done.
  99. bool target_visible_ = false;
  100. // Use scale in animating in the item to the tray.
  101. bool use_scale_in_animation_ = true;
  102. // Only one of |label_| and |image_view_| should be non-null.
  103. IconizedLabel* label_;
  104. views::ImageView* image_view_;
  105. // Measure animation smoothness metrics for `animation_`.
  106. absl::optional<ui::ThroughputTracker> throughput_tracker_;
  107. };
  108. } // namespace ash
  109. #endif // ASH_SYSTEM_TRAY_TRAY_ITEM_VIEW_H_