Chromium Code Reviews| Index: ui/views/controls/label.cc |
| diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc |
| index 7145323fa1fc6969a27e38c81245c09a1f02b19f..36dcf8be8d29f5bdbd176fb7fc0c2c51a39923e3 100644 |
| --- a/ui/views/controls/label.cc |
| +++ b/ui/views/controls/label.cc |
| @@ -63,6 +63,11 @@ void Label::SetText(const base::string16& text) { |
| if (text == text_) |
| return; |
| text_ = text; |
| + |
| + // 0x2022 is a nicely shaped bullet, perfect for obsfucation. |
| + obscured_text_ = |
| + is_obscured_ ? base::string16(text_.length(), 0x2022) : base::string16(); |
|
msw
2014/04/03 01:38:48
This should match obscured textfields, specificall
Mike West
2014/04/03 08:17:25
This was very helpful, thank you.
I've pulled in
|
| + |
| ResetCachedSize(); |
| PreferredSizeChanged(); |
| SchedulePaint(); |
| @@ -140,6 +145,21 @@ void Label::SetMultiLine(bool multi_line) { |
| } |
| } |
| +void Label::SetObscured(bool obscured) { |
| + if (obscured != is_obscured_) { |
| + is_obscured_ = obscured; |
| + obscured_text_ = is_obscured_ ? base::string16(text_.length(), 0x2022) |
|
msw
2014/04/03 01:38:48
Call SetText() here (by changing the initial text
Mike West
2014/04/03 08:17:25
Good idea, done.
|
| + : base::string16(); |
| + ResetCachedSize(); |
| + PreferredSizeChanged(); |
| + SchedulePaint(); |
| + } |
| +} |
| + |
| +const base::string16& Label::display_text() const { |
| + return is_obscured() ? obscured_text_ : text_; |
| +} |
| + |
| void Label::SetAllowCharacterBreak(bool allow_character_break) { |
| if (allow_character_break != allow_character_break_) { |
| allow_character_break_ = allow_character_break; |
| @@ -167,7 +187,7 @@ void Label::SizeToFit(int max_width) { |
| DCHECK(is_multi_line_); |
| std::vector<base::string16> lines; |
| - base::SplitString(text_, '\n', &lines); |
| + base::SplitString(display_text(), '\n', &lines); |
| int label_width = 0; |
| for (std::vector<base::string16>::const_iterator iter = lines.begin(); |
| @@ -242,7 +262,8 @@ int Label::GetHeightForWidth(int w) { |
| int h = font_list_.GetHeight(); |
| const int flags = ComputeDrawStringFlags(); |
| - gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); |
| + gfx::Canvas::SizeStringInt( |
| + display_text(), font_list_, &w, &h, line_height_, flags); |
| cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); |
| cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; |
| return h + GetInsets().height(); |
| @@ -282,7 +303,7 @@ bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| // Show the full text if the text does not fit. |
| if (ShouldShowDefaultTooltip()) { |
| - *tooltip = text_; |
| + *tooltip = display_text(); |
| return true; |
| } |
| @@ -292,7 +313,7 @@ bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| void Label::GetAccessibleState(ui::AXViewState* state) { |
| state->role = ui::AX_ROLE_STATIC_TEXT; |
| state->AddStateFlag(ui::AX_STATE_READ_ONLY); |
| - state->name = text_; |
| + state->name = display_text(); |
| } |
| void Label::PaintText(gfx::Canvas* canvas, |
| @@ -328,7 +349,8 @@ gfx::Size Label::GetTextSize() const { |
| int flags = ComputeDrawStringFlags(); |
| if (!is_multi_line_) |
| flags |= gfx::Canvas::NO_ELLIPSIS; |
| - gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); |
| + gfx::Canvas::SizeStringInt( |
| + display_text(), font_list_, &w, &h, line_height_, flags); |
| text_size_.SetSize(w, h); |
| text_size_valid_ = true; |
| } |
| @@ -366,6 +388,7 @@ void Label::Init(const base::string16& text, const gfx::FontList& font_list) { |
| horizontal_alignment_ = gfx::ALIGN_CENTER; |
| line_height_ = 0; |
| is_multi_line_ = false; |
| + is_obscured_ = false; |
| allow_character_break_ = false; |
| elide_behavior_ = ELIDE_AT_END; |
| collapse_when_hidden_ = false; |
| @@ -430,7 +453,7 @@ int Label::ComputeDrawStringFlags() const { |
| if (directionality_mode_ == AUTO_DETECT_DIRECTIONALITY) { |
| base::i18n::TextDirection direction = |
| - base::i18n::GetFirstStrongCharacterDirection(text_); |
| + base::i18n::GetFirstStrongCharacterDirection(display_text()); |
| if (direction == base::i18n::RIGHT_TO_LEFT) |
| flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| else |
| @@ -481,20 +504,26 @@ void Label::CalculateDrawStringParams(base::string16* paint_text, |
| // TODO(msw): Use ElideRectangleText to support eliding multi-line text. Once |
| // this is done, we can set NO_ELLIPSIS unconditionally at the bottom. |
| if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { |
| - *paint_text = text_; |
| + *paint_text = display_text(); |
| } else if (elide_behavior_ == ELIDE_AT_BEGINNING) { |
| - *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), |
| + *paint_text = gfx::ElideText(display_text(), |
| + font_list_, |
| + GetAvailableRect().width(), |
| gfx::ELIDE_AT_BEGINNING); |
| } else if (elide_behavior_ == ELIDE_IN_MIDDLE) { |
| - *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), |
| + *paint_text = gfx::ElideText(display_text(), |
| + font_list_, |
| + GetAvailableRect().width(), |
| gfx::ELIDE_IN_MIDDLE); |
| } else if (elide_behavior_ == ELIDE_AT_END) { |
| - *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), |
| + *paint_text = gfx::ElideText(display_text(), |
| + font_list_, |
| + GetAvailableRect().width(), |
| gfx::ELIDE_AT_END); |
| } else { |
| DCHECK_EQ(ELIDE_AS_EMAIL, elide_behavior_); |
| - *paint_text = gfx::ElideEmail(text_, font_list_, |
| - GetAvailableRect().width()); |
| + *paint_text = |
| + gfx::ElideEmail(display_text(), font_list_, GetAvailableRect().width()); |
| } |
| *text_bounds = GetTextBounds(); |
| @@ -527,8 +556,9 @@ void Label::ResetCachedSize() { |
| } |
| bool Label::ShouldShowDefaultTooltip() const { |
| - return !is_multi_line_ && |
| - gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); |
| + return !is_multi_line_ && !is_obscured_ && |
| + gfx::GetStringWidth(display_text(), font_list_) > |
| + GetAvailableRect().width(); |
| } |
| } // namespace views |