Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: chrome/browser/ui/views/infobars/translate_infobar_base.cc

Issue 10952016: Simplify the translate infobar code some. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/infobars/translate_infobar_base.h" 5 #include "chrome/browser/ui/views/infobars/translate_infobar_base.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/infobars/infobar_tab_helper.h" 8 #include "chrome/browser/infobars/infobar_tab_helper.h"
9 #include "chrome/browser/translate/translate_infobar_delegate.h" 9 #include "chrome/browser/translate/translate_infobar_delegate.h"
10 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h" 10 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h"
11 #include "chrome/browser/ui/views/infobars/before_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" 12 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h"
13 #include "grit/theme_resources.h" 13 #include "grit/theme_resources.h"
14 #include "ui/base/animation/slide_animation.h" 14 #include "ui/base/animation/slide_animation.h"
15 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
17 #include "ui/views/controls/button/menu_button.h" 17 #include "ui/views/controls/button/menu_button.h"
18 #include "ui/views/controls/label.h" 18 #include "ui/views/controls/label.h"
19 19
20 // TranslateInfoBarDelegate --------------------------------------------------- 20 // TranslateInfoBarDelegate ---------------------------------------------------
21 21
22 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { 22 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) {
23 InfoBarTabHelper* helper = static_cast<InfoBarTabHelper*>(owner); 23 InfoBarTabHelper* helper = static_cast<InfoBarTabHelper*>(owner);
24 TranslateInfoBarBase* infobar = NULL; 24 if (type_ == BEFORE_TRANSLATE)
25 switch (type_) { 25 return new BeforeTranslateInfoBar(helper, this);
26 case BEFORE_TRANSLATE: 26 if (type_ == AFTER_TRANSLATE)
27 infobar = new BeforeTranslateInfoBar(helper, this); 27 return new AfterTranslateInfoBar(helper, this);
28 break; 28 return new TranslateMessageInfoBar(helper, this);
29 case AFTER_TRANSLATE:
30 infobar = new AfterTranslateInfoBar(helper, this);
31 break;
32 case TRANSLATING:
33 case TRANSLATION_ERROR:
34 infobar = new TranslateMessageInfoBar(helper, this);
35 break;
36 default:
37 NOTREACHED();
38 }
39 infobar_view_ = infobar;
40 return infobar;
41 } 29 }
42 30
43 // TranslateInfoBarBase ------------------------------------------------------- 31 // TranslateInfoBarBase -------------------------------------------------------
44 32
45 // static 33 // static
46 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5; 34 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5;
47 35
48 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarTabHelper* owner, 36 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarTabHelper* owner,
49 TranslateInfoBarDelegate* delegate) 37 TranslateInfoBarDelegate* delegate)
50 : InfoBarView(owner, delegate), 38 : InfoBarView(owner, delegate),
51 error_background_(InfoBarDelegate::WARNING_TYPE) { 39 error_background_(InfoBarDelegate::WARNING_TYPE) {
52 } 40 }
53 41
54 TranslateInfoBarBase::~TranslateInfoBarBase() { 42 TranslateInfoBarBase::~TranslateInfoBarBase() {
55 } 43 }
56 44
45 void TranslateInfoBarBase::UpdateLanguageButtonText(views::MenuButton* button,
46 const string16& text) {
47 DCHECK(button);
48 button->SetText(text);
49 // The button may have to grow to show the new text.
50 Layout();
51 SchedulePaint();
52 }
53
57 void TranslateInfoBarBase::ViewHierarchyChanged(bool is_add, 54 void TranslateInfoBarBase::ViewHierarchyChanged(bool is_add,
58 View* parent, 55 View* parent,
59 View* child) { 56 View* child) {
60 if (is_add && (child == this) && (background_color_animation_ == NULL)) { 57 if (is_add && (child == this) && (background_color_animation_ == NULL)) {
61 background_color_animation_.reset(new ui::SlideAnimation(this)); 58 background_color_animation_.reset(new ui::SlideAnimation(this));
62 background_color_animation_->SetTweenType(ui::Tween::LINEAR); 59 background_color_animation_->SetTweenType(ui::Tween::LINEAR);
63 background_color_animation_->SetSlideDuration(500); 60 background_color_animation_->SetSlideDuration(500);
64 TranslateInfoBarDelegate::BackgroundAnimationType animation = 61 TranslateInfoBarDelegate::BackgroundAnimationType animation =
65 GetDelegate()->background_animation_type(); 62 GetDelegate()->background_animation_type();
66 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { 63 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) {
67 background_color_animation_->Show(); 64 background_color_animation_->Show();
68 } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) { 65 } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) {
69 // Hide() runs the animation in reverse. 66 // Hide() runs the animation in reverse.
70 background_color_animation_->Reset(1.0); 67 background_color_animation_->Reset(1.0);
71 background_color_animation_->Hide(); 68 background_color_animation_->Hide();
72 } 69 }
73 } 70 }
74 71
75 // This must happen after adding all other children so InfoBarView can ensure 72 // This must happen after adding all other children so InfoBarView can ensure
76 // the close button is the last child. 73 // the close button is the last child.
77 InfoBarView::ViewHierarchyChanged(is_add, parent, child); 74 InfoBarView::ViewHierarchyChanged(is_add, parent, child);
78 } 75 }
79 76
80 void TranslateInfoBarBase::UpdateLanguageButtonText(
81 views::MenuButton* button,
82 LanguagesMenuModel::LanguageType language_type) {
83 DCHECK(button);
84 TranslateInfoBarDelegate* delegate = GetDelegate();
85 bool is_original = language_type == LanguagesMenuModel::ORIGINAL;
86 int index = is_original ? delegate->original_language_index()
87 : delegate->target_language_index();
88 button->SetText(delegate->GetLanguageDisplayableNameAt(index));
89 // The button may have to grow to show the new text.
90 Layout();
91 SchedulePaint();
92 }
93
94 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { 77 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() {
95 return delegate()->AsTranslateInfoBarDelegate(); 78 return delegate()->AsTranslateInfoBarDelegate();
96 } 79 }
97 80
98 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) { 81 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) {
99 // We need to set the separator color for |error_background_| like 82 // We need to set the separator color for |error_background_| like
100 // InfoBarView::Layout() does for the normal background. 83 // InfoBarView::Layout() does for the normal background.
101 const InfoBarContainer::Delegate* delegate = container_delegate(); 84 const InfoBarContainer::Delegate* delegate = container_delegate();
102 if (delegate) 85 if (delegate)
103 error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor()); 86 error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor());
(...skipping 24 matching lines...) Expand all
128 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas, 111 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas,
129 double animation_value, 112 double animation_value,
130 const views::Background& background) { 113 const views::Background& background) {
131 // Draw the background into an offscreen buffer with alpha value per animation 114 // Draw the background into an offscreen buffer with alpha value per animation
132 // value, then blend it back into the current canvas. 115 // value, then blend it back into the current canvas.
133 canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255)); 116 canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255));
134 canvas->sk_canvas()->drawARGB(0, 255, 255, 255, SkXfermode::kClear_Mode); 117 canvas->sk_canvas()->drawARGB(0, 255, 255, 255, SkXfermode::kClear_Mode);
135 background.Paint(canvas, this); 118 background.Paint(canvas, this);
136 canvas->Restore(); 119 canvas->Restore();
137 } 120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698