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