shelf_background_animator.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2016 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_SHELF_SHELF_BACKGROUND_ANIMATOR_H_
  5. #define ASH_SHELF_SHELF_BACKGROUND_ANIMATOR_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/shelf_types.h"
  10. #include "ash/public/cpp/wallpaper/wallpaper_controller_observer.h"
  11. #include "ash/shelf/shelf_observer.h"
  12. #include "base/observer_list.h"
  13. #include "third_party/skia/include/core/SkColor.h"
  14. #include "ui/gfx/animation/animation_delegate.h"
  15. namespace gfx {
  16. class SlideAnimation;
  17. } // namespace gfx
  18. namespace ash {
  19. enum class AnimationChangeType;
  20. class Shelf;
  21. class ShelfBackgroundAnimatorObserver;
  22. class ShelfBackgroundAnimatorTestApi;
  23. class WallpaperControllerImpl;
  24. // Central controller for the Shelf and Dock opacity animations.
  25. //
  26. // The ShelfBackgroundAnimator is capable of observing a Shelf instance for
  27. // background type changes or clients can call PaintBackground() directly.
  28. //
  29. // The Shelf uses 2 surfaces for the animations:
  30. //
  31. // Material Design:
  32. // 1. Shelf button backgrounds
  33. // 2. Overlay for the ShelfBackgroundType::kMaximized state.
  34. class ASH_EXPORT ShelfBackgroundAnimator : public ShelfObserver,
  35. public gfx::AnimationDelegate,
  36. public WallpaperControllerObserver {
  37. public:
  38. // The maximum alpha value that can be used.
  39. static const int kMaxAlpha = SK_AlphaOPAQUE;
  40. ShelfBackgroundAnimator(Shelf* shelf,
  41. WallpaperControllerImpl* wallpaper_controller);
  42. ShelfBackgroundAnimator(const ShelfBackgroundAnimator&) = delete;
  43. ShelfBackgroundAnimator& operator=(const ShelfBackgroundAnimator&) = delete;
  44. ~ShelfBackgroundAnimator() override;
  45. // Initializes this with the given |background_type|. This will observe the
  46. // |shelf| for background type changes and the |wallpaper_controller| for
  47. // wallpaper changes if not null.
  48. void Init(ShelfBackgroundType background_type);
  49. ShelfBackgroundType target_background_type() const {
  50. return target_background_type_;
  51. }
  52. // Initializes |observer| with current values using the Initialize() function.
  53. void AddObserver(ShelfBackgroundAnimatorObserver* observer);
  54. void RemoveObserver(ShelfBackgroundAnimatorObserver* observer);
  55. // Updates |observer| with current values.
  56. void NotifyObserver(ShelfBackgroundAnimatorObserver* observer);
  57. // Conditionally animates the background to the specified |background_type|
  58. // and notifies observers of the new background parameters (e.g. color).
  59. // If |change_type| is BACKGROUND_CHANGE_IMMEDIATE then the
  60. // observers will only receive one notification with the final background
  61. // state, otherwise the observers will be notified multiple times in order to
  62. // animate the changes to the backgrounds.
  63. //
  64. // NOTE: If a second request to paint the same |background_type| using the
  65. // BACKGROUND_CHANGE_ANIMATE change type is received it will be ignored and
  66. // observers will NOT be notified.
  67. void PaintBackground(ShelfBackgroundType background_type,
  68. AnimationChangeType change_type);
  69. // gfx::AnimationDelegate:
  70. void AnimationProgressed(const gfx::Animation* animation) override;
  71. void AnimationEnded(const gfx::Animation* animation) override;
  72. // Gets the color corresponding with |background_type|.
  73. SkColor GetBackgroundColor(ShelfBackgroundType background_type) const;
  74. // Drives the current animation to the end.
  75. void CompleteAnimationForTesting();
  76. protected:
  77. // ShelfObserver:
  78. void OnBackgroundTypeChanged(ShelfBackgroundType background_type,
  79. AnimationChangeType change_type) override;
  80. // WallpaperControllerObserver:
  81. void OnWallpaperColorsChanged() override;
  82. private:
  83. friend class ShelfBackgroundAnimatorTestApi;
  84. // Track the values related to a single animation (e.g. Shelf background,
  85. // shelf item background)
  86. class AnimationValues {
  87. public:
  88. AnimationValues();
  89. AnimationValues(const AnimationValues&) = delete;
  90. AnimationValues& operator=(const AnimationValues&) = delete;
  91. ~AnimationValues();
  92. SkColor current_color() const { return current_color_; }
  93. SkColor target_color() const { return target_color_; }
  94. // Updates the |current_color_| based on |t| value between 0 and 1.
  95. void UpdateCurrentValues(double t);
  96. // Set the target color and assign the current color to the initial color.
  97. void SetTargetValues(SkColor target_color);
  98. // Returns true if the initial values of |this| equal the target values of
  99. // |other|.
  100. bool InitialValuesEqualTargetValuesOf(const AnimationValues& other) const;
  101. private:
  102. SkColor initial_color_ = SK_ColorTRANSPARENT;
  103. SkColor current_color_ = SK_ColorTRANSPARENT;
  104. SkColor target_color_ = SK_ColorTRANSPARENT;
  105. };
  106. // Helper function used by PaintBackground() to animate the background.
  107. void AnimateBackground(ShelfBackgroundType background_type,
  108. AnimationChangeType change_type);
  109. // Returns true if the current |animator_| should be re-used to animate to the
  110. // given |background_type|. This allows for pre-empted animations to take the
  111. // same amount of time to reverse to the |previous_background_type_|.
  112. bool CanReuseAnimator(ShelfBackgroundType background_type) const;
  113. // Creates a new |animator_| configured with the correct duration. If the
  114. // |animator_| is currently animating it will be stopped.
  115. void CreateAnimator(ShelfBackgroundType background_type);
  116. // Stops the animator owned by this.
  117. void StopAnimator();
  118. // Updates the |shelf_background_values_| and |shelf_item_background_values_|.
  119. void SetTargetValues(ShelfBackgroundType background_type);
  120. // Sets the target values for |shelf_background_values| and
  121. // |item_background_values| according to |background_type|.
  122. void GetTargetValues(ShelfBackgroundType background_type,
  123. AnimationValues* shelf_background_values) const;
  124. // Updates the animation values corresponding to the |t| value between 0 and
  125. // 1.
  126. void SetAnimationValues(double t);
  127. // Called when observers need to be notified.
  128. void NotifyObservers();
  129. // The shelf to observe for changes to the shelf background type, can be null.
  130. Shelf* shelf_;
  131. // The wallpaper controller to observe for changes and to extract colors from.
  132. WallpaperControllerImpl* wallpaper_controller_;
  133. // The background type that this is animating towards or has reached.
  134. ShelfBackgroundType target_background_type_ = ShelfBackgroundType::kDefaultBg;
  135. // The last background type this is animating away from.
  136. ShelfBackgroundType previous_background_type_ =
  137. ShelfBackgroundType::kMaximized;
  138. // Drives the animation.
  139. std::unique_ptr<gfx::SlideAnimation> animator_;
  140. // Tracks the shelf background animation values.
  141. AnimationValues shelf_background_values_;
  142. // Tracks the item background animation values.
  143. AnimationValues item_background_values_;
  144. base::ObserverList<ShelfBackgroundAnimatorObserver>::Unchecked observers_;
  145. };
  146. } // namespace ash
  147. #endif // ASH_SHELF_SHELF_BACKGROUND_ANIMATOR_H_