1// Copyright (c) 2011 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/browser/ui/views/location_bar/suggested_text_view.h"
6
7#include "chrome/browser/autocomplete/autocomplete_edit.h"
8#include "chrome/browser/instant/instant_controller.h"
9#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
10#include "ui/base/animation/multi_animation.h"
11#include "ui/gfx/canvas.h"
12#include "ui/gfx/color_utils.h"
13
14SuggestedTextView::SuggestedTextView(AutocompleteEditModel* edit_model)
15    : edit_model_(edit_model),
16      bg_color_(0) {
17}
18
19SuggestedTextView::~SuggestedTextView() {
20}
21
22void SuggestedTextView::StartAnimation() {
23  StopAnimation();
24
25  animation_.reset(CreateAnimation());
26  animation_->Start();
27  UpdateBackgroundColor();
28}
29
30void SuggestedTextView::StopAnimation() {
31  if (animation_.get()) {
32    // Reset the delegate so that we don't attempt to commit in AnimationEnded.
33    animation_->set_delegate(NULL);
34    animation_.reset(NULL);
35    SetColor(LocationBarView::GetColor(ToolbarModel::NONE,
36                                       LocationBarView::DEEMPHASIZED_TEXT));
37    SchedulePaint();
38  }
39}
40
41void SuggestedTextView::OnPaintBackground(gfx::Canvas* canvas) {
42  if (!animation_.get() || animation_->GetCurrentValue() == 0)
43    return;
44
45  // TODO(sky): these numbers need to come from the edit.
46  canvas->FillRectInt(bg_color_, 0, 2, width(), height() - 5);
47}
48
49void SuggestedTextView::AnimationEnded(const ui::Animation* animation) {
50  edit_model_->CommitSuggestedText(false);
51}
52
53void SuggestedTextView::AnimationProgressed(const ui::Animation* animation) {
54  UpdateBackgroundColor();
55
56  SkColor fg_color = LocationBarView::GetColor(
57      ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT);
58  SkColor sel_fg_color = LocationBarView::GetColor(
59      ToolbarModel::NONE, LocationBarView::SELECTED_TEXT);
60  SetColor(color_utils::AlphaBlend(
61      sel_fg_color, fg_color,
62      ui::Tween::ValueBetween(animation->GetCurrentValue(), 0, 255)));
63
64  SchedulePaint();
65}
66
67void SuggestedTextView::AnimationCanceled(const ui::Animation* animation) {
68}
69
70ui::Animation* SuggestedTextView::CreateAnimation() {
71  ui::MultiAnimation::Parts parts;
72  parts.push_back(ui::MultiAnimation::Part(
73      InstantController::kAutoCommitPauseTimeMS, ui::Tween::ZERO));
74  parts.push_back(ui::MultiAnimation::Part(
75      InstantController::kAutoCommitFadeInTimeMS, ui::Tween::EASE_IN));
76  ui::MultiAnimation* animation = new ui::MultiAnimation(parts);
77  animation->set_delegate(this);
78  animation->set_continuous(false);
79  return animation;
80}
81
82void SuggestedTextView::UpdateBackgroundColor() {
83  if (!animation_.get()) {
84    // Background only painted while animating.
85    return;
86  }
87
88  double value = animation_->GetCurrentValue();
89  SkColor bg_color = LocationBarView::GetColor(ToolbarModel::NONE,
90                                               LocationBarView::BACKGROUND);
91#if defined(OS_WIN)
92  SkColor s_color = color_utils::GetSysSkColor(COLOR_HIGHLIGHT);
93#else
94  // TODO(sky): fix me.
95  NOTIMPLEMENTED();
96  SkColor s_color = SK_ColorLTGRAY;
97#endif
98  bg_color_ = color_utils::AlphaBlend(s_color, bg_color,
99                                      ui::Tween::ValueBetween(value, 0, 255));
100}
101