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.h" | |
6 | |
7 #include "base/debug/trace_event.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/task.h" | |
10 #include "ui/base/animation/slide_animation.h" | |
11 #include "ui/gfx/compositor/layer.h" | |
12 #include "ui/gfx/interpolated_transform.h" | |
13 #include "ui/gfx/rect.h" | |
14 #include "ui/gfx/transform.h" | |
15 #include "views/paint_lock.h" | |
16 #include "views/view.h" | |
17 #include "views/widget/widget.h" | |
18 | |
19 namespace { | |
20 const int kDefaultTransitionDurationMs = 350; | |
21 | |
22 } // namespace | |
23 | |
24 //////////////////////////////////////////////////////////////////////////////// | |
25 // ScreenRotationListener public: | |
26 // | |
27 | |
28 ScreenRotationListener::~ScreenRotationListener() { | |
29 } | |
30 | |
31 //////////////////////////////////////////////////////////////////////////////// | |
32 // ScreenRotation public: | |
33 // | |
34 | |
35 ScreenRotation::ScreenRotation(views::View* view, | |
36 ScreenRotationListener* listener, | |
37 float old_degrees, | |
38 float new_degrees) | |
39 : view_(view), | |
40 widget_(view->GetWidget()), | |
41 listener_(listener), | |
42 old_degrees_(old_degrees), | |
43 new_degrees_(new_degrees), | |
44 duration_(kDefaultTransitionDurationMs), | |
45 animation_started_(false), | |
46 animation_stopped_(false) { | |
47 DCHECK(view); | |
48 DCHECK(listener); | |
49 | |
50 if (!view->layer() || !widget_) { | |
51 Finalize(); | |
52 } else { | |
53 // Screen rotations are trigged as a result of a call to SetTransform which | |
54 // causes a paint to be scheduled to occur. At this point, the paint has | |
55 // been scheduled, but has not yet been started. We will listen for this | |
56 // paint to be completed before we start animating. | |
57 view->layer()->GetCompositor()->AddObserver(this); | |
58 } | |
59 } | |
60 | |
61 ScreenRotation::~ScreenRotation() { | |
62 if (view_->layer()) | |
63 view_->layer()->GetCompositor()->RemoveObserver(this); | |
64 } | |
65 | |
66 void ScreenRotation::Stop() { | |
67 animation_.reset(); | |
68 if (view_->layer()) { | |
69 if (!interpolated_transform_.get()) { | |
70 // attempt to initialize. | |
71 Init(); | |
72 } | |
73 if (interpolated_transform_.get()) { | |
74 view_->layer()->SetTransform(interpolated_transform_->Interpolate(1.0)); | |
75 view_->GetWidget()->SchedulePaintInRect( | |
76 view_->GetWidget()->GetClientAreaScreenBounds()); | |
77 } | |
78 } | |
79 Finalize(); | |
80 } | |
81 | |
82 //////////////////////////////////////////////////////////////////////////////// | |
83 // ScreenRotation private: | |
84 // | |
85 | |
86 void ScreenRotation::AnimationProgressed(const ui::Animation* anim) { | |
87 TRACE_EVENT0("ScreenRotation", "step"); | |
88 view_->layer()->SetTransform(interpolated_transform_->Interpolate( | |
89 anim->GetCurrentValue())); | |
90 widget_->SchedulePaintInRect(widget_->GetClientAreaScreenBounds()); | |
91 } | |
92 | |
93 void ScreenRotation::AnimationEnded(const ui::Animation* anim) { | |
94 TRACE_EVENT_END0("ScreenRotation", "ScreenRotation"); | |
95 // TODO(vollick) massage matrix so that entries sufficiently close | |
96 // to 0, 1, or -1 are clamped to these values. The idea is to fight | |
97 // accumulated numeric error due to successive rotations. | |
98 ui::Transform xform = view_->layer()->transform(); | |
99 gfx::Point origin; | |
100 xform.TransformPoint(origin); | |
101 ui::Transform translation; | |
102 translation.SetTranslate(new_origin_.x() - origin.x(), | |
103 new_origin_.y() - origin.y()); | |
104 xform.ConcatTransform(translation); | |
105 view_->layer()->SetTransform(xform); | |
106 widget_->SchedulePaintInRect(widget_->GetClientAreaScreenBounds()); | |
107 animation_stopped_ = true; | |
108 } | |
109 | |
110 void ScreenRotation::OnCompositingEnded(ui::Compositor* compositor) { | |
111 DoPendingWork(); | |
112 } | |
113 | |
114 void ScreenRotation::Init() { | |
115 TRACE_EVENT0("ScreenRotation", "init"); | |
116 | |
117 ui::Transform current_transform = view_->layer()->transform(); | |
118 int degrees = new_degrees_ - old_degrees_; | |
119 degrees = NormalizeAngle(degrees); | |
120 | |
121 // No rotation required. | |
122 if (degrees == 0) | |
123 return; | |
124 | |
125 gfx::Point old_pivot; | |
126 gfx::Point new_pivot; | |
127 int width = view_->layer()->bounds().width(); | |
128 int height = view_->layer()->bounds().height(); | |
129 | |
130 switch (degrees) { | |
131 case 90: | |
132 new_origin_ = new_pivot = gfx::Point(width, 0); | |
133 new_size_.SetSize(height, width); | |
134 break; | |
135 case -90: | |
136 new_origin_ = new_pivot = gfx::Point(0, height); | |
137 new_size_.SetSize(height, width); | |
138 break; | |
139 case 180: | |
140 duration_ = 550; | |
141 new_pivot = old_pivot = gfx::Point(width / 2, height / 2); | |
142 new_origin_.SetPoint(width, height); | |
143 new_size_.SetSize(width, height); | |
144 break; | |
145 } | |
146 | |
147 // Convert points to world space. | |
148 current_transform.TransformPoint(old_pivot); | |
149 current_transform.TransformPoint(new_pivot); | |
150 current_transform.TransformPoint(new_origin_); | |
151 | |
152 scoped_ptr<ui::InterpolatedTransform> rotation( | |
153 new ui::InterpolatedTransformAboutPivot( | |
154 old_pivot, | |
155 new ui::InterpolatedRotation(0, degrees))); | |
156 | |
157 scoped_ptr<ui::InterpolatedTransform> translation( | |
158 new ui::InterpolatedTranslation( | |
159 gfx::Point(0, 0), | |
160 gfx::Point(new_pivot.x() - old_pivot.x(), | |
161 new_pivot.y() - old_pivot.y()))); | |
162 | |
163 float scale_factor = 0.9f; | |
164 scoped_ptr<ui::InterpolatedTransform> scale_down( | |
165 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f)); | |
166 | |
167 scoped_ptr<ui::InterpolatedTransform> scale_up( | |
168 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f)); | |
169 | |
170 interpolated_transform_.reset( | |
171 new ui::InterpolatedConstantTransform(current_transform)); | |
172 | |
173 scale_up->SetChild(scale_down.release()); | |
174 translation->SetChild(scale_up.release()); | |
175 rotation->SetChild(translation.release()); | |
176 interpolated_transform_->SetChild(rotation.release()); | |
177 } | |
178 | |
179 void ScreenRotation::Start() { | |
180 TRACE_EVENT_BEGIN0("ScreenRotation", "ScreenRotation"); | |
181 Init(); | |
182 if (interpolated_transform_.get()) { | |
183 paint_lock_.reset(new views::PaintLock(view_)); | |
184 animation_.reset(new ui::SlideAnimation(this)); | |
185 animation_->SetTweenType(ui::Tween::LINEAR); | |
186 animation_->SetSlideDuration(duration_); | |
187 animation_->Show(); | |
188 animation_started_ = true; | |
189 } else { | |
190 Finalize(); | |
191 } | |
192 } | |
193 | |
194 void ScreenRotation::Finalize() { | |
195 ui::Transform final_transform = view_->GetTransform(); | |
196 gfx::Rect final_bounds(0, 0, new_size_.width(), new_size_.height()); | |
197 listener_->OnScreenRotationCompleted(final_transform, final_bounds); | |
198 } | |
199 | |
200 int ScreenRotation::NormalizeAngle(int degrees) { | |
201 while (degrees <= -180) degrees += 360; | |
202 while (degrees > 180) degrees -= 360; | |
203 return degrees; | |
204 } | |
205 | |
206 void ScreenRotation::DoPendingWork() { | |
207 if (!animation_started_) | |
208 Start(); | |
209 else if (animation_stopped_) { | |
210 view_->layer()->GetCompositor()->RemoveObserver(this); | |
211 Finalize(); | |
212 } | |
213 } | |
OLD | NEW |