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