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 = 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_EQ(HIDDEN, state_); |
| 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 int radius = static_cast<int>(rect.width()); |
| 67 |
| 68 // Animate in or out using the alpha. |
| 69 int alpha = GetCurrentAlpha(); |
| 70 SkColor colors[] = { |
| 71 SkColorSetARGB(alpha, 255, 255, 255), |
| 72 SkColorSetARGB(alpha, 127, 127, 127) |
| 73 }; |
| 74 |
| 75 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateRadial(center_point, |
| 76 SkIntToScalar(radius), colors, NULL, arraysize(colors), |
| 77 SkShader::kClamp_TileMode)); |
| 78 SkPaint paint; |
| 79 paint.setShader(shader); |
| 80 paint.setDither(true); |
| 81 |
| 82 canvas->drawRect(rect, paint); |
| 83 } |
| 84 |
| 85 void WebViewAnimatingOverlay::AnimationEnded(const ui::Animation* animation) { |
| 86 if (state_ == ANIMATING_IN) { |
| 87 state_ = VISIBLE; |
| 88 } else { |
| 89 DCHECK_EQ(ANIMATING_OUT, state_); |
| 90 state_ = HIDDEN; |
| 91 if (render_view_->GetWebView()) |
| 92 render_view_->GetWebView()->removePageOverlay(this); |
| 93 } |
| 94 } |
| 95 |
| 96 void WebViewAnimatingOverlay::AnimationProgressed( |
| 97 const ui::Animation* animation) { |
| 98 render_view_->Repaint(render_view_->GetSize()); |
| 99 } |
| 100 |
| 101 int WebViewAnimatingOverlay::GetCurrentAlpha() { |
| 102 switch (state_) { |
| 103 case ANIMATING_IN: |
| 104 return animation_.CurrentValueBetween(0, kTargetAlpha); |
| 105 case ANIMATING_OUT: |
| 106 return animation_.CurrentValueBetween(kTargetAlpha, 0); |
| 107 case VISIBLE: |
| 108 case HIDDEN: |
| 109 return kTargetAlpha; |
| 110 } |
| 111 NOTREACHED(); |
| 112 return 1.0; |
| 113 } |
OLD | NEW |