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 "base/logging.h" | |
8 #include "content/public/renderer/render_view.h" | |
9 #include "third_party/skia/include/core/SkCanvas.h" | |
10 #include "third_party/skia/include/core/SkPaint.h" | |
11 #include "third_party/skia/include/core/SkRect.h" | |
12 #include "third_party/skia/include/effects/SkGradientShader.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
14 #include "ui/gfx/size.h" | |
15 #include "ui/gfx/skia_util.h" | |
16 | |
17 const int kAnimationDurationMiliseconds = 5000; | |
Nico
2012/08/15 18:26:14
5 seconds is surprisingly long
sail
2012/08/15 18:34:35
Done.
Oops, debug code. Changed back to 500.
| |
18 const int kAnimationFrameRate = 60; | |
19 const int kTargetAlpha = 191; | |
20 | |
21 WebViewAnimatingOverlay::WebViewAnimatingOverlay( | |
22 content::RenderView* render_view) | |
23 : render_view_(render_view), | |
24 state_(HIDDEN), | |
25 animation_(kAnimationDurationMiliseconds, kAnimationFrameRate, this) { | |
26 } | |
27 | |
28 WebViewAnimatingOverlay::~WebViewAnimatingOverlay() { | |
29 if (render_view_->GetWebView()) | |
30 render_view_->GetWebView()->removePageOverlay(this); | |
31 } | |
32 | |
33 void WebViewAnimatingOverlay::Show() { | |
34 if (state_ == ANIMATING_IN || state_ == VISIBLE) | |
35 return; | |
36 | |
37 if (state_ == ANIMATING_OUT) { | |
38 animation_.SetCurrentValue(1.0 - animation_.GetCurrentValue()); | |
39 } else { | |
40 DCHECK(state_ == HIDDEN); | |
Nico
2012/08/15 18:26:14
DCHECK_EQ
sail
2012/08/15 18:34:35
Done.
| |
41 if (render_view_->GetWebView()) | |
42 render_view_->GetWebView()->addPageOverlay(this, 0); | |
43 } | |
44 | |
45 state_ = ANIMATING_IN; | |
46 animation_.Start(); | |
47 } | |
48 | |
49 void WebViewAnimatingOverlay::Hide() { | |
50 if (state_ == ANIMATING_OUT || state_ == HIDDEN) | |
51 return; | |
52 | |
53 if (state_ == ANIMATING_IN) | |
54 animation_.SetCurrentValue(1.0 - animation_.GetCurrentValue()); | |
55 | |
56 state_ = ANIMATING_OUT; | |
57 animation_.Start(); | |
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 = GetCurrentAlpha(); | |
69 SkColor colors[] = { | |
70 SkColorSetARGB(alpha, 255, 255, 255), | |
71 SkColorSetARGB(alpha, 127, 127, 127) | |
72 }; | |
73 | |
74 SkAutoTUnref<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 | |
81 canvas->drawRect(rect, paint); | |
82 } | |
83 | |
84 void WebViewAnimatingOverlay::AnimationEnded(const ui::Animation* animation) { | |
85 if (state_ == ANIMATING_IN) { | |
86 state_ = VISIBLE; | |
87 } else { | |
88 DCHECK(state_ == ANIMATING_OUT); | |
Nico
2012/08/15 18:26:14
dcheck_eq
sail
2012/08/15 18:34:35
Done.
| |
89 state_ = HIDDEN; | |
90 if (render_view_->GetWebView()) | |
91 render_view_->GetWebView()->removePageOverlay(this); | |
92 } | |
93 } | |
94 | |
95 void WebViewAnimatingOverlay::AnimationProgressed( | |
96 const ui::Animation* animation) { | |
97 render_view_->Repaint(render_view_->GetSize()); | |
98 } | |
99 | |
100 int WebViewAnimatingOverlay::GetCurrentAlpha() { | |
101 switch (state_) { | |
102 case ANIMATING_IN: | |
103 return animation_.CurrentValueBetween(0, kTargetAlpha); | |
104 case ANIMATING_OUT: | |
105 return animation_.CurrentValueBetween(kTargetAlpha, 0); | |
106 case VISIBLE: | |
107 case HIDDEN: | |
108 return kTargetAlpha; | |
109 } | |
110 } | |
OLD | NEW |