Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: chrome/browser/ui/touch/animation/screen_rotation.cc

Issue 7273073: Animated Rotation (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Address reviewer comments Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 last_t_(0.0),
45 duration_(kDefaultTransitionDurationMs),
46 animation_started_(false),
47 animation_stopped_(false) {
48 DCHECK(view);
49 DCHECK(listener);
50
51 if (!view->layer() || !widget_) {
52 Finalize();
53 } else {
54 // Screen rotations are trigged as a result of a call to SetTransform which
55 // causes a paint to be scheduled to occur. At this point, the paint has
56 // been scheduled, but has not yet been started. We will listen for this
57 // paint to be completed before we start animating.
58 view->layer()->compositor()->AddObserver(this);
59 }
60 }
61
62 ScreenRotation::~ScreenRotation() {
63 if (view_->layer())
64 view_->layer()->compositor()->RemoveObserver(this);
65 }
66
67 void ScreenRotation::SetTarget(float degrees) {
68 if (new_degrees_ == degrees)
69 return;
70
71 new_degrees_ = degrees;
72 Init();
73 }
74
75 void ScreenRotation::Stop() {
76 animation_.reset();
77 if (view_->layer()) {
78 if (!interpolated_transform_.get()) {
79 // attempt to initialize.
80 Init();
81 }
82 if (interpolated_transform_.get()) {
83 view_->layer()->SetTransform(interpolated_transform_->Interpolate(1.0));
84 view_->GetWidget()->SchedulePaintInRect(
85 view_->GetWidget()->GetClientAreaScreenBounds());
86 }
87 }
88 Finalize();
89 }
90
91 ////////////////////////////////////////////////////////////////////////////////
92 // ScreenRotation private:
93 //
94
95 void ScreenRotation::AnimationProgressed(const ui::Animation* anim) {
96 TRACE_EVENT0("ScreenRotation", "step");
97 last_t_ = static_cast<float>(anim->GetCurrentValue());
98 view_->layer()->SetTransform(interpolated_transform_->Interpolate(last_t_));
99 widget_->SchedulePaintInRect(widget_->GetClientAreaScreenBounds());
100 }
101
102 void ScreenRotation::AnimationEnded(const ui::Animation* anim) {
103 TRACE_EVENT_END0("ScreenRotation", "ScreenRotation");
104 // TODO(vollick) massage matrix so that entries sufficiently close
105 // to 0, 1, or -1 are clamped to these values. The idea is to fight
106 // accumulated numeric error due to successive rotations.
107 ui::Transform xform = view_->layer()->transform();
108 gfx::Point origin;
109 xform.TransformPoint(origin);
110 ui::Transform translation;
111 translation.SetTranslate(new_origin_.x() - origin.x(),
112 new_origin_.y() - origin.y());
113 xform.ConcatTransform(translation);
114 view_->layer()->SetTransform(xform);
115 widget_->SchedulePaintInRect(widget_->GetClientAreaScreenBounds());
116 animation_stopped_ = true;
117 }
118
119 void ScreenRotation::OnCompositingEnded() {
120 DoPendingWork();
121 }
122
123 void ScreenRotation::Init() {
124 TRACE_EVENT0("ScreenRotation", "init");
125
126 ui::Transform current_transform = view_->layer()->transform();
127 int degrees = new_degrees_ - old_degrees_;
128 degrees = NormalizeAngle(degrees);
129
130 // No rotation required.
131 if (degrees == 0)
132 return;
133
134 gfx::Point old_pivot;
135 gfx::Point new_pivot;
136 int width = view_->layer()->bounds().width();
137 int height = view_->layer()->bounds().height();
138
139 switch (degrees) {
140 case 90:
141 new_origin_ = new_pivot = gfx::Point(width, 0);
142 new_size_.SetSize(height, width);
143 break;
144 case -90:
145 new_origin_ = new_pivot = gfx::Point(0, height);
146 new_size_.SetSize(height, width);
147 break;
148 case 180:
149 duration_ = 550;
150 new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
151 new_origin_.SetPoint(width, height);
152 new_size_.SetSize(width, height);
153 break;
154 }
155
156 // Convert points to world space.
157 current_transform.TransformPoint(old_pivot);
158 current_transform.TransformPoint(new_pivot);
159 current_transform.TransformPoint(new_origin_);
160
161 scoped_ptr<ui::InterpolatedTransform> rotation(
162 new ui::InterpolatedTransformAboutPivot(
163 old_pivot,
164 new ui::InterpolatedRotation(0, degrees)));
165
166 scoped_ptr<ui::InterpolatedTransform> translation(
167 new ui::InterpolatedTranslation(
168 gfx::Point(0, 0),
169 gfx::Point(new_pivot.x() - old_pivot.x(),
170 new_pivot.y() - old_pivot.y())));
171
172 float scale_factor = 0.9f;
173 scoped_ptr<ui::InterpolatedTransform> scale_down(
174 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f));
175
176 scoped_ptr<ui::InterpolatedTransform> scale_up(
177 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f));
178
179 scoped_ptr<ui::InterpolatedTransform> transition(
180 new ui::InterpolatedConstantTransform(current_transform));
181
182 scale_up->SetChild(scale_down.release());
183 translation->SetChild(scale_up.release());
184 rotation->SetChild(translation.release());
185 transition->SetChild(rotation.release());
186
187 if (interpolated_transform_.get()) {
188 // We are in the middle of a transition. In this case, we need to create
189 // an interpolated transform that gets us from where we are to the target
190 // transform.
191 ui::Transform target = transition->Interpolate(1.0);
192 interpolated_transform_.reset(
193 new ui::InterpolatedTRSTransform(
194 current_transform, target, last_t_, 1.0));
195 } else {
196 interpolated_transform_.reset(transition.release());
197 }
198 }
199
200 void ScreenRotation::Start() {
201 TRACE_EVENT_BEGIN0("ScreenRotation", "ScreenRotation");
202 Init();
203 if (interpolated_transform_.get()) {
204 paint_lock_.reset(new views::PaintLock(view_));
205 animation_.reset(new ui::SlideAnimation(this));
206 animation_->SetTweenType(ui::Tween::LINEAR);
207 animation_->SetSlideDuration(duration_);
208 animation_->Show();
209 animation_started_ = true;
210 } else {
211 Finalize();
212 }
213 }
214
215 void ScreenRotation::Finalize() {
216 ui::Transform final_transform = view_->GetTransform();
217 gfx::Rect final_bounds(0, 0, new_size_.width(), new_size_.height());
218 listener_->OnScreenRotationCompleted(final_transform, final_bounds);
219 }
220
221 int ScreenRotation::NormalizeAngle(int degrees) {
222 while (degrees <= -180) degrees += 360;
223 while (degrees > 180) degrees -= 360;
224 return degrees;
225 }
226
227 void ScreenRotation::DoPendingWork() {
228 if (!animation_started_)
229 Start();
230 else if (animation_stopped_) {
231 view_->layer()->compositor()->RemoveObserver(this);
232 Finalize();
233 }
234 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/touch/animation/screen_rotation.h ('k') | chrome/browser/ui/touch/animation/screen_rotation_setter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698