layer_copy_animator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 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. #ifndef ASH_UTILITY_LAYER_COPY_ANIMATOR_H_
  5. #define ASH_UTILITY_LAYER_COPY_ANIMATOR_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "base/callback.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/scoped_observation.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/aura/window_observer.h"
  13. #include "ui/compositor/layer.h"
  14. #include "ui/compositor/layer_animation_observer.h"
  15. namespace ash {
  16. // A utility class that first creates a copy of the layer tree, then runs
  17. // animation. This is more performant if you want to apply animation to a
  18. // window/layer tree, as it only apply animation to one layer.
  19. class ASH_EXPORT LayerCopyAnimator : public aura::WindowObserver,
  20. public ui::LayerAnimationObserver {
  21. public:
  22. // Returns an associated LayerCopyAnimator, if any.
  23. static LayerCopyAnimator* Get(aura::Window* window);
  24. using AnimationCallback =
  25. base::OnceCallback<void(ui::Layer*, ui::LayerAnimationObserver*)>;
  26. // Creates a LayerCopyAnimator for 'window'. The instance created for this
  27. // window can later be obtained by 'LayerCopyAnimator::Get()'. Creating an
  28. // instance will start copying the layer, but animation won't start until
  29. // 'Start' is called. This will also destroy the current LayerCopyAnimator if
  30. // any.
  31. explicit LayerCopyAnimator(aura::Window* window);
  32. LayerCopyAnimator(const LayerCopyAnimator& animator) = delete;
  33. LayerCopyAnimator& operator=(const LayerCopyAnimator& animator) = delete;
  34. ~LayerCopyAnimator() override;
  35. // Request to start an animation. It'll start animation of the layer's copy is
  36. // is already available, or wait until the copy is made.
  37. void MaybeStartAnimation(ui::LayerAnimationObserver* observer,
  38. AnimationCallback callback);
  39. // Called when a layer is copied. This is public to deal with the shutdown
  40. // scenario. This is virtual for testing purpose.
  41. virtual void OnLayerCopied(std::unique_ptr<ui::Layer> new_layer);
  42. // ui::LayerAnimationObserver:
  43. void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override;
  44. void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override;
  45. void OnLayerAnimationScheduled(ui::LayerAnimationSequence* seq) override {}
  46. // aura::WindowObserver:
  47. void OnWindowBoundsChanged(aura::Window* window,
  48. const gfx::Rect& old_bounds,
  49. const gfx::Rect& new_bounds,
  50. ui::PropertyChangeReason reason) override;
  51. bool animation_requested() const { return animation_requested_; }
  52. ui::Layer* copied_layer_for_test() { return copied_layer_.get(); }
  53. private:
  54. void RunAnimation();
  55. void FinishAndDelete(bool abort);
  56. void EnsureFakeSequence();
  57. void NotifyWithFakeSequence(bool abort);
  58. aura::Window* window_;
  59. ui::LayerAnimationObserver* observer_ = nullptr;
  60. AnimationCallback animation_callback_;
  61. std::unique_ptr<ui::Layer> copied_layer_;
  62. // A dummy sequence to keep AnimationSequence alive during copy.
  63. std::unique_ptr<ui::LayerAnimationSequence> fake_sequence_;
  64. ui::Layer full_layer_{ui::LAYER_SOLID_COLOR};
  65. bool fail_ = false;
  66. bool animation_requested_ = false;
  67. ui::LayerAnimationSequence* last_sequence_ = nullptr;
  68. base::ScopedObservation<aura::Window, aura::WindowObserver> observation_{
  69. this};
  70. base::WeakPtrFactory<LayerCopyAnimator> weak_ptr_factory_{this};
  71. };
  72. } // namespace ash
  73. #endif // ASH_UTILITY_LAYER_COPY_ANIMATOR_H_