Commit 7d9d5aee authored by wutao's avatar wutao Committed by Commit Bot

Fix StyledLabel last line height

The last line of sytled_label does not have enough space for customized
line height. This patch fixes this bug.

Bug: b/114129692
Test: StyledLabelTest.LineHeight
Change-Id: I2a218b244983043d9f23d413fe33acefb5ac890f
Reviewed-on: https://chromium-review.googlesource.com/1217068
Commit-Queue: Tao Wu <wutao@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590450}
parent ff741809
......@@ -28,13 +28,6 @@ namespace views {
namespace {
gfx::Insets FocusBorderInsets(const Label& label) {
// StyledLabel never adds a border, so the only Insets added are for the
// possible focus ring.
DCHECK(label.View::GetInsets().IsEmpty());
return label.GetInsets();
}
std::unique_ptr<Label> CreateLabelRange(
const base::string16& text,
int text_context,
......@@ -216,24 +209,6 @@ const char* StyledLabel::GetClassName() const {
return kViewClassName;
}
gfx::Insets StyledLabel::GetInsets() const {
gfx::Insets insets = View::GetInsets();
if (Link::GetDefaultFocusStyle() != Link::FocusStyle::RING)
return insets;
// We need a focus border iff we contain a link that will have a focus border.
// That in turn will be true only if the link is non-empty.
for (StyleRanges::const_iterator i(style_ranges_.begin());
i != style_ranges_.end(); ++i) {
if (i->style_info.IsLink() && !i->range.is_empty()) {
insets += gfx::Insets(Link::kFocusBorderPadding);
break;
}
}
return insets;
}
void StyledLabel::GetAccessibleNodeData(ui::AXNodeData* node_data) {
if (text_context_ == style::CONTEXT_DIALOG_TITLE)
node_data->role = ax::mojom::Role::kTitleBar;
......@@ -488,26 +463,18 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
gfx::Size view_size = child_view->GetPreferredSize();
// |offset.y()| already contains |insets.top()|.
gfx::Point view_origin(insets.left() + offset.x(), offset.y());
gfx::Insets focus_border_insets;
if (Link::GetDefaultFocusStyle() == Link::FocusStyle::RING && label) {
// Calculate the size of the optional focus border, and overlap by that
// amount. Otherwise, "<a>link</a>," will render as "link ,".
focus_border_insets = FocusBorderInsets(*label);
}
view_origin.Offset(-focus_border_insets.left(), -focus_border_insets.top());
// The custom view could be wider than the available width; clamp as needed.
if (custom_view) {
view_size.set_width(std::min(
view_size.width(), width - offset.x() + focus_border_insets.width()));
}
if (custom_view)
view_size.set_width(std::min(view_size.width(), width - offset.x()));
child_view->SetBoundsRect(gfx::Rect(view_origin, view_size));
offset.set_x(offset.x() + view_size.width() - focus_border_insets.width());
offset.set_x(offset.x() + view_size.width());
total_height =
std::max(total_height, child_view->bounds().bottom() + insets.bottom() -
focus_border_insets.bottom());
std::max(total_height, std::max(child_view->bounds().bottom(),
offset.y() + default_line_height) +
insets.bottom());
used_width = std::max(used_width, offset.x());
max_line_height = std::max(
max_line_height, view_size.height() - focus_border_insets.height());
max_line_height = std::max(max_line_height, view_size.height());
if (!dry_run) {
views_in_a_line.push_back(child_view);
......
......@@ -140,7 +140,6 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
// View:
const char* GetClassName() const override;
gfx::Insets GetInsets() const override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
gfx::Size CalculatePreferredSize() const override;
int GetHeightForWidth(int w) const override;
......
......@@ -513,13 +513,32 @@ TEST_F(StyledLabelTest, SetTextContextAndDefaultStyle) {
}
TEST_F(StyledLabelTest, LineHeight) {
const std::string text("one");
const std::string text("one\ntwo\nthree");
InitStyledLabel(text);
styled()->SetLineHeight(18);
EXPECT_EQ(18 * 3, styled()->GetHeightForWidth(100));
}
TEST_F(StyledLabelTest, LineHeightWithBorder) {
const std::string text("one\ntwo\nthree");
InitStyledLabel(text);
styled()->SetLineHeight(18);
styled()->SetBorder(views::CreateSolidBorder(1, SK_ColorGRAY));
EXPECT_EQ(18 * 3 + 2, styled()->GetHeightForWidth(100));
}
TEST_F(StyledLabelTest, LineHeightWithLink) {
const std::string text("one\ntwo\nthree");
InitStyledLabel(text);
int default_height = styled()->GetHeightForWidth(100);
const std::string newline_text("one\ntwo\nthree");
InitStyledLabel(newline_text);
styled()->SetLineHeight(18);
EXPECT_EQ(18 * 2 + default_height, styled()->GetHeightForWidth(100));
styled()->AddStyleRange(gfx::Range(0, 3),
StyledLabel::RangeStyleInfo::CreateForLink());
styled()->AddStyleRange(gfx::Range(4, 7),
StyledLabel::RangeStyleInfo::CreateForLink());
styled()->AddStyleRange(gfx::Range(8, 13),
StyledLabel::RangeStyleInfo::CreateForLink());
EXPECT_EQ(18 * 3, styled()->GetHeightForWidth(100));
}
TEST_F(StyledLabelTest, HandleEmptyLayout) {
......
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