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.
5 #include "ui/views/controls/glow_hover_controller.h"
7 #include "third_party/skia/include/effects/SkGradientShader.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/image/image_skia.h"
10 #include "ui/gfx/image/image_skia_operations.h"
11 #include "ui/views/view.h"
15 // Amount to scale the opacity.
16 static const double kTrackOpacityScale
= 0.5;
17 static const double kHighlightOpacityScale
= 1.0;
19 // How long the hover state takes.
20 static const int kTrackHoverDurationMs
= 400;
22 GlowHoverController::GlowHoverController(views::View
* view
)
25 opacity_scale_(kTrackOpacityScale
) {
26 animation_
.set_delegate(this);
29 GlowHoverController::~GlowHoverController() {
32 void GlowHoverController::SetAnimationContainer(
33 gfx::AnimationContainer
* container
) {
34 animation_
.SetContainer(container
);
37 void GlowHoverController::SetLocation(const gfx::Point
& location
) {
40 view_
->SchedulePaint();
43 void GlowHoverController::Show(Style style
) {
46 opacity_scale_
= kTrackOpacityScale
;
47 animation_
.SetSlideDuration(kTrackHoverDurationMs
);
48 animation_
.SetTweenType(gfx::Tween::EASE_OUT
);
52 opacity_scale_
= kHighlightOpacityScale
;
53 // Force the end state to show immediately.
60 void GlowHoverController::Hide() {
61 animation_
.SetTweenType(gfx::Tween::EASE_IN
);
65 void GlowHoverController::HideImmediately() {
67 view_
->SchedulePaint();
71 double GlowHoverController::GetAnimationValue() const {
72 return animation_
.GetCurrentValue();
75 bool GlowHoverController::ShouldDraw() const {
76 return animation_
.IsShowing() || animation_
.is_animating();
79 void GlowHoverController::Draw(gfx::Canvas
* canvas
,
80 const gfx::ImageSkia
& mask_image
) const {
84 // Draw a radial gradient to hover_canvas.
85 gfx::Canvas
hover_canvas(gfx::Size(mask_image
.width(), mask_image
.height()),
86 canvas
->image_scale(),
89 // Draw a radial gradient to hover_canvas.
90 int radius
= view_
->width() / 3;
93 center_point
.iset(location_
.x(), location_
.y());
96 static_cast<int>(255 * opacity_scale_
* animation_
.GetCurrentValue());
97 colors
[0] = SkColorSetARGB(hover_alpha
, 255, 255, 255);
98 colors
[1] = SkColorSetARGB(0, 255, 255, 255);
99 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
100 SkGradientShader::CreateRadial(
101 center_point
, SkIntToScalar(radius
), colors
, NULL
, 2,
102 SkShader::kClamp_TileMode
));
103 // Shader can end up null when radius = 0.
104 // If so, this results in default full tab glow behavior.
107 paint
.setStyle(SkPaint::kFill_Style
);
108 paint
.setAntiAlias(true);
109 paint
.setShader(shader
.get());
110 hover_canvas
.DrawRect(gfx::Rect(location_
.x() - radius
,
111 location_
.y() - radius
,
112 radius
* 2, radius
* 2), paint
);
114 gfx::ImageSkia result
= gfx::ImageSkiaOperations::CreateMaskedImage(
115 gfx::ImageSkia(hover_canvas
.ExtractImageRep()), mask_image
);
116 canvas
->DrawImageInt(result
, (view_
->width() - mask_image
.width()) / 2,
117 (view_
->height() - mask_image
.height()) / 2);
120 void GlowHoverController::AnimationEnded(const gfx::Animation
* animation
) {
121 view_
->SchedulePaint();
124 void GlowHoverController::AnimationProgressed(const gfx::Animation
* animation
) {
125 view_
->SchedulePaint();