Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 | |
| 5 #ifndef UI_COMPOSITOR_CALLBACK_LAYER_ANIMATION_OBSERVER_H_ | |
| 6 #define UI_COMPOSITOR_CALLBACK_LAYER_ANIMATION_OBSERVER_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "ui/compositor/compositor_export.h" | |
| 10 #include "ui/compositor/layer_animation_observer.h" | |
| 11 | |
| 12 namespace ui { | |
| 13 | |
| 14 class LayerAnimationSequence; | |
| 15 | |
| 16 // A LayerAnimationObserver that invokes a Callback when all observed | |
| 17 // LayerAnimationSequence's have started and finished. | |
| 18 // | |
| 19 // Example usage: | |
| 20 // class Foobar { | |
| 21 // // The Callback that will be invoked when all the animation sequences have | |
| 22 // // started. | |
| 23 // void AnimationStartedCallback( | |
| 24 // const CallbackLayerAnimationObserver& observer) { | |
| 25 // // Do stuff. | |
| 26 // } | |
| 27 // | |
| 28 // // The Callback that will be invoked when all the animation sequences have | |
| 29 // // finished. | |
| 30 // bool AnimationEndedCallback( | |
| 31 // const CallbackLayerAnimationObserver& observer) { | |
| 32 // // Do stuff. | |
| 33 // return true; // Returns true so that |observer| destroys itself. | |
| 34 // } | |
| 35 // | |
| 36 // // Example method that uses the CallbackLayerAnimationObserver. | |
| 37 // void Animate() { | |
| 38 // ui::LayerAnimator* animator_1 = layer_->GetAnimator(); | |
| 39 // ui::LayerAnimator* animator_2 = layer_->GetAnimator(); | |
|
ajuma
2015/10/07 20:14:55
Should these by layer_1 and layer_2, not the same
bruthig
2015/10/07 21:37:29
Yes, good catch!
| |
| 40 // CallbackLayerAnimationObserver* observer = | |
| 41 // new CallbackLayerAnimationObserver( | |
| 42 // base::Bind(&Foobar::AnimationStartedCallback), | |
| 43 // base::Unretained(this)); | |
| 44 // base::Bind(&Foobar::AnimationEndedCallback), | |
| 45 // base::Unretained(this)); | |
| 46 // animator_1->AddObserver(observer); | |
| 47 // animator_2->AddObserver(observer); | |
| 48 // | |
| 49 // // Set up animation sequences on |animator_1| and |animator_2|. | |
| 50 // | |
| 51 // // The callback won't be invoked until SetActive() is called. | |
| 52 // observer->SetActive(); | |
| 53 // } | |
| 54 // } | |
| 55 // | |
| 56 class COMPOSITOR_EXPORT CallbackLayerAnimationObserver | |
| 57 : public ui::LayerAnimationObserver { | |
| 58 public: | |
| 59 // The Callback type that will be invoked when all animation sequences have | |
| 60 // been started. | |
| 61 typedef base::Callback<void(const CallbackLayerAnimationObserver&)> | |
| 62 AnimationStartedCallback; | |
| 63 | |
| 64 // The Callback type that will be invoked when all animation sequences have | |
| 65 // finished. |this| will be destroyed after invoking the Callback if it | |
| 66 // returns true. | |
| 67 typedef base::Callback<bool(const CallbackLayerAnimationObserver&)> | |
| 68 AnimationEndedCallback; | |
| 69 | |
| 70 explicit CallbackLayerAnimationObserver( | |
|
bruthig
2015/10/06 21:30:37
TODO: Remove explicit.
bruthig
2015/10/07 21:37:29
Done.
| |
| 71 AnimationStartedCallback animation_started_callback, | |
| 72 AnimationEndedCallback animation_ended_callback); | |
| 73 ~CallbackLayerAnimationObserver() override; | |
| 74 | |
| 75 bool active() const { return active_; } | |
| 76 | |
| 77 // The callbacks will not be invoked until SetActive() has been called. This | |
| 78 // allows each sequence to be attached before checking if the sequences have | |
| 79 // finished. | |
| 80 void SetActive(); | |
| 81 | |
| 82 int aborted_count() const { return aborted_count_; } | |
| 83 int successful_count() const { return successful_count_; } | |
| 84 | |
| 85 // ui::LayerAnimationObserver: | |
| 86 void OnLayerAnimationStarted(ui::LayerAnimationSequence* sequence) override; | |
|
danakj
2015/10/07 15:11:10
This isn't a member of LayerAnimationObserver?
bruthig
2015/10/07 15:21:23
It's being added in my pending CL here: https://co
| |
| 87 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; | |
| 88 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; | |
| 89 void OnLayerAnimationScheduled(ui::LayerAnimationSequence* sequence) override; | |
| 90 | |
| 91 protected: | |
| 92 // ui::LayerAnimationObserver: | |
| 93 bool RequiresNotificationWhenAnimatorDestroyed() const override; | |
| 94 void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override; | |
| 95 void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override; | |
| 96 | |
| 97 private: | |
| 98 int GetNumSequencesCompleted(); | |
| 99 | |
| 100 // Checks if all attached sequences have been started and invokes | |
| 101 // |animation_started_callback_| if |active_| is true. | |
| 102 void CheckAllSequencesStarted(); | |
| 103 | |
| 104 // Checks if all attached sequences have completed and invokes | |
| 105 // |animation_ended_callback_| if |active_| is true. | |
| 106 void CheckAllSequencesCompleted(); | |
| 107 | |
| 108 // Allows the callbacks to be invoked when true. | |
| 109 bool active_; | |
| 110 | |
| 111 // The total number of animation sequences that have been attached. | |
| 112 int attached_sequence_count_; | |
| 113 | |
| 114 // The total number of animation sequences that have been detached. | |
| 115 int detached_sequence_count_; | |
| 116 | |
| 117 // The number of animation sequences that have been started. | |
| 118 int started_count_; | |
| 119 | |
| 120 // The number of animation sequences that were aborted. | |
| 121 int aborted_count_; | |
| 122 | |
| 123 // The number of animation sequences that completed successfully. | |
| 124 int successful_count_; | |
| 125 | |
| 126 // The callback to invoke once all the animation sequences have been started. | |
| 127 AnimationStartedCallback animation_started_callback_; | |
| 128 | |
| 129 // The callback to invoke once all the animation sequences have finished. | |
| 130 AnimationEndedCallback animation_ended_callback_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(CallbackLayerAnimationObserver); | |
| 133 }; | |
| 134 | |
| 135 } // namespace ui | |
| 136 | |
| 137 #endif // UI_COMPOSITOR_CALLBACK_LAYER_ANIMATION_OBSERVER_H_ | |
| OLD | NEW |