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() { ...@@ -140,39 +140,41 @@ void ToolbarButton::UpdateColorsAndInsets() {
ResetColorsFromNativeTheme(); 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 = base::Optional<SkColor> background_color =
highlight_color_animation_.GetBackgroundColor(); highlight_color_animation_.GetBackgroundColor();
if (background_color) { 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( SetBackground(views::CreateBackgroundFromPainter(
views::Painter::CreateSolidRoundRectPainter( views::Painter::CreateSolidRoundRectPainter(
*background_color, highlight_radius, bg_insets))); *background_color, highlight_radius, paint_insets)));
} else { } else {
SetBackground(nullptr); SetBackground(nullptr);
} }
gfx::Insets new_insets = gfx::Insets target_insets =
layout_insets_.value_or(GetLayoutInsets(TOOLBAR_BUTTON)) + layout_insets_.value_or(GetLayoutInsets(TOOLBAR_BUTTON)) +
layout_inset_delta_ + *GetProperty(views::kInternalPaddingKey); layout_inset_delta_ + *GetProperty(views::kInternalPaddingKey);
base::Optional<SkColor> border_color = base::Optional<SkColor> border_color =
highlight_color_animation_.GetBorderColor(); highlight_color_animation_.GetBorderColor();
if (!border() || new_insets != border()->GetInsets() || if (!border() || target_insets != border()->GetInsets() ||
last_border_color_ != border_color) { last_border_color_ != border_color) {
if (border_color) { if (border_color) {
int border_thickness_dp = GetText().empty() int border_thickness_dp = GetText().empty()
? kBorderThicknessDpWithoutLabel ? kBorderThicknessDpWithoutLabel
: kBorderThicknessDpWithLabel; : kBorderThicknessDpWithLabel;
// Create a border with insets equal to |new_insets|, just split into a // Create a border with insets totalling |target_insets|, split into
// solid border and padding. // painted insets (matching the background) and internal padding to
SetBorder(views::CreatePaddedBorder( // position child views correctly.
views::CreateRoundedRectBorder(border_thickness_dp, highlight_radius, std::unique_ptr<views::Border> border = views::CreateRoundedRectBorder(
*border_color), border_thickness_dp, highlight_radius, paint_insets, *border_color);
new_insets - gfx::Insets(border_thickness_dp))); const gfx::Insets extra_insets = target_insets - border->GetInsets();
SetBorder(views::CreatePaddedBorder(std::move(border), extra_insets));
} else { } else {
SetBorder(views::CreateEmptyBorder(new_insets)); SetBorder(views::CreateEmptyBorder(target_insets));
} }
last_border_color_ = border_color; last_border_color_ = border_color;
} }
......
...@@ -77,7 +77,10 @@ gfx::Size SolidSidedBorder::GetMinimumSize() const { ...@@ -77,7 +77,10 @@ gfx::Size SolidSidedBorder::GetMinimumSize() const {
// A border with a rounded rectangle and single color. // A border with a rounded rectangle and single color.
class RoundedRectBorder : public Border { class RoundedRectBorder : public Border {
public: 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: // Overridden from Border:
void Paint(const View& view, gfx::Canvas* canvas) override; void Paint(const View& view, gfx::Canvas* canvas) override;
...@@ -87,6 +90,7 @@ class RoundedRectBorder : public Border { ...@@ -87,6 +90,7 @@ class RoundedRectBorder : public Border {
private: private:
const int thickness_; const int thickness_;
const int corner_radius_; const int corner_radius_;
const gfx::Insets paint_insets_;
const SkColor color_; const SkColor color_;
DISALLOW_COPY_AND_ASSIGN(RoundedRectBorder); DISALLOW_COPY_AND_ASSIGN(RoundedRectBorder);
...@@ -94,8 +98,12 @@ class RoundedRectBorder : public Border { ...@@ -94,8 +98,12 @@ class RoundedRectBorder : public Border {
RoundedRectBorder::RoundedRectBorder(int thickness, RoundedRectBorder::RoundedRectBorder(int thickness,
int corner_radius, int corner_radius,
const gfx::Insets& paint_insets,
SkColor color) 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) { void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) {
cc::PaintFlags flags; cc::PaintFlags flags;
...@@ -106,12 +114,13 @@ void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) { ...@@ -106,12 +114,13 @@ void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) {
float half_thickness = thickness_ / 2.0f; float half_thickness = thickness_ / 2.0f;
gfx::RectF bounds(view.GetLocalBounds()); gfx::RectF bounds(view.GetLocalBounds());
bounds.Inset(paint_insets_);
bounds.Inset(half_thickness, half_thickness); bounds.Inset(half_thickness, half_thickness);
canvas->DrawRoundRect(bounds, corner_radius_, flags); canvas->DrawRoundRect(bounds, corner_radius_, flags);
} }
gfx::Insets RoundedRectBorder::GetInsets() const { gfx::Insets RoundedRectBorder::GetInsets() const {
return gfx::Insets(thickness_); return gfx::Insets(thickness_) + paint_insets_;
} }
gfx::Size RoundedRectBorder::GetMinimumSize() const { gfx::Size RoundedRectBorder::GetMinimumSize() const {
...@@ -236,7 +245,15 @@ std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) { ...@@ -236,7 +245,15 @@ std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) {
std::unique_ptr<Border> CreateRoundedRectBorder(int thickness, std::unique_ptr<Border> CreateRoundedRectBorder(int thickness,
int corner_radius, int corner_radius,
SkColor color) { 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, std::unique_ptr<Border> CreateEmptyBorder(int top,
......
...@@ -73,6 +73,11 @@ VIEWS_EXPORT std::unique_ptr<Border> CreateSolidBorder(int thickness, ...@@ -73,6 +73,11 @@ VIEWS_EXPORT std::unique_ptr<Border> CreateSolidBorder(int thickness,
VIEWS_EXPORT std::unique_ptr<Border> CreateRoundedRectBorder(int thickness, VIEWS_EXPORT std::unique_ptr<Border> CreateRoundedRectBorder(int thickness,
int corner_radius, int corner_radius,
SkColor color); 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 // Creates a border for reserving space. The returned border does not paint
// anything. // 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