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