Chromium Code Reviews| Index: chrome/renderer/webview_animating_overlay.cc |
| diff --git a/chrome/renderer/webview_animating_overlay.cc b/chrome/renderer/webview_animating_overlay.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f0e2a001a5f25677159ec2fe8b709ee3f28b6978 |
| --- /dev/null |
| +++ b/chrome/renderer/webview_animating_overlay.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/webview_animating_overlay.h" |
| + |
| +#include "base/logging.h" |
| +#include "content/public/renderer/render_view.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "third_party/skia/include/core/SkRect.h" |
| +#include "third_party/skia/include/effects/SkGradientShader.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| +#include "ui/gfx/size.h" |
| +#include "ui/gfx/skia_util.h" |
| + |
| +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.
|
| +const int kAnimationFrameRate = 60; |
| +const int kTargetAlpha = 191; |
| + |
| +WebViewAnimatingOverlay::WebViewAnimatingOverlay( |
| + content::RenderView* render_view) |
| + : render_view_(render_view), |
| + state_(HIDDEN), |
| + animation_(kAnimationDurationMiliseconds, kAnimationFrameRate, this) { |
| +} |
| + |
| +WebViewAnimatingOverlay::~WebViewAnimatingOverlay() { |
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->removePageOverlay(this); |
| +} |
| + |
| +void WebViewAnimatingOverlay::Show() { |
| + if (state_ == ANIMATING_IN || state_ == VISIBLE) |
| + return; |
| + |
| + if (state_ == ANIMATING_OUT) { |
| + animation_.SetCurrentValue(1.0 - animation_.GetCurrentValue()); |
| + } else { |
| + DCHECK(state_ == HIDDEN); |
|
Nico
2012/08/15 18:26:14
DCHECK_EQ
sail
2012/08/15 18:34:35
Done.
|
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->addPageOverlay(this, 0); |
| + } |
| + |
| + state_ = ANIMATING_IN; |
| + animation_.Start(); |
| +} |
| + |
| +void WebViewAnimatingOverlay::Hide() { |
| + if (state_ == ANIMATING_OUT || state_ == HIDDEN) |
| + return; |
| + |
| + if (state_ == ANIMATING_IN) |
| + animation_.SetCurrentValue(1.0 - animation_.GetCurrentValue()); |
| + |
| + state_ = ANIMATING_OUT; |
| + animation_.Start(); |
| +} |
| + |
| +void WebViewAnimatingOverlay::paintPageOverlay(WebKit::WebCanvas* canvas) { |
| + SkRect rect = gfx::RectToSkRect(gfx::Rect(render_view_->GetSize())); |
| + |
| + // The center of the radial gradient should be near the top middle. |
| + SkPoint center_point; |
| + center_point.iset(rect.width() * 0.5, rect.height() * 0.05); |
| + |
| + // Animate in or out using the alpha. |
| + int alpha = GetCurrentAlpha(); |
| + SkColor colors[] = { |
| + SkColorSetARGB(alpha, 255, 255, 255), |
| + SkColorSetARGB(alpha, 127, 127, 127) |
| + }; |
| + |
| + SkAutoTUnref<SkShader> shader(SkGradientShader::CreateRadial(center_point, |
| + SkIntToScalar(rect.width()), colors, NULL, arraysize(colors), |
| + SkShader::kClamp_TileMode)); |
| + SkPaint paint; |
| + paint.setShader(shader); |
| + paint.setDither(true); |
| + |
| + canvas->drawRect(rect, paint); |
| +} |
| + |
| +void WebViewAnimatingOverlay::AnimationEnded(const ui::Animation* animation) { |
| + if (state_ == ANIMATING_IN) { |
| + state_ = VISIBLE; |
| + } else { |
| + DCHECK(state_ == ANIMATING_OUT); |
|
Nico
2012/08/15 18:26:14
dcheck_eq
sail
2012/08/15 18:34:35
Done.
|
| + state_ = HIDDEN; |
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->removePageOverlay(this); |
| + } |
| +} |
| + |
| +void WebViewAnimatingOverlay::AnimationProgressed( |
| + const ui::Animation* animation) { |
| + render_view_->Repaint(render_view_->GetSize()); |
| +} |
| + |
| +int WebViewAnimatingOverlay::GetCurrentAlpha() { |
| + switch (state_) { |
| + case ANIMATING_IN: |
| + return animation_.CurrentValueBetween(0, kTargetAlpha); |
| + case ANIMATING_OUT: |
| + return animation_.CurrentValueBetween(kTargetAlpha, 0); |
| + case VISIBLE: |
| + case HIDDEN: |
| + return kTargetAlpha; |
| + } |
| +} |