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 { ...@@ -163,7 +163,7 @@ int KeyboardShortcutItemView::GetHeightForWidth(int w) const {
} }
void KeyboardShortcutItemView::Layout() { void KeyboardShortcutItemView::Layout() {
MaybeCalculateAndDoLayout(GetLocalBounds().width()); MaybeCalculateAndDoLayout(width());
} }
// static // static
...@@ -182,11 +182,12 @@ KeyboardShortcutItemView::GetKeycodeToString16Cache() { ...@@ -182,11 +182,12 @@ KeyboardShortcutItemView::GetKeycodeToString16Cache() {
void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const { void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const {
if (width == calculated_size_.width()) if (width == calculated_size_.width())
return; return;
TRACE_EVENT0("shortcut_viewer", "MaybeCalculateAndDoLayout"); TRACE_EVENT0("shortcut_viewer", "MaybeCalculateAndDoLayout");
const gfx::Insets insets = GetInsets(); const gfx::Insets insets = GetInsets();
width -= insets.width(); const int content_width = width - insets.width();
if (width <= 0) if (content_width <= 0)
return; return;
// The max width of |shortcut_label_view_| as a ratio of its parent view's // 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 { ...@@ -196,76 +197,68 @@ void KeyboardShortcutItemView::MaybeCalculateAndDoLayout(int width) const {
// |shortcut_label_view_|. // |shortcut_label_view_|.
constexpr int kMinimumSpacing = 64; constexpr int kMinimumSpacing = 64;
const int shortcut_view_preferred_width = const int shortcut_width = content_width * kShortcutViewPreferredWidthRatio;
width * kShortcutViewPreferredWidthRatio; const int shortcut_height =
const int shortcut_view_height = shortcut_label_view_->GetHeightForWidth(shortcut_width);
shortcut_label_view_->GetHeightForWidth(shortcut_view_preferred_width);
// Sets the bounds and layout in order to get the left most label in the // 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 // |shortcut_label_view_|, which is used to calculate the preferred width for
// |description_label_view_|. // |description_label_view_|.
shortcut_label_view_->SetBounds(0, 0, shortcut_view_preferred_width, shortcut_label_view_->SetBounds(0, 0, shortcut_width, shortcut_height);
shortcut_view_height);
const auto& children = shortcut_label_view_->children(); const auto& children = shortcut_label_view_->children();
DCHECK(!children.empty()); DCHECK(!children.empty());
// Labels in |shortcut_label_view_| are right aligned, so we need to find the // Labels in |shortcut_label_view_| are right aligned, so we need to find the
// minimum left coordinates of all the labels. // 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) 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 width of |description_label_view_| will be dynamically adjusted to fill
// the spacing. // the spacing.
int description_view_preferred_width = int description_width =
width - (shortcut_view_preferred_width - min_left) - kMinimumSpacing; content_width - (shortcut_width - min_left) - kMinimumSpacing;
if (description_view_preferred_width < kMinimumSpacing) { if (description_width < kMinimumSpacing) {
// The min width of |description_label_view_| as a ratio of its parent // The min width of |description_label_view_| as a ratio of its parent
// view's width when the |description_view_preferred_width| calculated above // view's width when the |description_width| calculated above is smaller
// is smaller than |kMinimumSpacing|. // than |kMinimumSpacing|.
constexpr float kDescriptionViewMinWidthRatio = 0.29f; constexpr float kDescriptionViewMinWidthRatio = 0.29f;
description_view_preferred_width = width * kDescriptionViewMinWidthRatio; description_width = content_width * kDescriptionViewMinWidthRatio;
} }
const int description_view_height = const int description_height =
description_label_view_->GetHeightForWidth( description_label_view_->GetHeightForWidth(description_width);
description_view_preferred_width);
// Sets the bounds and layout in order to get the center points of the views // 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. // 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. // 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_label_view_->SetBounds(0, 0, description_width,
description_view_height); description_height);
DCHECK(!shortcut_label_view_->children().empty() && DCHECK(!shortcut_label_view_->children().empty() &&
!description_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(); 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_->children().front()->bounds().CenterPoint().y();
// |shortcut_label_view_| could have bubble view in the top line, whose // |shortcut_label_view_| could have bubble view in the top line, whose
// height is larger than normal text in |description_label_view_|. Otherwise, // height is larger than normal text in |description_label_view_|. Otherwise,
// the top line height in the two views should be equal. // the top line height in the two views should be equal.
DCHECK_GE(shortcut_view_top_line_center_offset_y, DCHECK_GE(shortcut_top_line_center_y, description_top_line_center_y);
description_view_top_line_center_offset_y); const int description_delta_y =
const int description_delta_y = shortcut_view_top_line_center_offset_y - shortcut_top_line_center_y - description_top_line_center_y;
description_view_top_line_center_offset_y;
// Center align the top line in the two views. // 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_|. // Left align the |description_label_view_|.
description_label_view_->SetBoundsRect( description_label_view_->SetBounds(insets.left(),
gfx::Rect(left, top + description_delta_y, insets.top() + description_delta_y,
description_view_preferred_width, description_view_height)); description_width, description_height);
// Right align the |shortcut_label_view_|. // Right align the |shortcut_label_view_|.
shortcut_label_view_->SetBoundsRect( shortcut_label_view_->SetBounds(
gfx::Rect(left + width - shortcut_view_preferred_width, top, insets.left() + content_width - shortcut_width, insets.top(),
shortcut_view_preferred_width, shortcut_view_height)); shortcut_width, shortcut_height);
// Add 2 * |description_delta_y| to balance the top and bottom paddings. // Add 2 * |description_delta_y| to balance the top and bottom paddings.
const int total_height = const int content_height =
std::max(shortcut_view_height, std::max(shortcut_height, description_height + 2 * description_delta_y);
description_view_height + 2 * description_delta_y) + calculated_size_ = gfx::Size(width, content_height + insets.height());
insets.height();
calculated_size_ = gfx::Size(width + insets.width(), total_height);
} }
} // namespace keyboard_shortcut_viewer } // namespace keyboard_shortcut_viewer
...@@ -52,11 +52,7 @@ class KeyboardShortcutItemView : public views::View { ...@@ -52,11 +52,7 @@ class KeyboardShortcutItemView : public views::View {
static std::map<ui::KeyboardCode, base::string16>* static std::map<ui::KeyboardCode, base::string16>*
GetKeycodeToString16Cache(); GetKeycodeToString16Cache();
// Calculates how to layout child views, sets their size and position. |width| // Lays out child views for the given |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_|.
void MaybeCalculateAndDoLayout(int width) const; void MaybeCalculateAndDoLayout(int width) const;
// Not owned. Pointer to the keyboard shortcut item. // Not owned. Pointer to the keyboard shortcut item.
...@@ -70,8 +66,8 @@ class KeyboardShortcutItemView : public views::View { ...@@ -70,8 +66,8 @@ class KeyboardShortcutItemView : public views::View {
// View of the text listing the keys making up the shortcut. // View of the text listing the keys making up the shortcut.
views::StyledLabel* shortcut_label_view_; views::StyledLabel* shortcut_label_view_;
// This variable saves the result of the last GetHeightForWidth call in order // Saves the results of the last MaybeCalculateAndDoLayout() call to avoid
// to avoid repeated calculation. // repeated calculation.
mutable gfx::Size calculated_size_; mutable gfx::Size calculated_size_;
// Accessibility data. // 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