Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / infobars / infobar_view.cc
blob050092b5bf971a2cd19ce7c1c0e1d181992f76c2
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"
7 #include <algorithm>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/ui/infobar_container_delegate.h"
12 #include "chrome/browser/ui/views/infobars/infobar_background.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/infobars/core/infobar_delegate.h"
15 #include "grit/theme_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/material_design/material_design_controller.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/compositor/clip_transform_recorder.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/paint_vector_icon.h"
25 #include "ui/gfx/vector_icons_public.h"
26 #include "ui/native_theme/common_theme.h"
27 #include "ui/native_theme/native_theme.h"
28 #include "ui/resources/grit/ui_resources.h"
29 #include "ui/views/controls/button/image_button.h"
30 #include "ui/views/controls/button/label_button.h"
31 #include "ui/views/controls/button/label_button_border.h"
32 #include "ui/views/controls/button/menu_button.h"
33 #include "ui/views/controls/image_view.h"
34 #include "ui/views/controls/label.h"
35 #include "ui/views/controls/link.h"
36 #include "ui/views/controls/menu/menu_runner.h"
37 #include "ui/views/layout/layout_constants.h"
38 #include "ui/views/widget/widget.h"
39 #include "ui/views/window/non_client_view.h"
42 // Helpers --------------------------------------------------------------------
44 namespace {
46 const int kEdgeItemPadding = views::kRelatedControlHorizontalSpacing;
47 const int kIconToLabelSpacing = views::kRelatedControlHorizontalSpacing;
48 const int kBeforeCloseButtonSpacing = views::kUnrelatedControlHorizontalSpacing;
50 bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) {
51 return label_1->GetPreferredSize().width() >
52 label_2->GetPreferredSize().width();
55 const gfx::FontList& GetFontList() {
56 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
57 return rb.GetFontList(ui::MaterialDesignController::IsModeMaterial()
58 ? ui::ResourceBundle::BaseFont
59 : ui::ResourceBundle::MediumFont);
62 } // namespace
65 // InfoBarView ----------------------------------------------------------------
67 // static
68 const int InfoBarView::kButtonButtonSpacing = views::kRelatedButtonHSpacing;
69 const int InfoBarView::kEndOfLabelSpacing = views::kItemLabelSpacing;
71 InfoBarView::InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate)
72 : infobars::InfoBar(delegate.Pass()),
73 views::ExternalFocusTracker(this, NULL),
74 icon_(NULL),
75 close_button_(NULL) {
76 set_owned_by_client(); // InfoBar deletes itself at the appropriate time.
77 set_background(
78 new InfoBarBackground(infobars::InfoBar::delegate()->GetInfoBarType()));
81 InfoBarView::~InfoBarView() {
82 // We should have closed any open menus in PlatformSpecificHide(), then
83 // subclasses' RunMenu() functions should have prevented opening any new ones
84 // once we became unowned.
85 DCHECK(!menu_runner_.get());
88 views::Label* InfoBarView::CreateLabel(const base::string16& text) const {
89 views::Label* label = new views::Label(text, GetFontList());
90 label->SizeToPreferredSize();
91 label->SetBackgroundColor(background()->get_color());
92 label->SetEnabledColor(SK_ColorBLACK);
93 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
94 return label;
97 views::Link* InfoBarView::CreateLink(const base::string16& text,
98 views::LinkListener* listener) const {
99 views::Link* link = new views::Link(text);
100 link->SetFontList(GetFontList());
101 link->SizeToPreferredSize();
102 link->SetHorizontalAlignment(gfx::ALIGN_LEFT);
103 link->set_listener(listener);
104 link->SetBackgroundColor(background()->get_color());
105 return link;
108 // static
109 views::LabelButton* InfoBarView::CreateLabelButton(
110 views::ButtonListener* listener,
111 const base::string16& text) {
112 views::LabelButton* button = new views::LabelButton(listener, text);
113 if (ui::MaterialDesignController::IsModeMaterial()) {
114 button->SetStyle(views::Button::STYLE_BUTTON);
115 } else {
116 scoped_ptr<views::LabelButtonAssetBorder> button_border(
117 new views::LabelButtonAssetBorder(views::Button::STYLE_TEXTBUTTON));
118 const int kNormalImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_NORMAL);
119 button_border->SetPainter(
120 false, views::Button::STATE_NORMAL,
121 views::Painter::CreateImageGridPainter(kNormalImageSet));
122 const int kHoveredImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_HOVER);
123 button_border->SetPainter(
124 false, views::Button::STATE_HOVERED,
125 views::Painter::CreateImageGridPainter(kHoveredImageSet));
126 const int kPressedImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_PRESSED);
127 button_border->SetPainter(
128 false, views::Button::STATE_PRESSED,
129 views::Painter::CreateImageGridPainter(kPressedImageSet));
131 button->SetBorder(button_border.Pass());
132 button->set_animate_on_state_change(false);
133 button->SetTextColor(views::Button::STATE_NORMAL, SK_ColorBLACK);
134 button->SetTextColor(views::Button::STATE_HOVERED, SK_ColorBLACK);
135 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
136 button->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
138 button->SetFocusable(true);
139 return button;
142 // static
143 void InfoBarView::AssignWidths(Labels* labels, int available_width) {
144 std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth);
145 AssignWidthsSorted(labels, available_width);
148 void InfoBarView::Layout() {
149 // Calculate the fill and stroke paths. We do this here, rather than in
150 // PlatformSpecificRecalculateHeight(), because this is also reached when our
151 // width is changed, which affects both paths.
152 stroke_path_.rewind();
153 fill_path_.rewind();
154 const infobars::InfoBarContainer::Delegate* delegate = container_delegate();
155 if (delegate) {
156 static_cast<InfoBarBackground*>(background())->set_separator_color(
157 delegate->GetInfoBarSeparatorColor());
158 int arrow_x;
159 SkScalar arrow_fill_height = SkIntToScalar(std::max(
160 arrow_height() - InfoBarContainerDelegate::kSeparatorLineHeight, 0));
161 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width());
162 SkScalar separator_height =
163 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight);
164 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) {
165 // Skia pixel centers are at the half-values, so the arrow is horizontally
166 // centered at |arrow_x| + 0.5. Vertically, the stroke path is the center
167 // of the separator, while the fill path is a closed path that extends up
168 // through the entire height of the separator and down to the bottom of
169 // the arrow where it joins the bar.
170 stroke_path_.moveTo(
171 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width,
172 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf));
173 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height);
174 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height);
176 fill_path_ = stroke_path_;
177 // Move the top of the fill path up to the top of the separator and then
178 // extend it down all the way through.
179 fill_path_.offset(0, -separator_height * SK_ScalarHalf);
180 // This 0.01 hack prevents the fill from filling more pixels on the right
181 // edge of the arrow than on the left.
182 const SkScalar epsilon = 0.01f;
183 fill_path_.rLineTo(-epsilon, 0);
184 fill_path_.rLineTo(0, separator_height);
185 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0);
186 fill_path_.close();
189 if (bar_height()) {
190 fill_path_.addRect(
191 0.0, SkIntToScalar(arrow_height()), SkIntToScalar(width()),
192 SkIntToScalar(
193 height() - InfoBarContainerDelegate::kSeparatorLineHeight));
196 int start_x = kEdgeItemPadding;
197 if (icon_ != NULL) {
198 icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_)));
199 start_x = icon_->bounds().right() + kIconToLabelSpacing;
202 int content_minimum_width = ContentMinimumWidth();
203 close_button_->SetPosition(gfx::Point(
204 std::max(
205 start_x + content_minimum_width +
206 ((content_minimum_width > 0) ? kBeforeCloseButtonSpacing : 0),
207 width() - kEdgeItemPadding - close_button_->width()),
208 OffsetY(close_button_)));
211 void InfoBarView::ViewHierarchyChanged(
212 const ViewHierarchyChangedDetails& details) {
213 View::ViewHierarchyChanged(details);
215 if (details.is_add && (details.child == this) && (close_button_ == NULL)) {
216 gfx::Image image = delegate()->GetIcon();
217 if (!image.IsEmpty()) {
218 icon_ = new views::ImageView;
219 icon_->SetImage(image.ToImageSkia());
220 icon_->SizeToPreferredSize();
221 AddChildView(icon_);
224 close_button_ = new views::ImageButton(this);
226 if (ui::MaterialDesignController::IsModeMaterial()) {
227 SkColor grey;
228 ui::CommonThemeGetSystemColor(ui::NativeTheme::kColorId_ChromeIconGrey,
229 &grey);
230 gfx::ImageSkia image =
231 gfx::CreateVectorIcon(gfx::VectorIconId::BAR_CLOSE, 16, grey);
232 close_button_->SetImage(views::CustomButton::STATE_NORMAL, &image);
233 } else {
234 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
235 close_button_->SetImage(views::CustomButton::STATE_NORMAL,
236 rb.GetImageNamed(IDR_CLOSE_1).ToImageSkia());
237 close_button_->SetImage(views::CustomButton::STATE_HOVERED,
238 rb.GetImageNamed(IDR_CLOSE_1_H).ToImageSkia());
239 close_button_->SetImage(views::CustomButton::STATE_PRESSED,
240 rb.GetImageNamed(IDR_CLOSE_1_P).ToImageSkia());
242 close_button_->SizeToPreferredSize();
243 close_button_->SetAccessibleName(
244 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE));
245 close_button_->SetFocusable(true);
246 AddChildView(close_button_);
247 } else if ((close_button_ != NULL) && (details.parent == this) &&
248 (details.child != close_button_) && (close_button_->parent() == this) &&
249 (child_at(child_count() - 1) != close_button_)) {
250 // For accessibility, ensure the close button is the last child view.
251 RemoveChildView(close_button_);
252 AddChildView(close_button_);
255 // Ensure the infobar is tall enough to display its contents.
256 int height = ui::MaterialDesignController::IsModeMaterial()
257 ? InfoBarContainerDelegate::kDefaultBarTargetHeightMd
258 : InfoBarContainerDelegate::kDefaultBarTargetHeight;
259 const int kMinimumVerticalPadding = 6;
260 for (int i = 0; i < child_count(); ++i) {
261 const int child_height = child_at(i)->height();
262 height = std::max(height, child_height + kMinimumVerticalPadding);
264 SetBarTargetHeight(height);
267 void InfoBarView::PaintChildren(const ui::PaintContext& context) {
268 // TODO(scr): This really should be the |fill_path_|, but the clipPath seems
269 // broken on non-Windows platforms (crbug.com/75154). For now, just clip to
270 // the bar bounds.
272 // canvas->sk_canvas()->clipPath(fill_path_);
273 DCHECK_EQ(total_height(), height())
274 << "Infobar piecewise heights do not match overall height";
275 ui::ClipTransformRecorder clip_transform_recorder(context);
276 clip_transform_recorder.ClipRect(
277 gfx::Rect(0, arrow_height(), width(), bar_height()));
278 views::View::PaintChildren(context);
281 void InfoBarView::ButtonPressed(views::Button* sender,
282 const ui::Event& event) {
283 if (!owner())
284 return; // We're closing; don't call anything, it might access the owner.
285 if (sender == close_button_) {
286 delegate()->InfoBarDismissed();
287 RemoveSelf();
291 int InfoBarView::ContentMinimumWidth() const {
292 return 0;
295 int InfoBarView::StartX() const {
296 // Ensure we don't return a value greater than EndX(), so children can safely
297 // set something's width to "EndX() - StartX()" without risking that being
298 // negative.
299 return std::min(EndX(), (icon_ != NULL) ?
300 (icon_->bounds().right() + kIconToLabelSpacing) : kEdgeItemPadding);
303 int InfoBarView::EndX() const {
304 return close_button_->x() - kBeforeCloseButtonSpacing;
307 int InfoBarView::OffsetY(views::View* view) const {
308 return arrow_height() +
309 std::max((bar_target_height() - view->height()) / 2, 0) -
310 (bar_target_height() - bar_height());
313 const infobars::InfoBarContainer::Delegate* InfoBarView::container_delegate()
314 const {
315 const infobars::InfoBarContainer* infobar_container = container();
316 return infobar_container ? infobar_container->delegate() : NULL;
319 void InfoBarView::RunMenuAt(ui::MenuModel* menu_model,
320 views::MenuButton* button,
321 views::MenuAnchorPosition anchor) {
322 DCHECK(owner()); // We'd better not open any menus while we're closing.
323 gfx::Point screen_point;
324 views::View::ConvertPointToScreen(button, &screen_point);
325 menu_runner_.reset(
326 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS));
327 // Ignore the result since we don't need to handle a deleted menu specially.
328 ignore_result(menu_runner_->RunMenuAt(GetWidget(),
329 button,
330 gfx::Rect(screen_point, button->size()),
331 anchor,
332 ui::MENU_SOURCE_NONE));
335 // static
336 void InfoBarView::AssignWidthsSorted(Labels* labels, int available_width) {
337 if (labels->empty())
338 return;
339 gfx::Size back_label_size(labels->back()->GetPreferredSize());
340 back_label_size.set_width(
341 std::min(back_label_size.width(),
342 available_width / static_cast<int>(labels->size())));
343 labels->back()->SetSize(back_label_size);
344 labels->pop_back();
345 AssignWidthsSorted(labels, available_width - back_label_size.width());
348 void InfoBarView::PlatformSpecificShow(bool animate) {
349 // If we gain focus, we want to restore it to the previously-focused element
350 // when we're hidden. So when we're in a Widget, create a focus tracker so
351 // that if we gain focus we'll know what the previously-focused element was.
352 SetFocusManager(GetFocusManager());
354 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
357 void InfoBarView::PlatformSpecificHide(bool animate) {
358 // Cancel any menus we may have open. It doesn't make sense to leave them
359 // open while we're hidden, and if we're going to become unowned, we can't
360 // allow the user to choose any options and potentially call functions that
361 // try to access the owner.
362 menu_runner_.reset();
364 // It's possible to be called twice (once with |animate| true and once with it
365 // false); in this case the second SetFocusManager() call will silently no-op.
366 SetFocusManager(NULL);
368 if (!animate)
369 return;
371 // Do not restore focus (and active state with it) if some other top-level
372 // window became active.
373 views::Widget* widget = GetWidget();
374 if (!widget || widget->IsActive())
375 FocusLastFocusedExternalView();
378 void InfoBarView::PlatformSpecificOnHeightsRecalculated() {
379 // Ensure that notifying our container of our size change will result in a
380 // re-layout.
381 InvalidateLayout();
384 void InfoBarView::GetAccessibleState(ui::AXViewState* state) {
385 state->name = l10n_util::GetStringUTF16(
386 (delegate()->GetInfoBarType() ==
387 infobars::InfoBarDelegate::WARNING_TYPE) ?
388 IDS_ACCNAME_INFOBAR_WARNING : IDS_ACCNAME_INFOBAR_PAGE_ACTION);
389 state->role = ui::AX_ROLE_ALERT;
390 state->keyboard_shortcut = base::ASCIIToUTF16("Alt+Shift+A");
393 gfx::Size InfoBarView::GetPreferredSize() const {
394 return gfx::Size(
395 kEdgeItemPadding + (icon_ ? (icon_->width() + kIconToLabelSpacing) : 0) +
396 ContentMinimumWidth() + kBeforeCloseButtonSpacing +
397 close_button_->width() + kEdgeItemPadding,
398 total_height());
401 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) {
402 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now);
404 // This will trigger some screen readers to read the entire contents of this
405 // infobar.
406 if (focused_before && focused_now && !Contains(focused_before) &&
407 Contains(focused_now)) {
408 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);