Commit 61701d5b authored by Peter Boström's avatar Peter Boström Committed by Commit Bot

Make ToolbarButton border align to background

The background is inset visually to not necessarily fill the bounds of
the view. These insets are non-zero in touch mode and at the edges of
the screen. This splits the insets of the border into painted insets and
additional insets for positioning of elements.

Bug: None
Change-Id: Ia290aa0948e24c40f3fccb80861973f3d53981c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1948490Reviewed-by: default avatarCollin Baker <collinbaker@chromium.org>
Commit-Queue: Collin Baker <collinbaker@chromium.org>
Commit-Queue: Peter Boström <pbos@chromium.org>
Auto-Submit: Peter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721093}
parent 7b04c009
......@@ -140,39 +140,41 @@ void ToolbarButton::UpdateColorsAndInsets() {
ResetColorsFromNativeTheme();
}
// ToolbarButtons are always the height the location bar.
const gfx::Insets paint_insets =
gfx::Insets((height() - GetLayoutConstant(LOCATION_BAR_HEIGHT)) / 2) +
*GetProperty(views::kInternalPaddingKey);
base::Optional<SkColor> background_color =
highlight_color_animation_.GetBackgroundColor();
if (background_color) {
// ToolbarButtons are always the height the location bar.
const gfx::Insets bg_insets =
gfx::Insets((height() - GetLayoutConstant(LOCATION_BAR_HEIGHT)) / 2) +
*GetProperty(views::kInternalPaddingKey);
SetBackground(views::CreateBackgroundFromPainter(
views::Painter::CreateSolidRoundRectPainter(
*background_color, highlight_radius, bg_insets)));
*background_color, highlight_radius, paint_insets)));
} else {
SetBackground(nullptr);
}
gfx::Insets new_insets =
gfx::Insets target_insets =
layout_insets_.value_or(GetLayoutInsets(TOOLBAR_BUTTON)) +
layout_inset_delta_ + *GetProperty(views::kInternalPaddingKey);
base::Optional<SkColor> border_color =
highlight_color_animation_.GetBorderColor();
if (!border() || new_insets != border()->GetInsets() ||
if (!border() || target_insets != border()->GetInsets() ||
last_border_color_ != border_color) {
if (border_color) {
int border_thickness_dp = GetText().empty()
? kBorderThicknessDpWithoutLabel
: kBorderThicknessDpWithLabel;
// Create a border with insets equal to |new_insets|, just split into a
// solid border and padding.
SetBorder(views::CreatePaddedBorder(
views::CreateRoundedRectBorder(border_thickness_dp, highlight_radius,
*border_color),
new_insets - gfx::Insets(border_thickness_dp)));
// Create a border with insets totalling |target_insets|, split into
// painted insets (matching the background) and internal padding to
// position child views correctly.
std::unique_ptr<views::Border> border = views::CreateRoundedRectBorder(
border_thickness_dp, highlight_radius, paint_insets, *border_color);
const gfx::Insets extra_insets = target_insets - border->GetInsets();
SetBorder(views::CreatePaddedBorder(std::move(border), extra_insets));
} else {
SetBorder(views::CreateEmptyBorder(new_insets));
SetBorder(views::CreateEmptyBorder(target_insets));
}
last_border_color_ = border_color;
}
......
......@@ -77,7 +77,10 @@ gfx::Size SolidSidedBorder::GetMinimumSize() const {
// A border with a rounded rectangle and single color.
class RoundedRectBorder : public Border {
public:
RoundedRectBorder(int thickness, int corner_radius, SkColor color);
RoundedRectBorder(int thickness,
int corner_radius,
const gfx::Insets& paint_insets,
SkColor color);
// Overridden from Border:
void Paint(const View& view, gfx::Canvas* canvas) override;
......@@ -87,6 +90,7 @@ class RoundedRectBorder : public Border {
private:
const int thickness_;
const int corner_radius_;
const gfx::Insets paint_insets_;
const SkColor color_;
DISALLOW_COPY_AND_ASSIGN(RoundedRectBorder);
......@@ -94,8 +98,12 @@ class RoundedRectBorder : public Border {
RoundedRectBorder::RoundedRectBorder(int thickness,
int corner_radius,
const gfx::Insets& paint_insets,
SkColor color)
: thickness_(thickness), corner_radius_(corner_radius), color_(color) {}
: thickness_(thickness),
corner_radius_(corner_radius),
paint_insets_(paint_insets),
color_(color) {}
void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) {
cc::PaintFlags flags;
......@@ -106,12 +114,13 @@ void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) {
float half_thickness = thickness_ / 2.0f;
gfx::RectF bounds(view.GetLocalBounds());
bounds.Inset(paint_insets_);
bounds.Inset(half_thickness, half_thickness);
canvas->DrawRoundRect(bounds, corner_radius_, flags);
}
gfx::Insets RoundedRectBorder::GetInsets() const {
return gfx::Insets(thickness_);
return gfx::Insets(thickness_) + paint_insets_;
}
gfx::Size RoundedRectBorder::GetMinimumSize() const {
......@@ -236,7 +245,15 @@ std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) {
std::unique_ptr<Border> CreateRoundedRectBorder(int thickness,
int corner_radius,
SkColor color) {
return std::make_unique<RoundedRectBorder>(thickness, corner_radius, color);
return CreateRoundedRectBorder(thickness, corner_radius, gfx::Insets(),
color);
}
std::unique_ptr<Border> CreateRoundedRectBorder(int thickness,
int corner_radius,
const gfx::Insets& paint_insets,
SkColor color) {
return std::make_unique<RoundedRectBorder>(thickness, corner_radius,
paint_insets, color);
}
std::unique_ptr<Border> CreateEmptyBorder(int top,
......
......@@ -73,6 +73,11 @@ VIEWS_EXPORT std::unique_ptr<Border> CreateSolidBorder(int thickness,
VIEWS_EXPORT std::unique_ptr<Border> CreateRoundedRectBorder(int thickness,
int corner_radius,
SkColor color);
VIEWS_EXPORT std::unique_ptr<Border> CreateRoundedRectBorder(
int thickness,
int corner_radius,
const gfx::Insets& paint_insets,
SkColor color);
// Creates a border for reserving space. The returned border does not paint
// anything.
......
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