Chromium Code Reviews| Index: ui/views/controls/label.cc |
| diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc |
| index 6b8fa5835e84d202157f8d5bd3ee26e092d3d191..3bcfa2736167a5def7b6e7426fb5fb535ba5fdc4 100644 |
| --- a/ui/views/controls/label.cc |
| +++ b/ui/views/controls/label.cc |
| @@ -13,24 +13,13 @@ |
| #include "base/logging.h" |
| #include "base/profiler/scoped_tracker.h" |
| #include "base/strings/string_split.h" |
| -#include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "ui/accessibility/ax_view_state.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/color_utils.h" |
| #include "ui/gfx/geometry/insets.h" |
| #include "ui/gfx/text_elider.h" |
| -#include "ui/gfx/text_utils.h" |
| -#include "ui/gfx/utf16_indexing.h" |
| #include "ui/native_theme/native_theme.h" |
| -#include "ui/views/background.h" |
| - |
| -namespace { |
| - |
| -const int kCachedSizeLimit = 10; |
| -const base::char16 kPasswordReplacementChar = '*'; |
| - |
| -} // namespace |
| namespace views { |
| @@ -55,41 +44,29 @@ Label::~Label() { |
| void Label::SetFontList(const gfx::FontList& font_list) { |
| is_first_paint_text_ = true; |
| - font_list_ = font_list; |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| + render_text_->SetFontList(font_list); |
| + ResetLayout(); |
| } |
| void Label::SetText(const base::string16& text) { |
| - if (text != text_) |
| - SetTextInternal(text); |
| -} |
| - |
| -void Label::SetTextInternal(const base::string16& text) { |
| + if (text == render_text_->text()) |
| + return; |
| is_first_paint_text_ = true; |
| - text_ = text; |
| - |
| - if (obscured_) { |
| - size_t obscured_text_length = |
| - static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length())); |
| - layout_text_.assign(obscured_text_length, kPasswordReplacementChar); |
| - } else { |
| - layout_text_ = text_; |
| - } |
| - |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| + render_text_->SetText(text); |
| + ResetLayout(); |
| } |
| void Label::SetAutoColorReadabilityEnabled(bool enabled) { |
| + if (auto_color_readability_ == enabled) |
| + return; |
| is_first_paint_text_ = true; |
| auto_color_readability_ = enabled; |
| RecalculateColors(); |
| } |
| void Label::SetEnabledColor(SkColor color) { |
| + if (enabled_color_set_ && requested_enabled_color_ == color) |
| + return; |
| is_first_paint_text_ = true; |
| requested_enabled_color_ = color; |
| enabled_color_set_ = true; |
| @@ -97,6 +74,8 @@ void Label::SetEnabledColor(SkColor color) { |
| } |
| void Label::SetDisabledColor(SkColor color) { |
| + if (disabled_color_set_ && requested_disabled_color_ == color) |
| + return; |
| is_first_paint_text_ = true; |
| requested_disabled_color_ = color; |
| disabled_color_set_ = true; |
| @@ -104,97 +83,86 @@ void Label::SetDisabledColor(SkColor color) { |
| } |
| void Label::SetBackgroundColor(SkColor color) { |
| + if (background_color_set_ && background_color_ == color) |
| + return; |
| is_first_paint_text_ = true; |
| background_color_ = color; |
| background_color_set_ = true; |
| RecalculateColors(); |
| - cached_draw_params_.text.clear(); |
| } |
| void Label::SetShadows(const gfx::ShadowValues& shadows) { |
| + // TODO(mukai): early exit if the specified shadows are same. |
| is_first_paint_text_ = true; |
| - shadows_ = shadows; |
| - ResetLayoutCache(); |
| + render_text_->set_shadows(shadows); |
| + ResetLayout(); |
| } |
| void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) { |
| + if (subpixel_rendering_enabled_ == subpixel_rendering_enabled) |
| + return; |
| is_first_paint_text_ = true; |
| subpixel_rendering_enabled_ = subpixel_rendering_enabled; |
| + RecalculateColors(); |
| } |
| void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
| - is_first_paint_text_ = true; |
| // If the UI layout is right-to-left, flip the alignment direction. |
| if (base::i18n::IsRTL() && |
| (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) { |
| alignment = (alignment == gfx::ALIGN_LEFT) ? |
| gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| } |
| - if (horizontal_alignment_ != alignment) { |
| - horizontal_alignment_ = alignment; |
| - SchedulePaint(); |
| - } |
| -} |
| - |
| -gfx::HorizontalAlignment Label::GetHorizontalAlignment() const { |
| - if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD) |
| - return horizontal_alignment_; |
| - |
| - const base::i18n::TextDirection dir = |
| - base::i18n::GetFirstStrongCharacterDirection(layout_text_); |
| - return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| + if (render_text_->horizontal_alignment() == alignment) |
| + return; |
| + is_first_paint_text_ = true; |
| + render_text_->SetHorizontalAlignment(alignment); |
| + ResetLayout(); |
| } |
| void Label::SetLineHeight(int height) { |
| + if (line_height_ == height) |
| + return; |
| is_first_paint_text_ = true; |
| - if (height != line_height_) { |
| - line_height_ = height; |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| - } |
| + line_height_ = height; |
| + ResetLayout(); |
| } |
| void Label::SetMultiLine(bool multi_line) { |
| - is_first_paint_text_ = true; |
| DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || |
| elide_behavior_ == gfx::NO_ELIDE)); |
| - if (multi_line != multi_line_) { |
| - multi_line_ = multi_line; |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| - } |
| + if (multi_line_ == multi_line) |
| + return; |
| + is_first_paint_text_ = true; |
| + multi_line_ = multi_line; |
| + render_text_->SetReplaceNewlineCharsWithSymbols(!multi_line); |
| + ResetLayout(); |
| } |
| void Label::SetObscured(bool obscured) { |
| + if (render_text_->obscured() == obscured) |
| + return; |
| is_first_paint_text_ = true; |
| - if (obscured != obscured_) { |
| - obscured_ = obscured; |
| - SetTextInternal(text_); |
| - } |
| + render_text_->SetObscured(obscured); |
| + ResetLayout(); |
| } |
| void Label::SetAllowCharacterBreak(bool allow_character_break) { |
| + if (allow_character_break_ == allow_character_break) |
| + return; |
| is_first_paint_text_ = true; |
| - if (allow_character_break != allow_character_break_) { |
| - allow_character_break_ = allow_character_break; |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| - } |
| + allow_character_break_ = allow_character_break; |
| + ResetLayout(); |
| } |
| void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { |
| - is_first_paint_text_ = true; |
| DCHECK(!multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL || |
| elide_behavior_ == gfx::NO_ELIDE)); |
| - if (elide_behavior != elide_behavior_) { |
| - elide_behavior_ = elide_behavior; |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| - } |
| + if (elide_behavior_ == elide_behavior) |
| + return; |
| + is_first_paint_text_ = true; |
| + elide_behavior_ = elide_behavior; |
| + ResetLayout(); |
| } |
| void Label::SetTooltipText(const base::string16& tooltip_text) { |
| @@ -208,27 +176,14 @@ void Label::SetHandlesTooltips(bool enabled) { |
| void Label::SizeToFit(int max_width) { |
| DCHECK(multi_line_); |
| - |
| - std::vector<base::string16> lines; |
| - base::SplitString(layout_text_, '\n', &lines); |
| - |
| - int label_width = 0; |
| - for (std::vector<base::string16>::const_iterator iter = lines.begin(); |
| - iter != lines.end(); ++iter) { |
| - label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); |
| - } |
| - |
| - label_width += GetInsets().width(); |
| - |
| - if (max_width > 0) |
| - label_width = std::min(label_width, max_width); |
| - |
| - SetBounds(x(), y(), label_width, 0); |
| + if (max_width_ == max_width) |
| + return; |
| + max_width_ = max_width; |
| SizeToPreferredSize(); |
| } |
| const base::string16& Label::GetLayoutTextForTesting() const { |
| - return layout_text_; |
| + return render_text_->layout_text(); |
| } |
| gfx::Insets Label::GetInsets() const { |
| @@ -241,7 +196,7 @@ gfx::Insets Label::GetInsets() const { |
| } |
| int Label::GetBaseline() const { |
| - return GetInsets().top() + font_list_.GetBaseline(); |
| + return GetInsets().top() + font_list().GetBaseline(); |
| } |
| gfx::Size Label::GetPreferredSize() const { |
| @@ -257,49 +212,84 @@ gfx::Size Label::GetPreferredSize() const { |
| if (!visible() && collapse_when_hidden_) |
| return gfx::Size(); |
| + if (multi_line_ && max_width_ != 0 && !text().empty()) |
| + return gfx::Size(max_width_, GetHeightForWidth(max_width_)); |
| + |
| gfx::Size size(GetTextSize()); |
| - gfx::Insets insets = GetInsets(); |
| + const gfx::Insets insets = GetInsets(); |
| size.Enlarge(insets.width(), insets.height()); |
| return size; |
| } |
| gfx::Size Label::GetMinimumSize() const { |
| - gfx::Size text_size(GetTextSize()); |
| - if ((!visible() && collapse_when_hidden_) || text_size.IsEmpty()) |
| + if (!visible() && collapse_when_hidden_) |
| return gfx::Size(); |
| - gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16), |
| - font_list_), |
| - font_list_.GetHeight()); |
| - size.SetToMin(text_size); // The actual text may be shorter than an ellipsis. |
| - gfx::Insets insets = GetInsets(); |
| - size.Enlarge(insets.width(), insets.height()); |
| + gfx::Size size(0, font_list().GetHeight()); |
| + if (elide_behavior_ == gfx::ELIDE_HEAD || |
| + elide_behavior_ == gfx::ELIDE_MIDDLE || |
| + elide_behavior_ == gfx::ELIDE_TAIL || |
| + elide_behavior_ == gfx::ELIDE_EMAIL) { |
| + size.set_width(gfx::Canvas::GetStringWidth( |
| + base::string16(gfx::kEllipsisUTF16), font_list())); |
| + } |
| + if (!multi_line_) |
| + size.SetToMin(GetTextSize()); |
| + size.Enlarge(GetInsets().width(), GetInsets().height()); |
| return size; |
| } |
| int Label::GetHeightForWidth(int w) const { |
| - if (!multi_line_) |
| - return View::GetHeightForWidth(w); |
| + w -= GetInsets().width(); |
| + if (!multi_line_ || text().empty() || w <= 0) |
| + return std::max(line_height_, font_list().GetHeight()); |
| - w = std::max(0, w - GetInsets().width()); |
| + std::vector<base::string16> lines = GetLinesForWidth(w); |
| + int height = lines.size() * std::max(line_height_, font_list().GetHeight()); |
| + height -= gfx::ShadowValue::GetMargin(render_text_->shadows()).height(); |
| + return height + GetInsets().height(); |
| +} |
| - for (size_t i = 0; i < cached_heights_.size(); ++i) { |
| - const gfx::Size& s = cached_heights_[i]; |
| - if (s.width() == w) |
| - return s.height() + GetInsets().height(); |
| - } |
| +void Label::Layout() { |
| + lines_.clear(); |
| + gfx::Rect rect = GetContentsBounds(); |
| + if (rect.width() == 0 || rect.height() == 0) |
| + return; |
| - int cache_width = w; |
| + std::vector<base::string16> lines; |
| + gfx::HorizontalAlignment alignment = horizontal_alignment(); |
| + gfx::DirectionalityMode directionality = render_text_->directionality_mode(); |
| + if (multi_line_) { |
| + // Force the directionality and alignment of the first line on other lines. |
| + bool rtl = render_text_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT; |
| + if (alignment == gfx::ALIGN_TO_HEAD) |
| + alignment = rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| + directionality = |
| + rtl ? gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR; |
| + lines = GetLinesForWidth(rect.width()); |
| + if (lines.size() > 1) |
| + rect.set_height(std::max(line_height_, font_list().GetHeight())); |
| + } else { |
| + lines.push_back(render_text_->layout_text()); |
| + } |
| - int h = font_list_.GetHeight(); |
| - // Flags returned in the cached |DrawStringParams| has a different value |
| - // from the result of |ComputeDrawStringFlags()|. The latter is needed here. |
| - const int flags = ComputeDrawStringFlags(); |
| - gfx::Canvas::SizeStringInt( |
| - layout_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(); |
| + const int bottom = GetContentsBounds().bottom(); |
| + for (size_t i = 0; i < lines.size() && rect.y() <= bottom; ++i) { |
| + scoped_ptr<gfx::RenderText> line(gfx::RenderText::CreateInstance()); |
| + line->SetHorizontalAlignment(alignment); |
| + line->SetDirectionalityMode(directionality); |
| + line->SetElideBehavior(elide_behavior_); |
| + line->SetFontList(font_list()); |
| + line->set_shadows(shadows()); |
| + line->SetCursorEnabled(false); |
| + line->SetText(lines[i]); |
| + line->SetDisplayRect(rect); |
| + lines_.push_back(line.release()); |
| + rect.set_y(rect.y() + rect.height()); |
| + } |
| + for (size_t i = lines_.size(); i < lines.size(); ++i) |
| + lines_.back()->SetText(lines_.back()->text() + lines[i]); |
| + RecalculateColors(); |
| } |
| const char* Label::GetClassName() const { |
| @@ -322,7 +312,7 @@ bool Label::CanProcessEventsWithinSubtree() const { |
| void Label::GetAccessibleState(ui::AXViewState* state) { |
| state->role = ui::AX_ROLE_STATIC_TEXT; |
| state->AddStateFlag(ui::AX_STATE_READ_ONLY); |
| - state->name = layout_text_; |
| + state->name = render_text_->layout_text(); |
| } |
| bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| @@ -335,98 +325,47 @@ bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| } |
| if (ShouldShowDefaultTooltip()) { |
| - *tooltip = layout_text_; |
| + tooltip->assign(render_text_->layout_text()); |
| return true; |
| } |
| return false; |
| } |
| -void Label::PaintText(gfx::Canvas* canvas, |
| - const base::string16& text, |
| - const gfx::Rect& text_bounds, |
| - int flags) { |
| - SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; |
| - if (elide_behavior_ == gfx::FADE_TAIL) { |
| - canvas->DrawFadedString(text, font_list_, color, text_bounds, flags); |
| - } else { |
| - canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds, |
| - line_height_, flags, shadows_); |
| - } |
| - |
| - if (HasFocus()) { |
| - gfx::Rect focus_bounds = text_bounds; |
| - focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| - canvas->DrawFocusRect(focus_bounds); |
| - } |
| +void Label::OnEnabledChanged() { |
| + RecalculateColors(); |
| } |
| -gfx::Size Label::GetTextSize() const { |
| - if (!text_size_valid_) { |
| - // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
| - tracked_objects::ScopedTracker tracking_profile1( |
| - FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetTextSize1")); |
| - |
| - // For single-line strings, we supply the largest possible width, because |
| - // while adding NO_ELLIPSIS to the flags works on Windows for forcing |
| - // SizeStringInt() to calculate the desired width, it doesn't seem to work |
| - // on Linux. |
| - int w = multi_line_ ? |
| - GetAvailableRect().width() : std::numeric_limits<int>::max(); |
| - int h = font_list_.GetHeight(); |
| - // For single-line strings, ignore the available width and calculate how |
| - // wide the text wants to be. |
| - // Call |ComputeDrawStringFlags()| instead of |CalculateDrawStringParams()| |
| - // here since the latter calls this function and causes infinite recursion. |
| - int flags = ComputeDrawStringFlags(); |
| - if (!multi_line_) |
| - flags |= gfx::Canvas::NO_ELLIPSIS; |
| - { |
| - // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is |
| - // fixed. |
| - tracked_objects::ScopedTracker tracking_profile2( |
| - FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetTextSize2")); |
| - |
| - gfx::Canvas::SizeStringInt(layout_text_, font_list_, &w, &h, line_height_, |
| - flags); |
| - } |
| - text_size_.SetSize(w, h); |
| - const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows_); |
| - text_size_.Enlarge(shadow_margin.width(), shadow_margin.height()); |
| - text_size_valid_ = true; |
| - } |
| - |
| - return text_size_; |
| +void Label::PaintText(gfx::Canvas* canvas) { |
| + for (size_t i = 0; i < lines_.size(); ++i) |
| + lines_[i]->Draw(canvas); |
| } |
| void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| - text_size_valid_ &= !multi_line_; |
| - cached_draw_params_.text.clear(); |
| + if (previous_bounds.size() != size()) |
| + InvalidateLayout(); |
| } |
| void Label::OnPaint(gfx::Canvas* canvas) { |
| - OnPaintBackground(canvas); |
| - // We skip painting the focus border because it is being handled seperately by |
| - // some subclasses of Label. We do not want View's focus border painting to |
| - // interfere with that. |
| - OnPaintBorder(canvas); |
| - if (layout_text_.empty()) |
| - return; |
| - |
| - const DrawStringParams* params = CalculateDrawStringParams(); |
| + View::OnPaint(canvas); |
| if (is_first_paint_text_) { |
| // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
| tracked_objects::ScopedTracker tracking_profile( |
| FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText first")); |
| is_first_paint_text_ = false; |
| - PaintText(canvas, params->text, params->bounds, params->flags); |
| + PaintText(canvas); |
| } else { |
| // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
| tracked_objects::ScopedTracker tracking_profile( |
| FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); |
| - PaintText(canvas, params->text, params->bounds, params->flags); |
| + PaintText(canvas); |
| + } |
| + if (HasFocus()) { |
| + gfx::Rect focus_bounds = GetLocalBounds(); |
| + focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| + canvas->DrawFocusRect(focus_bounds); |
| } |
| } |
| @@ -439,32 +378,74 @@ void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { |
| // When the device scale factor is changed, some font rendering parameters is |
| // changed (especially, hinting). The bounding box of the text has to be |
| // re-computed based on the new parameters. See crbug.com/441439 |
| - ResetLayoutCache(); |
| - PreferredSizeChanged(); |
| - SchedulePaint(); |
| + ResetLayout(); |
| } |
| void Label::Init(const base::string16& text, const gfx::FontList& font_list) { |
| - font_list_ = font_list; |
| + render_text_.reset(gfx::RenderText::CreateInstance()); |
| + render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| + render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); |
| + render_text_->SetElideBehavior(gfx::NO_ELIDE); |
| + render_text_->SetFontList(font_list); |
| + render_text_->SetCursorEnabled(false); |
| + |
| + elide_behavior_ = gfx::ELIDE_TAIL; |
| enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; |
| subpixel_rendering_enabled_ = true; |
| auto_color_readability_ = true; |
| UpdateColorsFromTheme(ui::NativeTheme::instance()); |
|
oshima
2015/01/23 16:58:54
FYI: handles_tooltips_ is missing
Jun Mukai
2015/01/26 23:05:47
Done.
|
| - horizontal_alignment_ = gfx::ALIGN_CENTER; |
| - line_height_ = 0; |
| - multi_line_ = false; |
| - obscured_ = false; |
| - allow_character_break_ = false; |
| - elide_behavior_ = gfx::ELIDE_TAIL; |
| - handles_tooltips_ = true; |
| collapse_when_hidden_ = false; |
| - cached_heights_.resize(kCachedSizeLimit); |
| - ResetLayoutCache(); |
| + allow_character_break_ = false; |
| + multi_line_ = false; |
| + line_height_ = 0; |
| + max_width_ = 0; |
| is_first_paint_text_ = true; |
| - |
| SetText(text); |
| } |
| +void Label::ResetLayout() { |
| + InvalidateLayout(); |
| + PreferredSizeChanged(); |
| + SchedulePaint(); |
| +} |
| + |
| +std::vector<base::string16> Label::GetLinesForWidth(int width) const { |
| + std::vector<base::string16> lines; |
| + const gfx::WordWrapBehavior wrap = |
| + allow_character_break_ ? gfx::WRAP_LONG_WORDS : gfx::TRUNCATE_LONG_WORDS; |
| + gfx::ElideRectangleText(render_text_->layout_text(), |
| + font_list(), |
| + width, |
| + std::numeric_limits<int>::max(), |
| + wrap, |
| + &lines); |
| + return lines; |
| +} |
| + |
| +gfx::Size Label::GetTextSize() const { |
| + gfx::Size size; |
| + if (text().empty()) { |
| + size = gfx::Size(0, std::max(line_height_, font_list().GetHeight())); |
| + } else if (!multi_line_) { |
| + size = render_text_->GetStringSize(); |
| + } else { |
| + // Get the natural text size, unelided and only wrapped on newlines. |
| + std::vector<base::string16> lines; |
| + base::SplitString(render_text_->layout_text(), '\n', &lines); |
| + scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); |
| + render_text->SetFontList(font_list()); |
| + for (size_t i = 0; i < lines.size(); ++i) { |
| + render_text->SetText(lines[i]); |
| + const gfx::Size line = render_text->GetStringSize(); |
| + size.set_width(std::max(size.width(), line.width())); |
| + size.set_height(size.height() + std::max(line_height_, line.height())); |
| + } |
| + } |
| + const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows()); |
| + size.Enlarge(shadow_margin.width(), shadow_margin.height()); |
| + return size; |
| +} |
| + |
| void Label::RecalculateColors() { |
| actual_enabled_color_ = auto_color_readability_ ? |
| color_utils::GetReadableColor(requested_enabled_color_, |
| @@ -474,107 +455,15 @@ void Label::RecalculateColors() { |
| color_utils::GetReadableColor(requested_disabled_color_, |
| background_color_) : |
| requested_disabled_color_; |
| -} |
| - |
| -gfx::Rect Label::GetTextBounds() const { |
| - gfx::Rect available(GetAvailableRect()); |
| - gfx::Size text_size(GetTextSize()); |
| - text_size.set_width(std::min(available.width(), text_size.width())); |
| - gfx::Point origin(GetInsets().left(), GetInsets().top()); |
| - switch (GetHorizontalAlignment()) { |
| - case gfx::ALIGN_LEFT: |
| - break; |
| - case gfx::ALIGN_CENTER: |
| - // Put any extra margin pixel on the left to match the legacy behavior |
| - // from the use of GetTextExtentPoint32() on Windows. |
| - origin.Offset((available.width() + 1 - text_size.width()) / 2, 0); |
| - break; |
| - case gfx::ALIGN_RIGHT: |
| - origin.set_x(available.right() - text_size.width()); |
| - break; |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| - if (!multi_line_) |
| - text_size.set_height(available.height()); |
| - // Support vertical centering of multi-line labels: http://crbug.com/429595 |
| - origin.Offset(0, std::max(0, (available.height() - text_size.height())) / 2); |
| - return gfx::Rect(origin, text_size); |
| -} |
| - |
| -int Label::ComputeDrawStringFlags() const { |
| - int flags = 0; |
| - |
| - // We can't use subpixel rendering if the background is non-opaque. |
| - if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_) |
| - flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; |
| - |
| - base::i18n::TextDirection direction = |
| - base::i18n::GetFirstStrongCharacterDirection(layout_text_); |
| - if (direction == base::i18n::RIGHT_TO_LEFT) |
| - flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| - else |
| - flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| - |
| - switch (GetHorizontalAlignment()) { |
| - case gfx::ALIGN_LEFT: |
| - flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| - break; |
| - case gfx::ALIGN_CENTER: |
| - flags |= gfx::Canvas::TEXT_ALIGN_CENTER; |
| - break; |
| - case gfx::ALIGN_RIGHT: |
| - flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
| - break; |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| - if (!multi_line_) |
| - return flags; |
| - |
| - flags |= gfx::Canvas::MULTI_LINE; |
| -#if !defined(OS_WIN) |
| - // Don't elide multiline labels on Linux. |
| - // Todo(davemoore): Do we depend on eliding multiline text? |
| - // Pango insists on limiting the number of lines to one if text is |
| - // elided. You can get around this if you can pass a maximum height |
| - // but we don't currently have that data when we call the pango code. |
| - flags |= gfx::Canvas::NO_ELLIPSIS; |
| -#endif |
| - if (allow_character_break_) |
| - flags |= gfx::Canvas::CHARACTER_BREAK; |
| - |
| - return flags; |
| -} |
| - |
| -gfx::Rect Label::GetAvailableRect() const { |
| - gfx::Rect bounds(size()); |
| - bounds.Inset(GetInsets()); |
| - return bounds; |
| -} |
| - |
| -const Label::DrawStringParams* Label::CalculateDrawStringParams() const { |
| - if (cached_draw_params_.text.empty()) { |
| - const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE || |
| - elide_behavior_ == gfx::FADE_TAIL; |
| - if (multi_line_ || forbid_ellipsis) { |
| - cached_draw_params_.text = layout_text_; |
| - } else { |
| - cached_draw_params_.text = gfx::ElideText(layout_text_, font_list_, |
| - GetAvailableRect().width(), elide_behavior_); |
| - } |
| - |
| - cached_draw_params_.bounds = GetTextBounds(); |
| - cached_draw_params_.flags = ComputeDrawStringFlags(); |
| - // TODO(msw): Elide multi-line text with ElideRectangleText instead. |
| - if (!multi_line_ || forbid_ellipsis) |
| - cached_draw_params_.flags |= gfx::Canvas::NO_ELLIPSIS; |
| + SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; |
| + bool background_is_transparent = |
| + SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_; |
| + for (size_t i = 0; i < lines_.size(); ++i) { |
| + lines_[i]->SetColor(color); |
| + lines_[i]->set_background_is_transparent(background_is_transparent); |
| } |
| - |
| - return &cached_draw_params_; |
| + SchedulePaint(); |
| } |
| void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| @@ -593,14 +482,6 @@ void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| RecalculateColors(); |
| } |
| -void Label::ResetLayoutCache() { |
| - cached_draw_params_.text.clear(); |
| - text_size_valid_ = false; |
| - cached_heights_cursor_ = 0; |
| - for (int i = 0; i < kCachedSizeLimit; ++i) |
| - cached_heights_[i] = gfx::Size(); |
| -} |
| - |
| bool Label::ShouldShowDefaultTooltip() const { |
| const gfx::Size text_size = GetTextSize(); |
| const gfx::Size size = GetContentsBounds().size(); |