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