summaryrefslogtreecommitdiff
path: root/chromium/ui/views/controls/glow_hover_controller.cc
blob: f2e3bf26f30a51a49c456cf109087d8732da5c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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 "ui/views/controls/glow_hover_controller.h"

#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/views/view.h"

namespace views {

// Amount to scale the opacity.
static const double kTrackOpacityScale = 0.5;
static const double kHighlightOpacityScale = 1.0;

// How long the hover state takes.
static const int kTrackHoverDurationMs = 400;

GlowHoverController::GlowHoverController(views::View* view)
    : view_(view),
      animation_(this),
      opacity_scale_(kTrackOpacityScale) {
  animation_.set_delegate(this);
}

GlowHoverController::~GlowHoverController() {
}

void GlowHoverController::SetAnimationContainer(
    ui::AnimationContainer* container) {
  animation_.SetContainer(container);
}

void GlowHoverController::SetLocation(const gfx::Point& location) {
  location_ = location;
  if (ShouldDraw())
    view_->SchedulePaint();
}

void GlowHoverController::Show(Style style) {
  switch (style) {
    case SUBTLE:
      opacity_scale_ = kTrackOpacityScale;
      animation_.SetSlideDuration(kTrackHoverDurationMs);
      animation_.SetTweenType(ui::Tween::EASE_OUT);
      animation_.Show();
      break;
    case PRONOUNCED:
      opacity_scale_ = kHighlightOpacityScale;
      // Force the end state to show immediately.
      animation_.Show();
      animation_.End();
      break;
  }
}

void GlowHoverController::Hide() {
  animation_.SetTweenType(ui::Tween::EASE_IN);
  animation_.Hide();
}

void GlowHoverController::HideImmediately() {
  if (ShouldDraw())
    view_->SchedulePaint();
  animation_.Reset();
}

double GlowHoverController::GetAnimationValue() const {
  return animation_.GetCurrentValue();
}

bool GlowHoverController::ShouldDraw() const {
  return animation_.IsShowing() || animation_.is_animating();
}

void GlowHoverController::Draw(gfx::Canvas* canvas,
                               const gfx::ImageSkia& mask_image) const {
  if (!ShouldDraw())
    return;

  // Draw a radial gradient to hover_canvas.
  gfx::Canvas hover_canvas(gfx::Size(mask_image.width(), mask_image.height()),
                           canvas->scale_factor(),
                           false);

  // Draw a radial gradient to hover_canvas.
  int radius = view_->width() / 3;

  SkPoint center_point;
  center_point.iset(location_.x(), location_.y());
  SkColor colors[2];
  int hover_alpha =
      static_cast<int>(255 * opacity_scale_ * animation_.GetCurrentValue());
  colors[0] = SkColorSetARGB(hover_alpha, 255, 255, 255);
  colors[1] = SkColorSetARGB(0, 255, 255, 255);
  skia::RefPtr<SkShader> shader = skia::AdoptRef(
      SkGradientShader::CreateRadial(
          center_point, SkIntToScalar(radius), colors, NULL, 2,
          SkShader::kClamp_TileMode));
  // Shader can end up null when radius = 0.
  // If so, this results in default full tab glow behavior.
  if (shader) {
    SkPaint paint;
    paint.setStyle(SkPaint::kFill_Style);
    paint.setAntiAlias(true);
    paint.setShader(shader.get());
    hover_canvas.DrawRect(gfx::Rect(location_.x() - radius,
                                    location_.y() - radius,
                                    radius * 2, radius * 2), paint);
  }
  gfx::ImageSkia result = gfx::ImageSkiaOperations::CreateMaskedImage(
      gfx::ImageSkia(hover_canvas.ExtractImageRep()), mask_image);
  canvas->DrawImageInt(result, (view_->width() - mask_image.width()) / 2,
                       (view_->height() - mask_image.height()) / 2);
}

void GlowHoverController::AnimationEnded(const ui::Animation* animation) {
  view_->SchedulePaint();
}

void GlowHoverController::AnimationProgressed(const ui::Animation* animation) {
  view_->SchedulePaint();
}

}  // namespace views