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 "chrome/browser/ui/views/infobars/translate_infobar_base.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/infobars/infobar.h"
9 #include "chrome/browser/translate/translate_infobar_delegate.h"
10 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h"
11 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h"
12 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/animation/slide_animation.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/views/controls/button/menu_button.h"
18 #include "ui/views/controls/label.h"
21 // TranslateInfoBarDelegate ---------------------------------------------------
24 scoped_ptr
<InfoBar
> TranslateInfoBarDelegate::CreateInfoBar(
25 scoped_ptr
<TranslateInfoBarDelegate
> delegate
) {
26 if (delegate
->infobar_type() == BEFORE_TRANSLATE
)
27 return scoped_ptr
<InfoBar
>(new BeforeTranslateInfoBar(delegate
.Pass()));
28 if (delegate
->infobar_type() == AFTER_TRANSLATE
)
29 return scoped_ptr
<InfoBar
>(new AfterTranslateInfoBar(delegate
.Pass()));
30 return scoped_ptr
<InfoBar
>(new TranslateMessageInfoBar(delegate
.Pass()));
34 // TranslateInfoBarBase -------------------------------------------------------
37 const int TranslateInfoBarBase::kButtonInLabelSpacing
= 5;
39 void TranslateInfoBarBase::UpdateLanguageButtonText(
40 views::MenuButton
* button
,
41 const base::string16
& text
) {
43 button
->SetText(text
);
44 button
->ClearMaxTextSize();
45 button
->SizeToPreferredSize();
50 TranslateInfoBarBase::TranslateInfoBarBase(
51 scoped_ptr
<TranslateInfoBarDelegate
> delegate
)
52 : InfoBarView(delegate
.PassAs
<InfoBarDelegate
>()),
53 error_background_(InfoBarDelegate::WARNING_TYPE
) {
56 TranslateInfoBarBase::~TranslateInfoBarBase() {
59 void TranslateInfoBarBase::ViewHierarchyChanged(
60 const ViewHierarchyChangedDetails
& details
) {
61 if (details
.is_add
&& (details
.child
== this) &&
62 (background_color_animation_
== NULL
)) {
63 background_color_animation_
.reset(new gfx::SlideAnimation(this));
64 background_color_animation_
->SetTweenType(gfx::Tween::LINEAR
);
65 background_color_animation_
->SetSlideDuration(500);
66 TranslateInfoBarDelegate::BackgroundAnimationType animation
=
67 GetDelegate()->background_animation_type();
68 if (animation
== TranslateInfoBarDelegate::NORMAL_TO_ERROR
) {
69 background_color_animation_
->Show();
70 } else if (animation
== TranslateInfoBarDelegate::ERROR_TO_NORMAL
) {
71 // Hide() runs the animation in reverse.
72 background_color_animation_
->Reset(1.0);
73 background_color_animation_
->Hide();
77 // This must happen after adding all other children so InfoBarView can ensure
78 // the close button is the last child.
79 InfoBarView::ViewHierarchyChanged(details
);
82 TranslateInfoBarDelegate
* TranslateInfoBarBase::GetDelegate() {
83 return delegate()->AsTranslateInfoBarDelegate();
86 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas
* canvas
) {
87 // We need to set the separator color for |error_background_| like
88 // InfoBarView::Layout() does for the normal background.
89 const InfoBarContainer::Delegate
* delegate
= container_delegate();
91 error_background_
.set_separator_color(delegate
->GetInfoBarSeparatorColor());
93 // If we're not animating, simply paint the background for the current state.
94 if (!background_color_animation_
->is_animating()) {
95 GetBackground().Paint(canvas
, this);
99 FadeBackground(canvas
, 1.0 - background_color_animation_
->GetCurrentValue(),
101 FadeBackground(canvas
, background_color_animation_
->GetCurrentValue(),
105 void TranslateInfoBarBase::AnimationProgressed(
106 const gfx::Animation
* animation
) {
107 if (animation
== background_color_animation_
.get())
108 SchedulePaint(); // That'll trigger a PaintBackgroud.
110 InfoBarView::AnimationProgressed(animation
);
113 const views::Background
& TranslateInfoBarBase::GetBackground() {
114 return GetDelegate()->is_error() ? error_background_
: *background();
117 void TranslateInfoBarBase::FadeBackground(gfx::Canvas
* canvas
,
118 double animation_value
,
119 const views::Background
& background
) {
120 // Draw the background into an offscreen buffer with alpha value per animation
121 // value, then blend it back into the current canvas.
122 canvas
->SaveLayerAlpha(static_cast<int>(animation_value
* 255));
123 background
.Paint(canvas
, this);