Home | History | Annotate | Download | only in infobars
      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/browser/ui/views/infobars/translate_infobar_base.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "chrome/browser/translate/translate_infobar_delegate.h"
      9 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h"
     10 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h"
     11 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h"
     12 #include "grit/theme_resources.h"
     13 #include "ui/base/animation/slide_animation.h"
     14 #include "ui/base/resource/resource_bundle.h"
     15 #include "ui/gfx/canvas.h"
     16 #include "ui/views/controls/button/menu_button.h"
     17 #include "ui/views/controls/label.h"
     18 
     19 
     20 // TranslateInfoBarDelegate ---------------------------------------------------
     21 
     22 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) {
     23   if (infobar_type_ == BEFORE_TRANSLATE)
     24     return new BeforeTranslateInfoBar(owner, this);
     25   if (infobar_type_ == AFTER_TRANSLATE)
     26     return new AfterTranslateInfoBar(owner, this);
     27   return new TranslateMessageInfoBar(owner, this);
     28 }
     29 
     30 
     31 // TranslateInfoBarBase -------------------------------------------------------
     32 
     33 // static
     34 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5;
     35 
     36 void TranslateInfoBarBase::UpdateLanguageButtonText(views::MenuButton* button,
     37                                                     const string16& text) {
     38   DCHECK(button);
     39   button->SetText(text);
     40   // The button may have to grow to show the new text.
     41   Layout();
     42   SchedulePaint();
     43 }
     44 
     45 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarService* owner,
     46                                            TranslateInfoBarDelegate* delegate)
     47     : InfoBarView(owner, delegate),
     48       error_background_(InfoBarDelegate::WARNING_TYPE) {
     49 }
     50 
     51 TranslateInfoBarBase::~TranslateInfoBarBase() {
     52 }
     53 
     54 void TranslateInfoBarBase::ViewHierarchyChanged(
     55     const ViewHierarchyChangedDetails& details) {
     56   if (details.is_add && (details.child == this) &&
     57       (background_color_animation_ == NULL)) {
     58     background_color_animation_.reset(new ui::SlideAnimation(this));
     59     background_color_animation_->SetTweenType(ui::Tween::LINEAR);
     60     background_color_animation_->SetSlideDuration(500);
     61     TranslateInfoBarDelegate::BackgroundAnimationType animation =
     62         GetDelegate()->background_animation_type();
     63     if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) {
     64       background_color_animation_->Show();
     65     } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) {
     66       // Hide() runs the animation in reverse.
     67       background_color_animation_->Reset(1.0);
     68       background_color_animation_->Hide();
     69     }
     70   }
     71 
     72   // This must happen after adding all other children so InfoBarView can ensure
     73   // the close button is the last child.
     74   InfoBarView::ViewHierarchyChanged(details);
     75 }
     76 
     77 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() {
     78   return delegate()->AsTranslateInfoBarDelegate();
     79 }
     80 
     81 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) {
     82   // We need to set the separator color for |error_background_| like
     83   // InfoBarView::Layout() does for the normal background.
     84   const InfoBarContainer::Delegate* delegate = container_delegate();
     85   if (delegate)
     86     error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor());
     87 
     88   // If we're not animating, simply paint the background for the current state.
     89   if (!background_color_animation_->is_animating()) {
     90     GetBackground().Paint(canvas, this);
     91     return;
     92   }
     93 
     94   FadeBackground(canvas, 1.0 - background_color_animation_->GetCurrentValue(),
     95                  *background());
     96   FadeBackground(canvas, background_color_animation_->GetCurrentValue(),
     97                  error_background_);
     98 }
     99 
    100 void TranslateInfoBarBase::AnimationProgressed(const ui::Animation* animation) {
    101   if (animation == background_color_animation_.get())
    102     SchedulePaint();  // That'll trigger a PaintBackgroud.
    103   else
    104     InfoBarView::AnimationProgressed(animation);
    105 }
    106 
    107 const views::Background& TranslateInfoBarBase::GetBackground() {
    108   return GetDelegate()->is_error() ? error_background_ : *background();
    109 }
    110 
    111 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas,
    112                                           double animation_value,
    113                                           const views::Background& background) {
    114   // Draw the background into an offscreen buffer with alpha value per animation
    115   // value, then blend it back into the current canvas.
    116   canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255));
    117   background.Paint(canvas, this);
    118   canvas->Restore();
    119 }
    120