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/infobar_view.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/ui/views/infobars/infobar_background.h"
12 #include "components/infobars/core/infobar_delegate.h"
13 #include "grit/generated_resources.h"
14 #include "grit/theme_resources.h"
15 #include "grit/ui_resources.h"
16 #include "third_party/skia/include/effects/SkGradientShader.h"
17 #include "ui/accessibility/ax_view_state.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/views/controls/button/image_button.h"
23 #include "ui/views/controls/button/label_button.h"
24 #include "ui/views/controls/button/label_button_border.h"
25 #include "ui/views/controls/button/menu_button.h"
26 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/link.h"
29 #include "ui/views/controls/menu/menu_runner.h"
30 #include "ui/views/layout/layout_constants.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/window/non_client_view.h"
35 // Helpers --------------------------------------------------------------------
39 const int kEdgeItemPadding = views::kRelatedControlHorizontalSpacing;
40 const int kIconToLabelSpacing = views::kRelatedControlHorizontalSpacing;
41 const int kBeforeCloseButtonSpacing = views::kUnrelatedControlHorizontalSpacing;
43 bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) {
44 return label_1->GetPreferredSize().width() >
45 label_2->GetPreferredSize().width();
51 // InfoBar --------------------------------------------------------------------
54 const int infobars::InfoBar::kSeparatorLineHeight =
55 views::NonClientFrameView::kClientEdgeThickness;
56 const int infobars::InfoBar::kDefaultArrowTargetHeight = 9;
57 const int infobars::InfoBar::kMaximumArrowTargetHeight = 24;
58 const int infobars::InfoBar::kDefaultArrowTargetHalfWidth =
59 kDefaultArrowTargetHeight;
60 const int infobars::InfoBar::kMaximumArrowTargetHalfWidth = 14;
61 const int infobars::InfoBar::kDefaultBarTargetHeight = 36;
63 // InfoBarView ----------------------------------------------------------------
66 const int InfoBarView::kButtonButtonSpacing = views::kRelatedButtonHSpacing;
67 const int InfoBarView::kEndOfLabelSpacing = views::kItemLabelSpacing;
69 InfoBarView::InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate)
70 : infobars::InfoBar(delegate.Pass()),
71 views::ExternalFocusTracker(this, NULL),
74 set_owned_by_client(); // InfoBar deletes itself at the appropriate time.
76 new InfoBarBackground(infobars::InfoBar::delegate()->GetInfoBarType()));
79 InfoBarView::~InfoBarView() {
80 // We should have closed any open menus in PlatformSpecificHide(), then
81 // subclasses' RunMenu() functions should have prevented opening any new ones
82 // once we became unowned.
83 DCHECK(!menu_runner_.get());
86 views::Label* InfoBarView::CreateLabel(const base::string16& text) const {
87 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
88 views::Label* label = new views::Label(
89 text, rb.GetFontList(ui::ResourceBundle::MediumFont));
90 label->SizeToPreferredSize();
91 label->SetBackgroundColor(background()->get_color());
92 label->SetEnabledColor(SK_ColorBLACK);
93 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
97 views::Link* InfoBarView::CreateLink(const base::string16& text,
98 views::LinkListener* listener) const {
99 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
100 views::Link* link = new views::Link(text);
101 link->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
102 link->SizeToPreferredSize();
103 link->SetHorizontalAlignment(gfx::ALIGN_LEFT);
104 link->set_listener(listener);
105 link->SetBackgroundColor(background()->get_color());
110 views::MenuButton* InfoBarView::CreateMenuButton(
111 const base::string16& text,
112 views::MenuButtonListener* menu_button_listener) {
113 scoped_ptr<views::TextButtonDefaultBorder> menu_button_border(
114 new views::TextButtonDefaultBorder());
115 const int kNormalImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_NORMAL);
116 menu_button_border->set_normal_painter(
117 views::Painter::CreateImageGridPainter(kNormalImageSet));
118 const int kHotImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_HOVER);
119 menu_button_border->set_hot_painter(
120 views::Painter::CreateImageGridPainter(kHotImageSet));
121 const int kPushedImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_PRESSED);
122 menu_button_border->set_pushed_painter(
123 views::Painter::CreateImageGridPainter(kPushedImageSet));
125 views::MenuButton* menu_button = new views::MenuButton(
126 NULL, text, menu_button_listener, true);
127 menu_button->SetBorder(menu_button_border.PassAs<views::Border>());
128 menu_button->set_animate_on_state_change(false);
129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
130 menu_button->set_menu_marker(
131 rb.GetImageNamed(IDR_INFOBARBUTTON_MENU_DROPARROW).ToImageSkia());
132 menu_button->SetEnabledColor(SK_ColorBLACK);
133 menu_button->SetHoverColor(SK_ColorBLACK);
134 menu_button->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
135 menu_button->SizeToPreferredSize();
136 menu_button->SetFocusable(true);
141 views::LabelButton* InfoBarView::CreateLabelButton(
142 views::ButtonListener* listener,
143 const base::string16& text) {
144 scoped_ptr<views::LabelButtonBorder> label_button_border(
145 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON));
146 const int kNormalImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_NORMAL);
147 label_button_border->SetPainter(
148 false, views::Button::STATE_NORMAL,
149 views::Painter::CreateImageGridPainter(kNormalImageSet));
150 const int kHoveredImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_HOVER);
151 label_button_border->SetPainter(
152 false, views::Button::STATE_HOVERED,
153 views::Painter::CreateImageGridPainter(kHoveredImageSet));
154 const int kPressedImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_PRESSED);
155 label_button_border->SetPainter(
156 false, views::Button::STATE_PRESSED,
157 views::Painter::CreateImageGridPainter(kPressedImageSet));
159 views::LabelButton* label_button = new views::LabelButton(listener, text);
160 label_button->SetBorder(label_button_border.PassAs<views::Border>());
161 label_button->set_animate_on_state_change(false);
162 label_button->SetTextColor(views::Button::STATE_NORMAL, SK_ColorBLACK);
163 label_button->SetTextColor(views::Button::STATE_HOVERED, SK_ColorBLACK);
164 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
165 label_button->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
166 label_button->SizeToPreferredSize();
167 label_button->SetFocusable(true);
172 void InfoBarView::AssignWidths(Labels* labels, int available_width) {
173 std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth);
174 AssignWidthsSorted(labels, available_width);
177 void InfoBarView::Layout() {
178 // Calculate the fill and stroke paths. We do this here, rather than in
179 // PlatformSpecificRecalculateHeight(), because this is also reached when our
180 // width is changed, which affects both paths.
181 stroke_path_.rewind();
183 const infobars::InfoBarContainer::Delegate* delegate = container_delegate();
185 static_cast<InfoBarBackground*>(background())->set_separator_color(
186 delegate->GetInfoBarSeparatorColor());
188 SkScalar arrow_fill_height =
189 SkIntToScalar(std::max(arrow_height() - kSeparatorLineHeight, 0));
190 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width());
191 SkScalar separator_height = SkIntToScalar(kSeparatorLineHeight);
192 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) {
193 // Skia pixel centers are at the half-values, so the arrow is horizontally
194 // centered at |arrow_x| + 0.5. Vertically, the stroke path is the center
195 // of the separator, while the fill path is a closed path that extends up
196 // through the entire height of the separator and down to the bottom of
197 // the arrow where it joins the bar.
199 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width,
200 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf));
201 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height);
202 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height);
204 fill_path_ = stroke_path_;
205 // Move the top of the fill path up to the top of the separator and then
206 // extend it down all the way through.
207 fill_path_.offset(0, -separator_height * SK_ScalarHalf);
208 // This 0.01 hack prevents the fill from filling more pixels on the right
209 // edge of the arrow than on the left.
210 const SkScalar epsilon = 0.01f;
211 fill_path_.rLineTo(-epsilon, 0);
212 fill_path_.rLineTo(0, separator_height);
213 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0);
218 fill_path_.addRect(0.0, SkIntToScalar(arrow_height()),
219 SkIntToScalar(width()), SkIntToScalar(height() - kSeparatorLineHeight));
222 int start_x = kEdgeItemPadding;
224 icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_)));
225 start_x = icon_->bounds().right() + kIconToLabelSpacing;
228 int content_minimum_width = ContentMinimumWidth();
229 close_button_->SetPosition(gfx::Point(
231 start_x + content_minimum_width +
232 ((content_minimum_width > 0) ? kBeforeCloseButtonSpacing : 0),
233 width() - kEdgeItemPadding - close_button_->width()),
234 OffsetY(close_button_)));
237 void InfoBarView::ViewHierarchyChanged(
238 const ViewHierarchyChangedDetails& details) {
239 View::ViewHierarchyChanged(details);
241 if (details.is_add && (details.child == this) && (close_button_ == NULL)) {
242 gfx::Image image = delegate()->GetIcon();
243 if (!image.IsEmpty()) {
244 icon_ = new views::ImageView;
245 icon_->SetImage(image.ToImageSkia());
246 icon_->SizeToPreferredSize();
250 close_button_ = new views::ImageButton(this);
251 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
252 close_button_->SetImage(views::CustomButton::STATE_NORMAL,
253 rb.GetImageNamed(IDR_CLOSE_1).ToImageSkia());
254 close_button_->SetImage(views::CustomButton::STATE_HOVERED,
255 rb.GetImageNamed(IDR_CLOSE_1_H).ToImageSkia());
256 close_button_->SetImage(views::CustomButton::STATE_PRESSED,
257 rb.GetImageNamed(IDR_CLOSE_1_P).ToImageSkia());
258 close_button_->SizeToPreferredSize();
259 close_button_->SetAccessibleName(
260 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE));
261 close_button_->SetFocusable(true);
262 AddChildView(close_button_);
263 } else if ((close_button_ != NULL) && (details.parent == this) &&
264 (details.child != close_button_) && (close_button_->parent() == this) &&
265 (child_at(child_count() - 1) != close_button_)) {
266 // For accessibility, ensure the close button is the last child view.
267 RemoveChildView(close_button_);
268 AddChildView(close_button_);
271 // Ensure the infobar is tall enough to display its contents.
272 const int kMinimumVerticalPadding = 6;
273 int height = kDefaultBarTargetHeight;
274 for (int i = 0; i < child_count(); ++i) {
275 const int child_height = child_at(i)->height();
276 height = std::max(height, child_height + kMinimumVerticalPadding);
278 SetBarTargetHeight(height);
281 void InfoBarView::PaintChildren(gfx::Canvas* canvas) {
284 // TODO(scr): This really should be the |fill_path_|, but the clipPath seems
285 // broken on non-Windows platforms (crbug.com/75154). For now, just clip to
288 // canvas->sk_canvas()->clipPath(fill_path_);
289 DCHECK_EQ(total_height(), height())
290 << "Infobar piecewise heights do not match overall height";
291 canvas->ClipRect(gfx::Rect(0, arrow_height(), width(), bar_height()));
292 views::View::PaintChildren(canvas);
296 void InfoBarView::ButtonPressed(views::Button* sender,
297 const ui::Event& event) {
299 return; // We're closing; don't call anything, it might access the owner.
300 if (sender == close_button_) {
301 delegate()->InfoBarDismissed();
306 int InfoBarView::ContentMinimumWidth() {
310 int InfoBarView::StartX() const {
311 // Ensure we don't return a value greater than EndX(), so children can safely
312 // set something's width to "EndX() - StartX()" without risking that being
314 return std::min(EndX(), (icon_ != NULL) ?
315 (icon_->bounds().right() + kIconToLabelSpacing) : kEdgeItemPadding);
318 int InfoBarView::EndX() const {
319 return close_button_->x() - kBeforeCloseButtonSpacing;
322 int InfoBarView::OffsetY(views::View* view) const {
323 return arrow_height() +
324 std::max((bar_target_height() - view->height()) / 2, 0) -
325 (bar_target_height() - bar_height());
328 const infobars::InfoBarContainer::Delegate* InfoBarView::container_delegate()
330 const infobars::InfoBarContainer* infobar_container = container();
331 return infobar_container ? infobar_container->delegate() : NULL;
334 void InfoBarView::RunMenuAt(ui::MenuModel* menu_model,
335 views::MenuButton* button,
336 views::MenuAnchorPosition anchor) {
337 DCHECK(owner()); // We'd better not open any menus while we're closing.
338 gfx::Point screen_point;
339 views::View::ConvertPointToScreen(button, &screen_point);
340 menu_runner_.reset(new views::MenuRunner(menu_model));
341 // Ignore the result since we don't need to handle a deleted menu specially.
342 ignore_result(menu_runner_->RunMenuAt(
343 GetWidget(), button, gfx::Rect(screen_point, button->size()), anchor,
344 ui::MENU_SOURCE_NONE, views::MenuRunner::HAS_MNEMONICS));
348 void InfoBarView::AssignWidthsSorted(Labels* labels, int available_width) {
351 gfx::Size back_label_size(labels->back()->GetPreferredSize());
352 back_label_size.set_width(
353 std::min(back_label_size.width(),
354 available_width / static_cast<int>(labels->size())));
355 labels->back()->SetSize(back_label_size);
357 AssignWidthsSorted(labels, available_width - back_label_size.width());
360 void InfoBarView::PlatformSpecificShow(bool animate) {
361 // If we gain focus, we want to restore it to the previously-focused element
362 // when we're hidden. So when we're in a Widget, create a focus tracker so
363 // that if we gain focus we'll know what the previously-focused element was.
364 SetFocusManager(GetFocusManager());
366 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
369 void InfoBarView::PlatformSpecificHide(bool animate) {
370 // Cancel any menus we may have open. It doesn't make sense to leave them
371 // open while we're hidden, and if we're going to become unowned, we can't
372 // allow the user to choose any options and potentially call functions that
373 // try to access the owner.
374 menu_runner_.reset();
376 // It's possible to be called twice (once with |animate| true and once with it
377 // false); in this case the second SetFocusManager() call will silently no-op.
378 SetFocusManager(NULL);
383 // Do not restore focus (and active state with it) if some other top-level
384 // window became active.
385 views::Widget* widget = GetWidget();
386 if (!widget || widget->IsActive())
387 FocusLastFocusedExternalView();
390 void InfoBarView::PlatformSpecificOnHeightsRecalculated() {
391 // Ensure that notifying our container of our size change will result in a
396 void InfoBarView::GetAccessibleState(ui::AXViewState* state) {
397 state->name = l10n_util::GetStringUTF16(
398 (delegate()->GetInfoBarType() ==
399 infobars::InfoBarDelegate::WARNING_TYPE) ?
400 IDS_ACCNAME_INFOBAR_WARNING : IDS_ACCNAME_INFOBAR_PAGE_ACTION);
401 state->role = ui::AX_ROLE_ALERT;
402 state->keyboard_shortcut = base::ASCIIToUTF16("Alt+Shift+A");
405 gfx::Size InfoBarView::GetPreferredSize() {
407 kEdgeItemPadding + (icon_ ? (icon_->width() + kIconToLabelSpacing) : 0) +
408 ContentMinimumWidth() + kBeforeCloseButtonSpacing +
409 close_button_->width() + kEdgeItemPadding,
413 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) {
414 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now);
416 // This will trigger some screen readers to read the entire contents of this
418 if (focused_before && focused_now && !Contains(focused_before) &&
419 Contains(focused_now)) {
420 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);