Commit 306b915c authored by mgiuca's avatar mgiuca Committed by Commit bot

views::ImageButton: Added SetMinimumImageSize; removed SetPreferredSize.

ImageButton::SetMinimumImageSize allows the client to explicitly set a
size larger than the image contained within the button. The image will
be aligned inside the button according to its alignment setting.

ImageButton::SetPreferredSize had a misleading name (since in fact it
only sets the size when there is no image) and is no longer needed. Its
original usage (r50040) is now gone. It wasn't having any effect in
either of its two remaining callers, since they always set an image
immediately afterwards. Therefore, deleted this method and removed both
calls to it. Note that we are keeping the default behaviour that an
ImageButton with no image has a size of 16x14. I wanted to remove this,
but the list of potential clients that could be relying on it is huge.

BUG=415909

Review URL: https://codereview.chromium.org/583843003

Cr-Commit-Position: refs/heads/master@{#295917}
parent 07cad335
...@@ -47,7 +47,6 @@ class VolumeButton : public views::ToggleImageButton { ...@@ -47,7 +47,6 @@ class VolumeButton : public views::ToggleImageButton {
SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE);
image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_AURA_UBER_TRAY_VOLUME_LEVELS); IDR_AURA_UBER_TRAY_VOLUME_LEVELS);
SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight));
Update(); Update();
} }
......
...@@ -16,7 +16,6 @@ namespace app_list { ...@@ -16,7 +16,6 @@ namespace app_list {
namespace { namespace {
const int kButtonImageSize = 32;
const int kButtonSpacing = 4; const int kButtonSpacing = 4;
const int kMinimumHeight = 39; const int kMinimumHeight = 39;
...@@ -49,12 +48,9 @@ ContentsSwitcherView::~ContentsSwitcherView() {} ...@@ -49,12 +48,9 @@ ContentsSwitcherView::~ContentsSwitcherView() {}
void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) {
views::ImageButton* button = new views::ImageButton(this); views::ImageButton* button = new views::ImageButton(this);
button->SetPreferredSize(gfx::Size(kButtonImageSize, kButtonImageSize)); button->SetImage(
if (resource_id) { views::CustomButton::STATE_NORMAL,
button->SetImage( ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
views::CustomButton::STATE_NORMAL,
ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
}
button->set_tag(page_index); button->set_tag(page_index);
// Add an indicator for the current launcher page. // Add an indicator for the current launcher page.
......
...@@ -28,8 +28,7 @@ class ContentsSwitcherView : public views::View, ...@@ -28,8 +28,7 @@ class ContentsSwitcherView : public views::View,
ContentsView* contents_view() const { return contents_view_; } ContentsView* contents_view() const { return contents_view_; }
// Adds a switcher button using |resource_id| as the button's image, which // Adds a switcher button using |resource_id| as the button's image, which
// opens the page with index |page_index| in the ContentsView. |resource_id| // opens the page with index |page_index| in the ContentsView.
// is ignored if it is 0.
void AddSwitcherButton(int resource_id, int page_index); void AddSwitcherButton(int resource_id, int page_index);
private: private:
......
...@@ -15,8 +15,11 @@ ...@@ -15,8 +15,11 @@
namespace views { namespace views {
static const int kDefaultWidth = 16; // Default button width if no theme. // Default button size if no image is set. This is ignored if there is an image,
static const int kDefaultHeight = 14; // Default button height if no theme. // and exists for historical reasons (any number of clients could depend on this
// behaviour).
static const int kDefaultWidth = 16;
static const int kDefaultHeight = 14;
const char ImageButton::kViewClassName[] = "ImageButton"; const char ImageButton::kViewClassName[] = "ImageButton";
...@@ -27,7 +30,6 @@ ImageButton::ImageButton(ButtonListener* listener) ...@@ -27,7 +30,6 @@ ImageButton::ImageButton(ButtonListener* listener)
: CustomButton(listener), : CustomButton(listener),
h_alignment_(ALIGN_LEFT), h_alignment_(ALIGN_LEFT),
v_alignment_(ALIGN_TOP), v_alignment_(ALIGN_TOP),
preferred_size_(kDefaultWidth, kDefaultHeight),
draw_image_mirrored_(false), draw_image_mirrored_(false),
focus_painter_(Painter::CreateDashedFocusPainter()) { focus_painter_(Painter::CreateDashedFocusPainter()) {
// By default, we request that the gfx::Canvas passed to our View::OnPaint() // By default, we request that the gfx::Canvas passed to our View::OnPaint()
...@@ -71,16 +73,26 @@ void ImageButton::SetFocusPainter(scoped_ptr<Painter> focus_painter) { ...@@ -71,16 +73,26 @@ void ImageButton::SetFocusPainter(scoped_ptr<Painter> focus_painter) {
focus_painter_ = focus_painter.Pass(); focus_painter_ = focus_painter.Pass();
} }
void ImageButton::SetMinimumImageSize(const gfx::Size& size) {
if (minimum_image_size_ == size)
return;
minimum_image_size_ = size;
PreferredSizeChanged();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// ImageButton, View overrides: // ImageButton, View overrides:
gfx::Size ImageButton::GetPreferredSize() const { gfx::Size ImageButton::GetPreferredSize() const {
gfx::Size size = preferred_size_; gfx::Size size(kDefaultWidth, kDefaultHeight);
if (!images_[STATE_NORMAL].isNull()) { if (!images_[STATE_NORMAL].isNull()) {
size = gfx::Size(images_[STATE_NORMAL].width(), size = gfx::Size(images_[STATE_NORMAL].width(),
images_[STATE_NORMAL].height()); images_[STATE_NORMAL].height());
} }
size.SetToMax(minimum_image_size_);
gfx::Insets insets = GetInsets(); gfx::Insets insets = GetInsets();
size.Enlarge(insets.width(), insets.height()); size.Enlarge(insets.width(), insets.height());
return size; return size;
......
...@@ -57,11 +57,11 @@ class VIEWS_EXPORT ImageButton : public CustomButton { ...@@ -57,11 +57,11 @@ class VIEWS_EXPORT ImageButton : public CustomButton {
void SetFocusPainter(scoped_ptr<Painter> focus_painter); void SetFocusPainter(scoped_ptr<Painter> focus_painter);
// Sets preferred size, so it could be correctly positioned in layout even if // The minimum size of the contents (not including the border). The contents
// it is NULL. // will be at least this size, but may be larger if the image itself is
void SetPreferredSize(const gfx::Size& preferred_size) { // larger.
preferred_size_ = preferred_size; const gfx::Size& minimum_image_size() const { return minimum_image_size_; }
} void SetMinimumImageSize(const gfx::Size& size);
// Whether we should draw our images resources horizontally flipped. // Whether we should draw our images resources horizontally flipped.
void SetDrawImageMirrored(bool mirrored) { void SetDrawImageMirrored(bool mirrored) {
...@@ -104,7 +104,7 @@ class VIEWS_EXPORT ImageButton : public CustomButton { ...@@ -104,7 +104,7 @@ class VIEWS_EXPORT ImageButton : public CustomButton {
// Image alignment. // Image alignment.
HorizontalAlignment h_alignment_; HorizontalAlignment h_alignment_;
VerticalAlignment v_alignment_; VerticalAlignment v_alignment_;
gfx::Size preferred_size_; gfx::Size minimum_image_size_;
// 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
......
...@@ -28,12 +28,13 @@ TEST_F(ImageButtonTest, Basics) { ...@@ -28,12 +28,13 @@ TEST_F(ImageButtonTest, Basics) {
// Our image to paint starts empty. // Our image to paint starts empty.
EXPECT_TRUE(button.GetImageToPaint().isNull()); EXPECT_TRUE(button.GetImageToPaint().isNull());
// Without a theme, buttons are 16x14 by default. // Without an image, buttons are 16x14 by default.
EXPECT_EQ("16x14", button.GetPreferredSize().ToString()); EXPECT_EQ("16x14", button.GetPreferredSize().ToString());
// We can set a preferred size when we have no image. // The minimum image size should be applied even when there is no image.
button.SetPreferredSize(gfx::Size(5, 15)); button.SetMinimumImageSize(gfx::Size(5, 15));
EXPECT_EQ("5x15", button.GetPreferredSize().ToString()); EXPECT_EQ("5x15", button.minimum_image_size().ToString());
EXPECT_EQ("16x15", button.GetPreferredSize().ToString());
// Set a normal image. // Set a normal image.
gfx::ImageSkia normal_image = CreateTestImage(10, 20); gfx::ImageSkia normal_image = CreateTestImage(10, 20);
...@@ -59,6 +60,14 @@ TEST_F(ImageButtonTest, Basics) { ...@@ -59,6 +60,14 @@ TEST_F(ImageButtonTest, Basics) {
EXPECT_FALSE(button.GetImageToPaint().isNull()); EXPECT_FALSE(button.GetImageToPaint().isNull());
EXPECT_EQ(10, button.GetImageToPaint().width()); EXPECT_EQ(10, button.GetImageToPaint().width());
EXPECT_EQ(20, button.GetImageToPaint().height()); EXPECT_EQ(20, button.GetImageToPaint().height());
// The minimum image size should make the preferred size bigger.
button.SetMinimumImageSize(gfx::Size(15, 5));
EXPECT_EQ("15x5", button.minimum_image_size().ToString());
EXPECT_EQ("15x20", button.GetPreferredSize().ToString());
button.SetMinimumImageSize(gfx::Size(15, 25));
EXPECT_EQ("15x25", button.minimum_image_size().ToString());
EXPECT_EQ("15x25", button.GetPreferredSize().ToString());
} }
TEST_F(ImageButtonTest, SetAndGetImage) { TEST_F(ImageButtonTest, SetAndGetImage) {
...@@ -112,6 +121,13 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) { ...@@ -112,6 +121,13 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) {
button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0)); button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0));
EXPECT_EQ(gfx::Point(20, 15).ToString(), EXPECT_EQ(gfx::Point(20, 15).ToString(),
button.ComputeImagePaintPosition(image).ToString()); button.ComputeImagePaintPosition(image).ToString());
// The entire button's size should take the border into account.
EXPECT_EQ(gfx::Size(30, 40).ToString(), button.GetPreferredSize().ToString());
// The border should be added on top of the minimum image size.
button.SetMinimumImageSize(gfx::Size(30, 5));
EXPECT_EQ(gfx::Size(40, 40).ToString(), button.GetPreferredSize().ToString());
} }
TEST_F(ImageButtonTest, LeftAlignedMirrored) { TEST_F(ImageButtonTest, LeftAlignedMirrored) {
......
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