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

Side by Side Diff: ui/views/controls/label.cc

Issue 312233003: Add fade eliding for Views Labels; related cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refine alignment check; minor additional cleanup. Created 6 years, 6 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
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/label_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/controls/label.h" 5 #include "ui/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>
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 void Label::SetLineHeight(int height) { 137 void Label::SetLineHeight(int height) {
138 if (height != line_height_) { 138 if (height != line_height_) {
139 line_height_ = height; 139 line_height_ = height;
140 ResetCachedSize(); 140 ResetCachedSize();
141 PreferredSizeChanged(); 141 PreferredSizeChanged();
142 SchedulePaint(); 142 SchedulePaint();
143 } 143 }
144 } 144 }
145 145
146 void Label::SetMultiLine(bool multi_line) { 146 void Label::SetMultiLine(bool multi_line) {
147 DCHECK(!multi_line || (elide_behavior_ != ELIDE_IN_MIDDLE && 147 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL ||
148 elide_behavior_ != ELIDE_AT_BEGINNING)); 148 elide_behavior_ == gfx::TRUNCATE));
149 if (multi_line != is_multi_line_) { 149 if (multi_line != is_multi_line_) {
150 is_multi_line_ = multi_line; 150 is_multi_line_ = multi_line;
151 ResetCachedSize(); 151 ResetCachedSize();
152 PreferredSizeChanged(); 152 PreferredSizeChanged();
153 SchedulePaint(); 153 SchedulePaint();
154 } 154 }
155 } 155 }
156 156
157 void Label::SetObscured(bool obscured) { 157 void Label::SetObscured(bool obscured) {
158 if (obscured != is_obscured_) { 158 if (obscured != is_obscured_) {
159 is_obscured_ = obscured; 159 is_obscured_ = obscured;
160 SetTextInternal(text_); 160 SetTextInternal(text_);
161 } 161 }
162 } 162 }
163 163
164 void Label::SetAllowCharacterBreak(bool allow_character_break) { 164 void Label::SetAllowCharacterBreak(bool allow_character_break) {
165 if (allow_character_break != allow_character_break_) { 165 if (allow_character_break != allow_character_break_) {
166 allow_character_break_ = allow_character_break; 166 allow_character_break_ = allow_character_break;
167 ResetCachedSize(); 167 ResetCachedSize();
168 PreferredSizeChanged(); 168 PreferredSizeChanged();
169 SchedulePaint(); 169 SchedulePaint();
170 } 170 }
171 } 171 }
172 172
173 void Label::SetElideBehavior(ElideBehavior elide_behavior) { 173 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
174 DCHECK(elide_behavior != ELIDE_IN_MIDDLE || !is_multi_line_); 174 DCHECK(!is_multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL ||
175 elide_behavior_ == gfx::TRUNCATE));
175 if (elide_behavior != elide_behavior_) { 176 if (elide_behavior != elide_behavior_) {
176 elide_behavior_ = elide_behavior; 177 elide_behavior_ = elide_behavior;
177 ResetCachedSize(); 178 ResetCachedSize();
178 PreferredSizeChanged(); 179 PreferredSizeChanged();
179 SchedulePaint(); 180 SchedulePaint();
180 } 181 }
181 } 182 }
182 183
183 void Label::SetTooltipText(const base::string16& tooltip_text) { 184 void Label::SetTooltipText(const base::string16& tooltip_text) {
184 tooltip_text_ = tooltip_text; 185 tooltip_text_ = tooltip_text;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 void Label::GetAccessibleState(ui::AXViewState* state) { 316 void Label::GetAccessibleState(ui::AXViewState* state) {
316 state->role = ui::AX_ROLE_STATIC_TEXT; 317 state->role = ui::AX_ROLE_STATIC_TEXT;
317 state->AddStateFlag(ui::AX_STATE_READ_ONLY); 318 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
318 state->name = layout_text(); 319 state->name = layout_text();
319 } 320 }
320 321
321 void Label::PaintText(gfx::Canvas* canvas, 322 void Label::PaintText(gfx::Canvas* canvas,
322 const base::string16& text, 323 const base::string16& text,
323 const gfx::Rect& text_bounds, 324 const gfx::Rect& text_bounds,
324 int flags) { 325 int flags) {
325 gfx::ShadowValues shadows;
326 if (has_shadow_) {
327 shadows.push_back(gfx::ShadowValue(shadow_offset_, shadow_blur_,
328 enabled() ? enabled_shadow_color_ : disabled_shadow_color_));
329 }
330 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; 326 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_;
331 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds, 327 if (elide_behavior_ == gfx::FADE_TAIL) {
332 line_height_, flags, shadows); 328 canvas->DrawFadedString(text, font_list_, color, text_bounds, flags);
329 } else {
330 gfx::ShadowValues shadows;
331 if (has_shadow_) {
332 shadows.push_back(gfx::ShadowValue(shadow_offset_, shadow_blur_,
333 enabled() ? enabled_shadow_color_ : disabled_shadow_color_));
334 }
335 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds,
336 line_height_, flags, shadows);
333 337
334 if (SkColorGetA(halo_color_) != SK_AlphaTRANSPARENT) { 338 if (SkColorGetA(halo_color_) != SK_AlphaTRANSPARENT) {
335 canvas->DrawStringRectWithHalo(text, font_list_, color, halo_color_, 339 canvas->DrawStringRectWithHalo(text, font_list_, color, halo_color_,
336 text_bounds, flags); 340 text_bounds, flags);
341 }
337 } 342 }
338 343
339 if (HasFocus()) { 344 if (HasFocus()) {
340 gfx::Rect focus_bounds = text_bounds; 345 gfx::Rect focus_bounds = text_bounds;
341 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); 346 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
342 canvas->DrawFocusRect(focus_bounds); 347 canvas->DrawFocusRect(focus_bounds);
343 } 348 }
344 } 349 }
345 350
346 gfx::Size Label::GetTextSize() const { 351 gfx::Size Label::GetTextSize() const {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { 396 void Label::Init(const base::string16& text, const gfx::FontList& font_list) {
392 font_list_ = font_list; 397 font_list_ = font_list;
393 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 398 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
394 auto_color_readability_ = true; 399 auto_color_readability_ = true;
395 UpdateColorsFromTheme(ui::NativeTheme::instance()); 400 UpdateColorsFromTheme(ui::NativeTheme::instance());
396 horizontal_alignment_ = gfx::ALIGN_CENTER; 401 horizontal_alignment_ = gfx::ALIGN_CENTER;
397 line_height_ = 0; 402 line_height_ = 0;
398 is_multi_line_ = false; 403 is_multi_line_ = false;
399 is_obscured_ = false; 404 is_obscured_ = false;
400 allow_character_break_ = false; 405 allow_character_break_ = false;
401 elide_behavior_ = ELIDE_AT_END; 406 elide_behavior_ = gfx::ELIDE_TAIL;
402 collapse_when_hidden_ = false; 407 collapse_when_hidden_ = false;
403 directionality_mode_ = USE_UI_DIRECTIONALITY; 408 directionality_mode_ = USE_UI_DIRECTIONALITY;
404 enabled_shadow_color_ = 0; 409 enabled_shadow_color_ = 0;
405 disabled_shadow_color_ = 0; 410 disabled_shadow_color_ = 0;
406 shadow_offset_.SetPoint(1, 1); 411 shadow_offset_.SetPoint(1, 1);
407 has_shadow_ = false; 412 has_shadow_ = false;
408 shadow_blur_ = 0; 413 shadow_blur_ = 0;
409 halo_color_ = SK_ColorTRANSPARENT; 414 halo_color_ = SK_ColorTRANSPARENT;
410 cached_heights_.resize(kCachedSizeLimit); 415 cached_heights_.resize(kCachedSizeLimit);
411 ResetCachedSize(); 416 ResetCachedSize();
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 gfx::Rect bounds(size()); 509 gfx::Rect bounds(size());
505 bounds.Inset(GetInsets()); 510 bounds.Inset(GetInsets());
506 return bounds; 511 return bounds;
507 } 512 }
508 513
509 void Label::CalculateDrawStringParams(base::string16* paint_text, 514 void Label::CalculateDrawStringParams(base::string16* paint_text,
510 gfx::Rect* text_bounds, 515 gfx::Rect* text_bounds,
511 int* flags) const { 516 int* flags) const {
512 DCHECK(paint_text && text_bounds && flags); 517 DCHECK(paint_text && text_bounds && flags);
513 518
514 // TODO(msw): Use ElideRectangleText to support eliding multi-line text. Once 519 const bool forbid_ellipsis = elide_behavior_ == gfx::TRUNCATE ||
515 // this is done, we can set NO_ELLIPSIS unconditionally at the bottom. 520 elide_behavior_ == gfx::FADE_TAIL;
516 if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { 521 if (is_multi_line_ || forbid_ellipsis) {
517 *paint_text = layout_text(); 522 *paint_text = layout_text();
518 } else if (elide_behavior_ == ELIDE_AT_BEGINNING) {
519 *paint_text = gfx::ElideText(layout_text(),
520 font_list_,
521 GetAvailableRect().width(),
522 gfx::ELIDE_AT_BEGINNING);
523 } else if (elide_behavior_ == ELIDE_IN_MIDDLE) {
524 *paint_text = gfx::ElideText(layout_text(),
525 font_list_,
526 GetAvailableRect().width(),
527 gfx::ELIDE_IN_MIDDLE);
528 } else if (elide_behavior_ == ELIDE_AT_END) {
529 *paint_text = gfx::ElideText(layout_text(),
530 font_list_,
531 GetAvailableRect().width(),
532 gfx::ELIDE_AT_END);
533 } else { 523 } else {
534 DCHECK_EQ(ELIDE_AS_EMAIL, elide_behavior_); 524 *paint_text = gfx::ElideText(layout_text(), font_list_,
535 *paint_text = 525 GetAvailableRect().width(), elide_behavior_);
536 gfx::ElideEmail(layout_text(), font_list_, GetAvailableRect().width());
537 } 526 }
538 527
539 *text_bounds = GetTextBounds(); 528 *text_bounds = GetTextBounds();
540 *flags = ComputeDrawStringFlags(); 529 *flags = ComputeDrawStringFlags();
541 if (!is_multi_line_ || (elide_behavior_ == NO_ELIDE)) 530 // TODO(msw): Elide multi-line text with ElideRectangleText instead.
542 *flags |= gfx::Canvas::NO_ELLIPSIS; 531 if (!is_multi_line_ || forbid_ellipsis)
532 *flags |= gfx::Canvas::NO_ELLIPSIS;
543 } 533 }
544 534
545 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { 535 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
546 if (!enabled_color_set_) { 536 if (!enabled_color_set_) {
547 requested_enabled_color_ = theme->GetSystemColor( 537 requested_enabled_color_ = theme->GetSystemColor(
548 ui::NativeTheme::kColorId_LabelEnabledColor); 538 ui::NativeTheme::kColorId_LabelEnabledColor);
549 } 539 }
550 if (!disabled_color_set_) { 540 if (!disabled_color_set_) {
551 requested_disabled_color_ = theme->GetSystemColor( 541 requested_disabled_color_ = theme->GetSystemColor(
552 ui::NativeTheme::kColorId_LabelDisabledColor); 542 ui::NativeTheme::kColorId_LabelDisabledColor);
(...skipping 12 matching lines...) Expand all
565 cached_heights_[i] = gfx::Size(); 555 cached_heights_[i] = gfx::Size();
566 } 556 }
567 557
568 bool Label::ShouldShowDefaultTooltip() const { 558 bool Label::ShouldShowDefaultTooltip() const {
569 return !is_multi_line_ && !is_obscured_ && 559 return !is_multi_line_ && !is_obscured_ &&
570 gfx::GetStringWidth(layout_text(), font_list_) > 560 gfx::GetStringWidth(layout_text(), font_list_) >
571 GetAvailableRect().width(); 561 GetAvailableRect().width();
572 } 562 }
573 563
574 } // namespace views 564 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698