| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/touch/animation/screen_rotation_setter.h" | 5 #include "chrome/browser/ui/touch/animation/screen_rotation_animator.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/ui/touch/animation/screen_rotation.h" | 8 #include "chrome/browser/ui/touch/animation/screen_rotation.h" |
| 9 #include "ui/gfx/compositor/layer.h" | 9 #include "ui/gfx/compositor/layer.h" |
| 10 #include "ui/gfx/compositor/layer_animator.h" |
| 10 #include "ui/gfx/interpolated_transform.h" | 11 #include "ui/gfx/interpolated_transform.h" |
| 11 #include "views/layer_property_setter.h" | |
| 12 #include "views/view.h" | 12 #include "views/view.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 static int SymmetricRound(float x) { | 16 static int SymmetricRound(float x) { |
| 17 return static_cast<int>( | 17 return static_cast<int>( |
| 18 x > 0 | 18 x > 0 |
| 19 ? std::floor(x + 0.5f) | 19 ? std::floor(x + 0.5f) |
| 20 : std::ceil(x - 0.5f)); | 20 : std::ceil(x - 0.5f)); |
| 21 } | 21 } |
| 22 | 22 |
| 23 // A screen rotation setter is a LayerPropertySetter that initiates screen | 23 // A screen rotation animator is a LayerAnimator that initiates screen |
| 24 // rotations in response to calls to |SetTransform|. Calls to |SetBounds| are | 24 // rotations in response to calls to |SetTransform|. Calls to |SetBounds| are |
| 25 // applied to the layer immediately. | 25 // applied to the layer immediately. |
| 26 class ScreenRotationSetter : public views::LayerPropertySetter, | 26 class ScreenRotationAnimator : public ui::LayerAnimator, |
| 27 public ScreenRotationListener { | 27 public ScreenRotationListener { |
| 28 public: | 28 public: |
| 29 explicit ScreenRotationSetter(views::View* view); | 29 explicit ScreenRotationAnimator(views::View* view); |
| 30 virtual ~ScreenRotationAnimator(); |
| 30 | 31 |
| 31 // implementation of LayerPropertySetter | 32 private: |
| 32 virtual void Installed(ui::Layer* layer) OVERRIDE; | 33 // implementation of LayerAnimator |
| 33 virtual void Uninstalled(ui::Layer* layer) OVERRIDE; | 34 virtual void SetLayer(ui::Layer* layer) OVERRIDE; |
| 34 virtual void SetTransform(ui::Layer* layer, | 35 virtual void SetTransform(const ui::Transform& transform) OVERRIDE; |
| 35 const ui::Transform& transform) OVERRIDE; | 36 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE; |
| 36 virtual void SetBounds(ui::Layer* layer, const gfx::Rect& bounds) OVERRIDE; | |
| 37 | 37 |
| 38 // implementation of ScreenRotationListener | 38 // implementation of ScreenRotationListener |
| 39 virtual void OnScreenRotationCompleted(const ui::Transform& final_transform, | 39 virtual void OnScreenRotationCompleted(const ui::Transform& final_transform, |
| 40 const gfx::Rect& final_rect) OVERRIDE; | 40 const gfx::Rect& final_rect) OVERRIDE; |
| 41 | 41 |
| 42 private: | |
| 43 // This is the currently animating rotation. We hang onto it so that if a | 42 // This is the currently animating rotation. We hang onto it so that if a |
| 44 // call to |SetTransform| is made during the rotation, we can update the | 43 // call to |SetTransform| is made during the rotation, we can update the |
| 45 // target orientation of this rotation. | 44 // target orientation of this rotation. |
| 46 scoped_ptr<ScreenRotation> rotation_; | 45 scoped_ptr<ScreenRotation> rotation_; |
| 47 | 46 |
| 48 // If a call to SetBounds happens during a rotation its effect is delayed | 47 // If a call to SetBounds happens during a rotation its effect is delayed |
| 49 // until the rotation completes. If this happens several times, only the last | 48 // until the rotation completes. If this happens several times, only the last |
| 50 // call to SetBounds will have any effect. | 49 // call to SetBounds will have any effect. |
| 51 scoped_ptr<gfx::Rect> pending_bounds_; | 50 scoped_ptr<gfx::Rect> pending_bounds_; |
| 52 | 51 |
| 53 // If a call to SetTransform happens during a rotation its effect is delayed | 52 // If a call to SetTransform happens during a rotation its effect is delayed |
| 54 // until the rotation completes. If this happens several times, only the last | 53 // until the rotation completes. If this happens several times, only the last |
| 55 // call to SetTransform will have any effect. | 54 // call to SetTransform will have any effect. |
| 56 scoped_ptr<ui::Transform> pending_transform_; | 55 scoped_ptr<ui::Transform> pending_transform_; |
| 57 | 56 |
| 58 // The screen rotation setter is associated with a view so that the view's | 57 // The screen rotation animator is associated with a view so that the view's |
| 59 // bounds may be set when the animation completes. | 58 // bounds may be set when the animation completes. |
| 60 views::View* view_; | 59 views::View* view_; |
| 61 | 60 |
| 62 DISALLOW_COPY_AND_ASSIGN(ScreenRotationSetter); | 61 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimator); |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 | 64 |
| 66 ScreenRotationSetter::ScreenRotationSetter(views::View* view) : view_(view) { | 65 ScreenRotationAnimator::ScreenRotationAnimator(views::View* view) |
| 66 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)), |
| 67 view_(view) { |
| 67 } | 68 } |
| 68 | 69 |
| 69 void ScreenRotationSetter::Installed(ui::Layer* layer) OVERRIDE { | 70 ScreenRotationAnimator::~ScreenRotationAnimator() { |
| 70 } | 71 } |
| 71 | 72 |
| 72 void ScreenRotationSetter::Uninstalled(ui::Layer* layer) OVERRIDE { | 73 void ScreenRotationAnimator::SetLayer(ui::Layer* layer) { |
| 73 if (rotation_.get()) | 74 if (!layer && rotation_.get()) |
| 74 rotation_->Stop(); | 75 rotation_->Stop(); |
| 76 |
| 77 DCHECK(!layer || layer == view_->layer()); |
| 78 ui::LayerAnimator::SetDelegate(layer); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void ScreenRotationSetter::SetTransform(ui::Layer* layer, | 81 void ScreenRotationAnimator::SetTransform(const ui::Transform& transform) { |
| 78 const ui::Transform& transform) { | |
| 79 if (rotation_.get()) { | 82 if (rotation_.get()) { |
| 80 pending_transform_.reset(new ui::Transform(transform)); | 83 pending_transform_.reset(new ui::Transform(transform)); |
| 81 } else { | 84 } else { |
| 82 float new_degrees, old_degrees; | 85 float new_degrees, old_degrees; |
| 83 if (ui::InterpolatedTransform::FactorTRS(transform, | 86 if (ui::InterpolatedTransform::FactorTRS( |
| 84 NULL, &new_degrees, NULL) && | 87 transform, NULL, &new_degrees, NULL) && |
| 85 ui::InterpolatedTransform::FactorTRS(layer->transform(), | 88 ui::InterpolatedTransform::FactorTRS( |
| 86 NULL, &old_degrees, NULL)) { | 89 delegate()->GetTransformForAnimation(), NULL, &old_degrees, NULL)) { |
| 87 rotation_.reset(new ScreenRotation(view_, | 90 rotation_.reset(new ScreenRotation(view_, |
| 91 delegate(), |
| 88 this, | 92 this, |
| 89 SymmetricRound(old_degrees), | 93 SymmetricRound(old_degrees), |
| 90 SymmetricRound(new_degrees))); | 94 SymmetricRound(new_degrees))); |
| 91 } | 95 } |
| 92 } | 96 } |
| 93 } | 97 } |
| 94 | 98 |
| 95 void ScreenRotationSetter::SetBounds(ui::Layer* layer, | 99 void ScreenRotationAnimator::SetBounds(const gfx::Rect& bounds) { |
| 96 const gfx::Rect& bounds) { | |
| 97 // cache bounds changes during an animation. | 100 // cache bounds changes during an animation. |
| 98 if (rotation_.get()) | 101 if (rotation_.get()) |
| 99 pending_bounds_.reset(new gfx::Rect(bounds)); | 102 pending_bounds_.reset(new gfx::Rect(bounds)); |
| 100 else | 103 else |
| 101 layer->SetBounds(bounds); | 104 delegate()->SetBoundsFromAnimation(bounds); |
| 102 } | 105 } |
| 103 | 106 |
| 104 void ScreenRotationSetter::OnScreenRotationCompleted( | 107 void ScreenRotationAnimator::OnScreenRotationCompleted( |
| 105 const ui::Transform& final_transform, | 108 const ui::Transform& final_transform, |
| 106 const gfx::Rect& final_bounds) { | 109 const gfx::Rect& final_bounds) { |
| 107 // destroy the animation. | 110 // destroy the animation. |
| 108 rotation_.reset(); | 111 rotation_.reset(); |
| 109 | 112 |
| 110 if (pending_bounds_.get() && view_->layer()) { | 113 if (pending_bounds_.get() && delegate()) { |
| 111 // If there are any pending bounds changes, they will have already been | 114 // If there are any pending bounds changes, they will have already been |
| 112 // applied to the view, and are waiting to be applied to the layer. | 115 // applied to the view, and are waiting to be applied to the layer. |
| 113 view_->layer()->SetBounds(*pending_bounds_); | 116 delegate()->SetBoundsFromAnimation(*pending_bounds_); |
| 114 pending_bounds_.reset(); | 117 pending_bounds_.reset(); |
| 115 } else if (!final_bounds.IsEmpty()) { | 118 } else if (!final_bounds.IsEmpty()) { |
| 116 // Otherwise we may have new bounds as the result of the completed screen | 119 // Otherwise we may have new bounds as the result of the completed screen |
| 117 // rotation. Apply these to the view. | 120 // rotation. Apply these to the view. |
| 118 view_->SetBoundsRect(final_bounds); | 121 view_->SetBoundsRect(final_bounds); |
| 119 } | 122 } |
| 120 | 123 |
| 121 if (pending_transform_.get() && view_->layer()) { | 124 if (pending_transform_.get() && delegate()) { |
| 122 // If there is a pending transformation, we need to initiate another | 125 // If there is a pending transformation, we need to initiate another |
| 123 // animation. | 126 // animation. |
| 124 SetTransform(view_->layer(), *pending_transform_); | 127 SetTransform(*pending_transform_); |
| 125 pending_transform_.reset(); | 128 pending_transform_.reset(); |
| 126 } | 129 } |
| 127 } | 130 } |
| 128 | 131 |
| 129 } // namespace | 132 } // namespace |
| 130 | 133 |
| 131 views::LayerPropertySetter* ScreenRotationSetterFactory::Create( | 134 ui::LayerAnimator* ScreenRotationAnimatorFactory::Create(views::View* view) { |
| 132 views::View* view) { | 135 return new ScreenRotationAnimator(view); |
| 133 return new ScreenRotationSetter(view); | |
| 134 } | 136 } |
| OLD | NEW |