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

Unified Diff: ui/views/corewm/tooltip_aura.cc

Issue 340543004: views: Support longer tooltips. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update to use views::Label Created 6 years, 5 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
« no previous file with comments | « ui/views/corewm/tooltip_aura.h ('k') | ui/views/corewm/tooltip_aura_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/corewm/tooltip_aura.cc
diff --git a/ui/views/corewm/tooltip_aura.cc b/ui/views/corewm/tooltip_aura.cc
index 6f434997f045c50f3e38e36c642b09d085cd1807..ac0b3258ee5f372eaff131323525666d470d3f2c 100644
--- a/ui/views/corewm/tooltip_aura.cc
+++ b/ui/views/corewm/tooltip_aura.cc
@@ -18,19 +18,6 @@
namespace {
-const int kTooltipHorizontalPadding = 3;
-
-// Max visual tooltip width. If a tooltip is greater than this width, it will
-// be wrapped.
-const int kTooltipMaxWidthPixels = 400;
-
-const size_t kMaxLines = 10;
-
-// TODO(derat): This padding is needed on Chrome OS devices but seems excessive
-// when running the same binary on a Linux workstation; presumably there's a
-// difference in font metrics. Rationalize this.
-const int kTooltipVerticalPadding = 2;
-
// FIXME: get cursor offset from actual cursor size.
const int kCursorOffsetX = 10;
const int kCursorOffsetY = 15;
@@ -67,95 +54,15 @@ TooltipAura::~TooltipAura() {
DestroyWidget();
}
-// static
-void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list,
- int max_width,
- base::string16* text,
- int* width,
- int* line_count) {
- *width = 0;
- *line_count = 0;
-
- // Determine the available width for the tooltip.
- int available_width = std::min(kTooltipMaxWidthPixels, max_width);
-
- std::vector<base::string16> lines;
- base::SplitString(*text, '\n', &lines);
- std::vector<base::string16> result_lines;
-
- // Format each line to fit.
- for (std::vector<base::string16>::iterator l = lines.begin();
- l != lines.end(); ++l) {
- // We break the line at word boundaries, then stuff as many words as we can
- // in the available width to the current line, and move the remaining words
- // to a new line.
- std::vector<base::string16> words;
- base::SplitStringDontTrim(*l, ' ', &words);
- int current_width = 0;
- base::string16 line;
- for (std::vector<base::string16>::iterator w = words.begin();
- w != words.end(); ++w) {
- base::string16 word = *w;
- if (w + 1 != words.end())
- word.push_back(' ');
- int word_width = gfx::GetStringWidth(word, font_list);
- if (current_width + word_width > available_width) {
- // Current width will exceed the available width. Must start a new line.
- if (!line.empty())
- result_lines.push_back(line);
- current_width = 0;
- line.clear();
- }
- current_width += word_width;
- line.append(word);
- }
- result_lines.push_back(line);
- }
-
- // Clamp number of lines to |kMaxLines|.
- if (result_lines.size() > kMaxLines) {
- result_lines.resize(kMaxLines);
- // Add ellipses character to last line.
- result_lines[kMaxLines - 1] = gfx::TruncateString(
- result_lines.back(), result_lines.back().length() - 1, gfx::WORD_BREAK);
- }
- *line_count = result_lines.size();
-
- // Flatten the result.
- base::string16 result;
- for (std::vector<base::string16>::iterator l = result_lines.begin();
- l != result_lines.end(); ++l) {
- if (!result.empty())
- result.push_back('\n');
- int line_width = gfx::GetStringWidth(*l, font_list);
- // Since we only break at word boundaries, it could happen that due to some
- // very long word, line_width is greater than the available_width. In such
- // case, we simply truncate at available_width and add ellipses at the end.
- if (line_width > available_width) {
- *width = available_width;
- result.append(gfx::ElideText(*l, font_list, available_width,
- gfx::ELIDE_TAIL));
- } else {
- *width = std::max(*width, line_width);
- result.append(*l);
- }
- }
- *text = result;
-}
-
int TooltipAura::GetMaxWidth(const gfx::Point& location) const {
- // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure
- // out a way to merge.
gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_);
gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
- return (display_bounds.width() + 1) / 2;
+ return display_bounds.width() * 3 / 4;
}
void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
- int tooltip_width,
- int tooltip_height) {
- gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width,
- tooltip_height);
+ const gfx::Size& tooltip_size) {
+ gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_);
@@ -171,7 +78,7 @@ void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
// If tooltip is out of bounds on the y axis, we flip it to appear above the
// mouse cursor instead of below.
if (tooltip_rect.bottom() > display_bounds.bottom())
- tooltip_rect.set_y(mouse_pos.y() - tooltip_height);
+ tooltip_rect.set_y(mouse_pos.y() - tooltip_rect.height());
tooltip_rect.AdjustToFit(display_bounds);
widget_->SetBounds(tooltip_rect);
@@ -189,15 +96,9 @@ void TooltipAura::SetText(aura::Window* window,
const base::string16& tooltip_text,
const gfx::Point& location) {
tooltip_window_ = window;
- int max_width, line_count;
- base::string16 trimmed_text(tooltip_text);
- TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text,
- &max_width, &line_count);
- label_.SetText(trimmed_text);
- int width = max_width + 2 * kTooltipHorizontalPadding;
- int height = label_.GetHeightForWidth(max_width) +
- 2 * kTooltipVerticalPadding;
+ label_.SetText(tooltip_text);
+ label_.SizeToFit(GetMaxWidth(location));
if (!widget_) {
widget_ = CreateTooltipWidget(tooltip_window_);
@@ -205,7 +106,7 @@ void TooltipAura::SetText(aura::Window* window,
widget_->AddObserver(this);
}
- SetTooltipBounds(location, width, height);
+ SetTooltipBounds(location, label_.GetPreferredSize());
ui::NativeTheme* native_theme = widget_->GetNativeTheme();
label_.set_background(
« no previous file with comments | « ui/views/corewm/tooltip_aura.h ('k') | ui/views/corewm/tooltip_aura_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698