window_rotation.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2012 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/window_rotation.h"
  5. #include <memory>
  6. #include "base/time/time.h"
  7. #include "ui/compositor/layer.h"
  8. #include "ui/gfx/geometry/rect.h"
  9. #include "ui/gfx/geometry/transform.h"
  10. #include "ui/gfx/interpolated_transform.h"
  11. namespace ash {
  12. namespace {
  13. const int k90DegreeTransitionDurationMs = 350;
  14. const int k180DegreeTransitionDurationMs = 550;
  15. const int k360DegreeTransitionDurationMs = 750;
  16. base::TimeDelta GetTransitionDuration(int degrees) {
  17. if (degrees == 360)
  18. return base::Milliseconds(k360DegreeTransitionDurationMs);
  19. if (degrees == 180)
  20. return base::Milliseconds(k180DegreeTransitionDurationMs);
  21. if (degrees == 0)
  22. return base::Milliseconds(0);
  23. return base::Milliseconds(k90DegreeTransitionDurationMs);
  24. }
  25. } // namespace
  26. WindowRotation::WindowRotation(int degrees, ui::Layer* layer)
  27. : ui::LayerAnimationElement(LayerAnimationElement::TRANSFORM,
  28. GetTransitionDuration(degrees)),
  29. degrees_(degrees) {
  30. InitTransform(layer);
  31. }
  32. WindowRotation::~WindowRotation() = default;
  33. void WindowRotation::InitTransform(ui::Layer* layer) {
  34. // No rotation required, use the identity transform.
  35. if (degrees_ == 0) {
  36. interpolated_transform_ =
  37. std::make_unique<ui::InterpolatedConstantTransform>(gfx::Transform());
  38. return;
  39. }
  40. // Use the target transform/bounds in case the layer is already animating.
  41. const gfx::Transform& current_transform = layer->GetTargetTransform();
  42. const gfx::Rect& bounds = layer->GetTargetBounds();
  43. gfx::Point old_pivot;
  44. gfx::Point new_pivot;
  45. int width = bounds.width();
  46. int height = bounds.height();
  47. switch (degrees_) {
  48. case 90:
  49. new_origin_ = new_pivot = gfx::Point(width, 0);
  50. break;
  51. case -90:
  52. new_origin_ = new_pivot = gfx::Point(0, height);
  53. break;
  54. case 180:
  55. case 360:
  56. new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
  57. new_origin_.SetPoint(width, height);
  58. break;
  59. }
  60. // Convert points to world space.
  61. current_transform.TransformPoint(&old_pivot);
  62. current_transform.TransformPoint(&new_pivot);
  63. current_transform.TransformPoint(&new_origin_);
  64. std::unique_ptr<ui::InterpolatedTransform> rotation =
  65. std::make_unique<ui::InterpolatedTransformAboutPivot>(
  66. old_pivot, std::make_unique<ui::InterpolatedRotation>(0, degrees_));
  67. std::unique_ptr<ui::InterpolatedTransform> translation =
  68. std::make_unique<ui::InterpolatedTranslation>(
  69. gfx::PointF(), gfx::PointF(new_pivot.x() - old_pivot.x(),
  70. new_pivot.y() - old_pivot.y()));
  71. float scale_factor = 0.9f;
  72. std::unique_ptr<ui::InterpolatedTransform> scale_down =
  73. std::make_unique<ui::InterpolatedScale>(1.0f, scale_factor, 0.0f, 0.5f);
  74. std::unique_ptr<ui::InterpolatedTransform> scale_up =
  75. std::make_unique<ui::InterpolatedScale>(1.0f, 1.0f / scale_factor, 0.5f,
  76. 1.0f);
  77. interpolated_transform_ =
  78. std::make_unique<ui::InterpolatedConstantTransform>(current_transform);
  79. scale_up->SetChild(std::move(scale_down));
  80. translation->SetChild(std::move(scale_up));
  81. rotation->SetChild(std::move(translation));
  82. interpolated_transform_->SetChild(std::move(rotation));
  83. }
  84. void WindowRotation::OnStart(ui::LayerAnimationDelegate* delegate) {}
  85. bool WindowRotation::OnProgress(double t,
  86. ui::LayerAnimationDelegate* delegate) {
  87. delegate->SetTransformFromAnimation(interpolated_transform_->Interpolate(t),
  88. ui::PropertyChangeReason::FROM_ANIMATION);
  89. return true;
  90. }
  91. void WindowRotation::OnGetTarget(TargetValue* target) const {
  92. target->transform = interpolated_transform_->Interpolate(1.0);
  93. }
  94. void WindowRotation::OnAbort(ui::LayerAnimationDelegate* delegate) {}
  95. } // namespace ash