Skip to content

Commit

Permalink
Use BoxLayout instead of custom layout for CredentialsItemView.
Browse files Browse the repository at this point in the history
Bug: 1005568
Change-Id: Ia88783b47d8048f8b8e946b7c3492b7920f57cc3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1821858
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Allen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699792}
  • Loading branch information
Allen Bauer authored and Commit Bot committed Sep 25, 2019
1 parent 2ee81e2 commit b58ecb3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 67 deletions.
91 changes: 27 additions & 64 deletions chrome/browser/ui/views/passwords/credentials_item_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,16 @@
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/border.h"
#include "ui/views/bubble/tooltip_icon.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"

namespace {

gfx::Size GetTextLabelsSize(const views::Label* upper_label,
const views::Label* lower_label) {
gfx::Size upper_label_size = upper_label ? upper_label->GetPreferredSize()
: gfx::Size();
gfx::Size lower_label_size = lower_label ? lower_label->GetPreferredSize()
: gfx::Size();
return gfx::Size(std::max(upper_label_size.width(), lower_label_size.width()),
upper_label_size.height() + lower_label_size.height());
}

class CircularImageView : public views::ImageView {
public:
CircularImageView() = default;
Expand Down Expand Up @@ -67,6 +60,11 @@ CredentialsItemView::CredentialsItemView(
int lower_text_style)
: Button(button_listener), form_(form), hover_color_(hover_color) {
set_notify_enter_exit_on_child(true);
views::BoxLayout* layout =
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
// Create an image-view for the avatar. Make sure it ignores events so that
// the parent can receive the events instead.
auto image_view = std::make_unique<CircularImageView>();
Expand All @@ -89,19 +87,36 @@ CredentialsItemView::CredentialsItemView(
// http://crbug.com/651681.
const int kLabelContext = CONTEXT_BODY_TEXT_SMALL;

views::View* text_container = nullptr;
if (!upper_text.empty() || !lower_text.empty()) {
text_container = AddChildView(std::make_unique<views::View>());
views::BoxLayout* text_layout =
text_container->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
text_layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kStart);
text_container->SetProperty(
views::kMarginsKey,
gfx::Insets(0,
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL),
0, 0));
layout->SetFlexForView(text_container, 1);
}

if (!upper_text.empty()) {
auto upper_label = std::make_unique<views::Label>(upper_text, kLabelContext,
upper_text_style);
upper_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
upper_label_ = AddChildView(std::move(upper_label));
upper_label_ = text_container->AddChildView(std::move(upper_label));
}

if (!lower_text.empty()) {
auto lower_label = std::make_unique<views::Label>(lower_text, kLabelContext,
lower_text_style);
lower_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
lower_label->SetMultiLine(true);
lower_label_ = AddChildView(std::move(lower_label));
lower_label_ = text_container->AddChildView(std::move(lower_label));
}

if (form_->is_public_suffix_match) {
Expand Down Expand Up @@ -136,58 +151,6 @@ int CredentialsItemView::GetPreferredHeight() const {
return GetPreferredSize().height();
}

gfx::Size CredentialsItemView::CalculatePreferredSize() const {
gfx::Size labels_size = GetTextLabelsSize(upper_label_, lower_label_);
gfx::Size size = gfx::Size(kAvatarImageSize + labels_size.width(),
std::max(kAvatarImageSize, labels_size.height()));
const gfx::Insets insets(GetInsets());
size.Enlarge(insets.width(), insets.height());
size.Enlarge(ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL),
0);

// Make the size at least as large as the minimum size needed by the border.
size.SetToMax(border() ? border()->GetMinimumSize() : gfx::Size());
return size;
}

int CredentialsItemView::GetHeightForWidth(int w) const {
return View::GetHeightForWidth(w);
}

void CredentialsItemView::Layout() {
gfx::Rect child_area = GetContentsBounds();

gfx::Size image_size(image_view_->GetPreferredSize());
image_size.SetToMin(child_area.size());
gfx::Point image_origin(child_area.origin());
image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
image_view_->SetBoundsRect(gfx::Rect(image_origin, image_size));

gfx::Size upper_size =
upper_label_ ? upper_label_->GetPreferredSize() : gfx::Size();
gfx::Size lower_size =
lower_label_ ? lower_label_->GetPreferredSize() : gfx::Size();
int y_offset = (child_area.height() -
(upper_size.height() + lower_size.height())) / 2;
gfx::Point label_origin(image_origin.x() + image_size.width() +
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL),
child_area.origin().y() + y_offset);
if (upper_label_)
upper_label_->SetBoundsRect(gfx::Rect(label_origin, upper_size));
if (lower_label_) {
label_origin.Offset(0, upper_size.height());
lower_label_->SetBoundsRect(gfx::Rect(label_origin, lower_size));
}
if (info_icon_) {
info_icon_->SizeToPreferredSize();
info_icon_->SetPosition(
gfx::Point(child_area.right() - info_icon_->width(),
child_area.CenterPoint().y() - info_icon_->height() / 2));
}
}

void CredentialsItemView::OnPaintBackground(gfx::Canvas* canvas) {
if (state() == STATE_PRESSED || state() == STATE_HOVERED)
canvas->DrawColor(hover_color_);
Expand Down
3 changes: 0 additions & 3 deletions chrome/browser/ui/views/passwords/credentials_item_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class CredentialsItemView : public AccountAvatarFetcherDelegate,

private:
// views::View:
gfx::Size CalculatePreferredSize() const override;
int GetHeightForWidth(int w) const override;
void Layout() override;
void OnPaintBackground(gfx::Canvas* canvas) override;

const autofill::PasswordForm* form_;
Expand Down

0 comments on commit b58ecb3

Please sign in to comment.