OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/corewm/tooltip_aura.h" | 5 #include "ui/views/corewm/tooltip_aura.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "ui/aura/root_window.h" | 9 #include "ui/aura/root_window.h" |
10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 label_.set_owned_by_client(); | 79 label_.set_owned_by_client(); |
80 label_.SetMultiLine(true); | 80 label_.SetMultiLine(true); |
81 } | 81 } |
82 | 82 |
83 TooltipAura::~TooltipAura() { | 83 TooltipAura::~TooltipAura() { |
84 DestroyWidget(); | 84 DestroyWidget(); |
85 } | 85 } |
86 | 86 |
87 // static | 87 // static |
88 void TooltipAura::TrimTooltipToFit(int max_width, | 88 void TooltipAura::TrimTooltipToFit(int max_width, |
89 string16* text, | 89 base::string16* text, |
90 int* width, | 90 int* width, |
91 int* line_count) { | 91 int* line_count) { |
92 *width = 0; | 92 *width = 0; |
93 *line_count = 0; | 93 *line_count = 0; |
94 | 94 |
95 // Determine the available width for the tooltip. | 95 // Determine the available width for the tooltip. |
96 int available_width = std::min(kTooltipMaxWidthPixels, max_width); | 96 int available_width = std::min(kTooltipMaxWidthPixels, max_width); |
97 | 97 |
98 std::vector<string16> lines; | 98 std::vector<base::string16> lines; |
99 base::SplitString(*text, '\n', &lines); | 99 base::SplitString(*text, '\n', &lines); |
100 std::vector<string16> result_lines; | 100 std::vector<base::string16> result_lines; |
101 | 101 |
102 // Format each line to fit. | 102 // Format each line to fit. |
103 gfx::Font font = GetDefaultFont(); | 103 gfx::Font font = GetDefaultFont(); |
104 for (std::vector<string16>::iterator l = lines.begin(); l != lines.end(); | 104 for (std::vector<base::string16>::iterator l = lines.begin(); |
105 ++l) { | 105 l != lines.end(); ++l) { |
106 // We break the line at word boundaries, then stuff as many words as we can | 106 // We break the line at word boundaries, then stuff as many words as we can |
107 // in the available width to the current line, and move the remaining words | 107 // in the available width to the current line, and move the remaining words |
108 // to a new line. | 108 // to a new line. |
109 std::vector<string16> words; | 109 std::vector<base::string16> words; |
110 base::SplitStringDontTrim(*l, ' ', &words); | 110 base::SplitStringDontTrim(*l, ' ', &words); |
111 int current_width = 0; | 111 int current_width = 0; |
112 string16 line; | 112 base::string16 line; |
113 for (std::vector<string16>::iterator w = words.begin(); w != words.end(); | 113 for (std::vector<base::string16>::iterator w = words.begin(); |
114 ++w) { | 114 w != words.end(); ++w) { |
115 string16 word = *w; | 115 base::string16 word = *w; |
116 if (w + 1 != words.end()) | 116 if (w + 1 != words.end()) |
117 word.push_back(' '); | 117 word.push_back(' '); |
118 int word_width = font.GetStringWidth(word); | 118 int word_width = font.GetStringWidth(word); |
119 if (current_width + word_width > available_width) { | 119 if (current_width + word_width > available_width) { |
120 // Current width will exceed the available width. Must start a new line. | 120 // Current width will exceed the available width. Must start a new line. |
121 if (!line.empty()) | 121 if (!line.empty()) |
122 result_lines.push_back(line); | 122 result_lines.push_back(line); |
123 current_width = 0; | 123 current_width = 0; |
124 line.clear(); | 124 line.clear(); |
125 } | 125 } |
126 current_width += word_width; | 126 current_width += word_width; |
127 line.append(word); | 127 line.append(word); |
128 } | 128 } |
129 result_lines.push_back(line); | 129 result_lines.push_back(line); |
130 } | 130 } |
131 | 131 |
132 // Clamp number of lines to |kMaxLines|. | 132 // Clamp number of lines to |kMaxLines|. |
133 if (result_lines.size() > kMaxLines) { | 133 if (result_lines.size() > kMaxLines) { |
134 result_lines.resize(kMaxLines); | 134 result_lines.resize(kMaxLines); |
135 // Add ellipses character to last line. | 135 // Add ellipses character to last line. |
136 result_lines[kMaxLines - 1] = gfx::TruncateString( | 136 result_lines[kMaxLines - 1] = gfx::TruncateString( |
137 result_lines.back(), result_lines.back().length() - 1); | 137 result_lines.back(), result_lines.back().length() - 1); |
138 } | 138 } |
139 *line_count = result_lines.size(); | 139 *line_count = result_lines.size(); |
140 | 140 |
141 // Flatten the result. | 141 // Flatten the result. |
142 string16 result; | 142 base::string16 result; |
143 for (std::vector<string16>::iterator l = result_lines.begin(); | 143 for (std::vector<base::string16>::iterator l = result_lines.begin(); |
144 l != result_lines.end(); ++l) { | 144 l != result_lines.end(); ++l) { |
145 if (!result.empty()) | 145 if (!result.empty()) |
146 result.push_back('\n'); | 146 result.push_back('\n'); |
147 int line_width = font.GetStringWidth(*l); | 147 int line_width = font.GetStringWidth(*l); |
148 // Since we only break at word boundaries, it could happen that due to some | 148 // Since we only break at word boundaries, it could happen that due to some |
149 // very long word, line_width is greater than the available_width. In such | 149 // very long word, line_width is greater than the available_width. In such |
150 // case, we simply truncate at available_width and add ellipses at the end. | 150 // case, we simply truncate at available_width and add ellipses at the end. |
151 if (line_width > available_width) { | 151 if (line_width > available_width) { |
152 *width = available_width; | 152 *width = available_width; |
153 result.append(gfx::ElideText(*l, font, available_width, | 153 result.append(gfx::ElideText(*l, font, available_width, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 227 |
228 void TooltipAura::DestroyWidget() { | 228 void TooltipAura::DestroyWidget() { |
229 if (widget_) { | 229 if (widget_) { |
230 widget_->RemoveObserver(this); | 230 widget_->RemoveObserver(this); |
231 widget_->Close(); | 231 widget_->Close(); |
232 widget_ = NULL; | 232 widget_ = NULL; |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 void TooltipAura::SetText(aura::Window* window, | 236 void TooltipAura::SetText(aura::Window* window, |
237 const string16& tooltip_text, | 237 const base::string16& tooltip_text, |
238 const gfx::Point& location) { | 238 const gfx::Point& location) { |
239 tooltip_window_ = window; | 239 tooltip_window_ = window; |
240 int max_width, line_count; | 240 int max_width, line_count; |
241 string16 trimmed_text(tooltip_text); | 241 base::string16 trimmed_text(tooltip_text); |
242 TrimTooltipToFit( | 242 TrimTooltipToFit( |
243 GetMaxWidth(location), &trimmed_text, &max_width, &line_count); | 243 GetMaxWidth(location), &trimmed_text, &max_width, &line_count); |
244 label_.SetText(trimmed_text); | 244 label_.SetText(trimmed_text); |
245 | 245 |
246 int width = max_width + 2 * kTooltipHorizontalPadding; | 246 int width = max_width + 2 * kTooltipHorizontalPadding; |
247 int height = label_.GetHeightForWidth(max_width) + | 247 int height = label_.GetHeightForWidth(max_width) + |
248 2 * kTooltipVerticalPadding; | 248 2 * kTooltipVerticalPadding; |
249 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoDropShadows)) { | 249 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoDropShadows)) { |
250 width += 2 * kTooltipBorderWidth; | 250 width += 2 * kTooltipBorderWidth; |
251 height += 2 * kTooltipBorderWidth; | 251 height += 2 * kTooltipBorderWidth; |
(...skipping 18 matching lines...) Expand all Loading... |
270 } | 270 } |
271 | 271 |
272 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 272 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
273 DCHECK_EQ(widget_, widget); | 273 DCHECK_EQ(widget_, widget); |
274 widget_ = NULL; | 274 widget_ = NULL; |
275 tooltip_window_ = NULL; | 275 tooltip_window_ = NULL; |
276 } | 276 } |
277 | 277 |
278 } // namespace corewm | 278 } // namespace corewm |
279 } // namespace views | 279 } // namespace views |
OLD | NEW |