Commit 1d825800 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Clean up/simplify StyledLabel, part 2.

Indents the main loop in CalculateAndDoLayout() inside another block.

That's it.  No functional change, just a trivial one.  The entire purpose of
this CL is to make the next CL's diff actually readable, since otherwise it
looks like garbage.

TBR=tapted

Bug: 1015717
Change-Id: Ieda8fc854c3641bc70e4dd9cff0ae85755451cf8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1874849Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708834}
parent 4c1e202a
......@@ -319,14 +319,15 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
bool first_loop_iteration = true;
// Iterate over the text, creating a bunch of labels and links and laying them
// out in the appropriate positions.
{
// Max height of the views in a line.
int line_height = default_line_height;
// Temporary references to the views in a line, used for alignment.
std::vector<View*> views_in_a_line;
// Iterate over the text, creating a bunch of labels and links and laying them
// out in the appropriate positions.
while (!remaining_string.empty()) {
if (offset.x() == 0 && !first_loop_iteration) {
if (remaining_string.front() == L'\n') {
......@@ -348,11 +349,12 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
const size_t position = text_.size() - remaining_string.size();
std::vector<base::string16> substrings;
// If the current range is not a custom_view, then we use ElideRectangleText
// to determine the line wrapping. Note: if it is a custom_view, then the
// |position| should equal |range.start()| because the custom_view is
// treated as one unit.
if (position != range.start() || (current_range != style_ranges_.end() &&
// If the current range is not a custom_view, then we use
// ElideRectangleText to determine the line wrapping. Note: if it is a
// custom_view, then the |position| should equal |range.start()| because
// the custom_view is treated as one unit.
if (position != range.start() ||
(current_range != style_ranges_.end() &&
!current_range->style_info.custom_view)) {
const gfx::Rect chunk_bounds(offset.x(), 0, width - offset.x(),
default_line_height);
......@@ -367,22 +369,22 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
chunk_bounds.height(), gfx::WRAP_LONG_WORDS, &substrings);
if (substrings.empty()) {
// There is no room for anything; abort. Since wrapping is enabled, this
// should only occur if there is insufficient vertical space remaining.
// ElideRectangleText always adds a single character, even if there is
// no room horizontally.
// There is no room for anything; abort. Since wrapping is enabled,
// this should only occur if there is insufficient vertical space
// remaining. ElideRectangleText always adds a single character, even
// if there is no room horizontally.
DCHECK_NE(0, elide_result & gfx::INSUFFICIENT_SPACE_VERTICAL);
break;
}
// Views are aligned to integer coordinates, but typesetting is not. This
// means that it's possible for an ElideRectangleText on a prior iteration
// to fit a word on the current line, which does not fit after that word
// is wrapped in a View for its chunk at the end of the line. In most
// cases, this will just wrap more words on to the next line. However, if
// the remaining chunk width is insufficient for the very _first_ word,
// that word will be incorrectly split. In this case, start a new line
// instead.
// Views are aligned to integer coordinates, but typesetting is not.
// This means that it's possible for an ElideRectangleText on a prior
// iteration to fit a word on the current line, which does not fit after
// that word is wrapped in a View for its chunk at the end of the line.
// In most cases, this will just wrap more words on to the next line.
// However, if the remaining chunk width is insufficient for the very
// _first_ word, that word will be incorrectly split. In this case,
// start a new line instead.
bool truncated_chunk =
offset.x() != 0 &&
(elide_result & gfx::INSUFFICIENT_SPACE_FOR_FIRST_WORD) != 0;
......@@ -391,7 +393,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
// line. As for the first line, don't advance line number so that it
// will be handled again at the beginning of the loop.
if (offset.x() != 0 || line > 0)
AdvanceOneLine(&line, &offset, &line_height, width, &views_in_a_line);
AdvanceOneLine(&line, &offset, &line_height, width,
&views_in_a_line);
DCHECK(views_in_a_line.empty());
continue;
}
......@@ -406,8 +409,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
if (style_info.custom_view) {
custom_view = style_info.custom_view;
// Ownership of the custom view must be passed to StyledLabel.
DCHECK(
std::find_if(custom_views_.cbegin(), custom_views_.cend(),
DCHECK(std::find_if(
custom_views_.cbegin(), custom_views_.cend(),
[custom_view](const std::unique_ptr<View>& view_ptr) {
return view_ptr.get() == custom_view;
}) != custom_views_.cend());
......@@ -455,7 +458,8 @@ 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());
// The custom view could be wider than the available width; clamp as needed.
// 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()));
......@@ -489,6 +493,7 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
}
}
AdvanceOneLine(&line, &offset, &line_height, width, &views_in_a_line);
}
DCHECK_LE(used_width, width);
calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
return calculated_size_;
......
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