Commit db46f126 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Make Label's text style settable after construction.

This will be useful in some download item cleanup.

Bug: none
Change-Id: Iac4793db8fc13cdb935cee0360aee1f128fb8d6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2245346Reviewed-by: default avatarWei Li <weili@chromium.org>
Commit-Queue: Wei Li <weili@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779388}
parent 0faa5e49
...@@ -62,12 +62,22 @@ class LabelButtonLabelTest : public ViewsTestBase { ...@@ -62,12 +62,22 @@ class LabelButtonLabelTest : public ViewsTestBase {
void SetUp() override { void SetUp() override {
ViewsTestBase::SetUp(); ViewsTestBase::SetUp();
label_ = std::make_unique<TestLabel>(&last_color_);
widget_ = CreateTestWidget();
label_ =
widget_->SetContentsView(std::make_unique<TestLabel>(&last_color_));
label_->SetAutoColorReadabilityEnabled(false);
}
void TearDown() override {
widget_.reset();
ViewsTestBase::TearDown();
} }
protected: protected:
SkColor last_color_ = SK_ColorCYAN; SkColor last_color_ = gfx::kPlaceholderColor;
std::unique_ptr<TestLabel> label_; std::unique_ptr<views::Widget> widget_;
TestLabel* label_;
TestNativeTheme theme1_; TestNativeTheme theme1_;
TestNativeTheme theme2_; TestNativeTheme theme2_;
...@@ -77,16 +87,6 @@ class LabelButtonLabelTest : public ViewsTestBase { ...@@ -77,16 +87,6 @@ class LabelButtonLabelTest : public ViewsTestBase {
// Test that LabelButtonLabel reacts properly to themed and overridden colors. // Test that LabelButtonLabel reacts properly to themed and overridden colors.
TEST_F(LabelButtonLabelTest, Colors) { TEST_F(LabelButtonLabelTest, Colors) {
// The OnDidSchedulePaint() override won't be called while the base
// class is initialized. Not much we can do about that, so give it the first
// for free.
EXPECT_EQ(SK_ColorCYAN, last_color_); // Sanity check.
// At the same time we can check that changing the auto color readability
// schedules a paint. Currently it does. Although it technically doesn't need
// to since the color isn't actually changing.
label_->SetAutoColorReadabilityEnabled(false);
// First one comes from the default theme. This check ensures the SK_ColorRED // First one comes from the default theme. This check ensures the SK_ColorRED
// placeholder initializers were replaced. // placeholder initializers were replaced.
SkColor default_theme_enabled_color = SkColor default_theme_enabled_color =
......
...@@ -108,6 +108,19 @@ int Label::GetTextContext() const { ...@@ -108,6 +108,19 @@ int Label::GetTextContext() const {
return text_context_; return text_context_;
} }
int Label::GetTextStyle() const {
return text_style_;
}
void Label::SetTextStyle(int style) {
if (style == text_style_)
return;
text_style_ = style;
UpdateColorsFromTheme();
OnPropertyChanged(&text_style_, kPropertyEffectsPreferredSizeChanged);
}
bool Label::GetAutoColorReadabilityEnabled() const { bool Label::GetAutoColorReadabilityEnabled() const {
return auto_color_readability_enabled_; return auto_color_readability_enabled_;
} }
...@@ -958,7 +971,6 @@ void Label::Init(const base::string16& text, ...@@ -958,7 +971,6 @@ void Label::Init(const base::string16& text,
full_text_->SetCursorEnabled(false); full_text_->SetCursorEnabled(false);
full_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); full_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS);
UpdateColorsFromTheme();
SetText(text); SetText(text);
// Only selectable labels will get requests to show the context menu, due to // Only selectable labels will get requests to show the context menu, due to
...@@ -1110,8 +1122,9 @@ void Label::BuildContextMenuContents() { ...@@ -1110,8 +1122,9 @@ void Label::BuildContextMenuContents() {
BEGIN_METADATA(Label) BEGIN_METADATA(Label)
METADATA_PARENT_CLASS(View) METADATA_PARENT_CLASS(View)
ADD_PROPERTY_METADATA(Label, bool, AutoColorReadabilityEnabled)
ADD_PROPERTY_METADATA(Label, base::string16, Text) ADD_PROPERTY_METADATA(Label, base::string16, Text)
ADD_PROPERTY_METADATA(Label, int, TextStyle)
ADD_PROPERTY_METADATA(Label, bool, AutoColorReadabilityEnabled)
ADD_PROPERTY_METADATA(Label, SkColor, EnabledColor) ADD_PROPERTY_METADATA(Label, SkColor, EnabledColor)
ADD_PROPERTY_METADATA(Label, gfx::ElideBehavior, ElideBehavior) ADD_PROPERTY_METADATA(Label, gfx::ElideBehavior, ElideBehavior)
ADD_PROPERTY_METADATA(Label, SkColor, BackgroundColor) ADD_PROPERTY_METADATA(Label, SkColor, BackgroundColor)
......
...@@ -91,6 +91,11 @@ class VIEWS_EXPORT Label : public View, ...@@ -91,6 +91,11 @@ class VIEWS_EXPORT Label : public View,
// a value from views::style::TextContext or an enum that extends it. // a value from views::style::TextContext or an enum that extends it.
int GetTextContext() const; int GetTextContext() const;
// The style of the label. This is a value from views::style::TextStyle or an
// enum that extends it.
int GetTextStyle() const;
void SetTextStyle(int style);
// Enables or disables auto-color-readability (enabled by default). If this // Enables or disables auto-color-readability (enabled by default). If this
// is enabled, then calls to set any foreground or background color will // is enabled, then calls to set any foreground or background color will
// trigger an automatic mapper that uses color_utils::BlendForMinContrast() // trigger an automatic mapper that uses color_utils::BlendForMinContrast()
...@@ -384,7 +389,7 @@ class VIEWS_EXPORT Label : public View, ...@@ -384,7 +389,7 @@ class VIEWS_EXPORT Label : public View,
void BuildContextMenuContents(); void BuildContextMenuContents();
const int text_context_; const int text_context_;
const int text_style_; int text_style_;
// An un-elided and single-line RenderText object used for preferred sizing. // An un-elided and single-line RenderText object used for preferred sizing.
std::unique_ptr<gfx::RenderText> full_text_; std::unique_ptr<gfx::RenderText> full_text_;
......
...@@ -291,6 +291,11 @@ TEST_F(LabelTest, TextProperty) { ...@@ -291,6 +291,11 @@ TEST_F(LabelTest, TextProperty) {
EXPECT_EQ(test_text, label()->GetText()); EXPECT_EQ(test_text, label()->GetText());
} }
TEST_F(LabelTest, TextStyleProperty) {
label()->SetTextStyle(views::style::STYLE_DISABLED);
EXPECT_EQ(views::style::STYLE_DISABLED, label()->GetTextStyle());
}
TEST_F(LabelTest, ColorProperty) { TEST_F(LabelTest, ColorProperty) {
SkColor color = SkColorSetARGB(20, 40, 10, 5); SkColor color = SkColorSetARGB(20, 40, 10, 5);
label()->SetAutoColorReadabilityEnabled(false); label()->SetAutoColorReadabilityEnabled(false);
......
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