screen_rotation_animation.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "ash/rotator/screen_rotation_animation.h"
  5. #include <memory>
  6. #include "base/time/time.h"
  7. #include "ui/compositor/layer.h"
  8. #include "ui/compositor/layer_animation_delegate.h"
  9. #include "ui/gfx/animation/tween.h"
  10. #include "ui/gfx/geometry/point.h"
  11. #include "ui/gfx/geometry/transform.h"
  12. #include "ui/gfx/interpolated_transform.h"
  13. namespace ash {
  14. ScreenRotationAnimation::ScreenRotationAnimation(ui::Layer* layer,
  15. int start_degrees,
  16. int end_degrees,
  17. float initial_opacity,
  18. float target_opacity,
  19. gfx::Point pivot,
  20. base::TimeDelta duration,
  21. gfx::Tween::Type tween_type)
  22. : ui::LayerAnimationElement(
  23. LayerAnimationElement::TRANSFORM | LayerAnimationElement::OPACITY,
  24. duration),
  25. tween_type_(tween_type),
  26. initial_opacity_(initial_opacity),
  27. target_opacity_(target_opacity) {
  28. std::unique_ptr<ui::InterpolatedTransform> rotation =
  29. std::make_unique<ui::InterpolatedTransformAboutPivot>(
  30. pivot, std::make_unique<ui::InterpolatedRotation>(start_degrees,
  31. end_degrees));
  32. // Use the target transform/bounds in case the layer is already animating.
  33. gfx::Transform current_transform = layer->GetTargetTransform();
  34. interpolated_transform_ =
  35. std::make_unique<ui::InterpolatedConstantTransform>(current_transform);
  36. interpolated_transform_->SetChild(std::move(rotation));
  37. }
  38. ScreenRotationAnimation::~ScreenRotationAnimation() = default;
  39. void ScreenRotationAnimation::OnStart(ui::LayerAnimationDelegate* delegate) {}
  40. bool ScreenRotationAnimation::OnProgress(double current,
  41. ui::LayerAnimationDelegate* delegate) {
  42. const double tweened = gfx::Tween::CalculateValue(tween_type_, current);
  43. delegate->SetTransformFromAnimation(
  44. interpolated_transform_->Interpolate(tweened),
  45. ui::PropertyChangeReason::FROM_ANIMATION);
  46. delegate->SetOpacityFromAnimation(
  47. gfx::Tween::FloatValueBetween(tweened, initial_opacity_, target_opacity_),
  48. ui::PropertyChangeReason::FROM_ANIMATION);
  49. return true;
  50. }
  51. void ScreenRotationAnimation::OnGetTarget(TargetValue* target) const {
  52. target->transform = interpolated_transform_->Interpolate(1.0);
  53. }
  54. void ScreenRotationAnimation::OnAbort(ui::LayerAnimationDelegate* delegate) {
  55. // ui::Layer's d'tor passes its ui::LayerAnimator a null delegate before
  56. // deleting it. This is then passed here: http://crbug.com/661313
  57. if (!delegate)
  58. return;
  59. TargetValue target_value;
  60. OnGetTarget(&target_value);
  61. delegate->SetTransformFromAnimation(target_value.transform,
  62. ui::PropertyChangeReason::FROM_ANIMATION);
  63. }
  64. } // namespace ash