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