Commit 6561579f authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Make Label::GetHeightForWidth(0) take GetMaxLines() height.

Effectively, this says that zero-width labels are "as tall as possible",
not "always one line high".

Bug: none
Change-Id: If09da545ffd2dc0d92f5ffdc574fcc63f34f6144
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2420128
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarDana Fried <dfried@chromium.org>
Commit-Queue: Dana Fried <dfried@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809098}
parent 013aeeae
...@@ -541,8 +541,10 @@ int Label::GetHeightForWidth(int w) const { ...@@ -541,8 +541,10 @@ int Label::GetHeightForWidth(int w) const {
w -= GetInsets().width(); w -= GetInsets().width();
int height = 0; int height = 0;
int base_line_height = GetLineHeight(); int base_line_height = GetLineHeight();
if (!GetMultiLine() || GetText().empty() || w <= 0) { if (!GetMultiLine() || GetText().empty() || w < 0) {
height = base_line_height; height = base_line_height;
} else if (w == 0) {
height = std::max(GetMaxLines(), 1) * base_line_height;
} else { } else {
// SetDisplayRect() has a side effect for later calls of GetStringSize(). // SetDisplayRect() has a side effect for later calls of GetStringSize().
// Be careful to invoke |full_text_->SetDisplayRect(gfx::Rect())| to // Be careful to invoke |full_text_->SetDisplayRect(gfx::Rect())| to
......
...@@ -488,6 +488,45 @@ TEST_F(LabelTest, MultilinePreferredSizeTest) { ...@@ -488,6 +488,45 @@ TEST_F(LabelTest, MultilinePreferredSizeTest) {
EXPECT_LT(multi_line_size.height(), new_size.height()); EXPECT_LT(multi_line_size.height(), new_size.height());
} }
TEST_F(LabelTest, SingleLineGetHeightForWidth) {
// Even an empty label should take one line worth of height.
const int line_height = label()->GetLineHeight();
EXPECT_EQ(line_height, label()->GetHeightForWidth(100));
// Given any amount of width, the label should take one line.
label()->SetText(ASCIIToUTF16("This is an example."));
const int width = label()->GetPreferredSize().width();
EXPECT_EQ(line_height, label()->GetHeightForWidth(width));
EXPECT_EQ(line_height, label()->GetHeightForWidth(width * 2));
EXPECT_EQ(line_height, label()->GetHeightForWidth(width / 2));
EXPECT_EQ(line_height, label()->GetHeightForWidth(0));
}
TEST_F(LabelTest, MultiLineGetHeightForWidth) {
// Even an empty label should take one line worth of height.
label()->SetMultiLine(true);
const int line_height = label()->GetLineHeight();
EXPECT_EQ(line_height, label()->GetHeightForWidth(100));
// Given its preferred width or more, the label should take one line.
label()->SetText(ASCIIToUTF16("This is an example."));
const int width = label()->GetPreferredSize().width();
EXPECT_EQ(line_height, label()->GetHeightForWidth(width));
EXPECT_EQ(line_height, label()->GetHeightForWidth(width * 2));
// Given too little width, the required number of lines should increase.
// Linebreaking will affect this, so sanity-checks are sufficient.
const int height_for_half_width = label()->GetHeightForWidth(width / 2);
EXPECT_GT(height_for_half_width, line_height);
EXPECT_GT(label()->GetHeightForWidth(width / 4), height_for_half_width);
// Given zero width, the label should take GetMaxLines(); if this is not set,
// default to one.
EXPECT_EQ(line_height, label()->GetHeightForWidth(0));
label()->SetMaxLines(10);
EXPECT_EQ(line_height * 10, label()->GetHeightForWidth(0));
}
TEST_F(LabelTest, TooltipProperty) { TEST_F(LabelTest, TooltipProperty) {
label()->SetText(ASCIIToUTF16("My cool string.")); label()->SetText(ASCIIToUTF16("My cool string."));
......
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