Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Unified Diff: views/controls/label.cc

Issue 8221027: Make views::Label and views::Link auto-color themselves to be readable over their background colo... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « views/controls/label.h ('k') | views/controls/label_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: views/controls/label.cc
===================================================================
--- views/controls/label.cc (revision 104959)
+++ views/controls/label.cc (working copy)
@@ -25,10 +25,6 @@
namespace views {
-// The colors to use for enabled and disabled labels.
-static SkColor kEnabledColor;
-static SkColor kDisabledColor;
-
// static
const char Label::kViewClassName[] = "views/Label";
@@ -81,19 +77,26 @@
return url_set_ ? url_ : GURL(UTF16ToUTF8(text_));
}
-void Label::SetColor(const SkColor& color) {
- color_ = color;
+void Label::SetAutoColorReadabilityEnabled(bool enabled) {
+ auto_color_readability_ = enabled;
+ RecalculateColors();
}
-SkColor Label::GetColor() const {
- return color_;
+void Label::SetEnabledColor(const SkColor& color) {
+ requested_enabled_color_ = color;
+ RecalculateColors();
}
-void Label::MakeReadableOverBackgroundColor(const SkColor& background_color) {
- SetColor(color_utils::GetReadableColor(
- IsEnabled() ? kEnabledColor : kDisabledColor, background_color));
+void Label::SetDisabledColor(const SkColor& color) {
+ requested_disabled_color_ = color;
+ RecalculateColors();
}
+void Label::SetBackgroundColor(const SkColor& color) {
+ background_color_ = color;
+ RecalculateColors();
+}
+
void Label::SetHorizontalAlignment(Alignment alignment) {
// If the View's UI layout is right-to-left and rtl_alignment_mode_ is
// USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment
@@ -216,11 +219,6 @@
return h + GetInsets().height();
}
-void Label::OnEnabledChanged() {
- View::OnEnabledChanged();
- SetColor(IsEnabled() ? kEnabledColor : kDisabledColor);
-}
-
std::string Label::GetClassName() const {
return kViewClassName;
}
@@ -269,9 +267,10 @@
const string16& text,
const gfx::Rect& text_bounds,
int flags) {
- canvas->DrawStringInt(text, font_, color_,
- text_bounds.x(), text_bounds.y(),
- text_bounds.width(), text_bounds.height(), flags);
+ canvas->DrawStringInt(text, font_,
+ IsEnabled() ? actual_enabled_color_ : actual_disabled_color_,
+ text_bounds.x(), text_bounds.y(), text_bounds.width(),
+ text_bounds.height(), flags);
if (HasFocus() || paint_as_focused_) {
gfx::Rect focus_bounds = text_bounds;
@@ -332,14 +331,19 @@
void Label::Init(const string16& text, const gfx::Font& font) {
static bool initialized = false;
+ static SkColor kDefaultEnabledColor;
+ static SkColor kDefaultDisabledColor;
+ static SkColor kDefaultBackgroundColor;
if (!initialized) {
#if defined(OS_WIN)
- kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
- kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
+ kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
+ kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
+ kDefaultBackgroundColor = color_utils::GetSysSkColor(COLOR_WINDOW);
#else
// TODO(beng): source from theme provider.
- kEnabledColor = SK_ColorBLACK;
- kDisabledColor = SK_ColorGRAY;
+ kDefaultEnabledColor = SK_ColorBLACK;
+ kDefaultDisabledColor = SK_ColorGRAY;
+ kDefaultBackgroundColor = SK_ColorWHITE;
#endif
initialized = true;
@@ -348,9 +352,12 @@
contains_mouse_ = false;
font_ = font;
text_size_valid_ = false;
- SetText(text);
url_set_ = false;
- color_ = kEnabledColor;
+ requested_enabled_color_ = kDefaultEnabledColor;
+ requested_disabled_color_ = kDefaultDisabledColor;
+ background_color_ = kDefaultBackgroundColor;
+ auto_color_readability_ = true;
+ RecalculateColors();
horiz_alignment_ = ALIGN_CENTER;
is_multi_line_ = false;
allow_character_break_ = false;
@@ -359,8 +366,21 @@
rtl_alignment_mode_ = USE_UI_ALIGNMENT;
paint_as_focused_ = false;
has_focus_border_ = false;
+
+ SetText(text);
}
+void Label::RecalculateColors() {
+ actual_enabled_color_ = auto_color_readability_ ?
+ color_utils::GetReadableColor(requested_enabled_color_,
+ background_color_) :
+ requested_enabled_color_;
+ actual_disabled_color_ = auto_color_readability_ ?
+ color_utils::GetReadableColor(requested_disabled_color_,
+ background_color_) :
+ requested_disabled_color_;
+}
+
void Label::UpdateContainsMouse(const MouseEvent& event) {
SetContainsMouse(GetTextBounds().Contains(event.x(), event.y()));
}
« no previous file with comments | « views/controls/label.h ('k') | views/controls/label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698