Commit 7aa56072 authored by Marina Ciocea's avatar Marina Ciocea Committed by Commit Bot

Update InfoBarView to permit creating infobars without a close button.

The default behavior remains the same, with InfoBarView having a close button.
This change is needed for implementing the tab sharing infobar (see bug).

Bug: 927949
Change-Id: I795c868508148b5a8a1647768474b0d1dc89cd36
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1599689
Commit-Queue: Marina Ciocea <marinaciocea@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658463}
parent 58f09d89
...@@ -109,19 +109,22 @@ InfoBarView::InfoBarView(std::unique_ptr<infobars::InfoBarDelegate> delegate) ...@@ -109,19 +109,22 @@ InfoBarView::InfoBarView(std::unique_ptr<infobars::InfoBarDelegate> delegate)
AddChildView(icon_); AddChildView(icon_);
} }
auto close_button = views::CreateVectorImageButton(this); if (this->delegate()->IsCloseable()) {
// This is the wrong color, but allows the button's size to be computed auto close_button = views::CreateVectorImageButton(this);
// correctly. We'll reset this with the correct color in OnThemeChanged(). // This is the wrong color, but allows the button's size to be computed
views::SetImageFromVectorIcon(close_button.get(), // correctly. We'll reset this with the correct color in OnThemeChanged().
vector_icons::kCloseRoundedIcon, views::SetImageFromVectorIcon(close_button.get(),
gfx::kPlaceholderColor); vector_icons::kCloseRoundedIcon,
close_button->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); gfx::kPlaceholderColor);
close_button->SetFocusForPlatform(); close_button->SetAccessibleName(
gfx::Insets close_button_spacing = GetCloseButtonSpacing(); l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE));
close_button->SetProperty(views::kMarginsKey, close_button->SetFocusForPlatform();
new gfx::Insets(close_button_spacing.top(), 0, gfx::Insets close_button_spacing = GetCloseButtonSpacing();
close_button->SetProperty(
views::kMarginsKey, new gfx::Insets(close_button_spacing.top(), 0,
close_button_spacing.bottom(), 0)); close_button_spacing.bottom(), 0));
close_button_ = AddChildView(std::move(close_button)); close_button_ = AddChildView(std::move(close_button));
}
} }
InfoBarView::~InfoBarView() { InfoBarView::~InfoBarView() {
...@@ -154,15 +157,18 @@ void InfoBarView::Layout() { ...@@ -154,15 +157,18 @@ void InfoBarView::Layout() {
if (content_minimum_width > 0) if (content_minimum_width > 0)
start_x += spacing + content_minimum_width; start_x += spacing + content_minimum_width;
const gfx::Insets close_button_spacing = GetCloseButtonSpacing(); if (close_button_) {
close_button_->SizeToPreferredSize(); const gfx::Insets close_button_spacing = GetCloseButtonSpacing();
close_button_->SetPosition(gfx::Point( close_button_->SizeToPreferredSize();
std::max(start_x + close_button_spacing.left(), close_button_->SetPosition(gfx::Point(
width() - close_button_spacing.right() - close_button_->width()), std::max(
OffsetY(close_button_))); start_x + close_button_spacing.left(),
width() - close_button_spacing.right() - close_button_->width()),
// For accessibility reasons, the close button should come last. OffsetY(close_button_)));
DCHECK_EQ(close_button_, close_button_->parent()->children().back());
// For accessibility reasons, the close button should come last.
DCHECK_EQ(close_button_, close_button_->parent()->children().back());
}
} }
void InfoBarView::GetAccessibleNodeData(ui::AXNodeData* node_data) { void InfoBarView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
...@@ -183,9 +189,10 @@ gfx::Size InfoBarView::CalculatePreferredSize() const { ...@@ -183,9 +189,10 @@ gfx::Size InfoBarView::CalculatePreferredSize() const {
if (content_width) if (content_width)
width += spacing + content_width; width += spacing + content_width;
return gfx::Size( const int trailing_space =
width + GetCloseButtonSpacing().width() + close_button_->width(), close_button_ ? GetCloseButtonSpacing().width() + close_button_->width()
computed_height()); : GetElementSpacing();
return gfx::Size(width + trailing_space, computed_height());
} }
void InfoBarView::ViewHierarchyChanged( void InfoBarView::ViewHierarchyChanged(
...@@ -194,7 +201,8 @@ void InfoBarView::ViewHierarchyChanged( ...@@ -194,7 +201,8 @@ void InfoBarView::ViewHierarchyChanged(
// Anything that needs to happen once after all subclasses add their children. // Anything that needs to happen once after all subclasses add their children.
if (details.is_add && (details.child == this)) { if (details.is_add && (details.child == this)) {
ReorderChildView(close_button_, -1); if (close_button_)
ReorderChildView(close_button_, -1);
RecalculateHeight(); RecalculateHeight();
} }
} }
...@@ -214,8 +222,10 @@ void InfoBarView::OnThemeChanged() { ...@@ -214,8 +222,10 @@ void InfoBarView::OnThemeChanged() {
SetBackground(views::CreateSolidBackground(background_color)); SetBackground(views::CreateSolidBackground(background_color));
const SkColor text_color = GetColor(kInfoBarLabelTextColor); const SkColor text_color = GetColor(kInfoBarLabelTextColor);
views::SetImageFromVectorIcon(close_button_, vector_icons::kCloseRoundedIcon, if (close_button_) {
text_color); views::SetImageFromVectorIcon(close_button_,
vector_icons::kCloseRoundedIcon, text_color);
}
for (views::View* child : children()) { for (views::View* child : children()) {
LabelType label_type = child->GetProperty(kLabelType); LabelType label_type = child->GetProperty(kLabelType);
...@@ -288,7 +298,8 @@ int InfoBarView::StartX() const { ...@@ -288,7 +298,8 @@ int InfoBarView::StartX() const {
} }
int InfoBarView::EndX() const { int InfoBarView::EndX() const {
return close_button_->x() - GetCloseButtonSpacing().left(); return close_button_ ? close_button_->x() - GetCloseButtonSpacing().left()
: width() - GetElementSpacing();
} }
int InfoBarView::OffsetY(views::View* view) const { int InfoBarView::OffsetY(views::View* view) const {
......
...@@ -72,6 +72,10 @@ bool InfoBarDelegate::ShouldExpire(const NavigationDetails& details) const { ...@@ -72,6 +72,10 @@ bool InfoBarDelegate::ShouldExpire(const NavigationDetails& details) const {
void InfoBarDelegate::InfoBarDismissed() { void InfoBarDelegate::InfoBarDismissed() {
} }
bool InfoBarDelegate::IsCloseable() const {
return true;
}
ConfirmInfoBarDelegate* InfoBarDelegate::AsConfirmInfoBarDelegate() { ConfirmInfoBarDelegate* InfoBarDelegate::AsConfirmInfoBarDelegate() {
return nullptr; return nullptr;
} }
......
...@@ -224,6 +224,9 @@ class InfoBarDelegate { ...@@ -224,6 +224,9 @@ class InfoBarDelegate {
// Called when the user clicks on the close button to dismiss the infobar. // Called when the user clicks on the close button to dismiss the infobar.
virtual void InfoBarDismissed(); virtual void InfoBarDismissed();
// Returns true if the InfoBar has a close button; true by default.
virtual bool IsCloseable() const;
// Type-checking downcast routines: // Type-checking downcast routines:
virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate(); virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate();
virtual HungRendererInfoBarDelegate* AsHungRendererInfoBarDelegate(); virtual HungRendererInfoBarDelegate* AsHungRendererInfoBarDelegate();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment