| OLD | NEW |
| 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.h" | 8 #include "chrome/browser/infobars/infobar.h" |
| 9 #include "chrome/browser/infobars/infobar_tab_helper.h" | 9 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 10 #include "chrome/browser/translate/translate_infobar_delegate.h" | 10 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 11 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h" | 11 #include "chrome/browser/ui/views/infobars/after_translate_infobar.h" |
| 12 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h" | 12 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h" |
| 13 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h" | 13 #include "chrome/browser/ui/views/infobars/translate_message_infobar.h" |
| 14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
| 15 #include "ui/base/animation/slide_animation.h" | 15 #include "ui/base/animation/slide_animation.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 18 #include "ui/views/controls/button/menu_button.h" | 18 #include "ui/views/controls/button/menu_button.h" |
| 19 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
| 20 | 20 |
| 21 // TranslateInfoBarDelegate --------------------------------------------------- | 21 // TranslateInfoBarDelegate --------------------------------------------------- |
| 22 | 22 |
| 23 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { | 23 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
| 24 InfoBarTabHelper* helper = static_cast<InfoBarTabHelper*>(owner); | 24 InfoBarTabHelper* helper = static_cast<InfoBarTabHelper*>(owner); |
| 25 TranslateInfoBarBase* infobar = NULL; | 25 if (type_ == BEFORE_TRANSLATE) |
| 26 switch (type_) { | 26 return new BeforeTranslateInfoBar(helper, this); |
| 27 case BEFORE_TRANSLATE: | 27 if (type_ == AFTER_TRANSLATE) |
| 28 infobar = new BeforeTranslateInfoBar(helper, this); | 28 return new AfterTranslateInfoBar(helper, this); |
| 29 break; | 29 return new TranslateMessageInfoBar(helper, this); |
| 30 case AFTER_TRANSLATE: | |
| 31 infobar = new AfterTranslateInfoBar(helper, this); | |
| 32 break; | |
| 33 case TRANSLATING: | |
| 34 case TRANSLATION_ERROR: | |
| 35 infobar = new TranslateMessageInfoBar(helper, this); | |
| 36 break; | |
| 37 default: | |
| 38 NOTREACHED(); | |
| 39 } | |
| 40 infobar_view_ = infobar; | |
| 41 return infobar; | |
| 42 } | 30 } |
| 43 | 31 |
| 44 // TranslateInfoBarBase ------------------------------------------------------- | 32 // TranslateInfoBarBase ------------------------------------------------------- |
| 45 | 33 |
| 46 // static | 34 // static |
| 47 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5; | 35 const int TranslateInfoBarBase::kButtonInLabelSpacing = 5; |
| 48 | 36 |
| 49 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarTabHelper* owner, | 37 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarTabHelper* owner, |
| 50 TranslateInfoBarDelegate* delegate) | 38 TranslateInfoBarDelegate* delegate) |
| 51 : InfoBarView(owner, delegate), | 39 : InfoBarView(owner, delegate), |
| 52 error_background_(GetInfoBarTopColor(InfoBarDelegate::WARNING_TYPE), | 40 error_background_(GetInfoBarTopColor(InfoBarDelegate::WARNING_TYPE), |
| 53 GetInfoBarBottomColor(InfoBarDelegate::WARNING_TYPE)) { | 41 GetInfoBarBottomColor(InfoBarDelegate::WARNING_TYPE)) { |
| 54 } | 42 } |
| 55 | 43 |
| 56 TranslateInfoBarBase::~TranslateInfoBarBase() { | 44 TranslateInfoBarBase::~TranslateInfoBarBase() { |
| 57 } | 45 } |
| 58 | 46 |
| 47 void TranslateInfoBarBase::UpdateLanguageButtonText(views::MenuButton* button, |
| 48 const string16& text) { |
| 49 DCHECK(button); |
| 50 button->SetText(text); |
| 51 // The button may have to grow to show the new text. |
| 52 Layout(); |
| 53 SchedulePaint(); |
| 54 } |
| 55 |
| 59 void TranslateInfoBarBase::ViewHierarchyChanged(bool is_add, | 56 void TranslateInfoBarBase::ViewHierarchyChanged(bool is_add, |
| 60 View* parent, | 57 View* parent, |
| 61 View* child) { | 58 View* child) { |
| 62 if (is_add && (child == this) && (background_color_animation_ == NULL)) { | 59 if (is_add && (child == this) && (background_color_animation_ == NULL)) { |
| 63 background_color_animation_.reset(new ui::SlideAnimation(this)); | 60 background_color_animation_.reset(new ui::SlideAnimation(this)); |
| 64 background_color_animation_->SetTweenType(ui::Tween::LINEAR); | 61 background_color_animation_->SetTweenType(ui::Tween::LINEAR); |
| 65 background_color_animation_->SetSlideDuration(500); | 62 background_color_animation_->SetSlideDuration(500); |
| 66 TranslateInfoBarDelegate::BackgroundAnimationType animation = | 63 TranslateInfoBarDelegate::BackgroundAnimationType animation = |
| 67 GetDelegate()->background_animation_type(); | 64 GetDelegate()->background_animation_type(); |
| 68 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { | 65 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { |
| 69 background_color_animation_->Show(); | 66 background_color_animation_->Show(); |
| 70 } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) { | 67 } else if (animation == TranslateInfoBarDelegate::ERROR_TO_NORMAL) { |
| 71 // Hide() runs the animation in reverse. | 68 // Hide() runs the animation in reverse. |
| 72 background_color_animation_->Reset(1.0); | 69 background_color_animation_->Reset(1.0); |
| 73 background_color_animation_->Hide(); | 70 background_color_animation_->Hide(); |
| 74 } | 71 } |
| 75 } | 72 } |
| 76 | 73 |
| 77 // This must happen after adding all other children so InfoBarView can ensure | 74 // This must happen after adding all other children so InfoBarView can ensure |
| 78 // the close button is the last child. | 75 // the close button is the last child. |
| 79 InfoBarView::ViewHierarchyChanged(is_add, parent, child); | 76 InfoBarView::ViewHierarchyChanged(is_add, parent, child); |
| 80 } | 77 } |
| 81 | 78 |
| 82 void TranslateInfoBarBase::UpdateLanguageButtonText( | |
| 83 views::MenuButton* button, | |
| 84 LanguagesMenuModel::LanguageType language_type) { | |
| 85 DCHECK(button); | |
| 86 TranslateInfoBarDelegate* delegate = GetDelegate(); | |
| 87 bool is_original = language_type == LanguagesMenuModel::ORIGINAL; | |
| 88 int index = is_original ? delegate->original_language_index() | |
| 89 : delegate->target_language_index(); | |
| 90 button->SetText(delegate->GetLanguageDisplayableNameAt(index)); | |
| 91 // The button may have to grow to show the new text. | |
| 92 Layout(); | |
| 93 SchedulePaint(); | |
| 94 } | |
| 95 | |
| 96 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { | 79 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { |
| 97 return delegate()->AsTranslateInfoBarDelegate(); | 80 return delegate()->AsTranslateInfoBarDelegate(); |
| 98 } | 81 } |
| 99 | 82 |
| 100 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) { | 83 void TranslateInfoBarBase::OnPaintBackground(gfx::Canvas* canvas) { |
| 101 // We need to set the separator color for |error_background_| like | 84 // We need to set the separator color for |error_background_| like |
| 102 // InfoBarView::Layout() does for the normal background. | 85 // InfoBarView::Layout() does for the normal background. |
| 103 const InfoBarContainer::Delegate* delegate = container_delegate(); | 86 const InfoBarContainer::Delegate* delegate = container_delegate(); |
| 104 if (delegate) | 87 if (delegate) |
| 105 error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor()); | 88 error_background_.set_separator_color(delegate->GetInfoBarSeparatorColor()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 130 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas, | 113 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas, |
| 131 double animation_value, | 114 double animation_value, |
| 132 const views::Background& background) { | 115 const views::Background& background) { |
| 133 // Draw the background into an offscreen buffer with alpha value per animation | 116 // Draw the background into an offscreen buffer with alpha value per animation |
| 134 // value, then blend it back into the current canvas. | 117 // value, then blend it back into the current canvas. |
| 135 canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255)); | 118 canvas->SaveLayerAlpha(static_cast<int>(animation_value * 255)); |
| 136 canvas->sk_canvas()->drawARGB(0, 255, 255, 255, SkXfermode::kClear_Mode); | 119 canvas->sk_canvas()->drawARGB(0, 255, 255, 255, SkXfermode::kClear_Mode); |
| 137 background.Paint(canvas, this); | 120 background.Paint(canvas, this); |
| 138 canvas->Restore(); | 121 canvas->Restore(); |
| 139 } | 122 } |
| OLD | NEW |