summaryrefslogtreecommitdiffstats
path: root/views
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 23:08:10 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 23:08:10 +0000
commit9b0e63e553262024ffa72deaa8a650ee93e17e7d (patch)
tree7087b906a4335c72c45dc1491a15e7505904e9f5 /views
parentdac271e105147a82477735f261a0b224db1f4e30 (diff)
downloadchromium_src-9b0e63e553262024ffa72deaa8a650ee93e17e7d.zip
chromium_src-9b0e63e553262024ffa72deaa8a650ee93e17e7d.tar.gz
chromium_src-9b0e63e553262024ffa72deaa8a650ee93e17e7d.tar.bz2
Make views::Label and views::Link auto-color themselves to be readable over their background color. Unfortunately since they can't automatically determine what that backgruond color is it has to be manually set. (I suppose I could try to add some crazy hierarchy-walking code looking for views::Background objects, but that seems like a bad move.) There is also a disable switch, which I use in a few places where I couldn't figure out what the background color actually is, or where updating it is really annoying.
In theory, we might want to apply this to other controls like text buttons, but since those are usually rendered with explicit background images there's probably not a ton of win there. This also makes some other cleanup changes like converting a few wstrings to string16s, de-inlining some class member functions, etc. Some of these were mandated by the presubmitter :/ BUG=92 TEST=Things still look OK Review URL: http://codereview.chromium.org/8221027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105189 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r--views/controls/label.cc70
-rw-r--r--views/controls/label.h35
-rw-r--r--views/controls/label_unittest.cc5
-rw-r--r--views/controls/link.cc147
-rw-r--r--views/controls/link.h29
-rw-r--r--views/view_text_utils.cc12
6 files changed, 141 insertions, 157 deletions
diff --git a/views/controls/label.cc b/views/controls/label.cc
index 2340ba6..33fbcd4 100644
--- a/views/controls/label.cc
+++ b/views/controls/label.cc
@@ -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,17 +77,24 @@ const GURL Label::GetURL() const {
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();
+}
+
+void Label::SetEnabledColor(const SkColor& color) {
+ requested_enabled_color_ = color;
+ RecalculateColors();
}
-SkColor Label::GetColor() const {
- return color_;
+void Label::SetDisabledColor(const SkColor& color) {
+ requested_disabled_color_ = color;
+ RecalculateColors();
}
-void Label::MakeReadableOverBackgroundColor(const SkColor& background_color) {
- SetColor(color_utils::GetReadableColor(
- IsEnabled() ? kEnabledColor : kDisabledColor, background_color));
+void Label::SetBackgroundColor(const SkColor& color) {
+ background_color_ = color;
+ RecalculateColors();
}
void Label::SetHorizontalAlignment(Alignment alignment) {
@@ -216,11 +219,6 @@ int Label::GetHeightForWidth(int w) {
return h + GetInsets().height();
}
-void Label::OnEnabledChanged() {
- View::OnEnabledChanged();
- SetColor(IsEnabled() ? kEnabledColor : kDisabledColor);
-}
-
std::string Label::GetClassName() const {
return kViewClassName;
}
@@ -269,9 +267,10 @@ void Label::PaintText(gfx::Canvas* canvas,
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 @@ gfx::Font Label::GetDefaultFont() {
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 @@ void Label::Init(const string16& text, const gfx::Font& font) {
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,6 +366,19 @@ void Label::Init(const string16& text, const gfx::Font& font) {
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) {
diff --git a/views/controls/label.h b/views/controls/label.h
index 936a29d..1b7035c 100644
--- a/views/controls/label.h
+++ b/views/controls/label.h
@@ -75,16 +75,22 @@ class VIEWS_EXPORT Label : public View {
// Return the label URL.
const GURL GetURL() const;
- // Set the color
- virtual void SetColor(const SkColor& color);
+ // Enables or disables auto-color-readability (enabled by default). If this
+ // is enabled, then calls to set any foreground or background color will
+ // trigger an automatic mapper that uses color_utils::GetReadableColor() to
+ // ensure that the foreground colors are readable over the background color.
+ void SetAutoColorReadabilityEnabled(bool enabled);
- // Return a reference to the currently used color.
- virtual SkColor GetColor() const;
+ // Set the color. This will automatically force the color to be readable
+ // over the current background color.
+ virtual void SetEnabledColor(const SkColor& color);
+ void SetDisabledColor(const SkColor& color);
- // If you'll be displaying the label over some non-system background color,
- // call this with the relevant color and the label will auto-set its color to
- // be readable.
- virtual void MakeReadableOverBackgroundColor(const SkColor& background_color);
+ SkColor enabled_color() const { return actual_enabled_color_; }
+
+ // Set the background color. This won't be explicitly drawn, but the label
+ // will force the text color to be readable over it.
+ void SetBackgroundColor(const SkColor& color);
// Set horizontal alignment. If the locale is RTL, and the RTL alignment
// setting is set as USE_UI_ALIGNMENT, the alignment is flipped around.
@@ -168,8 +174,6 @@ class VIEWS_EXPORT Label : public View {
// This method is used to layout multi-line labels. It is equivalent to
// GetPreferredSize().height() if the receiver is not multi-line.
virtual int GetHeightForWidth(int w);
- // Sets the enabled state. Setting the enabled state resets the color.
- virtual void OnEnabledChanged() OVERRIDE;
virtual std::string GetClassName() const OVERRIDE;
virtual bool HitTest(const gfx::Point& l) const OVERRIDE;
// Mouse enter/exit are overridden to render mouse over background color.
@@ -196,6 +200,8 @@ class VIEWS_EXPORT Label : public View {
virtual gfx::Size GetTextSize() const;
+ SkColor disabled_color() const { return actual_disabled_color_; }
+
// Overridden from View:
// Overridden to dirty our text bounds if we're multi-line.
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
@@ -216,6 +222,8 @@ class VIEWS_EXPORT Label : public View {
void Init(const string16& text, const gfx::Font& font);
+ void RecalculateColors();
+
// If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise
// SetContainsMouse(false) is invoked.
void UpdateContainsMouse(const MouseEvent& event);
@@ -240,7 +248,12 @@ class VIEWS_EXPORT Label : public View {
string16 text_;
GURL url_;
gfx::Font font_;
- SkColor color_;
+ SkColor requested_enabled_color_;
+ SkColor actual_enabled_color_;
+ SkColor requested_disabled_color_;
+ SkColor actual_disabled_color_;
+ SkColor background_color_;
+ bool auto_color_readability_;
mutable gfx::Size text_size_;
mutable bool text_size_valid_;
bool is_multi_line_;
diff --git a/views/controls/label_unittest.cc b/views/controls/label_unittest.cc
index 68fc296..9a86fc8 100644
--- a/views/controls/label_unittest.cc
+++ b/views/controls/label_unittest.cc
@@ -58,8 +58,9 @@ TEST(LabelTest, UrlProperty) {
TEST(LabelTest, ColorProperty) {
Label label;
SkColor color = SkColorSetARGB(20, 40, 10, 5);
- label.SetColor(color);
- EXPECT_EQ(color, label.GetColor());
+ label.SetAutoColorReadabilityEnabled(false);
+ label.SetEnabledColor(color);
+ EXPECT_EQ(color, label.enabled_color());
}
TEST(LabelTest, AlignmentProperty) {
diff --git a/views/controls/link.cc b/views/controls/link.cc
index 2131ed1..08f2447 100644
--- a/views/controls/link.cc
+++ b/views/controls/link.cc
@@ -23,74 +23,23 @@
#include "ui/gfx/gtk_util.h"
#endif
-namespace {
-
-void GetColors(const SkColor* background_color, // NULL means "use default"
- SkColor* highlighted_color,
- SkColor* disabled_color,
- SkColor* normal_color) {
- static SkColor kHighlightedColor, kDisabledColor, kNormalColor;
- static bool initialized = false;
- if (!initialized) {
-#if defined(OS_WIN)
- kHighlightedColor = color_utils::GetReadableColor(
- SkColorSetRGB(200, 0, 0), color_utils::GetSysSkColor(COLOR_WINDOW));
- kDisabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
- kNormalColor = color_utils::GetSysSkColor(COLOR_HOTLIGHT);
-#else
- // TODO(beng): source from theme provider.
- kHighlightedColor = SK_ColorRED;
- kDisabledColor = SK_ColorBLACK;
- kNormalColor = SkColorSetRGB(0, 51, 153);
-#endif
-
- initialized = true;
- }
-
- if (background_color) {
- *highlighted_color = color_utils::GetReadableColor(kHighlightedColor,
- *background_color);
- *disabled_color = color_utils::GetReadableColor(kDisabledColor,
- *background_color);
- *normal_color = color_utils::GetReadableColor(kNormalColor,
- *background_color);
- } else {
- *highlighted_color = kHighlightedColor;
- *disabled_color = kDisabledColor;
- *normal_color = kNormalColor;
- }
-}
-}
-
namespace views {
const char Link::kViewClassName[] = "views/Link";
-Link::Link() : Label(string16()),
- listener_(NULL),
- highlighted_(false) {
+Link::Link() : Label(string16()) {
Init();
- set_focusable(true);
}
-Link::Link(const string16& title) : Label(title),
- listener_(NULL),
- highlighted_(false) {
+Link::Link(const string16& title) : Label(title) {
Init();
- set_focusable(true);
-}
-
-void Link::Init() {
- GetColors(NULL, &highlighted_color_, &disabled_color_, &normal_color_);
- SetColor(normal_color_);
- ValidateStyle();
}
Link::~Link() {
}
void Link::OnEnabledChanged() {
- ValidateStyle();
+ RecalculateFont();
View::OnEnabledChanged();
}
@@ -122,14 +71,14 @@ bool Link::OnMousePressed(const MouseEvent& event) {
if (!IsEnabled() ||
(!event.IsLeftMouseButton() && !event.IsMiddleMouseButton()))
return false;
- SetHighlighted(true);
+ SetPressed(true);
return true;
}
bool Link::OnMouseDragged(const MouseEvent& event) {
- SetHighlighted(IsEnabled() &&
- (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
- HitTest(event.location()));
+ SetPressed(IsEnabled() &&
+ (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
+ HitTest(event.location()));
return true;
}
@@ -149,7 +98,7 @@ void Link::OnMouseReleased(const MouseEvent& event) {
}
void Link::OnMouseCaptureLost() {
- SetHighlighted(false);
+ SetPressed(false);
}
bool Link::OnKeyPressed(const KeyEvent& event) {
@@ -158,7 +107,7 @@ bool Link::OnKeyPressed(const KeyEvent& event) {
if (!activate)
return false;
- SetHighlighted(false);
+ SetPressed(false);
// Focus the link on key pressed.
RequestFocus();
@@ -182,50 +131,66 @@ void Link::GetAccessibleState(ui::AccessibleViewState* state) {
void Link::SetFont(const gfx::Font& font) {
Label::SetFont(font);
- ValidateStyle();
+ RecalculateFont();
}
-void Link::SetHighlightedColor(const SkColor& color) {
- highlighted_color_ = color;
- ValidateStyle();
+void Link::SetEnabledColor(const SkColor& color) {
+ requested_enabled_color_ = color;
+ if (!pressed_)
+ Label::SetEnabledColor(requested_enabled_color_);
}
-void Link::SetDisabledColor(const SkColor& color) {
- disabled_color_ = color;
- ValidateStyle();
+void Link::SetPressedColor(const SkColor& color) {
+ requested_pressed_color_ = color;
+ if (pressed_)
+ Label::SetEnabledColor(requested_pressed_color_);
}
-void Link::SetNormalColor(const SkColor& color) {
- normal_color_ = color;
- ValidateStyle();
-}
+void Link::Init() {
+ static bool initialized = false;
+ static SkColor kDefaultEnabledColor;
+ static SkColor kDefaultDisabledColor;
+ static SkColor kDefaultPressedColor;
+ if (!initialized) {
+#if defined(OS_WIN)
+ kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_HOTLIGHT);
+ kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
+ kDefaultPressedColor = SkColorSetRGB(200, 0, 0);
+#else
+ // TODO(beng): source from theme provider.
+ kDefaultEnabledColor = SkColorSetRGB(0, 51, 153);
+ kDefaultDisabledColor = SK_ColorBLACK;
+ kDefaultPressedColor = SK_ColorRED;
+#endif
-void Link::MakeReadableOverBackgroundColor(const SkColor& color) {
- GetColors(&color, &highlighted_color_, &disabled_color_, &normal_color_);
- ValidateStyle();
+ initialized = true;
+ }
+
+ listener_ = NULL;
+ pressed_ = false;
+ SetEnabledColor(kDefaultEnabledColor);
+ SetDisabledColor(kDefaultDisabledColor);
+ SetPressedColor(kDefaultPressedColor);
+ RecalculateFont();
+ set_focusable(true);
}
-void Link::SetHighlighted(bool f) {
- if (f != highlighted_) {
- highlighted_ = f;
- ValidateStyle();
+void Link::SetPressed(bool pressed) {
+ if (pressed_ != pressed) {
+ pressed_ = pressed;
+ Label::SetEnabledColor(pressed_ ?
+ requested_pressed_color_ : requested_enabled_color_);
+ RecalculateFont();
SchedulePaint();
}
}
-void Link::ValidateStyle() {
- if (IsEnabled()) {
- if (!(font().GetStyle() & gfx::Font::UNDERLINED)) {
- Label::SetFont(
- font().DeriveFont(0, font().GetStyle() | gfx::Font::UNDERLINED));
- }
- Label::SetColor(highlighted_ ? highlighted_color_ : normal_color_);
- } else {
- if (font().GetStyle() & gfx::Font::UNDERLINED) {
- Label::SetFont(
- font().DeriveFont(0, font().GetStyle() & ~gfx::Font::UNDERLINED));
- }
- Label::SetColor(disabled_color_);
+void Link::RecalculateFont() {
+ // The font should be underlined iff the link is enabled.
+ if (IsEnabled() == !(font().GetStyle() & gfx::Font::UNDERLINED)) {
+ Label::SetFont(font().DeriveFont(0, IsEnabled() ?
+ (font().GetStyle() | gfx::Font::UNDERLINED) :
+ (font().GetStyle() & ~gfx::Font::UNDERLINED)));
}
}
diff --git a/views/controls/link.h b/views/controls/link.h
index ac39f15..4a07f46 100644
--- a/views/controls/link.h
+++ b/views/controls/link.h
@@ -46,36 +46,29 @@ class VIEWS_EXPORT Link : public Label {
// Overridden from Label:
virtual void SetFont(const gfx::Font& font) OVERRIDE;
- virtual void MakeReadableOverBackgroundColor(const SkColor& color) OVERRIDE;
- void SetHighlightedColor(const SkColor& color);
- void SetDisabledColor(const SkColor& color);
- void SetNormalColor(const SkColor& color);
+ virtual void SetEnabledColor(const SkColor& color) OVERRIDE;
+ void SetPressedColor(const SkColor& color);
static const char kViewClassName[];
private:
- // A highlighted link is clicked.
- void SetHighlighted(bool f);
+ void Init();
- // Make sure the label style matched the current state.
- void ValidateStyle();
+ void SetPressed(bool pressed);
- void Init();
+ void RecalculateFont();
LinkListener* listener_;
- // Whether the link is currently highlighted.
- bool highlighted_;
-
- // The color when the link is highlighted.
- SkColor highlighted_color_;
+ // Whether the link is currently pressed.
+ bool pressed_;
- // The color when the link is disabled.
- SkColor disabled_color_;
+ // The color when the link is neither pressed nor disabled.
+ SkColor requested_enabled_color_;
- // The color when the link is neither highlighted nor disabled.
- SkColor normal_color_;
+ // The color when the link is pressed.
+ SkColor requested_pressed_color_;
DISALLOW_COPY_AND_ASSIGN(Link);
};
diff --git a/views/view_text_utils.cc b/views/view_text_utils.cc
index d37498a..645f402 100644
--- a/views/view_text_utils.cc
+++ b/views/view_text_utils.cc
@@ -9,7 +9,6 @@
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "ui/gfx/canvas_skia.h"
-#include "ui/gfx/color_utils.h"
#include "ui/gfx/size.h"
#include "views/controls/label.h"
#include "views/controls/link.h"
@@ -88,13 +87,6 @@ void DrawTextStartingFrom(gfx::Canvas* canvas,
const gfx::Font& font,
bool text_direction_is_rtl,
bool ltr_within_rtl) {
-#if defined(OS_WIN)
- const SkColor text_color = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
-#else
- // TODO(beng): source from theme provider.
- const SkColor text_color = SK_ColorBLACK;
-#endif
-
// Iterate through line breaking opportunities (which in English would be
// spaces and such). This tells us where to wrap.
string16 text16(WideToUTF16(text));
@@ -140,8 +132,8 @@ void DrawTextStartingFrom(gfx::Canvas* canvas,
int y = position->height() + bounds.y();
// Draw the text on the screen (mirrored, if RTL run).
- canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(),
- flags);
+ canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w,
+ font.GetHeight(), flags);
if (!word.empty() && word[word.size() - 1] == '\x0a') {
// When we come across '\n', we move to the beginning of the next line.