diff options
author | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-21 13:43:48 +0000 |
---|---|---|
committer | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-21 13:43:48 +0000 |
commit | fe22ba158a40ef0593297aced79a444e057f63e5 (patch) | |
tree | 976ee1de258b5fc6dcbd9f99b9d2cd819b3f5529 /ui/views/controls/button | |
parent | a7aca1560b44b2e1a6a77f79bf80c8ee5306f047 (diff) | |
download | chromium_src-fe22ba158a40ef0593297aced79a444e057f63e5.zip chromium_src-fe22ba158a40ef0593297aced79a444e057f63e5.tar.gz chromium_src-fe22ba158a40ef0593297aced79a444e057f63e5.tar.bz2 |
Add views::Button style enum for LabelButton [native] styling, etc.
Add Button::ButtonStyle enum for existing [native] LabelButton styles.
Add ButtonStyle member and accessors (or ctor arg) to LabelButton[Border].
These replace LabelButton[Border] native_style flag, will support more later.
Add LabelButton::kViewClassName, use for manual RTTI in a DCHECK.
Move CustomButton::ButtonState to Button; update some usage.
Other minor cleanup and refactoring.
This should facilitate implementation and use of other LabelButton styles/types:
TODO(followup): Add toggled state for checkboxes, radio buttons, etc.
TODO(followup): Add Styles: "Chrome Style", Checkbox, Radio, Menu, ComboBox, etc.
(each style probably just needs to set some IDRs/colors/insets/etc. on LabelButton)
BUG=155363
TEST=No observable Views Button changes.
R=sky@chromium.org
NOTRY=true
(added notry for weird concatenated linux_rel step "browser_testsnet_unittests")
Review URL: https://chromiumcodereview.appspot.com/12330002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@183797 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views/controls/button')
-rw-r--r-- | ui/views/controls/button/button.h | 18 | ||||
-rw-r--r-- | ui/views/controls/button/custom_button.h | 11 | ||||
-rw-r--r-- | ui/views/controls/button/label_button.cc | 38 | ||||
-rw-r--r-- | ui/views/controls/button/label_button.h | 14 | ||||
-rw-r--r-- | ui/views/controls/button/label_button_border.cc | 45 | ||||
-rw-r--r-- | ui/views/controls/button/label_button_border.h | 17 | ||||
-rw-r--r-- | ui/views/controls/button/label_button_unittest.cc | 22 |
7 files changed, 91 insertions, 74 deletions
diff --git a/ui/views/controls/button/button.h b/ui/views/controls/button/button.h index 0563c1d..50180e4 100644 --- a/ui/views/controls/button/button.h +++ b/ui/views/controls/button/button.h @@ -28,6 +28,24 @@ class VIEWS_EXPORT Button : public View { public: virtual ~Button(); + // Button states for various button sub-types. + // TODO(msw): Add toggled state for checkboxes, radio buttons, etc. + enum ButtonState { + STATE_NORMAL = 0, + STATE_HOVERED, + STATE_PRESSED, + STATE_DISABLED, + STATE_COUNT, + }; + + // Button styles with associated images and border painters. + // TODO(msw): Add "Chrome Style", Checkbox, Radio, Menu, ComboBox, etc. + enum ButtonStyle { + STYLE_TEXTBUTTON = 0, + STYLE_NATIVE_TEXTBUTTON, + STYLE_COUNT, + }; + void SetTooltipText(const string16& tooltip_text); int tag() const { return tag_; } diff --git a/ui/views/controls/button/custom_button.h b/ui/views/controls/button/custom_button.h index bb3a6a2..c44d3c8 100644 --- a/ui/views/controls/button/custom_button.h +++ b/ui/views/controls/button/custom_button.h @@ -31,15 +31,6 @@ class VIEWS_EXPORT CustomButton : public Button, virtual ~CustomButton(); - // Possible states - enum ButtonState { - STATE_NORMAL = 0, - STATE_HOVERED, - STATE_PRESSED, - STATE_DISABLED, - STATE_COUNT - }; - // Get/sets the current display state of the button. ButtonState state() const { return state_; } void SetState(ButtonState state); @@ -158,7 +149,7 @@ class VIEWS_EXPORT CustomButton : public Button, class VIEWS_EXPORT CustomButtonStateChangedDelegate { public: virtual ~CustomButtonStateChangedDelegate() {} - virtual void StateChanged(CustomButton::ButtonState state) = 0; + virtual void StateChanged(Button::ButtonState state) = 0; protected: CustomButtonStateChangedDelegate() {} diff --git a/ui/views/controls/button/label_button.cc b/ui/views/controls/button/label_button.cc index 39164de..1063648 100644 --- a/ui/views/controls/button/label_button.cc +++ b/ui/views/controls/button/label_button.cc @@ -29,6 +29,9 @@ const int kHoverAnimationDurationMs = 170; namespace views { +// static +const char LabelButton::kViewClassName[] = "views/LabelButton"; + LabelButton::LabelButton(ButtonListener* listener, const string16& text) : CustomButton(listener), image_(new ImageView()), @@ -37,10 +40,7 @@ LabelButton::LabelButton(ButtonListener* listener, const string16& text) button_state_colors_(), explicitly_set_colors_(), is_default_(false), - native_theme_(false) { - set_border(new LabelButtonBorder()); - // Inset the button focus rect from the actual border; roughly match Windows. - set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); + style_(STYLE_TEXTBUTTON) { SetAnimationDuration(kHoverAnimationDurationMs); AddChildView(image_); @@ -50,8 +50,8 @@ LabelButton::LabelButton(ButtonListener* listener, const string16& text) label_->SetAutoColorReadabilityEnabled(false); label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); - // Initialize the colors, border, and layout for a Views-themed button. - SetNativeTheme(false); + // Initialize the colors, border, and layout. + SetStyle(style_); } LabelButton::~LabelButton() {} @@ -117,11 +117,11 @@ void LabelButton::SetIsDefault(bool is_default) { is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); } -void LabelButton::SetNativeTheme(bool native_theme) { - native_theme_ = native_theme; - LabelButtonBorder* border = new LabelButtonBorder(); - border->set_native_theme(native_theme); - set_border(border); +void LabelButton::SetStyle(ButtonStyle style) { + style_ = style; + set_border(new LabelButtonBorder(style)); + // Inset the button focus rect from the actual border; roughly match Windows. + set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); // Invalidate the layout to pickup the new insets from the border. InvalidateLayout(); ResetColorsFromNativeTheme(); @@ -155,6 +155,10 @@ gfx::Size LabelButton::GetPreferredSize() { return size; } +std::string LabelButton::GetClassName() const { + return kViewClassName; +} + void LabelButton::ResetColorsFromNativeTheme() { const ui::NativeTheme* theme = GetNativeTheme(); SkColor colors[STATE_COUNT] = { @@ -165,7 +169,8 @@ void LabelButton::ResetColorsFromNativeTheme() { }; #if defined(OS_WIN) // Native Windows buttons do not change color on hover or when pressed. - if (native_theme_ && theme == ui::NativeThemeWin::instance()) + if (style() == STYLE_NATIVE_TEXTBUTTON && + theme == ui::NativeThemeWin::instance()) colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; #endif for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { @@ -229,10 +234,6 @@ void LabelButton::Layout() { label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); } -std::string LabelButton::GetClassName() const { - return "views/LabelButton"; -} - void LabelButton::ChildPreferredSizeChanged(View* child) { PreferredSizeChanged(); } @@ -264,7 +265,8 @@ ui::NativeTheme::State LabelButton::GetThemeState( const ui::Animation* LabelButton::GetThemeAnimation() const { #if defined(OS_WIN) - if (native_theme_ && GetNativeTheme() == ui::NativeThemeWin::instance()) { + if (style() == STYLE_NATIVE_TEXTBUTTON && + GetNativeTheme() == ui::NativeThemeWin::instance()) { return ui::NativeThemeWin::instance()->IsThemingActive() ? hover_animation_.get() : NULL; } @@ -289,7 +291,7 @@ void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { params->button.indeterminate = false; params->button.is_default = is_default_; params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); - params->button.has_border = native_theme(); + params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; params->button.classic_state = 0; params->button.background_color = GetNativeTheme()->GetSystemColor( ui::NativeTheme::kColorId_TextButtonBackgroundColor); diff --git a/ui/views/controls/button/label_button.h b/ui/views/controls/button/label_button.h index f68a198..6e98f7c 100644 --- a/ui/views/controls/button/label_button.h +++ b/ui/views/controls/button/label_button.h @@ -20,6 +20,8 @@ namespace views { class VIEWS_EXPORT LabelButton : public CustomButton, public NativeThemeDelegate { public: + static const char kViewClassName[]; + LabelButton(ButtonListener* listener, const string16& text); virtual ~LabelButton(); @@ -56,12 +58,13 @@ class VIEWS_EXPORT LabelButton : public CustomButton, bool is_default() const { return is_default_; } void SetIsDefault(bool is_default); - // Get or set the option to use a native button appearance; false by default. - bool native_theme() const { return native_theme_; } - void SetNativeTheme(bool native_theme); + // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|. + ButtonStyle style() const { return style_; } + void SetStyle(ButtonStyle style); // Overridden from View: virtual gfx::Size GetPreferredSize() OVERRIDE; + virtual std::string GetClassName() const OVERRIDE; private: FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init); @@ -78,7 +81,6 @@ class VIEWS_EXPORT LabelButton : public CustomButton, // Overridden from View: virtual void Layout() OVERRIDE; - virtual std::string GetClassName() const OVERRIDE; virtual void ChildPreferredSizeChanged(View* child) OVERRIDE; virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; @@ -117,8 +119,8 @@ class VIEWS_EXPORT LabelButton : public CustomButton, // current context; bool is_default_; - // Flag indicating native theme styling (or Views styling) of the button. - bool native_theme_; + // The button's overall style. + ButtonStyle style_; DISALLOW_COPY_AND_ASSIGN(LabelButton); }; diff --git a/ui/views/controls/button/label_button_border.cc b/ui/views/controls/button/label_button_border.cc index 1c48efe..dccc3cc 100644 --- a/ui/views/controls/button/label_button_border.cc +++ b/ui/views/controls/button/label_button_border.cc @@ -29,15 +29,15 @@ static const int kPreferredNativeThemePaddingVertical = 5; const int kHotImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER); const int kPushedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED); -CustomButton::ButtonState GetButtonState(ui::NativeTheme::State state) { +Button::ButtonState GetButtonState(ui::NativeTheme::State state) { switch(state) { - case ui::NativeTheme::kDisabled: return CustomButton::STATE_DISABLED; - case ui::NativeTheme::kHovered: return CustomButton::STATE_HOVERED; - case ui::NativeTheme::kNormal: return CustomButton::STATE_NORMAL; - case ui::NativeTheme::kPressed: return CustomButton::STATE_PRESSED; + case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED; + case ui::NativeTheme::kHovered: return Button::STATE_HOVERED; + case ui::NativeTheme::kNormal: return Button::STATE_NORMAL; + case ui::NativeTheme::kPressed: return Button::STATE_PRESSED; case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state; } - return CustomButton::STATE_NORMAL; + return Button::STATE_NORMAL; } // A helper function to paint the native theme or images as appropriate. @@ -48,7 +48,7 @@ void PaintHelper(LabelButtonBorder* border, ui::NativeTheme::State state, const gfx::Rect& rect, const ui::NativeTheme::ExtraParams& extra) { - if (border->native_theme()) { + if (border->style() == Button::STYLE_NATIVE_TEXTBUTTON) { theme->Paint(canvas->sk_canvas(), part, state, rect, extra); } else { Painter* painter = border->GetPainter(GetButtonState(state)); @@ -59,16 +59,20 @@ void PaintHelper(LabelButtonBorder* border, } // namespace -LabelButtonBorder::LabelButtonBorder() : native_theme_(false) { - SetPainter(CustomButton::STATE_HOVERED, - Painter::CreateImageGridPainter(kHotImages)); - SetPainter(CustomButton::STATE_PRESSED, - Painter::CreateImageGridPainter(kPushedImages)); +LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style) + : style_(style) { + if (style == Button::STYLE_TEXTBUTTON) { + SetPainter(Button::STATE_HOVERED, + Painter::CreateImageGridPainter(kHotImages)); + SetPainter(Button::STATE_PRESSED, + Painter::CreateImageGridPainter(kPushedImages)); + } } LabelButtonBorder::~LabelButtonBorder() {} void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { + DCHECK(view.GetClassName() == LabelButton::kViewClassName); const NativeThemeDelegate* native_theme_delegate = static_cast<const LabelButton*>(&view); ui::NativeTheme::Part part = native_theme_delegate->GetThemePart(); @@ -95,28 +99,29 @@ void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { } // Draw the Views focus border for the native theme style. - if (native_theme() && view.focus_border() && extra.button.is_focused) + if (style() == Button::STYLE_NATIVE_TEXTBUTTON && + view.focus_border() && extra.button.is_focused) view.focus_border()->Paint(view, canvas); } gfx::Insets LabelButtonBorder::GetInsets() const { - if (native_theme()) { + if (style() == Button::STYLE_NATIVE_TEXTBUTTON) { return gfx::Insets(kPreferredNativeThemePaddingVertical, kPreferredNativeThemePaddingHorizontal, kPreferredNativeThemePaddingVertical, kPreferredNativeThemePaddingHorizontal); - } else { - return gfx::Insets(kPreferredPaddingVertical, kPreferredPaddingHorizontal, - kPreferredPaddingVertical, kPreferredPaddingHorizontal); } + DCHECK_EQ(style(), Button::STYLE_TEXTBUTTON); + return gfx::Insets(kPreferredPaddingVertical, kPreferredPaddingHorizontal, + kPreferredPaddingVertical, kPreferredPaddingHorizontal); } -Painter* LabelButtonBorder::GetPainter(CustomButton::ButtonState state) { +Painter* LabelButtonBorder::GetPainter(Button::ButtonState state) { return painters_[state].get(); } -void LabelButtonBorder::SetPainter(CustomButton::ButtonState state, - Painter* painter) { +void LabelButtonBorder::SetPainter(Button::ButtonState state, + Painter* painter) { painters_[state].reset(painter); } diff --git a/ui/views/controls/button/label_button_border.h b/ui/views/controls/button/label_button_border.h index 3bb4374..5ff011d 100644 --- a/ui/views/controls/button/label_button_border.h +++ b/ui/views/controls/button/label_button_border.h @@ -9,7 +9,7 @@ #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "ui/views/border.h" -#include "ui/views/controls/button/custom_button.h" +#include "ui/views/controls/button/button.h" #include "ui/views/painter.h" namespace views { @@ -17,11 +17,10 @@ namespace views { // A Border that paints a LabelButton's background frame. class VIEWS_EXPORT LabelButtonBorder : public Border { public: - LabelButtonBorder(); + explicit LabelButtonBorder(Button::ButtonStyle style); virtual ~LabelButtonBorder(); - bool native_theme() const { return native_theme_; } - void set_native_theme(bool native_theme) { native_theme_ = native_theme; } + Button::ButtonStyle style() const { return style_; } // Overridden from Border: virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE; @@ -29,15 +28,15 @@ class VIEWS_EXPORT LabelButtonBorder : public Border { // Get or set the painter used for the specified button state. // LabelButtonBorder takes and retains ownership of |painter|. - Painter* GetPainter(CustomButton::ButtonState state); - void SetPainter(CustomButton::ButtonState state, Painter* painter); + Painter* GetPainter(Button::ButtonState state); + void SetPainter(Button::ButtonState state, Painter* painter); private: // The painters used for each button state. - scoped_ptr<Painter> painters_[CustomButton::STATE_COUNT]; + scoped_ptr<Painter> painters_[Button::STATE_COUNT]; - // A flag controlling native (true) or Views theme styling; false by default. - bool native_theme_; + // The button style supplied in part by this border. + Button::ButtonStyle style_; DISALLOW_COPY_AND_ASSIGN(LabelButtonBorder); }; diff --git a/ui/views/controls/button/label_button_unittest.cc b/ui/views/controls/button/label_button_unittest.cc index be1c080..0cc8b27 100644 --- a/ui/views/controls/button/label_button_unittest.cc +++ b/ui/views/controls/button/label_button_unittest.cc @@ -29,16 +29,16 @@ TEST_F(LabelButtonTest, Init) { const string16 text(ASCIIToUTF16("abc")); LabelButton button(NULL, text); - EXPECT_TRUE(button.GetImage(CustomButton::STATE_NORMAL).isNull()); - EXPECT_TRUE(button.GetImage(CustomButton::STATE_HOVERED).isNull()); - EXPECT_TRUE(button.GetImage(CustomButton::STATE_PRESSED).isNull()); - EXPECT_TRUE(button.GetImage(CustomButton::STATE_DISABLED).isNull()); + EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); + EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); + EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); + EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); EXPECT_EQ(text, button.GetText()); EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); EXPECT_FALSE(button.is_default()); - EXPECT_FALSE(button.native_theme()); - EXPECT_EQ(CustomButton::STATE_NORMAL, button.state()); + EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); + EXPECT_EQ(Button::STATE_NORMAL, button.state()); EXPECT_EQ(button.image_->parent(), &button); EXPECT_EQ(button.label_->parent(), &button); @@ -85,15 +85,15 @@ TEST_F(LabelButtonTest, Image) { // The width increases monotonically with image size (it does not shrink). EXPECT_LT(button.GetPreferredSize().width(), small_size); EXPECT_LT(button.GetPreferredSize().height(), small_size); - button.SetImage(CustomButton::STATE_NORMAL, small_image); + button.SetImage(Button::STATE_NORMAL, small_image); EXPECT_GT(button.GetPreferredSize().width(), small_size); EXPECT_GT(button.GetPreferredSize().height(), small_size); EXPECT_LT(button.GetPreferredSize().width(), large_size); EXPECT_LT(button.GetPreferredSize().height(), large_size); - button.SetImage(CustomButton::STATE_NORMAL, large_image); + button.SetImage(Button::STATE_NORMAL, large_image); EXPECT_GT(button.GetPreferredSize().width(), large_size); EXPECT_GT(button.GetPreferredSize().height(), large_size); - button.SetImage(CustomButton::STATE_NORMAL, small_image); + button.SetImage(Button::STATE_NORMAL, small_image); EXPECT_GT(button.GetPreferredSize().width(), large_size); EXPECT_GT(button.GetPreferredSize().height(), large_size); @@ -127,7 +127,7 @@ TEST_F(LabelButtonTest, LabelAndImage) { EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size); EXPECT_LT(button.GetPreferredSize().height(), image_size); - button.SetImage(CustomButton::STATE_NORMAL, image); + button.SetImage(Button::STATE_NORMAL, image); EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); EXPECT_GT(button.GetPreferredSize().height(), image_size); @@ -149,7 +149,7 @@ TEST_F(LabelButtonTest, LabelAndImage) { button.SetText(string16()); EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); EXPECT_GT(button.GetPreferredSize().height(), image_size); - button.SetImage(CustomButton::STATE_NORMAL, gfx::ImageSkia()); + button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia()); EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); EXPECT_GT(button.GetPreferredSize().height(), image_size); |