Commit dc920535 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Convert KeyboardShortcutItemView to use StyledLabel::GetLayoutSizesForWidth().

No functional change, but simplifies the code, and is a necessary precursor for
fixing bug 1015717.

Bug: 1015717
Change-Id: I0f5212ce85841540a2ca85b8c73d8a1149576240
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1884814
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711558}
parent 6d252d9c
......@@ -198,55 +198,46 @@ void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const {
constexpr int kMinimumSpacing = 64;
const int shortcut_width = content_width * kShortcutViewPreferredWidthRatio;
const int shortcut_height =
shortcut_label_view_->GetHeightForWidth(shortcut_width);
// Sets the bounds and layout in order to get the left most label in the
// |shortcut_label_view_|, which is used to calculate the preferred width for
// |description_label_view_|.
shortcut_label_view_->SetBounds(0, 0, shortcut_width, shortcut_height);
const auto& children = shortcut_label_view_->children();
DCHECK(!children.empty());
// Labels in |shortcut_label_view_| are right aligned, so we need to find the
// minimum left coordinates of all the labels.
int min_left = shortcut_width;
for (const views::View* label : children)
min_left = std::min(min_left, label->x());
const auto& shortcut_size_info =
shortcut_label_view_->GetLayoutSizeInfoForWidth(shortcut_width);
const int shortcut_height = shortcut_size_info.total_size.height();
const auto top_line_height = [](const auto& size_info) {
// When nothing fits, it doesn't really matter what we do; using the overall
// height (which is the height of the label insets) is sane.
return size_info.line_sizes.empty() ? size_info.total_size.height()
: size_info.line_sizes[0].height();
};
const int shortcut_top_line_center_y =
shortcut_label_view_->GetInsets().top() +
(top_line_height(shortcut_size_info) / 2);
// The width of |description_label_view_| will be dynamically adjusted to fill
// the spacing.
int description_width =
content_width - (shortcut_width - min_left) - kMinimumSpacing;
content_width - shortcut_size_info.total_size.width() - kMinimumSpacing;
if (description_width < kMinimumSpacing) {
// The min width of |description_label_view_| as a ratio of its parent
// view's width when the |description_width| calculated above is smaller
// than |kMinimumSpacing|.
// view's width when the |description_view_width| calculated above is
// smaller than |kMinimumSpacing|.
constexpr float kDescriptionViewMinWidthRatio = 0.29f;
description_width = content_width * kDescriptionViewMinWidthRatio;
}
const int description_height =
description_label_view_->GetHeightForWidth(description_width);
// Sets the bounds and layout in order to get the center points of the views
// making up the top lines in both the description and shortcut views.
// We want the center of the top lines in both views to align with each other.
description_label_view_->SetBounds(0, 0, description_width,
description_height);
DCHECK(!shortcut_label_view_->children().empty() &&
!description_label_view_->children().empty());
const auto& description_size_info =
description_label_view_->GetLayoutSizeInfoForWidth(description_width);
const int description_height = description_size_info.total_size.height();
const int description_top_line_center_y =
description_label_view_->children().front()->bounds().CenterPoint().y();
const int shortcut_top_line_center_y =
shortcut_label_view_->children().front()->bounds().CenterPoint().y();
description_label_view_->GetInsets().top() +
(top_line_height(description_size_info) / 2);
// |shortcut_label_view_| could have bubble view in the top line, whose
// height is larger than normal text in |description_label_view_|. Otherwise,
// the top line height in the two views should be equal.
DCHECK_GE(shortcut_top_line_center_y, description_top_line_center_y);
// Align the vertical center of the top lines of both views.
const int description_delta_y =
shortcut_top_line_center_y - description_top_line_center_y;
// Center align the top line in the two views.
// Left align the |description_label_view_|.
description_label_view_->SetBounds(insets.left(),
insets.top() + description_delta_y,
......
......@@ -151,7 +151,7 @@ TEST_F(KeyboardShortcutViewTest, TopLineCenterAlignedInItemView) {
if (item_view->category() != ShortcutCategory::kPopular)
continue;
DCHECK_EQ(2u, item_view->children().size());
ASSERT_EQ(2u, item_view->children().size());
// The top lines in both |description_label_view_| and
// |shortcut_label_view_| should be center aligned. Only need to check one
......
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