OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/renderer/webview_animating_overlay.h" | |
6 | |
7 #include "content/public/renderer/render_view.h" | |
8 #include "third_party/skia/include/core/SkCanvas.h" | |
9 #include "third_party/skia/include/core/SkPaint.h" | |
10 #include "third_party/skia/include/core/SkRect.h" | |
11 #include "third_party/skia/include/effects/SkGradientShader.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
13 #include "ui/gfx/size.h" | |
14 #include "ui/gfx/skia_util.h" | |
15 | |
16 const int kAnimationDurationMiliseconds = 500; | |
17 | |
18 WebViewAnimatingOverlay::WebViewAnimatingOverlay( | |
19 content::RenderView* render_view) | |
20 : render_view_(render_view), | |
21 state_(HIDDEN) { | |
22 } | |
23 | |
24 WebViewAnimatingOverlay::~WebViewAnimatingOverlay() { | |
25 if (render_view_->GetWebView()) | |
26 render_view_->GetWebView()->removePageOverlay(this); | |
27 } | |
28 | |
29 void WebViewAnimatingOverlay::Show() { | |
30 if (state_ == ANIMATING_IN || state_ == VISIBLE) | |
31 return; | |
32 | |
33 if (state_ == ANIMATING_OUT) { | |
34 start_time_ = base::Time::Now() - GetAnimationTimeRemaining(); | |
35 } else { | |
Nico
2012/08/14 21:14:12
DCHECK(state_ == HIDDEN);
Alternatively, you coul
sail
2012/08/15 05:26:20
Done.
| |
36 start_time_ = base::Time::Now(); | |
37 if (render_view_->GetWebView()) | |
38 render_view_->GetWebView()->addPageOverlay(this, 0); | |
39 } | |
40 | |
41 state_ = ANIMATING_IN; | |
42 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), this, | |
Nico
2012/08/14 21:14:12
constant
sail
2012/08/15 05:26:20
Done.
| |
43 &WebViewAnimatingOverlay::OnTimer); | |
44 } | |
45 | |
46 void WebViewAnimatingOverlay::Hide() { | |
47 if (state_ == ANIMATING_OUT || state_ == HIDDEN) | |
48 return; | |
49 | |
50 if (state_ == ANIMATING_IN) | |
51 start_time_ = base::Time::Now() - GetAnimationTimeRemaining(); | |
52 else | |
53 start_time_ = base::Time::Now(); | |
54 | |
55 state_ = ANIMATING_OUT; | |
56 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), this, | |
57 &WebViewAnimatingOverlay::OnTimer); | |
58 } | |
59 | |
60 void WebViewAnimatingOverlay::paintPageOverlay(WebKit::WebCanvas* canvas) { | |
61 SkRect rect = gfx::RectToSkRect(gfx::Rect(render_view_->GetSize())); | |
62 | |
63 // The center of the radial gradient should be near the top middle. | |
64 SkPoint center_point; | |
65 center_point.iset(rect.width() * 0.5, rect.height() * 0.05); | |
66 | |
67 // Animate in or out using the alpha. | |
68 int alpha = GetAnimationProgress() * 191; | |
Nico
2012/08/14 21:14:12
This looks like a linear animation. Could ui/base/
sail
2012/08/15 05:26:20
Done.
| |
69 SkColor colors[] = { | |
70 SkColorSetARGB(alpha, 255, 255, 255), | |
71 SkColorSetARGB(alpha, 127, 127, 127) | |
72 }; | |
73 | |
74 SkShader* shader = SkGradientShader::CreateRadial(center_point, | |
75 SkIntToScalar(rect.width()), colors, NULL, arraysize(colors), | |
76 SkShader::kClamp_TileMode); | |
77 SkPaint paint; | |
78 paint.setShader(shader); | |
79 paint.setDither(true); | |
80 shader->unref(); | |
jamesr
2012/08/14 19:08:24
Could you use SkAutoTUnref<> here instead of manua
sail
2012/08/15 05:26:20
Done.
| |
81 | |
82 canvas->drawRect(rect, paint); | |
83 } | |
84 | |
85 void WebViewAnimatingOverlay::OnTimer() { | |
86 if (GetAnimationTimeRemaining() <= base::TimeDelta()) { | |
87 timer_.Stop(); | |
88 if (state_ == ANIMATING_IN) { | |
89 state_ = VISIBLE; | |
90 } else { | |
91 DCHECK(state_ == ANIMATING_OUT); | |
92 state_ = HIDDEN; | |
93 if (render_view_->GetWebView()) | |
94 render_view_->GetWebView()->removePageOverlay(this); | |
95 } | |
96 } | |
97 render_view_->Repaint(render_view_->GetSize()); | |
98 } | |
99 | |
100 base::TimeDelta WebViewAnimatingOverlay::GetAnimationTimeRemaining() const { | |
101 base::Time end_time = start_time_ + | |
102 base::TimeDelta::FromMilliseconds(kAnimationDurationMiliseconds); | |
103 return end_time - base::Time::Now(); | |
jamesr
2012/08/14 19:08:24
we drive most web-based animations of of base::Tim
sail
2012/08/15 05:26:20
Done.
Switched to ui/base/animation which already
| |
104 } | |
105 | |
106 float WebViewAnimatingOverlay::GetAnimationProgress() const { | |
107 if (state_ != ANIMATING_IN && state_ != ANIMATING_OUT) | |
108 return 1.0; | |
109 | |
110 base::TimeDelta delta = base::Time::Now() - start_time_; | |
111 float progress = delta.InMillisecondsF() / kAnimationDurationMiliseconds; | |
112 if (state_ == ANIMATING_OUT) | |
113 progress = 1.0 - progress; | |
114 return std::max(std::min(progress, 1.0f), 0.0f); | |
115 } | |
OLD | NEW |