Chromium Code Reviews| 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 "ash/tooltips/tooltip_controller.h" | 5 #include "ash/tooltips/tooltip_controller.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "ui/views/border.h" | 26 #include "ui/views/border.h" |
| 27 #include "ui/views/controls/label.h" | 27 #include "ui/views/controls/label.h" |
| 28 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const SkColor kTooltipBackground = 0xFFFFFFCC; | 32 const SkColor kTooltipBackground = 0xFFFFFFCC; |
| 33 const SkColor kTooltipBorder = 0xFF646450; | 33 const SkColor kTooltipBorder = 0xFF646450; |
| 34 const int kTooltipBorderWidth = 1; | 34 const int kTooltipBorderWidth = 1; |
| 35 const int kTooltipHorizontalPadding = 3; | 35 const int kTooltipHorizontalPadding = 3; |
| 36 | |
| 37 // Max visual tooltip width. If a tooltip is greater than this width, it will | |
| 38 // be wrapped. | |
| 39 const int kTooltipMaxWidthPixels = 400; | |
|
sky
2012/03/21 04:17:23
On the windows side we initially had tooltips exte
varunjain
2012/03/21 04:48:38
I restrict them here to the smaller of available s
| |
| 40 | |
| 41 // Maximum number of lines we allow in the tooltip. | |
| 42 const size_t kMaxLines = 10; | |
| 43 | |
| 36 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive | 44 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive |
| 37 // when running the same binary on a Linux workstation; presumably there's a | 45 // when running the same binary on a Linux workstation; presumably there's a |
| 38 // difference in font metrics. Rationalize this. | 46 // difference in font metrics. Rationalize this. |
| 39 const int kTooltipVerticalPadding = 2; | 47 const int kTooltipVerticalPadding = 2; |
| 40 const int kTooltipTimeoutMs = 500; | 48 const int kTooltipTimeoutMs = 500; |
| 41 | 49 |
| 42 // FIXME: get cursor offset from actual cursor size. | 50 // FIXME: get cursor offset from actual cursor size. |
| 43 const int kCursorOffsetX = 10; | 51 const int kCursorOffsetX = 10; |
| 44 const int kCursorOffsetY = 15; | 52 const int kCursorOffsetY = 15; |
| 45 | 53 |
| 46 // Maximum number of characters we allow in a tooltip. | 54 // Maximum number of characters we allow in a tooltip. |
| 47 const size_t kMaxTooltipLength = 1024; | 55 const size_t kMaxTooltipLength = 1024; |
| 48 | 56 |
| 49 // Maximum number of lines we allow in the tooltip. | |
| 50 const size_t kMaxLines = 6; | |
| 51 | |
| 52 gfx::Font GetDefaultFont() { | 57 gfx::Font GetDefaultFont() { |
| 53 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure | 58 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure |
| 54 // out a way to merge. | 59 // out a way to merge. |
| 55 return ui::ResourceBundle::GetSharedInstance().GetFont( | 60 return ui::ResourceBundle::GetSharedInstance().GetFont( |
| 56 ui::ResourceBundle::BaseFont); | 61 ui::ResourceBundle::BaseFont); |
| 57 } | 62 } |
| 58 | 63 |
| 59 int GetMaxWidth(int x, int y) { | 64 int GetMaxWidth(int x, int y) { |
| 60 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure | 65 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure |
| 61 // out a way to merge. | 66 // out a way to merge. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 75 int y) { | 80 int y) { |
| 76 *max_width = 0; | 81 *max_width = 0; |
| 77 *line_count = 0; | 82 *line_count = 0; |
| 78 | 83 |
| 79 // Clamp the tooltip length to kMaxTooltipLength so that we don't | 84 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
| 80 // accidentally DOS the user with a mega tooltip. | 85 // accidentally DOS the user with a mega tooltip. |
| 81 if (text->length() > kMaxTooltipLength) | 86 if (text->length() > kMaxTooltipLength) |
| 82 *text = text->substr(0, kMaxTooltipLength); | 87 *text = text->substr(0, kMaxTooltipLength); |
| 83 | 88 |
| 84 // Determine the available width for the tooltip. | 89 // Determine the available width for the tooltip. |
| 85 int available_width = GetMaxWidth(x, y); | 90 int available_width = std::min(kTooltipMaxWidthPixels, GetMaxWidth(x, y)); |
| 86 | 91 |
| 87 // Split the string into at most kMaxLines lines. | |
| 88 std::vector<string16> lines; | 92 std::vector<string16> lines; |
| 89 base::SplitString(*text, '\n', &lines); | 93 base::SplitString(*text, '\n', &lines); |
| 90 if (lines.size() > kMaxLines) | 94 std::vector<string16> result_lines; |
| 91 lines.resize(kMaxLines); | |
| 92 *line_count = static_cast<int>(lines.size()); | |
| 93 | 95 |
| 94 // Format each line to fit. | 96 // Format each line to fit. |
| 95 gfx::Font font = GetDefaultFont(); | 97 gfx::Font font = GetDefaultFont(); |
| 98 for (std::vector<string16>::iterator l = lines.begin(); l != lines.end(); | |
| 99 ++l) { | |
| 100 // We break the line at word boundaries, then stuff as many words as we can | |
| 101 // in the available width to the current line, and move the remaining words | |
| 102 // to a new line. | |
| 103 std::vector<string16> words; | |
| 104 base::SplitStringDontTrim(*l, ' ', &words); | |
|
Daniel Erat
2012/03/21 00:55:40
is it okay to rebuild the string like this, or are
varunjain
2012/03/21 04:48:38
whitespace is preserver when I use SplitStringDont
| |
| 105 int current_width = 0; | |
| 106 string16 line; | |
| 107 for (std::vector<string16>::iterator w = words.begin(); w != words.end(); | |
| 108 ++w) { | |
| 109 string16 word = *w; | |
| 110 if (w + 1 != words.end()) | |
| 111 word.push_back(' '); | |
| 112 int word_width = font.GetStringWidth(word); | |
| 113 if (current_width + word_width > available_width) { | |
| 114 // Current width will exceed the available width. Must start a new line. | |
| 115 result_lines.push_back(line); | |
| 116 current_width = 0; | |
| 117 line.clear(); | |
| 118 } | |
| 119 current_width += word_width; | |
| 120 line.append(word); | |
| 121 } | |
| 122 result_lines.push_back(line); | |
| 123 } | |
| 124 | |
| 125 // Clamp number of lines to |kMaxLines|. | |
| 126 if (result_lines.size() > kMaxLines) { | |
| 127 result_lines.resize(kMaxLines); | |
| 128 // Add ellipses character to last line. | |
| 129 result_lines[kMaxLines - 1] = ui::TruncateString( | |
| 130 result_lines.back(), result_lines.back().length() - 1); | |
| 131 } | |
| 132 *line_count = result_lines.size(); | |
| 133 | |
| 134 // Flatten the result. | |
| 96 string16 result; | 135 string16 result; |
| 97 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); | 136 for (std::vector<string16>::iterator l = result_lines.begin(); |
| 98 ++i) { | 137 l != result_lines.end(); ++l) { |
| 99 string16 elided_text = | |
| 100 ui::ElideText(*i, font, available_width, ui::ELIDE_AT_END); | |
| 101 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); | |
| 102 if (!result.empty()) | 138 if (!result.empty()) |
| 103 result.push_back('\n'); | 139 result.push_back('\n'); |
| 104 result.append(elided_text); | 140 int line_width = font.GetStringWidth(*l); |
| 141 // Since we only break at word boundaries, it could happen that due to some | |
| 142 // very long word, line_width is greater than the available_width. In such | |
| 143 // case, we simply truncate at available_width and add ellipses at the end. | |
| 144 if (line_width > available_width) { | |
| 145 *max_width = available_width; | |
| 146 result.append(ui::ElideText(*l, font, available_width, ui::ELIDE_AT_END)); | |
| 147 } else { | |
| 148 *max_width = std::max(*max_width, line_width); | |
| 149 result.append(*l); | |
| 150 } | |
| 105 } | 151 } |
| 106 *text = result; | 152 *text = result; |
| 107 } | 153 } |
| 108 | 154 |
| 109 // Creates a widget of type TYPE_TOOLTIP | 155 // Creates a widget of type TYPE_TOOLTIP |
| 110 views::Widget* CreateTooltip() { | 156 views::Widget* CreateTooltip() { |
| 111 views::Widget* widget = new views::Widget; | 157 views::Widget* widget = new views::Widget; |
| 112 views::Widget::InitParams params; | 158 views::Widget::InitParams params; |
| 113 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get | 159 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get |
| 114 // auto-parented to the MenuAndTooltipsContainer. | 160 // auto-parented to the MenuAndTooltipsContainer. |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 bool TooltipController::IsDragDropInProgress() { | 417 bool TooltipController::IsDragDropInProgress() { |
| 372 aura::client::DragDropClient* client = aura::client::GetDragDropClient( | 418 aura::client::DragDropClient* client = aura::client::GetDragDropClient( |
| 373 Shell::GetRootWindow()); | 419 Shell::GetRootWindow()); |
| 374 if (client) | 420 if (client) |
| 375 return client->IsDragDropInProgress(); | 421 return client->IsDragDropInProgress(); |
| 376 return false; | 422 return false; |
| 377 } | 423 } |
| 378 | 424 |
| 379 } // namespace internal | 425 } // namespace internal |
| 380 } // namespace ash | 426 } // namespace ash |
| OLD | NEW |