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

Cleanup for KeyboardShortcutItemView.

Mostly shortens variable names, does some other comment shortening and inlining
as well.

Bug: 1015717
Change-Id: I27552ae241a1f3194026c966f7bc556d53cad2c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1884813
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710532}
parent 6b124193
......@@ -163,7 +163,7 @@ int KeyboardShortcutItemView::GetHeightForWidth(int w) const {
}
void KeyboardShortcutItemView::Layout() {
MaybeCalculateAndDoLayout(GetLocalBounds().width());
MaybeCalculateAndDoLayout(width());
}
// static
......@@ -182,11 +182,12 @@ KeyboardShortcutItemView::GetKeycodeToString16Cache() {
void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const {
if (width == calculated_size_.width())
return;
TRACE_EVENT0("shortcut_viewer", "MaybeCalculateAndDoLayout");
const gfx::Insets insets = GetInsets();
width -= insets.width();
if (width <= 0)
const int content_width = width - insets.width();
if (content_width <= 0)
return;
// The max width of |shortcut_label_view_| as a ratio of its parent view's
......@@ -196,76 +197,68 @@ void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const {
// |shortcut_label_view_|.
constexpr int kMinimumSpacing = 64;
const int shortcut_view_preferred_width =
width * kShortcutViewPreferredWidthRatio;
const int shortcut_view_height =
shortcut_label_view_->GetHeightForWidth(shortcut_view_preferred_width);
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_view_preferred_width,
shortcut_view_height);
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_view_preferred_width;
int min_left = shortcut_width;
for (const views::View* label : children)
min_left = std::min(min_left, label->bounds().x());
min_left = std::min(min_left, label->x());
// The width of |description_label_view_| will be dynamically adjusted to fill
// the spacing.
int description_view_preferred_width =
width - (shortcut_view_preferred_width - min_left) - kMinimumSpacing;
if (description_view_preferred_width < kMinimumSpacing) {
int description_width =
content_width - (shortcut_width - min_left) - kMinimumSpacing;
if (description_width < kMinimumSpacing) {
// The min width of |description_label_view_| as a ratio of its parent
// view's width when the |description_view_preferred_width| calculated above
// is smaller than |kMinimumSpacing|.
// view's width when the |description_width| calculated above is smaller
// than |kMinimumSpacing|.
constexpr float kDescriptionViewMinWidthRatio = 0.29f;
description_view_preferred_width = width * kDescriptionViewMinWidthRatio;
description_width = content_width * kDescriptionViewMinWidthRatio;
}
const int description_view_height =
description_label_view_->GetHeightForWidth(
description_view_preferred_width);
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_view_preferred_width,
description_view_height);
description_label_view_->SetBounds(0, 0, description_width,
description_height);
DCHECK(!shortcut_label_view_->children().empty() &&
!description_label_view_->children().empty());
const int description_view_top_line_center_offset_y =
const int description_top_line_center_y =
description_label_view_->children().front()->bounds().CenterPoint().y();
const int shortcut_view_top_line_center_offset_y =
const int shortcut_top_line_center_y =
shortcut_label_view_->children().front()->bounds().CenterPoint().y();
// |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_view_top_line_center_offset_y,
description_view_top_line_center_offset_y);
const int description_delta_y = shortcut_view_top_line_center_offset_y -
description_view_top_line_center_offset_y;
DCHECK_GE(shortcut_top_line_center_y, description_top_line_center_y);
const int description_delta_y =
shortcut_top_line_center_y - description_top_line_center_y;
// Center align the top line in the two views.
const int left = insets.left();
const int top = insets.top();
// Left align the |description_label_view_|.
description_label_view_->SetBoundsRect(
gfx::Rect(left, top + description_delta_y,
description_view_preferred_width, description_view_height));
description_label_view_->SetBounds(insets.left(),
insets.top() + description_delta_y,
description_width, description_height);
// Right align the |shortcut_label_view_|.
shortcut_label_view_->SetBoundsRect(
gfx::Rect(left + width - shortcut_view_preferred_width, top,
shortcut_view_preferred_width, shortcut_view_height));
shortcut_label_view_->SetBounds(
insets.left() + content_width - shortcut_width, insets.top(),
shortcut_width, shortcut_height);
// Add 2 * |description_delta_y| to balance the top and bottom paddings.
const int total_height =
std::max(shortcut_view_height,
description_view_height + 2 * description_delta_y) +
insets.height();
calculated_size_ = gfx::Size(width + insets.width(), total_height);
const int content_height =
std::max(shortcut_height, description_height + 2 * description_delta_y);
calculated_size_ = gfx::Size(width, content_height + insets.height());
}
} // namespace keyboard_shortcut_viewer
......@@ -52,11 +52,7 @@ class KeyboardShortcutItemView : public views::View {
static std::map<ui::KeyboardCode, base::string16>*
GetKeycodeToString16Cache();
// Calculates how to layout child views, sets their size and position. |width|
// is the horizontal space, in pixels, that the view has to work with.
// Returns the needed size and caches the result in |calculated_size_|.
// Does nothing when the |width| equals the cached width of
// |calculated_size_|.
// Lays out child views for the given |width|.
void MaybeCalculateAndDoLayout(int width) const;
// Not owned. Pointer to the keyboard shortcut item.
......@@ -70,8 +66,8 @@ class KeyboardShortcutItemView : public views::View {
// View of the text listing the keys making up the shortcut.
views::StyledLabel* shortcut_label_view_;
// This variable saves the result of the last GetHeightForWidth call in order
// to avoid repeated calculation.
// Saves the results of the last MaybeCalculateAndDoLayout() call to avoid
// repeated calculation.
mutable gfx::Size calculated_size_;
// Accessibility data.
......
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