Commit 1e740e13 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Remove image button background alignment.

This was only set once, and the caller set it to the default behavior.

Bug: 957264
Change-Id: I13b47fe69e44fe8394bf22fe6433655023d1b4c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1640604
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Ahmed Fakhry <afakhry@chromium.org>
Reviewed-by: default avatarAhmed Fakhry <afakhry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#665707}
parent 91ec7eca
...@@ -37,8 +37,6 @@ CloseDeskButton::CloseDeskButton(views::ButtonListener* listener) ...@@ -37,8 +37,6 @@ CloseDeskButton::CloseDeskButton(views::ButtonListener* listener)
gfx::CreateVectorIcon(kDesksCloseDeskButtonIcon, SK_ColorWHITE)); gfx::CreateVectorIcon(kDesksCloseDeskButtonIcon, SK_ColorWHITE));
SetImageHorizontalAlignment(views::ImageButton::ALIGN_CENTER); SetImageHorizontalAlignment(views::ImageButton::ALIGN_CENTER);
SetImageVerticalAlignment(views::ImageButton::ALIGN_MIDDLE); SetImageVerticalAlignment(views::ImageButton::ALIGN_MIDDLE);
SetBackgroundImageAlignment(views::ImageButton::ALIGN_CENTER,
views::ImageButton::ALIGN_MIDDLE);
SetBackground( SetBackground(
CreateBackgroundFromPainter(views::Painter::CreateSolidRoundRectPainter( CreateBackgroundFromPainter(views::Painter::CreateSolidRoundRectPainter(
kBackgroundColor, kCornerRadius))); kBackgroundColor, kCornerRadius)));
......
...@@ -94,13 +94,6 @@ void ImageButton::SetImageVerticalAlignment(VerticalAlignment v_alignment) { ...@@ -94,13 +94,6 @@ void ImageButton::SetImageVerticalAlignment(VerticalAlignment v_alignment) {
OnPropertyChanged(&v_alignment_, kPropertyEffectsPaint); OnPropertyChanged(&v_alignment_, kPropertyEffectsPaint);
} }
void ImageButton::SetBackgroundImageAlignment(HorizontalAlignment h_align,
VerticalAlignment v_align) {
h_background_alignment_ = h_align;
v_background_alignment_ = v_align;
SchedulePaint();
}
gfx::Size ImageButton::GetMinimumImageSize() const { gfx::Size ImageButton::GetMinimumImageSize() const {
return minimum_image_size_; return minimum_image_size_;
} }
...@@ -160,20 +153,14 @@ void ImageButton::PaintButtonContents(gfx::Canvas* canvas) { ...@@ -160,20 +153,14 @@ void ImageButton::PaintButtonContents(gfx::Canvas* canvas) {
} }
if (!background_image_.isNull()) { if (!background_image_.isNull()) {
// If the background image alignment was not set, use the image // The background image alignment is the same as for the image.
// alignment. gfx::Point background_position =
HorizontalAlignment h_alignment = ComputeImagePaintPosition(background_image_);
h_background_alignment_.value_or(GetImageHorizontalAlignment());
VerticalAlignment v_alignment =
v_background_alignment_.value_or(GetImageVerticalAlignment());
gfx::Point background_position = ComputeImagePaintPosition(
background_image_, h_alignment, v_alignment);
canvas->DrawImageInt(background_image_, background_position.x(), canvas->DrawImageInt(background_image_, background_position.x(),
background_position.y()); background_position.y());
} }
gfx::Point position = ComputeImagePaintPosition( gfx::Point position = ComputeImagePaintPosition(img);
img, GetImageHorizontalAlignment(), GetImageVerticalAlignment());
canvas->DrawImageInt(img, position.x(), position.y()); canvas->DrawImageInt(img, position.x(), position.y());
} }
} }
...@@ -199,12 +186,9 @@ gfx::ImageSkia ImageButton::GetImageToPaint() { ...@@ -199,12 +186,9 @@ gfx::ImageSkia ImageButton::GetImageToPaint() {
// ImageButton, private: // ImageButton, private:
const gfx::Point ImageButton::ComputeImagePaintPosition( const gfx::Point ImageButton::ComputeImagePaintPosition(
const gfx::ImageSkia& image, const gfx::ImageSkia& image) const {
HorizontalAlignment h_alignment, HorizontalAlignment h_alignment = GetImageHorizontalAlignment();
VerticalAlignment v_alignment) { VerticalAlignment v_alignment = GetImageVerticalAlignment();
int x = 0, y = 0;
gfx::Rect rect = GetContentsBounds();
if (draw_image_mirrored_) { if (draw_image_mirrored_) {
if (h_alignment == ALIGN_RIGHT) if (h_alignment == ALIGN_RIGHT)
h_alignment = ALIGN_LEFT; h_alignment = ALIGN_LEFT;
...@@ -212,20 +196,21 @@ const gfx::Point ImageButton::ComputeImagePaintPosition( ...@@ -212,20 +196,21 @@ const gfx::Point ImageButton::ComputeImagePaintPosition(
h_alignment = ALIGN_RIGHT; h_alignment = ALIGN_RIGHT;
} }
const gfx::Rect rect = GetContentsBounds();
int x = 0;
if (h_alignment == ALIGN_CENTER) if (h_alignment == ALIGN_CENTER)
x = (rect.width() - image.width()) / 2; x = (rect.width() - image.width()) / 2;
else if (h_alignment == ALIGN_RIGHT) else if (h_alignment == ALIGN_RIGHT)
x = rect.width() - image.width(); x = rect.width() - image.width();
int y = 0;
if (v_alignment == ALIGN_MIDDLE) if (v_alignment == ALIGN_MIDDLE)
y = (rect.height() - image.height()) / 2; y = (rect.height() - image.height()) / 2;
else if (v_alignment == ALIGN_BOTTOM) else if (v_alignment == ALIGN_BOTTOM)
y = rect.height() - image.height(); y = rect.height() - image.height();
x += rect.x(); return rect.origin() + gfx::Vector2d(x, y);
y += rect.y();
return gfx::Point(x, y);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
...@@ -53,7 +53,8 @@ class VIEWS_EXPORT ImageButton : public Button { ...@@ -53,7 +53,8 @@ class VIEWS_EXPORT ImageButton : public Button {
// consolidated. // consolidated.
virtual void SetImage(ButtonState state, const gfx::ImageSkia& image); virtual void SetImage(ButtonState state, const gfx::ImageSkia& image);
// Set the background details. // Set the background details. The background image uses the same alignment
// as the image.
void SetBackgroundImage(SkColor color, void SetBackgroundImage(SkColor color,
const gfx::ImageSkia* image, const gfx::ImageSkia* image,
const gfx::ImageSkia* mask); const gfx::ImageSkia* mask);
...@@ -64,10 +65,6 @@ class VIEWS_EXPORT ImageButton : public Button { ...@@ -64,10 +65,6 @@ class VIEWS_EXPORT ImageButton : public Button {
void SetImageHorizontalAlignment(HorizontalAlignment h_alignment); void SetImageHorizontalAlignment(HorizontalAlignment h_alignment);
void SetImageVerticalAlignment(VerticalAlignment v_alignment); void SetImageVerticalAlignment(VerticalAlignment v_alignment);
// Sets how the background is laid out within the button's bounds.
void SetBackgroundImageAlignment(HorizontalAlignment h_align,
VerticalAlignment v_align);
// The minimum size of the contents (not including the border). The contents // The minimum size of the contents (not including the border). The contents
// will be at least this size, but may be larger if the image itself is // will be at least this size, but may be larger if the image itself is
// larger. // larger.
...@@ -110,20 +107,13 @@ class VIEWS_EXPORT ImageButton : public Button { ...@@ -110,20 +107,13 @@ class VIEWS_EXPORT ImageButton : public Button {
FRIEND_TEST_ALL_PREFIXES(ImageButtonFactoryTest, CreateVectorImageButton); FRIEND_TEST_ALL_PREFIXES(ImageButtonFactoryTest, CreateVectorImageButton);
// Returns the correct position of the image for painting. // Returns the correct position of the image for painting.
const gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image, const gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image) const;
HorizontalAlignment h_alignment,
VerticalAlignment v_alignment);
// Image alignment. // Image alignment.
HorizontalAlignment h_alignment_ = ALIGN_LEFT; HorizontalAlignment h_alignment_ = ALIGN_LEFT;
VerticalAlignment v_alignment_ = ALIGN_TOP; VerticalAlignment v_alignment_ = ALIGN_TOP;
gfx::Size minimum_image_size_; gfx::Size minimum_image_size_;
// Background alignment. If these are not set, the background image uses the
// image alignment.
base::Optional<HorizontalAlignment> h_background_alignment_;
base::Optional<VerticalAlignment> v_background_alignment_;
// Whether we draw our resources horizontally flipped. This can happen in the // Whether we draw our resources horizontally flipped. This can happen in the
// linux titlebar, where image resources were designed to be flipped so a // linux titlebar, where image resources were designed to be flipped so a
// small curved corner in the close button designed to fit into the frame // small curved corner in the close button designed to fit into the frame
......
...@@ -39,13 +39,6 @@ class Parent : public views::View { ...@@ -39,13 +39,6 @@ class Parent : public views::View {
namespace views { namespace views {
namespace {
const ImageButton::HorizontalAlignment kDefaultHorizontalAlignment =
ImageButton::ALIGN_LEFT;
const ImageButton::VerticalAlignment kDefaultVerticalAlignment =
ImageButton::ALIGN_TOP;
} // namespace
using ImageButtonTest = ViewsTestBase; using ImageButtonTest = ViewsTestBase;
TEST_F(ImageButtonTest, Basics) { TEST_F(ImageButtonTest, Basics) {
...@@ -128,30 +121,20 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) { ...@@ -128,30 +121,20 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) {
button.SetImage(Button::STATE_NORMAL, &image); button.SetImage(Button::STATE_NORMAL, &image);
// The image should be painted at the top-left corner. // The image should be painted at the top-left corner.
EXPECT_EQ(gfx::Point(), EXPECT_EQ(gfx::Point(), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, kDefaultHorizontalAlignment,
kDefaultVerticalAlignment));
button.SetBorder(views::CreateEmptyBorder(10, 5, 0, 0)); button.SetBorder(views::CreateEmptyBorder(10, 5, 0, 0));
EXPECT_EQ(gfx::Point(5, 10), EXPECT_EQ(gfx::Point(5, 10), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, kDefaultHorizontalAlignment,
kDefaultVerticalAlignment));
button.SetBorder(NullBorder()); button.SetBorder(NullBorder());
button.SetBounds(0, 0, 50, 50); button.SetBounds(0, 0, 50, 50);
EXPECT_EQ(gfx::Point(), EXPECT_EQ(gfx::Point(), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, kDefaultHorizontalAlignment,
kDefaultVerticalAlignment));
button.SetImageHorizontalAlignment(ImageButton::ALIGN_CENTER); button.SetImageHorizontalAlignment(ImageButton::ALIGN_CENTER);
button.SetImageVerticalAlignment(ImageButton::ALIGN_MIDDLE); button.SetImageVerticalAlignment(ImageButton::ALIGN_MIDDLE);
EXPECT_EQ(gfx::Point(15, 10), EXPECT_EQ(gfx::Point(15, 10), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, ImageButton::ALIGN_CENTER,
ImageButton::ALIGN_MIDDLE));
button.SetBorder(views::CreateEmptyBorder(10, 10, 0, 0)); button.SetBorder(views::CreateEmptyBorder(10, 10, 0, 0));
EXPECT_EQ(gfx::Point(20, 15), EXPECT_EQ(gfx::Point(20, 15), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, ImageButton::ALIGN_CENTER,
ImageButton::ALIGN_MIDDLE));
// The entire button's size should take the border into account. // The entire button's size should take the border into account.
EXPECT_EQ(gfx::Size(30, 40), button.GetPreferredSize()); EXPECT_EQ(gfx::Size(30, 40), button.GetPreferredSize());
...@@ -171,9 +154,7 @@ TEST_F(ImageButtonTest, LeftAlignedMirrored) { ...@@ -171,9 +154,7 @@ TEST_F(ImageButtonTest, LeftAlignedMirrored) {
// Because the coordinates are flipped, we should expect this to draw as if // Because the coordinates are flipped, we should expect this to draw as if
// it were ALIGN_RIGHT. // it were ALIGN_RIGHT.
EXPECT_EQ(gfx::Point(30, 0), EXPECT_EQ(gfx::Point(30, 0), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, ImageButton::ALIGN_LEFT,
ImageButton::ALIGN_BOTTOM));
} }
TEST_F(ImageButtonTest, RightAlignedMirrored) { TEST_F(ImageButtonTest, RightAlignedMirrored) {
...@@ -187,9 +168,7 @@ TEST_F(ImageButtonTest, RightAlignedMirrored) { ...@@ -187,9 +168,7 @@ TEST_F(ImageButtonTest, RightAlignedMirrored) {
// Because the coordinates are flipped, we should expect this to draw as if // Because the coordinates are flipped, we should expect this to draw as if
// it were ALIGN_LEFT. // it were ALIGN_LEFT.
EXPECT_EQ(gfx::Point(0, 0), EXPECT_EQ(gfx::Point(0, 0), button.ComputeImagePaintPosition(image));
button.ComputeImagePaintPosition(image, ImageButton::ALIGN_RIGHT,
ImageButton::ALIGN_BOTTOM));
} }
TEST_F(ImageButtonTest, PreferredSizeInvalidation) { TEST_F(ImageButtonTest, PreferredSizeInvalidation) {
......
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