Index: ui/views/corewm/tooltip_aura.cc |
diff --git a/ui/views/corewm/tooltip_aura.cc b/ui/views/corewm/tooltip_aura.cc |
index f9f41b9372255a915840a190c52eef49623bc6e7..3ae99df1ba89762010dd5854b2e109342921f0b5 100644 |
--- a/ui/views/corewm/tooltip_aura.cc |
+++ b/ui/views/corewm/tooltip_aura.cc |
@@ -5,6 +5,7 @@ |
#include "ui/views/corewm/tooltip_aura.h" |
#include "base/strings/string_split.h" |
+#include "base/strings/utf_string_conversions.h" |
#include "ui/aura/window.h" |
#include "ui/aura/window_tree_host.h" |
#include "ui/base/resource/resource_bundle.h" |
@@ -20,12 +21,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. |
@@ -55,6 +50,8 @@ views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { |
namespace views { |
namespace corewm { |
+const int TooltipAura::kPreferredWidthPixels = 400; |
+ |
TooltipAura::TooltipAura(gfx::ScreenType screen_type) |
: screen_type_(screen_type), |
widget_(NULL), |
@@ -69,93 +66,105 @@ TooltipAura::~TooltipAura() { |
// static |
void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list, |
sky
2014/06/18 03:20:07
I think we should convert this to use views::Label
Daniel Erat
2014/06/18 04:37:11
i don't know the history here, but i believe that
varunjain
2014/06/22 20:00:30
Sorry I do not remember why label was not used dir
|
- int max_width, |
+ const gfx::Size& max_size, |
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; |
+ |
+ // TODO: Use ICU to split into words. |
+ int max_word_width = 0; |
+ std::vector<std::vector<base::string16> > words_by_line; |
+ for (size_t i = 0; i < lines.size(); ++i) { |
+ std::vector<base::string16> words; |
+ base::SplitStringDontTrim(lines[i], ' ', &words); |
+ for (size_t j = 0; j < words.size(); ++j) { |
+ const base::string16& word = words[j]; |
+ max_word_width = std::max(gfx::GetStringWidth(word, font_list), |
+ max_word_width); |
+ } |
+ words_by_line.push_back(words); |
+ } |
+ |
+ // Determine the available width for the tooltip. |
+ const int available_width = std::min(max_size.width(), |
+ std::max(max_word_width, kPreferredWidthPixels)); |
// Format each line to fit. |
- for (std::vector<base::string16>::iterator l = lines.begin(); |
- l != lines.end(); ++l) { |
+ std::vector<base::string16> result_lines; |
+ const base::string16 space(base::ASCIIToUTF16(" ")); |
+ for (size_t i = 0; i < words_by_line.size(); ++i) { |
// 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; |
+ const std::vector<base::string16>& words = words_by_line[i]; |
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(); |
+ for (size_t j = 0; j < words.size(); ++j) { |
+ const base::string16& word = words[j]; |
+ if (!line.empty()) { |
+ // Put the word on the end of the line if it fits. |
+ base::string16 proposed_line = line + space + word; |
+ if (gfx::GetStringWidth(proposed_line, font_list) <= available_width) { |
+ line = proposed_line; |
+ continue; |
+ } |
+ // Otherwise, save the in-progress line and start a new one. |
+ result_lines.push_back(line); |
} |
- current_width += word_width; |
- line.append(word); |
+ line = word; |
} |
- result_lines.push_back(line); |
+ if (!line.empty()) |
+ result_lines.push_back(line); |
} |
- // Clamp number of lines to |kMaxLines|. |
- if (result_lines.size() > kMaxLines) { |
- result_lines.resize(kMaxLines); |
+ // Clamp the number of lines. |
+ const size_t max_lines = static_cast<size_t>( |
+ std::max(1, max_size.height() / font_list.GetHeight())); |
+ if (result_lines.size() > max_lines) { |
+ result_lines.resize(max_lines); |
// Add ellipses character to last line. |
- result_lines[kMaxLines - 1] = gfx::TruncateString( |
+ result_lines[max_lines - 1] = gfx::TruncateString( |
result_lines.back(), result_lines.back().length() - 1); |
} |
*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) { |
+ for (size_t i = 0; i < result_lines.size(); ++i) { |
if (!result.empty()) |
result.push_back('\n'); |
- int line_width = gfx::GetStringWidth(*l, font_list); |
+ |
+ base::string16 line = result_lines[i]; |
+ int line_width = gfx::GetStringWidth(line, 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); |
+ line = gfx::ElideText(line, font_list, available_width, gfx::ELIDE_TAIL); |
+ line_width = gfx::GetStringWidth(line, font_list); |
} |
+ |
+ result.append(line); |
+ *width = std::max(*width, line_width); |
} |
*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::Size TooltipAura::GetMaxSize(const gfx::Point& location) const { |
gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); |
- return (display_bounds.width() + 1) / 2; |
+ return gfx::Size(display_bounds.width() * 3 / 4, |
sky
2014/06/18 03:20:07
I'm inclined to give the tooltip the full width. W
|
+ display_bounds.height() * 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 +180,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); |
@@ -191,7 +200,7 @@ void TooltipAura::SetText(aura::Window* window, |
tooltip_window_ = window; |
int max_width, line_count; |
base::string16 trimmed_text(tooltip_text); |
- TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, |
+ TrimTooltipToFit(label_.font_list(), GetMaxSize(location), &trimmed_text, |
&max_width, &line_count); |
label_.SetText(trimmed_text); |
@@ -205,7 +214,7 @@ void TooltipAura::SetText(aura::Window* window, |
widget_->AddObserver(this); |
} |
- SetTooltipBounds(location, width, height); |
+ SetTooltipBounds(location, gfx::Size(width, height)); |
ui::NativeTheme* native_theme = widget_->GetNativeTheme(); |
label_.set_background( |