Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 result.append(lines_[0]->GetDisplayText()); | 206 result.append(lines_[0]->GetDisplayText()); |
| 207 for (size_t i = 1; i < lines_.size(); ++i) { | 207 for (size_t i = 1; i < lines_.size(); ++i) { |
| 208 result.append(1, '\n'); | 208 result.append(1, '\n'); |
| 209 result.append(lines_[i]->GetDisplayText()); | 209 result.append(lines_[i]->GetDisplayText()); |
| 210 } | 210 } |
| 211 return result; | 211 return result; |
| 212 } | 212 } |
| 213 | 213 |
| 214 gfx::Insets Label::GetInsets() const { | 214 gfx::Insets Label::GetInsets() const { |
| 215 gfx::Insets insets = View::GetInsets(); | 215 gfx::Insets insets = View::GetInsets(); |
| 216 if (focusable()) { | 216 if (IsFocusable()) { |
| 217 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, | 217 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, |
| 218 kFocusBorderPadding, kFocusBorderPadding); | 218 kFocusBorderPadding, kFocusBorderPadding); |
| 219 } | 219 } |
| 220 return insets; | 220 return insets; |
| 221 } | 221 } |
| 222 | 222 |
| 223 int Label::GetBaseline() const { | 223 int Label::GetBaseline() const { |
| 224 return GetInsets().top() + font_list().GetBaseline(); | 224 return GetInsets().top() + font_list().GetBaseline(); |
| 225 } | 225 } |
| 226 | 226 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 // Note that |render_text_| is never elided (see the comment in Init() too). | 326 // Note that |render_text_| is never elided (see the comment in Init() too). |
| 327 tooltip->assign(render_text_->GetDisplayText()); | 327 tooltip->assign(render_text_->GetDisplayText()); |
| 328 return true; | 328 return true; |
| 329 } | 329 } |
| 330 | 330 |
| 331 return false; | 331 return false; |
| 332 } | 332 } |
| 333 | 333 |
| 334 void Label::OnEnabledChanged() { | 334 void Label::OnEnabledChanged() { |
| 335 RecalculateColors(); | 335 RecalculateColors(); |
| 336 | |
| 337 // If the label has become focusable now due to being enabled, GetInsets() | |
| 338 // will return a different value. Hence reset layout. | |
|
karandeepb
2016/04/27 23:53:17
I am not totally sure whether this is needed or no
| |
| 339 if (IsFocusable()) | |
| 340 ResetLayout(); | |
| 336 } | 341 } |
| 337 | 342 |
| 338 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( | 343 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( |
| 339 const base::string16& text, | 344 const base::string16& text, |
| 340 gfx::HorizontalAlignment alignment, | 345 gfx::HorizontalAlignment alignment, |
| 341 gfx::DirectionalityMode directionality, | 346 gfx::DirectionalityMode directionality, |
| 342 gfx::ElideBehavior elide_behavior) { | 347 gfx::ElideBehavior elide_behavior) { |
| 343 std::unique_ptr<gfx::RenderText> render_text( | 348 std::unique_ptr<gfx::RenderText> render_text( |
| 344 render_text_->CreateInstanceOfSameType()); | 349 render_text_->CreateInstanceOfSameType()); |
| 345 render_text->SetHorizontalAlignment(alignment); | 350 render_text->SetHorizontalAlignment(alignment); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 View::OnDeviceScaleFactorChanged(device_scale_factor); | 395 View::OnDeviceScaleFactorChanged(device_scale_factor); |
| 391 // When the device scale factor is changed, some font rendering parameters is | 396 // When the device scale factor is changed, some font rendering parameters is |
| 392 // changed (especially, hinting). The bounding box of the text has to be | 397 // changed (especially, hinting). The bounding box of the text has to be |
| 393 // re-computed based on the new parameters. See crbug.com/441439 | 398 // re-computed based on the new parameters. See crbug.com/441439 |
| 394 ResetLayout(); | 399 ResetLayout(); |
| 395 } | 400 } |
| 396 | 401 |
| 397 void Label::VisibilityChanged(View* starting_from, bool is_visible) { | 402 void Label::VisibilityChanged(View* starting_from, bool is_visible) { |
| 398 if (!is_visible) | 403 if (!is_visible) |
| 399 lines_.clear(); | 404 lines_.clear(); |
| 405 | |
| 406 // If the label has become focusable now due to being visible, GetInsets() | |
| 407 // will return a different value. Hence reset layout. | |
| 408 if (IsFocusable()) | |
| 409 ResetLayout(); | |
| 400 } | 410 } |
| 401 | 411 |
| 402 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { | 412 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { |
| 403 render_text_.reset(gfx::RenderText::CreateInstance()); | 413 render_text_.reset(gfx::RenderText::CreateInstance()); |
| 404 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 414 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 405 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); | 415 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 406 // NOTE: |render_text_| should not be elided at all. This is used to keep some | 416 // NOTE: |render_text_| should not be elided at all. This is used to keep some |
| 407 // properties and to compute the size of the string. | 417 // properties and to compute the size of the string. |
| 408 render_text_->SetElideBehavior(gfx::NO_ELIDE); | 418 render_text_->SetElideBehavior(gfx::NO_ELIDE); |
| 409 render_text_->SetFontList(font_list); | 419 render_text_->SetFontList(font_list); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 428 PreferredSizeChanged(); | 438 PreferredSizeChanged(); |
| 429 SchedulePaint(); | 439 SchedulePaint(); |
| 430 lines_.clear(); | 440 lines_.clear(); |
| 431 } | 441 } |
| 432 | 442 |
| 433 void Label::MaybeBuildRenderTextLines() { | 443 void Label::MaybeBuildRenderTextLines() { |
| 434 if (!lines_.empty()) | 444 if (!lines_.empty()) |
| 435 return; | 445 return; |
| 436 | 446 |
| 437 gfx::Rect rect = GetContentsBounds(); | 447 gfx::Rect rect = GetContentsBounds(); |
| 438 if (focusable()) | 448 if (IsFocusable()) |
| 439 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); | 449 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); |
| 440 if (rect.IsEmpty()) | 450 if (rect.IsEmpty()) |
| 441 return; | 451 return; |
| 442 rect.Inset(-gfx::ShadowValue::GetMargin(shadows())); | 452 rect.Inset(-gfx::ShadowValue::GetMargin(shadows())); |
| 443 | 453 |
| 444 gfx::HorizontalAlignment alignment = horizontal_alignment(); | 454 gfx::HorizontalAlignment alignment = horizontal_alignment(); |
| 445 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); | 455 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); |
| 446 if (multi_line()) { | 456 if (multi_line()) { |
| 447 // Force the directionality and alignment of the first line on other lines. | 457 // Force the directionality and alignment of the first line on other lines. |
| 448 bool rtl = | 458 bool rtl = |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 } | 597 } |
| 588 | 598 |
| 589 bool Label::ShouldShowDefaultTooltip() const { | 599 bool Label::ShouldShowDefaultTooltip() const { |
| 590 const gfx::Size text_size = GetTextSize(); | 600 const gfx::Size text_size = GetTextSize(); |
| 591 const gfx::Size size = GetContentsBounds().size(); | 601 const gfx::Size size = GetContentsBounds().size(); |
| 592 return !obscured() && (text_size.width() > size.width() || | 602 return !obscured() && (text_size.width() > size.width() || |
| 593 (multi_line() && text_size.height() > size.height())); | 603 (multi_line() && text_size.height() > size.height())); |
| 594 } | 604 } |
| 595 | 605 |
| 596 } // namespace views | 606 } // namespace views |
| OLD | NEW |