Commit 4ce4cf93 authored by Jay Harris's avatar Jay Harris Committed by Commit Bot

Updates the CustomTabBar with FlexRules to trigger text eliding.

This CL also introduces a CalculatePreferredSize function, as the
FlexLayout has a DCHECK ensuring that GetPreferredSize is at least as big
as GetMinimumSize (which previously, was not true when the title and
origin of the current page was less than 20 characters).

Bug: 934782
Change-Id: I6e61b43c1486eab50fa7f237a041fbd92c7bb6fb
Reviewed-on: https://chromium-review.googlesource.com/c/1485692Reviewed-by: default avatarDana Fried <dfried@chromium.org>
Commit-Queue: Jay Harris <harrisjay@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635809}
parent d47fa5cb
...@@ -87,19 +87,28 @@ class CustomTabBarTitleOriginView : public views::View { ...@@ -87,19 +87,28 @@ class CustomTabBarTitleOriginView : public views::View {
title_label_->SetBackgroundColor(background_color); title_label_->SetBackgroundColor(background_color);
title_label_->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL); title_label_->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL);
title_label_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
location_label_->SetBackgroundColor(background_color); location_label_->SetBackgroundColor(background_color);
location_label_->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL); location_label_->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL);
location_label_->SetHorizontalAlignment(
gfx::HorizontalAlignment::ALIGN_LEFT);
AddChildView(title_label_); AddChildView(title_label_);
AddChildView(location_label_); AddChildView(location_label_);
auto layout = std::make_unique<views::FlexLayout>(); auto* layout = SetLayoutManager(std::make_unique<views::FlexLayout>());
layout->SetOrientation(views::LayoutOrientation::kVertical) layout->SetOrientation(views::LayoutOrientation::kVertical)
.SetMainAxisAlignment(views::LayoutAlignment::kCenter) .SetMainAxisAlignment(views::LayoutAlignment::kCenter)
.SetCrossAxisAlignment(views::LayoutAlignment::kStart); .SetCrossAxisAlignment(views::LayoutAlignment::kStart)
.SetFlexForView(title_label_,
SetLayoutManager(std::move(layout)); views::FlexSpecification::ForSizeRule(
views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kPreferred))
.SetFlexForView(location_label_,
views::FlexSpecification::ForSizeRule(
views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kPreferred));
} }
void Update(base::string16 title, base::string16 location) { void Update(base::string16 title, base::string16 location) {
...@@ -107,16 +116,30 @@ class CustomTabBarTitleOriginView : public views::View { ...@@ -107,16 +116,30 @@ class CustomTabBarTitleOriginView : public views::View {
location_label_->SetText(location); location_label_->SetText(location);
} }
// views::View: int GetMinimumWidth() const {
gfx::Size GetMinimumSize() const override {
// As labels are not multi-line, the layout will calculate a minimum size // As labels are not multi-line, the layout will calculate a minimum size
// that would fit the entire text (potentially a long url). Instead, set a // that would fit the entire text (potentially a long url). Instead, set a
// minimum number of characters we want to display and elide the text if it // minimum number of characters we want to display and elide the text if it
// overflows. // overflows.
// This is in a helper function because we also have to ensure that the
// preferred size is at least as wide as the minimum size, and the
// minimum height of the control should be the preferred height.
constexpr int kMinCharacters = 20; constexpr int kMinCharacters = 20;
return gfx::Size( return title_label_->font_list().GetExpectedTextWidth(kMinCharacters);
title_label_->font_list().GetExpectedTextWidth(kMinCharacters), }
GetPreferredSize().height());
// views::View:
gfx::Size GetMinimumSize() const override {
return gfx::Size(GetMinimumWidth(), GetPreferredSize().height());
}
gfx::Size CalculatePreferredSize() const override {
// If we don't also override CalculatePreferredSize, we violate some
// assumptions in the FlexLayout (that our PreferredSize is always larger
// than our MinimumSize).
gfx::Size preferred_size = views::View::CalculatePreferredSize();
preferred_size.SetToMax(gfx::Size(GetMinimumWidth(), 0));
return preferred_size;
} }
private: private:
...@@ -157,13 +180,15 @@ CustomTabBarView::CustomTabBarView(BrowserView* browser_view, ...@@ -157,13 +180,15 @@ CustomTabBarView::CustomTabBarView(BrowserView* browser_view,
new CustomTabBarTitleOriginView(kCustomTabBarViewBackgroundColor); new CustomTabBarTitleOriginView(kCustomTabBarViewBackgroundColor);
AddChildView(title_origin_view_); AddChildView(title_origin_view_);
auto layout = std::make_unique<views::FlexLayout>(); auto* layout = SetLayoutManager(std::make_unique<views::FlexLayout>());
layout->SetOrientation(views::LayoutOrientation::kHorizontal) layout->SetOrientation(views::LayoutOrientation::kHorizontal)
.SetMainAxisAlignment(views::LayoutAlignment::kStart) .SetMainAxisAlignment(views::LayoutAlignment::kStart)
.SetCrossAxisAlignment(views::LayoutAlignment::kCenter) .SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
.SetInteriorMargin(GetLayoutInsets(LayoutInset::TOOLBAR_INTERIOR_MARGIN)); .SetInteriorMargin(GetLayoutInsets(LayoutInset::TOOLBAR_INTERIOR_MARGIN))
.SetFlexForView(title_origin_view_,
SetLayoutManager(std::move(layout)); views::FlexSpecification::ForSizeRule(
views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kPreferred));
tab_strip_model_observer_.Add(browser->tab_strip_model()); tab_strip_model_observer_.Add(browser->tab_strip_model());
} }
......
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