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/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
8 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
9 #include "ui/aura/window_tree_host.h" | 9 #include "ui/aura/window_tree_host.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/gfx/screen.h" | 11 #include "ui/gfx/screen.h" |
12 #include "ui/gfx/text_elider.h" | 12 #include "ui/gfx/text_elider.h" |
13 #include "ui/gfx/text_utils.h" | 13 #include "ui/gfx/text_utils.h" |
14 #include "ui/native_theme/native_theme.h" | 14 #include "ui/native_theme/native_theme.h" |
15 #include "ui/views/background.h" | 15 #include "ui/views/background.h" |
16 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const int kTooltipHorizontalPadding = 3; | |
22 | |
23 // Max visual tooltip width. If a tooltip is greater than this width, it will | |
24 // be wrapped. | |
25 const int kTooltipMaxWidthPixels = 400; | |
26 | |
27 const size_t kMaxLines = 10; | |
28 | |
29 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive | |
30 // when running the same binary on a Linux workstation; presumably there's a | |
31 // difference in font metrics. Rationalize this. | |
32 const int kTooltipVerticalPadding = 2; | |
33 | |
34 // FIXME: get cursor offset from actual cursor size. | 21 // FIXME: get cursor offset from actual cursor size. |
35 const int kCursorOffsetX = 10; | 22 const int kCursorOffsetX = 10; |
36 const int kCursorOffsetY = 15; | 23 const int kCursorOffsetY = 15; |
37 | 24 |
38 // Creates a widget of type TYPE_TOOLTIP | 25 // Creates a widget of type TYPE_TOOLTIP |
39 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { | 26 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { |
40 views::Widget* widget = new views::Widget; | 27 views::Widget* widget = new views::Widget; |
41 views::Widget::InitParams params; | 28 views::Widget::InitParams params; |
42 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get | 29 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get |
43 // auto-parented to the right container. | 30 // auto-parented to the right container. |
(...skipping 16 matching lines...) Expand all Loading... |
60 widget_(NULL), | 47 widget_(NULL), |
61 tooltip_window_(NULL) { | 48 tooltip_window_(NULL) { |
62 label_.set_owned_by_client(); | 49 label_.set_owned_by_client(); |
63 label_.SetMultiLine(true); | 50 label_.SetMultiLine(true); |
64 } | 51 } |
65 | 52 |
66 TooltipAura::~TooltipAura() { | 53 TooltipAura::~TooltipAura() { |
67 DestroyWidget(); | 54 DestroyWidget(); |
68 } | 55 } |
69 | 56 |
70 // static | |
71 void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list, | |
72 int max_width, | |
73 base::string16* text, | |
74 int* width, | |
75 int* line_count) { | |
76 *width = 0; | |
77 *line_count = 0; | |
78 | |
79 // Determine the available width for the tooltip. | |
80 int available_width = std::min(kTooltipMaxWidthPixels, max_width); | |
81 | |
82 std::vector<base::string16> lines; | |
83 base::SplitString(*text, '\n', &lines); | |
84 std::vector<base::string16> result_lines; | |
85 | |
86 // Format each line to fit. | |
87 for (std::vector<base::string16>::iterator l = lines.begin(); | |
88 l != lines.end(); ++l) { | |
89 // We break the line at word boundaries, then stuff as many words as we can | |
90 // in the available width to the current line, and move the remaining words | |
91 // to a new line. | |
92 std::vector<base::string16> words; | |
93 base::SplitStringDontTrim(*l, ' ', &words); | |
94 int current_width = 0; | |
95 base::string16 line; | |
96 for (std::vector<base::string16>::iterator w = words.begin(); | |
97 w != words.end(); ++w) { | |
98 base::string16 word = *w; | |
99 if (w + 1 != words.end()) | |
100 word.push_back(' '); | |
101 int word_width = gfx::GetStringWidth(word, font_list); | |
102 if (current_width + word_width > available_width) { | |
103 // Current width will exceed the available width. Must start a new line. | |
104 if (!line.empty()) | |
105 result_lines.push_back(line); | |
106 current_width = 0; | |
107 line.clear(); | |
108 } | |
109 current_width += word_width; | |
110 line.append(word); | |
111 } | |
112 result_lines.push_back(line); | |
113 } | |
114 | |
115 // Clamp number of lines to |kMaxLines|. | |
116 if (result_lines.size() > kMaxLines) { | |
117 result_lines.resize(kMaxLines); | |
118 // Add ellipses character to last line. | |
119 result_lines[kMaxLines - 1] = gfx::TruncateString( | |
120 result_lines.back(), result_lines.back().length() - 1, gfx::WORD_BREAK); | |
121 } | |
122 *line_count = result_lines.size(); | |
123 | |
124 // Flatten the result. | |
125 base::string16 result; | |
126 for (std::vector<base::string16>::iterator l = result_lines.begin(); | |
127 l != result_lines.end(); ++l) { | |
128 if (!result.empty()) | |
129 result.push_back('\n'); | |
130 int line_width = gfx::GetStringWidth(*l, font_list); | |
131 // Since we only break at word boundaries, it could happen that due to some | |
132 // very long word, line_width is greater than the available_width. In such | |
133 // case, we simply truncate at available_width and add ellipses at the end. | |
134 if (line_width > available_width) { | |
135 *width = available_width; | |
136 result.append(gfx::ElideText(*l, font_list, available_width, | |
137 gfx::ELIDE_TAIL)); | |
138 } else { | |
139 *width = std::max(*width, line_width); | |
140 result.append(*l); | |
141 } | |
142 } | |
143 *text = result; | |
144 } | |
145 | |
146 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { | 57 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { |
147 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure | |
148 // out a way to merge. | |
149 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 58 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
150 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); | 59 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); |
151 return (display_bounds.width() + 1) / 2; | 60 return display_bounds.width() * 3 / 4; |
152 } | 61 } |
153 | 62 |
154 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, | 63 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, |
155 int tooltip_width, | 64 const gfx::Size& tooltip_size) { |
156 int tooltip_height) { | 65 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); |
157 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, | |
158 tooltip_height); | |
159 | 66 |
160 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 67 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
161 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 68 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
162 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); | 69 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); |
163 | 70 |
164 // If tooltip is out of bounds on the x axis, we simply shift it | 71 // If tooltip is out of bounds on the x axis, we simply shift it |
165 // horizontally by the offset. | 72 // horizontally by the offset. |
166 if (tooltip_rect.right() > display_bounds.right()) { | 73 if (tooltip_rect.right() > display_bounds.right()) { |
167 int h_offset = tooltip_rect.right() - display_bounds.right(); | 74 int h_offset = tooltip_rect.right() - display_bounds.right(); |
168 tooltip_rect.Offset(-h_offset, 0); | 75 tooltip_rect.Offset(-h_offset, 0); |
169 } | 76 } |
170 | 77 |
171 // If tooltip is out of bounds on the y axis, we flip it to appear above the | 78 // If tooltip is out of bounds on the y axis, we flip it to appear above the |
172 // mouse cursor instead of below. | 79 // mouse cursor instead of below. |
173 if (tooltip_rect.bottom() > display_bounds.bottom()) | 80 if (tooltip_rect.bottom() > display_bounds.bottom()) |
174 tooltip_rect.set_y(mouse_pos.y() - tooltip_height); | 81 tooltip_rect.set_y(mouse_pos.y() - tooltip_rect.height()); |
175 | 82 |
176 tooltip_rect.AdjustToFit(display_bounds); | 83 tooltip_rect.AdjustToFit(display_bounds); |
177 widget_->SetBounds(tooltip_rect); | 84 widget_->SetBounds(tooltip_rect); |
178 } | 85 } |
179 | 86 |
180 void TooltipAura::DestroyWidget() { | 87 void TooltipAura::DestroyWidget() { |
181 if (widget_) { | 88 if (widget_) { |
182 widget_->RemoveObserver(this); | 89 widget_->RemoveObserver(this); |
183 widget_->Close(); | 90 widget_->Close(); |
184 widget_ = NULL; | 91 widget_ = NULL; |
185 } | 92 } |
186 } | 93 } |
187 | 94 |
188 void TooltipAura::SetText(aura::Window* window, | 95 void TooltipAura::SetText(aura::Window* window, |
189 const base::string16& tooltip_text, | 96 const base::string16& tooltip_text, |
190 const gfx::Point& location) { | 97 const gfx::Point& location) { |
191 tooltip_window_ = window; | 98 tooltip_window_ = window; |
192 int max_width, line_count; | |
193 base::string16 trimmed_text(tooltip_text); | |
194 TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, | |
195 &max_width, &line_count); | |
196 label_.SetText(trimmed_text); | |
197 | 99 |
198 int width = max_width + 2 * kTooltipHorizontalPadding; | 100 label_.SetText(tooltip_text); |
199 int height = label_.GetHeightForWidth(max_width) + | 101 label_.SizeToFit(GetMaxWidth(location)); |
200 2 * kTooltipVerticalPadding; | |
201 | 102 |
202 if (!widget_) { | 103 if (!widget_) { |
203 widget_ = CreateTooltipWidget(tooltip_window_); | 104 widget_ = CreateTooltipWidget(tooltip_window_); |
204 widget_->SetContentsView(&label_); | 105 widget_->SetContentsView(&label_); |
205 widget_->AddObserver(this); | 106 widget_->AddObserver(this); |
206 } | 107 } |
207 | 108 |
208 SetTooltipBounds(location, width, height); | 109 SetTooltipBounds(location, label_.GetPreferredSize()); |
209 | 110 |
210 ui::NativeTheme* native_theme = widget_->GetNativeTheme(); | 111 ui::NativeTheme* native_theme = widget_->GetNativeTheme(); |
211 label_.set_background( | 112 label_.set_background( |
212 views::Background::CreateSolidBackground( | 113 views::Background::CreateSolidBackground( |
213 native_theme->GetSystemColor( | 114 native_theme->GetSystemColor( |
214 ui::NativeTheme::kColorId_TooltipBackground))); | 115 ui::NativeTheme::kColorId_TooltipBackground))); |
215 | 116 |
216 label_.SetAutoColorReadabilityEnabled(false); | 117 label_.SetAutoColorReadabilityEnabled(false); |
217 label_.SetEnabledColor(native_theme->GetSystemColor( | 118 label_.SetEnabledColor(native_theme->GetSystemColor( |
218 ui::NativeTheme::kColorId_TooltipText)); | 119 ui::NativeTheme::kColorId_TooltipText)); |
(...skipping 17 matching lines...) Expand all Loading... |
236 } | 137 } |
237 | 138 |
238 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 139 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
239 DCHECK_EQ(widget_, widget); | 140 DCHECK_EQ(widget_, widget); |
240 widget_ = NULL; | 141 widget_ = NULL; |
241 tooltip_window_ = NULL; | 142 tooltip_window_ = NULL; |
242 } | 143 } |
243 | 144 |
244 } // namespace corewm | 145 } // namespace corewm |
245 } // namespace views | 146 } // namespace views |
OLD | NEW |