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

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

Issue 2602003: Refactored the translate infobars. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Synced Created 10 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/views/infobars/translate_infobar_base.h"
6
7 #include "app/resource_bundle.h"
8 #include "app/slide_animation.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/translate/translate_infobar_delegate2.h"
11 #include "chrome/browser/views/infobars/after_translate_infobar.h"
12 #include "chrome/browser/views/infobars/before_translate_infobar.h"
13 #include "chrome/browser/views/infobars/translate_message_infobar.h"
14 #include "chrome/browser/views/infobars/infobar_button_border.h"
15 #include "gfx/canvas.h"
16 #include "grit/app_resources.h"
17 #include "views/controls/button/menu_button.h"
18 #include "views/controls/image_view.h"
19
20 TranslateInfoBarBase::TranslateInfoBarBase(
21 TranslateInfoBarDelegate2* delegate)
22 : InfoBar(delegate),
23 normal_background_(InfoBarDelegate::PAGE_ACTION_TYPE),
24 error_background_(InfoBarDelegate::ERROR_TYPE) {
25 icon_ = new views::ImageView;
26 SkBitmap* image = delegate->GetIcon();
27 if (image)
28 icon_->SetImage(image);
29 AddChildView(icon_);
30
31 TranslateInfoBarDelegate2::BackgroundAnimationType animation =
32 delegate->background_animation_type();
33 if (animation != TranslateInfoBarDelegate2::NONE) {
34 background_color_animation_.reset(new SlideAnimation(this));
35 background_color_animation_->SetTweenType(Tween::LINEAR);
36 background_color_animation_->SetSlideDuration(500);
37 if (animation == TranslateInfoBarDelegate2::NORMAL_TO_ERROR) {
38 background_color_animation_->Show();
39 } else {
40 DCHECK_EQ(TranslateInfoBarDelegate2::ERROR_TO_NORMAL, animation);
41 // Hide() runs the animation in reverse.
42 background_color_animation_->Reset(1.0);
43 background_color_animation_->Hide();
44 }
45 }
46 }
47
48 TranslateInfoBarBase::~TranslateInfoBarBase() {
49 }
50
51 // Overridden from views::View:
52 void TranslateInfoBarBase::Layout() {
53 // Layout the close button.
54 InfoBar::Layout();
55
56 // Layout the icon on left of bar.
57 gfx::Size icon_ps = icon_->GetPreferredSize();
58 icon_->SetBounds(InfoBar::kHorizontalPadding, InfoBar::OffsetY(this, icon_ps),
59 icon_ps.width(), icon_ps.height());
60 }
61
62 views::Label* TranslateInfoBarBase::CreateLabel(const string16& text) {
63 views::Label* label = new views::Label(UTF16ToWideHack(text),
64 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont));
65 label->SetColor(SK_ColorBLACK);
66 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
67 return label;
68 }
69
70 void TranslateInfoBarBase::PaintBackground(gfx::Canvas* canvas) {
71 // If we're not animating, simply paint the background for the current state.
72 if (background_color_animation_ == NULL ||
73 !background_color_animation_->is_animating()) {
74 GetBackground().Paint(canvas, this);
75 return;
76 }
77
78 FadeBackground(canvas, 1.0 - background_color_animation_->GetCurrentValue(),
79 normal_background_);
80 FadeBackground(canvas, background_color_animation_->GetCurrentValue(),
81 error_background_);
82 }
83
84 void TranslateInfoBarBase::AnimationProgressed(const Animation* animation) {
85 if (background_color_animation_.get() == animation)
86 SchedulePaint(); // That'll trigger a PaintBackgroud.
87 else
88 InfoBar::AnimationProgressed(animation);
89 }
90
91 views::MenuButton* TranslateInfoBarBase::CreateMenuButton(
92 const string16& text,
93 bool normal_has_border,
94 views::ViewMenuDelegate* menu_delegate) {
95 // Don't use text to instantiate MenuButton because we need to set font before
96 // setting text so that the button will resize to fit the entire text.
97 views::MenuButton* menu_button =
98 new views::MenuButton(NULL, std::wstring(), menu_delegate, true);
99 menu_button->set_border(new InfoBarButtonBorder);
100 menu_button->set_menu_marker(ResourceBundle::GetSharedInstance().
101 GetBitmapNamed(IDR_INFOBARBUTTON_MENU_DROPARROW));
102 if (normal_has_border) {
103 menu_button->SetNormalHasBorder(true); // Normal button state has border.
104 // Disable animation during state change.
105 menu_button->SetAnimationDuration(0);
106 }
107 // Set font colors for different states.
108 menu_button->SetEnabledColor(SK_ColorBLACK);
109 menu_button->SetHighlightColor(SK_ColorBLACK);
110 menu_button->SetHoverColor(SK_ColorBLACK);
111
112 // Set font then text, then size button to fit text.
113 menu_button->SetFont(ResourceBundle::GetSharedInstance().GetFont(
114 ResourceBundle::MediumFont));
115 menu_button->SetText(UTF16ToWideHack(text));
116 menu_button->ClearMaxTextSize();
117 menu_button->SizeToPreferredSize();
118 return menu_button;
119 }
120
121 gfx::Point TranslateInfoBarBase::DetermineMenuPosition(
122 views::MenuButton* menu_button) {
123 gfx::Rect lb = menu_button->GetLocalBounds(true);
124 gfx::Point menu_position(lb.origin());
125 menu_position.Offset(2, lb.height() - 3);
126 if (base::i18n::IsRTL())
127 menu_position.Offset(lb.width() - 4, 0);
128
129 View::ConvertPointToScreen(menu_button, &menu_position);
130 #if defined(OS_WIN)
131 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
132 if (menu_position.x() < left_bound)
133 menu_position.set_x(left_bound);
134 #endif
135 return menu_position;
136 }
137
138 TranslateInfoBarDelegate2* TranslateInfoBarBase::GetDelegate() const {
139 return static_cast<TranslateInfoBarDelegate2*>(delegate());
140 }
141
142 const InfoBarBackground& TranslateInfoBarBase::GetBackground() const {
143 return GetDelegate()->IsError() ? error_background_ : normal_background_;
144 }
145
146 void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas,
147 double animation_value,
148 const InfoBarBackground& background) {
149 // Draw the background into an offscreen buffer with alpha value per animation
150 // value, then blend it back into the current canvas.
151 canvas->saveLayerAlpha(NULL, static_cast<int>(animation_value * 255),
152 SkCanvas::kARGB_NoClipLayer_SaveFlag);
153 canvas->drawARGB(0, 255, 255, 255, SkXfermode::kClear_Mode);
154 background.Paint(canvas, this);
155 canvas->restore();
156 }
157
158 // TranslateInfoBarDelegate views specific method:
159 InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() {
160 TranslateInfoBarBase* infobar = NULL;
161 switch (type_) {
162 case BEFORE_TRANSLATE:
163 infobar = new BeforeTranslateInfoBar(this);
164 break;
165 case AFTER_TRANSLATE:
166 infobar = new AfterTranslateInfoBar(this);
167 break;
168 case TRANSLATING:
169 case TRANSLATION_ERROR:
170 infobar = new TranslateMessageInfoBar(this);
171 break;
172 default:
173 NOTREACHED();
174 }
175 // Set |infobar_view_| so that the model can notify the infobar when it
176 // changes.
177 infobar_view_ = infobar;
178 return infobar;
179 }
OLDNEW
« no previous file with comments | « chrome/browser/views/infobars/translate_infobar_base.h ('k') | chrome/browser/views/infobars/translate_infobars.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698