| 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/corewm/tooltip_controller.h" | 5 #include "ui/views/corewm/tooltip_controller.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const SkColor kTooltipBackground = 0xFFFFFFCC; | 34 const SkColor kTooltipBackground = 0xFFFFFFCC; |
| 35 const SkColor kTooltipBorder = 0xFF646450; | 35 const SkColor kTooltipBorder = 0xFF646450; |
| 36 const int kTooltipBorderWidth = 1; | 36 const int kTooltipBorderWidth = 1; |
| 37 const int kTooltipHorizontalPadding = 3; | 37 const int kTooltipHorizontalPadding = 3; |
| 38 | 38 |
| 39 // Max visual tooltip width. If a tooltip is greater than this width, it will | 39 // Max visual tooltip width. If a tooltip is greater than this width, it will |
| 40 // be wrapped. | 40 // be wrapped. |
| 41 const int kTooltipMaxWidthPixels = 400; | 41 const float kTooltipMaxWidthPixels = 400; |
| 42 | 42 |
| 43 // Maximum number of lines we allow in the tooltip. | 43 // Maximum number of lines we allow in the tooltip. |
| 44 const size_t kMaxLines = 10; | 44 const size_t kMaxLines = 10; |
| 45 | 45 |
| 46 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive | 46 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive |
| 47 // when running the same binary on a Linux workstation; presumably there's a | 47 // when running the same binary on a Linux workstation; presumably there's a |
| 48 // difference in font metrics. Rationalize this. | 48 // difference in font metrics. Rationalize this. |
| 49 const int kTooltipVerticalPadding = 2; | 49 const int kTooltipVerticalPadding = 2; |
| 50 const int kTooltipTimeoutMs = 500; | 50 const int kTooltipTimeoutMs = 500; |
| 51 const int kDefaultTooltipShownTimeoutMs = 10000; | 51 const int kDefaultTooltipShownTimeoutMs = 10000; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 | 103 |
| 104 virtual ~Tooltip() { | 104 virtual ~Tooltip() { |
| 105 DestroyWidget(); | 105 DestroyWidget(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Updates the text on the tooltip and resizes to fit. | 108 // Updates the text on the tooltip and resizes to fit. |
| 109 void SetText(aura::Window* window, | 109 void SetText(aura::Window* window, |
| 110 const string16& tooltip_text, | 110 const string16& tooltip_text, |
| 111 const gfx::Point& location) { | 111 const gfx::Point& location) { |
| 112 int max_width, line_count; | 112 float max_width; |
| 113 int line_count; |
| 113 string16 trimmed_text(tooltip_text); | 114 string16 trimmed_text(tooltip_text); |
| 114 controller_->TrimTooltipToFit( | 115 controller_->TrimTooltipToFit( |
| 115 controller_->GetMaxWidth(location), &trimmed_text, &max_width, | 116 controller_->GetMaxWidth(location), &trimmed_text, &max_width, |
| 116 &line_count); | 117 &line_count); |
| 117 label_.SetText(trimmed_text); | 118 label_.SetText(trimmed_text); |
| 118 | 119 |
| 119 int width = max_width + 2 * kTooltipHorizontalPadding; | 120 float width = max_width + 2 * kTooltipHorizontalPadding; |
| 120 int height = label_.GetHeightForWidth(max_width) + | 121 int height = label_.GetHeightForWidth(max_width) + |
| 121 2 * kTooltipVerticalPadding; | 122 2 * kTooltipVerticalPadding; |
| 122 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoDropShadows)) { | 123 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoDropShadows)) { |
| 123 width += 2 * kTooltipBorderWidth; | 124 width += 2 * kTooltipBorderWidth; |
| 124 height += 2 * kTooltipBorderWidth; | 125 height += 2 * kTooltipBorderWidth; |
| 125 } | 126 } |
| 126 GetWidgetForWindow(window); | 127 GetWidgetForWindow(window); |
| 127 SetTooltipBounds(location, width, height); | 128 SetTooltipBounds(location, width, height); |
| 128 } | 129 } |
| 129 | 130 |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 widget_bounds = gfx::Rect(root->GetHostOrigin(), root->GetHostSize()); | 361 widget_bounds = gfx::Rect(root->GetHostOrigin(), root->GetHostSize()); |
| 361 } | 362 } |
| 362 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 363 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
| 363 gfx::Rect bounds(screen->GetDisplayNearestPoint(origin).bounds()); | 364 gfx::Rect bounds(screen->GetDisplayNearestPoint(origin).bounds()); |
| 364 if (!widget_bounds.IsEmpty()) | 365 if (!widget_bounds.IsEmpty()) |
| 365 bounds.Intersect(widget_bounds); | 366 bounds.Intersect(widget_bounds); |
| 366 return bounds; | 367 return bounds; |
| 367 } | 368 } |
| 368 | 369 |
| 369 // static | 370 // static |
| 370 void TooltipController::TrimTooltipToFit(int max_width, | 371 void TooltipController::TrimTooltipToFit(float max_width, |
| 371 string16* text, | 372 string16* text, |
| 372 int* width, | 373 float* width, |
| 373 int* line_count) { | 374 int* line_count) { |
| 374 *width = 0; | 375 *width = 0; |
| 375 *line_count = 0; | 376 *line_count = 0; |
| 376 | 377 |
| 377 // Clamp the tooltip length to kMaxTooltipLength so that we don't | 378 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
| 378 // accidentally DOS the user with a mega tooltip. | 379 // accidentally DOS the user with a mega tooltip. |
| 379 if (text->length() > kMaxTooltipLength) | 380 if (text->length() > kMaxTooltipLength) |
| 380 *text = text->substr(0, kMaxTooltipLength); | 381 *text = text->substr(0, kMaxTooltipLength); |
| 381 | 382 |
| 382 // Determine the available width for the tooltip. | 383 // Determine the available width for the tooltip. |
| 383 int available_width = std::min(kTooltipMaxWidthPixels, max_width); | 384 float available_width = std::min(kTooltipMaxWidthPixels, max_width); |
| 384 | 385 |
| 385 std::vector<string16> lines; | 386 std::vector<string16> lines; |
| 386 base::SplitString(*text, '\n', &lines); | 387 base::SplitString(*text, '\n', &lines); |
| 387 std::vector<string16> result_lines; | 388 std::vector<string16> result_lines; |
| 388 | 389 |
| 389 // Format each line to fit. | 390 // Format each line to fit. |
| 390 gfx::Font font = GetDefaultFont(); | 391 gfx::Font font = GetDefaultFont(); |
| 391 for (std::vector<string16>::iterator l = lines.begin(); l != lines.end(); | 392 for (std::vector<string16>::iterator l = lines.begin(); l != lines.end(); |
| 392 ++l) { | 393 ++l) { |
| 393 // We break the line at word boundaries, then stuff as many words as we can | 394 // We break the line at word boundaries, then stuff as many words as we can |
| 394 // in the available width to the current line, and move the remaining words | 395 // in the available width to the current line, and move the remaining words |
| 395 // to a new line. | 396 // to a new line. |
| 396 std::vector<string16> words; | 397 std::vector<string16> words; |
| 397 base::SplitStringDontTrim(*l, ' ', &words); | 398 base::SplitStringDontTrim(*l, ' ', &words); |
| 398 int current_width = 0; | 399 float current_width = 0; |
| 399 string16 line; | 400 string16 line; |
| 400 for (std::vector<string16>::iterator w = words.begin(); w != words.end(); | 401 for (std::vector<string16>::iterator w = words.begin(); w != words.end(); |
| 401 ++w) { | 402 ++w) { |
| 402 string16 word = *w; | 403 string16 word = *w; |
| 403 if (w + 1 != words.end()) | 404 if (w + 1 != words.end()) |
| 404 word.push_back(' '); | 405 word.push_back(' '); |
| 405 int word_width = font.GetStringWidth(word); | 406 float word_width = font.GetStringWidth(word); |
| 406 if (current_width + word_width > available_width) { | 407 if (current_width + word_width > available_width) { |
| 407 // Current width will exceed the available width. Must start a new line. | 408 // Current width will exceed the available width. Must start a new line. |
| 408 if (!line.empty()) | 409 if (!line.empty()) |
| 409 result_lines.push_back(line); | 410 result_lines.push_back(line); |
| 410 current_width = 0; | 411 current_width = 0; |
| 411 line.clear(); | 412 line.clear(); |
| 412 } | 413 } |
| 413 current_width += word_width; | 414 current_width += word_width; |
| 414 line.append(word); | 415 line.append(word); |
| 415 } | 416 } |
| 416 result_lines.push_back(line); | 417 result_lines.push_back(line); |
| 417 } | 418 } |
| 418 | 419 |
| 419 // Clamp number of lines to |kMaxLines|. | 420 // Clamp number of lines to |kMaxLines|. |
| 420 if (result_lines.size() > kMaxLines) { | 421 if (result_lines.size() > kMaxLines) { |
| 421 result_lines.resize(kMaxLines); | 422 result_lines.resize(kMaxLines); |
| 422 // Add ellipses character to last line. | 423 // Add ellipses character to last line. |
| 423 result_lines[kMaxLines - 1] = gfx::TruncateString( | 424 result_lines[kMaxLines - 1] = gfx::TruncateString( |
| 424 result_lines.back(), result_lines.back().length() - 1); | 425 result_lines.back(), result_lines.back().length() - 1); |
| 425 } | 426 } |
| 426 *line_count = result_lines.size(); | 427 *line_count = result_lines.size(); |
| 427 | 428 |
| 428 // Flatten the result. | 429 // Flatten the result. |
| 429 string16 result; | 430 string16 result; |
| 430 for (std::vector<string16>::iterator l = result_lines.begin(); | 431 for (std::vector<string16>::iterator l = result_lines.begin(); |
| 431 l != result_lines.end(); ++l) { | 432 l != result_lines.end(); ++l) { |
| 432 if (!result.empty()) | 433 if (!result.empty()) |
| 433 result.push_back('\n'); | 434 result.push_back('\n'); |
| 434 int line_width = font.GetStringWidth(*l); | 435 float line_width = font.GetStringWidth(*l); |
| 435 // Since we only break at word boundaries, it could happen that due to some | 436 // Since we only break at word boundaries, it could happen that due to some |
| 436 // very long word, line_width is greater than the available_width. In such | 437 // very long word, line_width is greater than the available_width. In such |
| 437 // case, we simply truncate at available_width and add ellipses at the end. | 438 // case, we simply truncate at available_width and add ellipses at the end. |
| 438 if (line_width > available_width) { | 439 if (line_width > available_width) { |
| 439 *width = available_width; | 440 *width = available_width; |
| 440 result.append(gfx::ElideText(*l, font, available_width, | 441 result.append(gfx::ElideText(*l, font, available_width, |
| 441 gfx::ELIDE_AT_END)); | 442 gfx::ELIDE_AT_END)); |
| 442 } else { | 443 } else { |
| 443 *width = std::max(*width, line_width); | 444 *width = std::max(*width, line_width); |
| 444 result.append(*l); | 445 result.append(*l); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 int TooltipController::GetTooltipShownTimeout() { | 547 int TooltipController::GetTooltipShownTimeout() { |
| 547 std::map<aura::Window*, int>::const_iterator it = | 548 std::map<aura::Window*, int>::const_iterator it = |
| 548 tooltip_shown_timeout_map_.find(tooltip_window_); | 549 tooltip_shown_timeout_map_.find(tooltip_window_); |
| 549 if (it == tooltip_shown_timeout_map_.end()) | 550 if (it == tooltip_shown_timeout_map_.end()) |
| 550 return kDefaultTooltipShownTimeoutMs; | 551 return kDefaultTooltipShownTimeoutMs; |
| 551 return it->second; | 552 return it->second; |
| 552 } | 553 } |
| 553 | 554 |
| 554 } // namespace corewm | 555 } // namespace corewm |
| 555 } // namespace views | 556 } // namespace views |
| OLD | NEW |