Commit b3cd517a authored by Wei Li's avatar Wei Li Committed by Commit Bot

Remove manual Layout() from HoverButton

The only custom action in HoverButton's manual layout is to
position title label vertically in its parent. We create a small
custom view class for its parent to change its position upon bound
change. Therefore, we can remove the manual layout in HoverButton.

BUG=1005568

Change-Id: Iea0f9a8236a519373e299b31550d4240551cda7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866981
Commit-Queue: Wei Li <weili@chromium.org>
Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707575}
parent 08d43079
...@@ -71,6 +71,27 @@ void SetTooltipAndAccessibleName(views::Button* parent, ...@@ -71,6 +71,27 @@ void SetTooltipAndAccessibleName(views::Button* parent,
} // namespace } // namespace
SingleLineStyledLabelWrapper::SingleLineStyledLabelWrapper(
const base::string16& title) {
auto title_label = std::make_unique<views::StyledLabel>(title, nullptr);
// Size without a maximum width to get a single line label.
title_label->SizeToFit(0);
label_ = AddChildView(std::move(title_label));
}
views::StyledLabel* SingleLineStyledLabelWrapper::label() {
return label_;
}
void SingleLineStyledLabelWrapper::OnBoundsChanged(
const gfx::Rect& previous_bounds) {
// Vertically center its child manually since it doesn't have a LayoutManager.
DCHECK(label_);
int y_center = (height() - label_->size().height()) / 2;
label_->SetPosition(gfx::Point(GetLocalBounds().x(), y_center));
}
HoverButton::HoverButton(views::ButtonListener* button_listener, HoverButton::HoverButton(views::ButtonListener* button_listener,
const base::string16& text) const base::string16& text)
: views::LabelButton(/*button_listener*/ nullptr, : views::LabelButton(/*button_listener*/ nullptr,
...@@ -174,17 +195,8 @@ HoverButton::HoverButton(views::ButtonListener* button_listener, ...@@ -174,17 +195,8 @@ HoverButton::HoverButton(views::ButtonListener* button_listener,
row_height); row_height);
icon_view_ = grid_layout->AddView(std::move(icon_view), 1, num_labels); icon_view_ = grid_layout->AddView(std::move(icon_view), 1, num_labels);
auto title_label = std::make_unique<views::StyledLabel>(title, nullptr); auto title_wrapper = std::make_unique<SingleLineStyledLabelWrapper>(title);
// Size without a maximum width to get a single line label. title_ = title_wrapper->label();
title_label->SizeToFit(0);
// |views::StyledLabel|s are all multi-line. With a layout manager,
// |StyledLabel| will try use the available space to size itself, and long
// titles will wrap to the next line (for smaller |HoverButton|s, this will
// also cover up |subtitle_|). Wrap it in a parent view with no layout manager
// to ensure it keeps its original size set by SizeToFit() above. Long titles
// will then be truncated.
auto title_wrapper = std::make_unique<views::View>();
title_ = title_wrapper->AddChildView(std::move(title_label));
// Hover the whole button when hovering |title_|. This is OK because |title_| // Hover the whole button when hovering |title_|. This is OK because |title_|
// will never have a link in it. // will never have a link in it.
title_wrapper->set_can_process_events_within_subtree(false); title_wrapper->set_can_process_events_within_subtree(false);
...@@ -315,17 +327,6 @@ std::unique_ptr<views::InkDrop> HoverButton::CreateInkDrop() { ...@@ -315,17 +327,6 @@ std::unique_ptr<views::InkDrop> HoverButton::CreateInkDrop() {
return ink_drop; return ink_drop;
} }
void HoverButton::Layout() {
LabelButton::Layout();
// Vertically center |title_| manually since it doesn't have a LayoutManager.
if (title_) {
DCHECK(title_->parent());
int y_center = title_->parent()->height() / 2 - title_->size().height() / 2;
title_->SetPosition(gfx::Point(title_->x(), y_center));
}
}
views::View* HoverButton::GetTooltipHandlerForPoint(const gfx::Point& point) { views::View* HoverButton::GetTooltipHandlerForPoint(const gfx::Point& point) {
if (!HitTestPoint(point)) if (!HitTestPoint(point))
return nullptr; return nullptr;
......
...@@ -30,6 +30,27 @@ class View; ...@@ -30,6 +30,27 @@ class View;
class PageInfoBubbleViewBrowserTest; class PageInfoBubbleViewBrowserTest;
// A special class used for wrapping a single line styled label.
// |views::StyledLabel|s are all multi-line. With a layout manager,
// |StyledLabel| will try use the available space to size itself, and long
// titles will wrap to the next line (for smaller |HoverButton|s, this will
// also cover up |subtitle_|). Wrap it in a parent view with no layout manager
// to ensure it keeps its original size set by SizeToFit(). Long titles
// will then be truncated.
class SingleLineStyledLabelWrapper : public views::View {
public:
explicit SingleLineStyledLabelWrapper(const base::string16& title);
~SingleLineStyledLabelWrapper() override = default;
// views::View
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
views::StyledLabel* label();
private:
views::StyledLabel* label_;
};
// A button taking the full width of its parent that shows a background color // A button taking the full width of its parent that shows a background color
// when hovered over. // when hovered over.
// TODO (cyan): HoverButton should extend ButtonListener. // TODO (cyan): HoverButton should extend ButtonListener.
...@@ -108,7 +129,6 @@ class HoverButton : public views::LabelButton, ...@@ -108,7 +129,6 @@ class HoverButton : public views::LabelButton,
void StateChanged(ButtonState old_state) override; void StateChanged(ButtonState old_state) override;
SkColor GetInkDropBaseColor() const override; SkColor GetInkDropBaseColor() const override;
std::unique_ptr<views::InkDrop> CreateInkDrop() override; std::unique_ptr<views::InkDrop> CreateInkDrop() override;
void Layout() override;
views::View* GetTooltipHandlerForPoint(const gfx::Point& point) override; views::View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
void OnBoundsChanged(const gfx::Rect& previous_bounds) override; void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
......
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