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

Unified Diff: ui/views/controls/styled_label.cc

Issue 843023002: [Smart Lock] Add a private API to show an error bubble anchored to the Smart Lock app window. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/styled_label.cc
diff --git a/ui/views/controls/styled_label.cc b/ui/views/controls/styled_label.cc
index 44e42ed6549d271288d9aeefd5b50c4c6bad50d4..e10c14a4d481c4411ac87ae44e047265c3b2be9a 100644
--- a/ui/views/controls/styled_label.cc
+++ b/ui/views/controls/styled_label.cc
@@ -4,6 +4,7 @@
#include "ui/views/controls/styled_label.h"
+#include <limits>
#include <vector>
#include "base/strings/string_util.h"
@@ -96,6 +97,7 @@ StyledLabel::StyledLabel(const base::string16& text,
StyledLabelListener* listener)
: specified_line_height_(0),
listener_(listener),
+ width_at_last_size_calculation_(0),
width_at_last_layout_(0),
displayed_on_background_color_(SkColorSetRGB(0xFF, 0xFF, 0xFF)),
displayed_on_background_color_set_(false),
@@ -156,6 +158,14 @@ void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) {
}
}
+void StyledLabel::SizeToFit(int max_width) {
+ if (max_width == 0)
+ max_width = std::numeric_limits<int>::max();
+
+ SetSize(CalculateAndDoLayout(max_width, true));
+}
+
+
gfx::Insets StyledLabel::GetInsets() const {
gfx::Insets insets = View::GetInsets();
@@ -180,18 +190,17 @@ int StyledLabel::GetHeightForWidth(int w) const {
// doesn't actually make any changes to member variables when |dry_run| is
// set to true. In general, the mutating and non-mutating parts shouldn't
// be in the same codepath.
- calculated_size_ =
- const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true);
- return calculated_size_.height();
+ return const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true).height();
}
void StyledLabel::Layout() {
- calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false);
- width_at_last_layout_ = calculated_size_.width();
+ CalculateAndDoLayout(GetLocalBounds().width(), false);
}
void StyledLabel::PreferredSizeChanged() {
calculated_size_ = gfx::Size();
+ width_at_last_size_calculation_ = 0;
+ width_at_last_layout_ = 0;
View::PreferredSizeChanged();
}
@@ -201,11 +210,15 @@ void StyledLabel::LinkClicked(Link* source, int event_flags) {
}
gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
- width -= GetInsets().width();
- if (width == calculated_size_.width() &&
- (dry_run || width_at_last_layout_ == width))
+ if (!dry_run)
+ width_at_last_layout_ = width;
+
+ if (width == width_at_last_size_calculation_ &&
+ (dry_run || width == width_at_last_layout_))
return calculated_size_;
+ width -= GetInsets().width();
+
if (!dry_run) {
RemoveAllChildViews(true);
link_targets_.clear();
@@ -221,6 +234,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
// The x position (in pixels) of the line we're on, relative to content
// bounds.
int x = 0;
+ // The width that was actually used. Guaranteed to be no larger than |width|.
+ int used_width = 0;
base::string16 remaining_string = text_;
StyleRanges::const_iterator current_range = style_ranges_.begin();
@@ -326,15 +341,18 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
AddChildView(label.release());
}
x += view_size.width() - focus_border_insets.width();
+ used_width = std::max(used_width, x);
remaining_string = remaining_string.substr(chunk.size());
}
+ DCHECK_LE(used_width, width);
// The user-specified line height only applies to interline spacing, so the
// final line's height is unaffected.
int total_height = line * line_height +
CalculateLineHeight(font_list_) + GetInsets().height();
- return gfx::Size(width, total_height);
+ calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
+ return calculated_size_;
}
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698