infobar_view.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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. 4 5#include "chrome/browser/ui/views/infobars/infobar_view.h" 6 7#include <algorithm> 8 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" 33 34 35// Helpers -------------------------------------------------------------------- 36 37namespace { 38 39const int kEdgeItemPadding = views::kRelatedControlHorizontalSpacing; 40const int kIconToLabelSpacing = views::kRelatedControlHorizontalSpacing; 41const int kBeforeCloseButtonSpacing = views::kUnrelatedControlHorizontalSpacing; 42 43bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) { 44 return label_1->GetPreferredSize().width() > 45 label_2->GetPreferredSize().width(); 46} 47 48} // namespace 49 50 51// InfoBar -------------------------------------------------------------------- 52 53// static 54const int infobars::InfoBar::kSeparatorLineHeight = 55 views::NonClientFrameView::kClientEdgeThickness; 56const int infobars::InfoBar::kDefaultArrowTargetHeight = 9; 57const int infobars::InfoBar::kMaximumArrowTargetHeight = 24; 58const int infobars::InfoBar::kDefaultArrowTargetHalfWidth = 59 kDefaultArrowTargetHeight; 60const int infobars::InfoBar::kMaximumArrowTargetHalfWidth = 14; 61const int infobars::InfoBar::kDefaultBarTargetHeight = 36; 62 63// InfoBarView ---------------------------------------------------------------- 64 65// static 66const int InfoBarView::kButtonButtonSpacing = views::kRelatedButtonHSpacing; 67const int InfoBarView::kEndOfLabelSpacing = views::kItemLabelSpacing; 68 69InfoBarView::InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate) 70 : infobars::InfoBar(delegate.Pass()), 71 views::ExternalFocusTracker(this, NULL), 72 icon_(NULL), 73 close_button_(NULL) { 74 set_owned_by_client(); // InfoBar deletes itself at the appropriate time. 75 set_background( 76 new InfoBarBackground(infobars::InfoBar::delegate()->GetInfoBarType())); 77} 78 79InfoBarView::~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()); 84} 85 86views::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); 94 return label; 95} 96 97views::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()); 106 return link; 107} 108 109// static 110views::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)); 124 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); 137 return menu_button; 138} 139 140// static 141views::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)); 158 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); 168 return label_button; 169} 170 171// static 172void InfoBarView::AssignWidths(Labels* labels, int available_width) { 173 std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth); 174 AssignWidthsSorted(labels, available_width); 175} 176 177void 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(); 182 fill_path_.rewind(); 183 const infobars::InfoBarContainer::Delegate* delegate = container_delegate(); 184 if (delegate) { 185 static_cast<InfoBarBackground*>(background())->set_separator_color( 186 delegate->GetInfoBarSeparatorColor()); 187 int arrow_x; 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. 198 stroke_path_.moveTo( 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); 203 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); 214 fill_path_.close(); 215 } 216 } 217 if (bar_height()) { 218 fill_path_.addRect(0.0, SkIntToScalar(arrow_height()), 219 SkIntToScalar(width()), SkIntToScalar(height() - kSeparatorLineHeight)); 220 } 221 222 int start_x = kEdgeItemPadding; 223 if (icon_ != NULL) { 224 icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_))); 225 start_x = icon_->bounds().right() + kIconToLabelSpacing; 226 } 227 228 int content_minimum_width = ContentMinimumWidth(); 229 close_button_->SetPosition(gfx::Point( 230 std::max( 231 start_x + content_minimum_width + 232 ((content_minimum_width > 0) ? kBeforeCloseButtonSpacing : 0), 233 width() - kEdgeItemPadding - close_button_->width()), 234 OffsetY(close_button_))); 235} 236 237void InfoBarView::ViewHierarchyChanged( 238 const ViewHierarchyChangedDetails& details) { 239 View::ViewHierarchyChanged(details); 240 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(); 247 AddChildView(icon_); 248 } 249 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_); 269 } 270 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); 277 } 278 SetBarTargetHeight(height); 279} 280 281void InfoBarView::PaintChildren(gfx::Canvas* canvas, 282 const views::CullSet& cull_set) { 283 canvas->Save(); 284 285 // TODO(scr): This really should be the |fill_path_|, but the clipPath seems 286 // broken on non-Windows platforms (crbug.com/75154). For now, just clip to 287 // the bar bounds. 288 // 289 // canvas->sk_canvas()->clipPath(fill_path_); 290 DCHECK_EQ(total_height(), height()) 291 << "Infobar piecewise heights do not match overall height"; 292 canvas->ClipRect(gfx::Rect(0, arrow_height(), width(), bar_height())); 293 views::View::PaintChildren(canvas, cull_set); 294 canvas->Restore(); 295} 296 297void InfoBarView::ButtonPressed(views::Button* sender, 298 const ui::Event& event) { 299 if (!owner()) 300 return; // We're closing; don't call anything, it might access the owner. 301 if (sender == close_button_) { 302 delegate()->InfoBarDismissed(); 303 RemoveSelf(); 304 } 305} 306 307int InfoBarView::ContentMinimumWidth() const { 308 return 0; 309} 310 311int InfoBarView::StartX() const { 312 // Ensure we don't return a value greater than EndX(), so children can safely 313 // set something's width to "EndX() - StartX()" without risking that being 314 // negative. 315 return std::min(EndX(), (icon_ != NULL) ? 316 (icon_->bounds().right() + kIconToLabelSpacing) : kEdgeItemPadding); 317} 318 319int InfoBarView::EndX() const { 320 return close_button_->x() - kBeforeCloseButtonSpacing; 321} 322 323int InfoBarView::OffsetY(views::View* view) const { 324 return arrow_height() + 325 std::max((bar_target_height() - view->height()) / 2, 0) - 326 (bar_target_height() - bar_height()); 327} 328 329const infobars::InfoBarContainer::Delegate* InfoBarView::container_delegate() 330 const { 331 const infobars::InfoBarContainer* infobar_container = container(); 332 return infobar_container ? infobar_container->delegate() : NULL; 333} 334 335void InfoBarView::RunMenuAt(ui::MenuModel* menu_model, 336 views::MenuButton* button, 337 views::MenuAnchorPosition anchor) { 338 DCHECK(owner()); // We'd better not open any menus while we're closing. 339 gfx::Point screen_point; 340 views::View::ConvertPointToScreen(button, &screen_point); 341 menu_runner_.reset(new views::MenuRunner(menu_model)); 342 // Ignore the result since we don't need to handle a deleted menu specially. 343 ignore_result(menu_runner_->RunMenuAt( 344 GetWidget(), button, gfx::Rect(screen_point, button->size()), anchor, 345 ui::MENU_SOURCE_NONE, views::MenuRunner::HAS_MNEMONICS)); 346} 347 348// static 349void InfoBarView::AssignWidthsSorted(Labels* labels, int available_width) { 350 if (labels->empty()) 351 return; 352 gfx::Size back_label_size(labels->back()->GetPreferredSize()); 353 back_label_size.set_width( 354 std::min(back_label_size.width(), 355 available_width / static_cast<int>(labels->size()))); 356 labels->back()->SetSize(back_label_size); 357 labels->pop_back(); 358 AssignWidthsSorted(labels, available_width - back_label_size.width()); 359} 360 361void InfoBarView::PlatformSpecificShow(bool animate) { 362 // If we gain focus, we want to restore it to the previously-focused element 363 // when we're hidden. So when we're in a Widget, create a focus tracker so 364 // that if we gain focus we'll know what the previously-focused element was. 365 SetFocusManager(GetFocusManager()); 366 367 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); 368} 369 370void InfoBarView::PlatformSpecificHide(bool animate) { 371 // Cancel any menus we may have open. It doesn't make sense to leave them 372 // open while we're hidden, and if we're going to become unowned, we can't 373 // allow the user to choose any options and potentially call functions that 374 // try to access the owner. 375 menu_runner_.reset(); 376 377 // It's possible to be called twice (once with |animate| true and once with it 378 // false); in this case the second SetFocusManager() call will silently no-op. 379 SetFocusManager(NULL); 380 381 if (!animate) 382 return; 383 384 // Do not restore focus (and active state with it) if some other top-level 385 // window became active. 386 views::Widget* widget = GetWidget(); 387 if (!widget || widget->IsActive()) 388 FocusLastFocusedExternalView(); 389} 390 391void InfoBarView::PlatformSpecificOnHeightsRecalculated() { 392 // Ensure that notifying our container of our size change will result in a 393 // re-layout. 394 InvalidateLayout(); 395} 396 397void InfoBarView::GetAccessibleState(ui::AXViewState* state) { 398 state->name = l10n_util::GetStringUTF16( 399 (delegate()->GetInfoBarType() == 400 infobars::InfoBarDelegate::WARNING_TYPE) ? 401 IDS_ACCNAME_INFOBAR_WARNING : IDS_ACCNAME_INFOBAR_PAGE_ACTION); 402 state->role = ui::AX_ROLE_ALERT; 403 state->keyboard_shortcut = base::ASCIIToUTF16("Alt+Shift+A"); 404} 405 406gfx::Size InfoBarView::GetPreferredSize() const { 407 return gfx::Size( 408 kEdgeItemPadding + (icon_ ? (icon_->width() + kIconToLabelSpacing) : 0) + 409 ContentMinimumWidth() + kBeforeCloseButtonSpacing + 410 close_button_->width() + kEdgeItemPadding, 411 total_height()); 412} 413 414void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) { 415 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now); 416 417 // This will trigger some screen readers to read the entire contents of this 418 // infobar. 419 if (focused_before && focused_now && !Contains(focused_before) && 420 Contains(focused_now)) { 421 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); 422 } 423} 424