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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "views/controls/label.h" 5 #include "views/controls/label.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/string_split.h" 14 #include "base/string_split.h"
15 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
17 #include "ui/base/accessibility/accessible_view_state.h" 17 #include "ui/base/accessibility/accessible_view_state.h"
18 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/text/text_elider.h" 19 #include "ui/base/text/text_elider.h"
20 #include "ui/gfx/canvas_skia.h" 20 #include "ui/gfx/canvas_skia.h"
21 #include "ui/gfx/color_utils.h" 21 #include "ui/gfx/color_utils.h"
22 #include "ui/gfx/font.h" 22 #include "ui/gfx/font.h"
23 #include "ui/gfx/insets.h" 23 #include "ui/gfx/insets.h"
24 #include "views/background.h" 24 #include "views/background.h"
25 25
26 namespace views { 26 namespace {
27 27
28 // The colors to use for enabled and disabled labels. 28 // The colors to use for enabled and disabled labels.
29 static SkColor kEnabledColor; 29 SkColor kDefaultEnabledColor;
sky 2011/10/11 00:42:35 Having these away from where you initialize them m
Peter Kasting 2011/10/11 01:05:47 Sure. Moved. (Also for link.cc.)
30 static SkColor kDisabledColor; 30 SkColor kDefaultDisabledColor;
31 SkColor kDefaultBackgroundColor;
32
33 };
sky 2011/10/11 00:42:35 nit: no ;
34
35 namespace views {
31 36
32 // static 37 // static
33 const char Label::kViewClassName[] = "views/Label"; 38 const char Label::kViewClassName[] = "views/Label";
34 39
35 const int Label::kFocusBorderPadding = 1; 40 const int Label::kFocusBorderPadding = 1;
36 41
37 Label::Label() { 42 Label::Label() {
38 Init(string16(), GetDefaultFont()); 43 Init(string16(), GetDefaultFont());
39 } 44 }
40 45
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 url_set_ = true; 79 url_set_ = true;
75 text_size_valid_ = false; 80 text_size_valid_ = false;
76 PreferredSizeChanged(); 81 PreferredSizeChanged();
77 SchedulePaint(); 82 SchedulePaint();
78 } 83 }
79 84
80 const GURL Label::GetURL() const { 85 const GURL Label::GetURL() const {
81 return url_set_ ? url_ : GURL(UTF16ToUTF8(text_)); 86 return url_set_ ? url_ : GURL(UTF16ToUTF8(text_));
82 } 87 }
83 88
84 void Label::SetColor(const SkColor& color) { 89 void Label::SetEnabledColor(const SkColor& color) {
85 color_ = color; 90 requested_enabled_color_ = color;
91 RecalculateColors();
86 } 92 }
87 93
88 SkColor Label::GetColor() const { 94 void Label::SetDisabledColor(const SkColor& color) {
89 return color_; 95 requested_disabled_color_ = color;
96 RecalculateColors();
90 } 97 }
91 98
92 void Label::MakeReadableOverBackgroundColor(const SkColor& background_color) { 99 void Label::SetBackgroundColor(const SkColor& color) {
93 SetColor(color_utils::GetReadableColor( 100 background_color_ = color;
94 IsEnabled() ? kEnabledColor : kDisabledColor, background_color)); 101 RecalculateColors();
95 } 102 }
96 103
97 void Label::SetHorizontalAlignment(Alignment alignment) { 104 void Label::SetHorizontalAlignment(Alignment alignment) {
98 // If the View's UI layout is right-to-left and rtl_alignment_mode_ is 105 // If the View's UI layout is right-to-left and rtl_alignment_mode_ is
99 // USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment 106 // USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment
100 // settings take into account the text directionality. 107 // settings take into account the text directionality.
101 if (base::i18n::IsRTL() && (rtl_alignment_mode_ == USE_UI_ALIGNMENT) && 108 if (base::i18n::IsRTL() && (rtl_alignment_mode_ == USE_UI_ALIGNMENT) &&
102 (alignment != ALIGN_CENTER)) 109 (alignment != ALIGN_CENTER))
103 alignment = (alignment == ALIGN_LEFT) ? ALIGN_RIGHT : ALIGN_LEFT; 110 alignment = (alignment == ALIGN_LEFT) ? ALIGN_RIGHT : ALIGN_LEFT;
104 if (horiz_alignment_ != alignment) { 111 if (horiz_alignment_ != alignment) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (!is_multi_line_) 216 if (!is_multi_line_)
210 return View::GetHeightForWidth(w); 217 return View::GetHeightForWidth(w);
211 218
212 w = std::max(0, w - GetInsets().width()); 219 w = std::max(0, w - GetInsets().width());
213 int h = font_.GetHeight(); 220 int h = font_.GetHeight();
214 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, 221 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h,
215 ComputeMultiLineFlags()); 222 ComputeMultiLineFlags());
216 return h + GetInsets().height(); 223 return h + GetInsets().height();
217 } 224 }
218 225
219 void Label::OnEnabledChanged() {
220 View::OnEnabledChanged();
221 SetColor(IsEnabled() ? kEnabledColor : kDisabledColor);
222 }
223
224 std::string Label::GetClassName() const { 226 std::string Label::GetClassName() const {
225 return kViewClassName; 227 return kViewClassName;
226 } 228 }
227 229
228 bool Label::HitTest(const gfx::Point& l) const { 230 bool Label::HitTest(const gfx::Point& l) const {
229 return false; 231 return false;
230 } 232 }
231 233
232 void Label::OnMouseMoved(const MouseEvent& event) { 234 void Label::OnMouseMoved(const MouseEvent& event) {
233 UpdateContainsMouse(event); 235 UpdateContainsMouse(event);
(...skipping 28 matching lines...) Expand all
262 void Label::GetAccessibleState(ui::AccessibleViewState* state) { 264 void Label::GetAccessibleState(ui::AccessibleViewState* state) {
263 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; 265 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT;
264 state->state = ui::AccessibilityTypes::STATE_READONLY; 266 state->state = ui::AccessibilityTypes::STATE_READONLY;
265 state->name = text_; 267 state->name = text_;
266 } 268 }
267 269
268 void Label::PaintText(gfx::Canvas* canvas, 270 void Label::PaintText(gfx::Canvas* canvas,
269 const string16& text, 271 const string16& text,
270 const gfx::Rect& text_bounds, 272 const gfx::Rect& text_bounds,
271 int flags) { 273 int flags) {
272 canvas->DrawStringInt(text, font_, color_, 274 canvas->DrawStringInt(text, font_,
273 text_bounds.x(), text_bounds.y(), 275 IsEnabled() ? actual_enabled_color_ : actual_disabled_color_,
274 text_bounds.width(), text_bounds.height(), flags); 276 text_bounds.x(), text_bounds.y(), text_bounds.width(),
277 text_bounds.height(), flags);
275 278
276 if (HasFocus() || paint_as_focused_) { 279 if (HasFocus() || paint_as_focused_) {
277 gfx::Rect focus_bounds = text_bounds; 280 gfx::Rect focus_bounds = text_bounds;
278 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); 281 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
279 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), 282 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(),
280 focus_bounds.width(), focus_bounds.height()); 283 focus_bounds.width(), focus_bounds.height());
281 } 284 }
282 } 285 }
283 286
284 gfx::Size Label::GetTextSize() const { 287 gfx::Size Label::GetTextSize() const {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 330
328 // static 331 // static
329 gfx::Font Label::GetDefaultFont() { 332 gfx::Font Label::GetDefaultFont() {
330 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); 333 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
331 } 334 }
332 335
333 void Label::Init(const string16& text, const gfx::Font& font) { 336 void Label::Init(const string16& text, const gfx::Font& font) {
334 static bool initialized = false; 337 static bool initialized = false;
335 if (!initialized) { 338 if (!initialized) {
336 #if defined(OS_WIN) 339 #if defined(OS_WIN)
337 kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); 340 kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
338 kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); 341 kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
342 kDefaultBackgroundColor = color_utils::GetSysSkColor(COLOR_WINDOW);
339 #else 343 #else
340 // TODO(beng): source from theme provider. 344 // TODO(beng): source from theme provider.
341 kEnabledColor = SK_ColorBLACK; 345 kDefaultEnabledColor = SK_ColorBLACK;
342 kDisabledColor = SK_ColorGRAY; 346 kDefaultDisabledColor = SK_ColorGRAY;
347 kDefaultBackgroundColor = SK_ColorWHITE;
343 #endif 348 #endif
344 349
345 initialized = true; 350 initialized = true;
346 } 351 }
347 352
348 contains_mouse_ = false; 353 contains_mouse_ = false;
349 font_ = font; 354 font_ = font;
350 text_size_valid_ = false; 355 text_size_valid_ = false;
351 SetText(text);
352 url_set_ = false; 356 url_set_ = false;
353 color_ = kEnabledColor; 357 requested_enabled_color_ = kDefaultEnabledColor;
358 requested_disabled_color_ = kDefaultDisabledColor;
359 background_color_ = kDefaultBackgroundColor;
360 auto_color_readability_ = true;
361 RecalculateColors();
354 horiz_alignment_ = ALIGN_CENTER; 362 horiz_alignment_ = ALIGN_CENTER;
355 is_multi_line_ = false; 363 is_multi_line_ = false;
356 allow_character_break_ = false; 364 allow_character_break_ = false;
357 elide_in_middle_ = false; 365 elide_in_middle_ = false;
358 collapse_when_hidden_ = false; 366 collapse_when_hidden_ = false;
359 rtl_alignment_mode_ = USE_UI_ALIGNMENT; 367 rtl_alignment_mode_ = USE_UI_ALIGNMENT;
360 paint_as_focused_ = false; 368 paint_as_focused_ = false;
361 has_focus_border_ = false; 369 has_focus_border_ = false;
370
371 SetText(text);
372 }
373
374 void Label::RecalculateColors() {
375 actual_enabled_color_ = auto_color_readability_ ?
376 color_utils::GetReadableColor(requested_enabled_color_,
377 background_color_) :
378 requested_enabled_color_;
379 actual_disabled_color_ = auto_color_readability_ ?
380 color_utils::GetReadableColor(requested_disabled_color_,
381 background_color_) :
382 requested_disabled_color_;
362 } 383 }
363 384
364 void Label::UpdateContainsMouse(const MouseEvent& event) { 385 void Label::UpdateContainsMouse(const MouseEvent& event) {
365 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); 386 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y()));
366 } 387 }
367 388
368 void Label::SetContainsMouse(bool contains_mouse) { 389 void Label::SetContainsMouse(bool contains_mouse) {
369 if (contains_mouse_ == contains_mouse) 390 if (contains_mouse_ == contains_mouse)
370 return; 391 return;
371 contains_mouse_ = contains_mouse; 392 contains_mouse_ = contains_mouse;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 // it is right aligned. Otherwise, its directionality is forced to be LTR. 496 // it is right aligned. Otherwise, its directionality is forced to be LTR.
476 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { 497 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) {
477 if (horiz_alignment_ == ALIGN_RIGHT) 498 if (horiz_alignment_ == ALIGN_RIGHT)
478 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; 499 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
479 else 500 else
480 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; 501 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
481 } 502 }
482 } 503 }
483 504
484 } // namespace views 505 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698