Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | 5 #ifndef ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ |
| 6 #define ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | 6 #define ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "ash/ash_export.h" | 11 #include "ash/ash_export.h" |
| 12 #include "ash/common/shelf/wm_shelf_observer.h" | 12 #include "ash/common/shelf/wm_shelf_observer.h" |
| 13 #include "ash/common/wm/background_animator.h" | |
| 14 #include "ash/public/cpp/shelf_types.h" | 13 #include "ash/public/cpp/shelf_types.h" |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "ui/gfx/animation/animation_delegate.h" | |
| 17 #include "ui/gfx/animation/tween.h" | |
|
James Cook
2017/02/10 21:17:34
needed?
bruthig
2017/02/10 22:45:36
Nope, removed.
| |
| 18 | |
| 19 namespace gfx { | |
| 20 class SlideAnimation; | |
| 21 enum class AnimationChangeType; | |
| 22 } // namespace gfx | |
| 17 | 23 |
| 18 namespace ash { | 24 namespace ash { |
| 19 | 25 |
| 20 class ShelfBackgroundAnimatorObserver; | 26 class ShelfBackgroundAnimatorObserver; |
| 21 class ShelfBackgroundAnimatorTestApi; | 27 class ShelfBackgroundAnimatorTestApi; |
| 22 class WmShelf; | 28 class WmShelf; |
| 23 | 29 |
| 24 // Central controller for the Shelf and Dock opacity animations. | 30 // Central controller for the Shelf and Dock opacity animations. |
| 25 // | 31 // |
| 26 // The ShelfBackgroundAnimator is capable of observing a WmShelf instance for | 32 // The ShelfBackgroundAnimator is capable of observing a WmShelf instance for |
| 27 // background type changes or clients can call PaintBackground() directly. | 33 // background type changes or clients can call PaintBackground() directly. |
| 28 // | 34 // |
| 29 // The Shelf uses 2 surfaces for the animations: | 35 // The Shelf uses 2 surfaces for the animations: |
| 30 // | 36 // |
| 31 // Material Design: | 37 // Material Design: |
| 32 // 1. Shelf button backgrounds | 38 // 1. Shelf button backgrounds |
| 33 // 2. Opaque overlay for the SHELF_BACKGROUND_OVERLAP and | 39 // 2. Overlay for the SHELF_BACKGROUND_OVERLAP and SHELF_BACKGROUND_MAXIMIZED |
| 34 // SHELF_BACKGROUND_MAXIMIZED states. | 40 // states. |
| 35 class ASH_EXPORT ShelfBackgroundAnimator : public WmShelfObserver, | 41 class ASH_EXPORT ShelfBackgroundAnimator : public WmShelfObserver, |
| 36 public BackgroundAnimatorDelegate { | 42 public gfx::AnimationDelegate { |
| 37 public: | 43 public: |
| 38 // Initializes this with the given |background_type|. This will observe the | 44 // Initializes this with the given |background_type|. This will observe the |
| 39 // |wm_shelf| for background type changes if |wm_shelf| is not null. | 45 // |wm_shelf| for background type changes if |wm_shelf| is not null. |
| 40 ShelfBackgroundAnimator(ShelfBackgroundType background_type, | 46 ShelfBackgroundAnimator(ShelfBackgroundType background_type, |
| 41 WmShelf* wm_shelf); | 47 WmShelf* wm_shelf); |
| 42 ~ShelfBackgroundAnimator() override; | 48 ~ShelfBackgroundAnimator() override; |
| 43 | 49 |
| 44 ShelfBackgroundType target_background_type() const { | 50 ShelfBackgroundType target_background_type() const { |
| 45 return target_background_type_; | 51 return target_background_type_; |
| 46 } | 52 } |
| 47 | 53 |
| 48 // Initializes |observer| with current values using the Initialize() function. | 54 // Initializes |observer| with current values using the Initialize() function. |
| 49 void AddObserver(ShelfBackgroundAnimatorObserver* observer); | 55 void AddObserver(ShelfBackgroundAnimatorObserver* observer); |
| 50 void RemoveObserver(ShelfBackgroundAnimatorObserver* observer); | 56 void RemoveObserver(ShelfBackgroundAnimatorObserver* observer); |
| 51 | 57 |
| 52 // Updates |observer| with current values. | 58 // Updates |observer| with current values. |
| 53 void Initialize(ShelfBackgroundAnimatorObserver* observer) const; | 59 void NotifyObserver(ShelfBackgroundAnimatorObserver* observer); |
| 54 | 60 |
| 55 // Conditionally animates the background to the specified |background_type| | 61 // Conditionally animates the background to the specified |background_type| |
| 56 // and notifies observers of the new background parameters (e.g. alpha). | 62 // and notifies observers of the new background parameters (e.g. alpha). |
| 57 // If |change_type| is BACKGROUND_CHANGE_IMMEDIATE then the | 63 // If |change_type| is BACKGROUND_CHANGE_IMMEDIATE then the |
| 58 // observers will only receive one notification with the final background | 64 // observers will only receive one notification with the final background |
| 59 // state, otherwise the observers will be notified multiple times in order to | 65 // state, otherwise the observers will be notified multiple times in order to |
| 60 // animate the changes to the backgrounds. | 66 // animate the changes to the backgrounds. |
| 61 // | 67 // |
| 62 // NOTE: If a second request to paint the same |background_type| using the | 68 // NOTE: If a second request to paint the same |background_type| using the |
| 63 // BACKGROUND_CHANGE_ANIMATE change type is received it will be ignored and | 69 // BACKGROUND_CHANGE_ANIMATE change type is received it will be ignored and |
| 64 // observers will NOT be notified. | 70 // observers will NOT be notified. |
| 65 void PaintBackground(ShelfBackgroundType background_type, | 71 void PaintBackground(ShelfBackgroundType background_type, |
| 66 BackgroundAnimatorChangeType change_type); | 72 gfx::AnimationChangeType change_type); |
| 73 | |
| 74 // gfx::AnimationDelegate: | |
| 75 void AnimationProgressed(const gfx::Animation* animation) override; | |
| 76 void AnimationEnded(const gfx::Animation* animation) override; | |
| 77 void AnimationCanceled(const gfx::Animation* animation) override; | |
| 67 | 78 |
| 68 protected: | 79 protected: |
| 69 // WmShelfObserver: | 80 // WmShelfObserver: |
| 70 void OnBackgroundTypeChanged( | 81 void OnBackgroundTypeChanged(ShelfBackgroundType background_type, |
| 71 ShelfBackgroundType background_type, | 82 gfx::AnimationChangeType change_type) override; |
| 72 BackgroundAnimatorChangeType change_type) override; | |
| 73 | |
| 74 // BackgroundAnimatorDelegate: | |
| 75 void UpdateBackground(BackgroundAnimator* animator, int alpha) override; | |
| 76 void BackgroundAnimationEnded(BackgroundAnimator* animator) override; | |
| 77 | 83 |
| 78 private: | 84 private: |
| 79 friend class ShelfBackgroundAnimatorTestApi; | 85 friend class ShelfBackgroundAnimatorTestApi; |
| 80 | 86 |
| 87 // Track the values related to a single animation (e.g. Shelf background, | |
| 88 // shelf item background) | |
| 89 class AnimationValues { | |
|
James Cook
2017/02/10 21:17:34
nit: I would provide empty constructor and destruc
bruthig
2017/02/10 22:45:36
Done.
| |
| 90 public: | |
| 91 int current_alpha() const { return current_alpha_; } | |
| 92 | |
| 93 // Updates the current values based on |t| value between 0 and 1. | |
| 94 void UpdateCurrentValues(double t); | |
| 95 | |
| 96 // Set the target values and assign the current values to the initial | |
| 97 // values. | |
| 98 void SetTargetValues(int target_alpha); | |
| 99 | |
| 100 // Returns true if the initial values of |this| equal the target values of | |
| 101 // |other|. | |
| 102 bool InitialValuesEqualTargetValuesOf(const AnimationValues& other) const; | |
| 103 | |
| 104 private: | |
| 105 int initial_alpha_ = 0; | |
| 106 int current_alpha_ = 0; | |
| 107 int target_alpha_ = 0; | |
| 108 }; | |
| 109 | |
| 110 // The maximum alpha value that can be used. | |
| 111 static const int kMaxAlpha = 255; | |
| 112 | |
| 81 // Helper function used by PaintBackground() to animate the background. | 113 // Helper function used by PaintBackground() to animate the background. |
| 82 void AnimateBackground(ShelfBackgroundType background_type, | 114 void AnimateBackground(ShelfBackgroundType background_type, |
| 83 BackgroundAnimatorChangeType change_type); | 115 gfx::AnimationChangeType change_type); |
| 84 | 116 |
| 85 // Creates new BackgroundAnimators configured with the correct durations and | 117 // Returns true if the current |animator_| should be re-used to animate to the |
| 86 // initial/target alpha values. If any BackgroundAnimators are currently | 118 // given |background_type|. This allows for pre-empted animations to take the |
| 87 // animating they will be stopped. | 119 // same amount of time to reverse to the |previous_background_type_|. |
| 88 void CreateAnimators(ShelfBackgroundType background_type, | 120 bool CanReuseAnimator(ShelfBackgroundType background_type) const; |
| 89 BackgroundAnimatorChangeType change_type); | |
| 90 | 121 |
| 91 // Stops all existing animators owned by this. | 122 // Creates a new |animator_| configured with the correct duration. If the |
| 92 void StopAnimators(); | 123 // |animator_| is currently animating it will be stopped. |
| 124 void CreateAnimator(ShelfBackgroundType background_type); | |
| 93 | 125 |
| 94 // Called when an alpha value changes and observers need to be notified. | 126 // Stops the animator owned by this. |
| 95 void OnAlphaChanged(BackgroundAnimator* animator, int alpha); | 127 void StopAnimator(); |
| 128 | |
| 129 // Updates the |shelf_background_values_| and |shelf_item_background_values_|. | |
| 130 void SetTargetValues(ShelfBackgroundType background_type); | |
| 131 | |
| 132 // Sets the target values for |shelf_background_values| and | |
| 133 // |item_background_values| according to |background_type|. | |
| 134 void GetTargetValues(ShelfBackgroundType background_type, | |
| 135 AnimationValues* shelf_background_values, | |
| 136 AnimationValues* item_background_values) const; | |
| 137 | |
| 138 // Updates the animation values corresponding to the |t| value between 0 and | |
| 139 // 1. | |
| 140 void SetAnimationValues(double t); | |
| 141 | |
| 142 // Called when observers need to be notified. | |
| 143 void NotifyObservers(); | |
| 144 | |
| 145 // The shelf to observe for changes to the shelf background type, can be null. | |
| 146 WmShelf* wm_shelf_; | |
| 96 | 147 |
| 97 // The background type that this is animating towards or has reached. | 148 // The background type that this is animating towards or has reached. |
| 98 ShelfBackgroundType target_background_type_ = SHELF_BACKGROUND_DEFAULT; | 149 ShelfBackgroundType target_background_type_ = SHELF_BACKGROUND_DEFAULT; |
| 99 | 150 |
| 100 // The last background type this is animating away from. | 151 // The last background type this is animating away from. |
| 101 ShelfBackgroundType previous_background_type_ = SHELF_BACKGROUND_MAXIMIZED; | 152 ShelfBackgroundType previous_background_type_ = SHELF_BACKGROUND_MAXIMIZED; |
| 102 | 153 |
| 103 // Animates the solid color background of the Shelf. | 154 // Drives the animtion. |
| 104 // TODO(bruthig): Replace all BackgroundAnimators with a single | 155 std::unique_ptr<gfx::SlideAnimation> animator_; |
| 105 // gfx::SlideAnimation. | |
| 106 std::unique_ptr<BackgroundAnimator> opaque_background_animator_; | |
| 107 | 156 |
| 108 // Animates the backgrounds of Shelf child Views. | 157 // Tracks the shelf background animation values. |
| 109 std::unique_ptr<BackgroundAnimator> item_background_animator_; | 158 AnimationValues shelf_background_values_; |
| 159 | |
| 160 // Tracks the item background animation values. | |
| 161 AnimationValues item_background_values_; | |
| 110 | 162 |
| 111 base::ObserverList<ShelfBackgroundAnimatorObserver> observers_; | 163 base::ObserverList<ShelfBackgroundAnimatorObserver> observers_; |
| 112 | 164 |
| 113 // True if the existing BackgroundAnimators can be re-used to animate to the | |
| 114 // |previous_background_type_|. This allows for pre-empted animations to take | |
| 115 // the same amount of time to reverse to the |previous_background_type_|. | |
| 116 bool can_reuse_animators_ = false; | |
| 117 | |
| 118 // Tracks how many animators completed successfully since the last | |
| 119 // CreateAnimators() call. Used to properly set |can_reuse_animators_|. | |
| 120 int successful_animator_count_ = 0; | |
| 121 | |
| 122 // The shelf to observe for changes to the shelf background type, can be null. | |
| 123 WmShelf* wm_shelf_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimator); | 165 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimator); |
| 126 }; | 166 }; |
| 127 | 167 |
| 128 } // namespace ash | 168 } // namespace ash |
| 129 | 169 |
| 130 #endif // ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | 170 #endif // ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ |
| OLD | NEW |