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_change_type.h" | |
| 17 #include "ui/gfx/animation/animation_delegate.h" | |
| 18 #include "ui/gfx/animation/tween.h" | |
| 19 | |
| 20 namespace gfx { | |
| 21 class SlideAnimation; | |
| 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; | |
| 67 | 77 |
| 68 protected: | 78 protected: |
| 69 // WmShelfObserver: | 79 // WmShelfObserver: |
| 70 void OnBackgroundTypeChanged( | 80 void OnBackgroundTypeChanged(ShelfBackgroundType background_type, |
| 71 ShelfBackgroundType background_type, | 81 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 | 82 |
| 78 private: | 83 private: |
| 79 friend class ShelfBackgroundAnimatorTestApi; | 84 friend class ShelfBackgroundAnimatorTestApi; |
| 80 | 85 |
| 86 // Track the values related to a single animation (e.g. Shelf background, | |
| 87 // shelf item background) | |
| 88 struct AnimationValues { | |
|
James Cook
2017/02/08 00:31:27
nit: if all the members are private, why not class
bruthig
2017/02/10 18:49:10
Done.
| |
| 89 int current_alpha() const { return current_alpha_; } | |
| 90 | |
| 91 // Updates the current values based on |t| value between 0 and 1. | |
| 92 void UpdateCurrentValues(double t); | |
| 93 | |
| 94 // Set the target values and assign the current values to the initial | |
| 95 // values. | |
| 96 void SetTargetValues(int target_alpha); | |
| 97 | |
| 98 private: | |
| 99 int initial_alpha_ = 0; | |
| 100 int current_alpha_ = 0; | |
| 101 int target_alpha_ = 0; | |
| 102 }; | |
| 103 | |
| 104 // The maximum alpha value that can be used. | |
| 105 static const int kMaxAlpha; | |
| 106 | |
| 81 // Helper function used by PaintBackground() to animate the background. | 107 // Helper function used by PaintBackground() to animate the background. |
| 82 void AnimateBackground(ShelfBackgroundType background_type, | 108 void AnimateBackground(ShelfBackgroundType background_type, |
| 83 BackgroundAnimatorChangeType change_type); | 109 gfx::AnimationChangeType change_type); |
| 84 | 110 |
| 85 // Creates new BackgroundAnimators configured with the correct durations and | 111 // Creates a new |animator_| configured with the correct duration and updates |
| 86 // initial/target alpha values. If any BackgroundAnimators are currently | 112 // the |shelf_background_values_| and |shelf_item_background_values_|. If the |
| 87 // animating they will be stopped. | 113 // |animator_| is currently animating it will be stopped. |
| 88 void CreateAnimators(ShelfBackgroundType background_type, | 114 void CreateAnimator(ShelfBackgroundType background_type); |
| 89 BackgroundAnimatorChangeType change_type); | |
| 90 | 115 |
| 91 // Stops all existing animators owned by this. | 116 // Stops the animator owned by this. |
| 92 void StopAnimators(); | 117 void StopAnimator(); |
| 93 | 118 |
| 94 // Called when an alpha value changes and observers need to be notified. | 119 // Called when observers need to be notified. |
| 95 void OnAlphaChanged(BackgroundAnimator* animator, int alpha); | 120 void NotifyObservers(); |
| 121 | |
| 122 // The shelf to observe for changes to the shelf background type, can be null. | |
| 123 WmShelf* wm_shelf_; | |
| 96 | 124 |
| 97 // The background type that this is animating towards or has reached. | 125 // The background type that this is animating towards or has reached. |
| 98 ShelfBackgroundType target_background_type_ = SHELF_BACKGROUND_DEFAULT; | 126 ShelfBackgroundType target_background_type_ = SHELF_BACKGROUND_DEFAULT; |
| 99 | 127 |
| 100 // The last background type this is animating away from. | 128 // The last background type this is animating away from. |
| 101 ShelfBackgroundType previous_background_type_ = SHELF_BACKGROUND_MAXIMIZED; | 129 ShelfBackgroundType previous_background_type_ = SHELF_BACKGROUND_MAXIMIZED; |
| 102 | 130 |
| 103 // Animates the solid color background of the Shelf. | 131 // Drives the animtion. |
| 104 // TODO(bruthig): Replace all BackgroundAnimators with a single | 132 std::unique_ptr<gfx::SlideAnimation> animator_; |
| 105 // gfx::SlideAnimation. | |
| 106 std::unique_ptr<BackgroundAnimator> opaque_background_animator_; | |
| 107 | 133 |
| 108 // Animates the backgrounds of Shelf child Views. | 134 // Tracks the shelf background animation values. |
| 109 std::unique_ptr<BackgroundAnimator> item_background_animator_; | 135 AnimationValues shelf_background_values_; |
| 136 | |
| 137 // Tracks the item background animation values. | |
| 138 AnimationValues item_background_values_; | |
| 110 | 139 |
| 111 base::ObserverList<ShelfBackgroundAnimatorObserver> observers_; | 140 base::ObserverList<ShelfBackgroundAnimatorObserver> observers_; |
| 112 | 141 |
| 113 // True if the existing BackgroundAnimators can be re-used to animate to the | 142 // True if the existing |animator_| can be re-used to animate to the |
| 114 // |previous_background_type_|. This allows for pre-empted animations to take | 143 // |previous_background_type_|. This allows for pre-empted animations to take |
| 115 // the same amount of time to reverse to the |previous_background_type_|. | 144 // the same amount of time to reverse to the |previous_background_type_|. |
| 116 bool can_reuse_animators_ = false; | 145 bool can_reuse_animator_ = 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 | 146 |
| 125 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimator); | 147 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimator); |
| 126 }; | 148 }; |
| 127 | 149 |
| 128 } // namespace ash | 150 } // namespace ash |
| 129 | 151 |
| 130 #endif // ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | 152 #endif // ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ |
| OLD | NEW |