layer_animation_stopped_waiter.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) 2021 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. #include "ash/test/layer_animation_stopped_waiter.h"
  5. #include "base/run_loop.h"
  6. #include "ui/compositor/layer.h"
  7. namespace ash {
  8. LayerAnimationStoppedWaiter::LayerAnimationStoppedWaiter() = default;
  9. LayerAnimationStoppedWaiter::~LayerAnimationStoppedWaiter() = default;
  10. void LayerAnimationStoppedWaiter::Wait(ui::Layer* layer) {
  11. if (!layer->GetAnimator()->is_animating())
  12. return;
  13. // Temporarily cache and observe `layer`'s animator.
  14. layer_animator_ = layer->GetAnimator();
  15. layer_animator_observer_.Observe(layer_animator_);
  16. // Loop until the `layer`'s animation is stopped.
  17. wait_loop_ = std::make_unique<base::RunLoop>();
  18. wait_loop_->Run();
  19. // Reset.
  20. layer_animator_ = nullptr;
  21. wait_loop_.reset();
  22. }
  23. void LayerAnimationStoppedWaiter::OnLayerAnimationAborted(
  24. ui::LayerAnimationSequence* sequence) {
  25. if (!layer_animator_->is_animating()) {
  26. layer_animator_observer_.Reset();
  27. wait_loop_->Quit();
  28. }
  29. }
  30. void LayerAnimationStoppedWaiter::OnLayerAnimationEnded(
  31. ui::LayerAnimationSequence* sequence) {
  32. if (!layer_animator_->is_animating()) {
  33. layer_animator_observer_.Reset();
  34. wait_loop_->Quit();
  35. }
  36. }
  37. } // namespace ash