OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/gtk/status_bubble_gtk.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "base/i18n/rtl.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | |
15 #include "chrome/browser/themes/theme_properties.h" | |
16 #include "chrome/browser/ui/elide_url.h" | |
17 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | |
18 #include "chrome/browser/ui/gtk/gtk_util.h" | |
19 #include "chrome/browser/ui/gtk/rounded_window.h" | |
20 #include "content/public/browser/notification_source.h" | |
21 #include "ui/base/gtk/gtk_hig_constants.h" | |
22 #include "ui/gfx/animation/slide_animation.h" | |
23 #include "ui/gfx/font_list.h" | |
24 #include "ui/gfx/gtk_compat.h" | |
25 #include "ui/gfx/text_elider.h" | |
26 | |
27 namespace { | |
28 | |
29 // Inner padding between the border and the text label. | |
30 const int kInternalTopBottomPadding = 1; | |
31 const int kInternalLeftRightPadding = 2; | |
32 | |
33 // The radius of the edges of our bubble. | |
34 const int kCornerSize = 3; | |
35 | |
36 // Milliseconds before we hide the status bubble widget when you mouseout. | |
37 const int kHideDelayMS = 250; | |
38 | |
39 // How close the mouse can get to the infobubble before it starts sliding | |
40 // off-screen. | |
41 const int kMousePadding = 20; | |
42 | |
43 } // namespace | |
44 | |
45 StatusBubbleGtk::StatusBubbleGtk(Profile* profile) | |
46 : theme_service_(GtkThemeService::GetFrom(profile)), | |
47 padding_(NULL), | |
48 start_width_(0), | |
49 desired_width_(0), | |
50 flip_horizontally_(false), | |
51 y_offset_(0), | |
52 download_shelf_is_visible_(false), | |
53 last_mouse_left_content_(false), | |
54 ignore_next_left_content_(false) { | |
55 InitWidgets(); | |
56 | |
57 theme_service_->InitThemesFor(this); | |
58 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
59 content::Source<ThemeService>(theme_service_)); | |
60 } | |
61 | |
62 StatusBubbleGtk::~StatusBubbleGtk() { | |
63 label_.Destroy(); | |
64 container_.Destroy(); | |
65 } | |
66 | |
67 void StatusBubbleGtk::SetStatus(const base::string16& status_text_wide) { | |
68 std::string status_text = base::UTF16ToUTF8(status_text_wide); | |
69 if (status_text_ == status_text) | |
70 return; | |
71 | |
72 status_text_ = status_text; | |
73 if (!status_text_.empty()) | |
74 SetStatusTextTo(status_text_); | |
75 else if (!url_text_.empty()) | |
76 SetStatusTextTo(url_text_); | |
77 else | |
78 SetStatusTextTo(std::string()); | |
79 } | |
80 | |
81 void StatusBubbleGtk::SetURL(const GURL& url, const std::string& languages) { | |
82 url_ = url; | |
83 languages_ = languages; | |
84 | |
85 // If we want to clear a displayed URL but there is a status still to | |
86 // display, display that status instead. | |
87 if (url.is_empty() && !status_text_.empty()) { | |
88 url_text_ = std::string(); | |
89 SetStatusTextTo(status_text_); | |
90 return; | |
91 } | |
92 | |
93 SetStatusTextToURL(); | |
94 } | |
95 | |
96 void StatusBubbleGtk::SetStatusTextToURL() { | |
97 GtkWidget* parent = gtk_widget_get_parent(container_.get()); | |
98 | |
99 // It appears that parent can be NULL (probably only during shutdown). | |
100 if (!parent || !gtk_widget_get_realized(parent)) | |
101 return; | |
102 | |
103 GtkAllocation allocation; | |
104 gtk_widget_get_allocation(parent, &allocation); | |
105 int desired_width = allocation.width; | |
106 if (!expanded()) { | |
107 expand_timer_.Stop(); | |
108 expand_timer_.Start( | |
109 FROM_HERE, | |
110 base::TimeDelta::FromMilliseconds(kExpandHoverDelayMS), | |
111 this, &StatusBubbleGtk::ExpandURL); | |
112 // When not expanded, we limit the size to one third the browser's | |
113 // width. | |
114 desired_width /= 3; | |
115 } | |
116 | |
117 // TODO(tc): We don't actually use gfx::Font as the font in the status | |
118 // bubble. We should extend ElideUrl to take some sort of pango font. | |
119 url_text_ = base::UTF16ToUTF8( | |
120 ElideUrl(url_, gfx::FontList(), desired_width, languages_)); | |
121 SetStatusTextTo(url_text_); | |
122 } | |
123 | |
124 void StatusBubbleGtk::Show() { | |
125 // If we were going to hide, stop. | |
126 hide_timer_.Stop(); | |
127 | |
128 gtk_widget_show(container_.get()); | |
129 GdkWindow* gdk_window = gtk_widget_get_window(container_.get()); | |
130 if (gdk_window) | |
131 gdk_window_raise(gdk_window); | |
132 } | |
133 | |
134 void StatusBubbleGtk::Hide() { | |
135 // If we were going to expand the bubble, stop. | |
136 expand_timer_.Stop(); | |
137 expand_animation_.reset(); | |
138 | |
139 gtk_widget_hide(container_.get()); | |
140 } | |
141 | |
142 void StatusBubbleGtk::SetStatusTextTo(const std::string& status_utf8) { | |
143 if (status_utf8.empty()) { | |
144 hide_timer_.Stop(); | |
145 hide_timer_.Start(FROM_HERE, | |
146 base::TimeDelta::FromMilliseconds(kHideDelayMS), | |
147 this, &StatusBubbleGtk::Hide); | |
148 } else { | |
149 gtk_label_set_text(GTK_LABEL(label_.get()), status_utf8.c_str()); | |
150 GtkRequisition req; | |
151 gtk_widget_size_request(label_.get(), &req); | |
152 desired_width_ = req.width; | |
153 | |
154 UpdateLabelSizeRequest(); | |
155 | |
156 if (!last_mouse_left_content_) { | |
157 // Show the padding and label to update our requisition and then | |
158 // re-process the last mouse event -- if the label was empty before or the | |
159 // text changed, our size will have changed and we may need to move | |
160 // ourselves away from the pointer now. | |
161 gtk_widget_show_all(padding_); | |
162 MouseMoved(last_mouse_location_, false); | |
163 } | |
164 Show(); | |
165 } | |
166 } | |
167 | |
168 void StatusBubbleGtk::MouseMoved( | |
169 const gfx::Point& location, bool left_content) { | |
170 if (left_content && ignore_next_left_content_) { | |
171 ignore_next_left_content_ = false; | |
172 return; | |
173 } | |
174 | |
175 last_mouse_location_ = location; | |
176 last_mouse_left_content_ = left_content; | |
177 | |
178 if (!gtk_widget_get_realized(container_.get())) | |
179 return; | |
180 | |
181 GtkWidget* parent = gtk_widget_get_parent(container_.get()); | |
182 if (!parent || !gtk_widget_get_realized(parent)) | |
183 return; | |
184 | |
185 int old_y_offset = y_offset_; | |
186 bool old_flip_horizontally = flip_horizontally_; | |
187 | |
188 if (left_content) { | |
189 SetFlipHorizontally(false); | |
190 y_offset_ = 0; | |
191 } else { | |
192 GtkWidget* toplevel = gtk_widget_get_toplevel(container_.get()); | |
193 if (!toplevel || !gtk_widget_get_realized(toplevel)) | |
194 return; | |
195 | |
196 bool ltr = !base::i18n::IsRTL(); | |
197 | |
198 GtkRequisition requisition; | |
199 gtk_widget_size_request(container_.get(), &requisition); | |
200 | |
201 GtkAllocation parent_allocation; | |
202 gtk_widget_get_allocation(parent, &parent_allocation); | |
203 | |
204 // Get our base position (that is, not including the current offset) | |
205 // relative to the origin of the root window. | |
206 gint toplevel_x = 0, toplevel_y = 0; | |
207 GdkWindow* gdk_window = gtk_widget_get_window(toplevel); | |
208 gdk_window_get_position(gdk_window, &toplevel_x, &toplevel_y); | |
209 gfx::Rect parent_rect = | |
210 gtk_util::GetWidgetRectRelativeToToplevel(parent); | |
211 gfx::Rect bubble_rect( | |
212 toplevel_x + parent_rect.x() + | |
213 (ltr ? 0 : parent_allocation.width - requisition.width), | |
214 toplevel_y + parent_rect.y() + | |
215 parent_allocation.height - requisition.height, | |
216 requisition.width, | |
217 requisition.height); | |
218 | |
219 int left_threshold = | |
220 bubble_rect.x() - bubble_rect.height() - kMousePadding; | |
221 int right_threshold = | |
222 bubble_rect.right() + bubble_rect.height() + kMousePadding; | |
223 int top_threshold = bubble_rect.y() - kMousePadding; | |
224 | |
225 if (((ltr && location.x() < right_threshold) || | |
226 (!ltr && location.x() > left_threshold)) && | |
227 location.y() > top_threshold) { | |
228 if (download_shelf_is_visible_) { | |
229 SetFlipHorizontally(true); | |
230 y_offset_ = 0; | |
231 } else { | |
232 SetFlipHorizontally(false); | |
233 int distance = std::max(ltr ? | |
234 location.x() - right_threshold : | |
235 left_threshold - location.x(), | |
236 top_threshold - location.y()); | |
237 y_offset_ = std::min(-1 * distance, requisition.height); | |
238 } | |
239 } else { | |
240 SetFlipHorizontally(false); | |
241 y_offset_ = 0; | |
242 } | |
243 } | |
244 | |
245 if (y_offset_ != old_y_offset || flip_horizontally_ != old_flip_horizontally) | |
246 gtk_widget_queue_resize_no_redraw(parent); | |
247 } | |
248 | |
249 void StatusBubbleGtk::UpdateDownloadShelfVisibility(bool visible) { | |
250 download_shelf_is_visible_ = visible; | |
251 } | |
252 | |
253 void StatusBubbleGtk::Observe(int type, | |
254 const content::NotificationSource& source, | |
255 const content::NotificationDetails& details) { | |
256 if (type == chrome::NOTIFICATION_BROWSER_THEME_CHANGED) { | |
257 UserChangedTheme(); | |
258 } | |
259 } | |
260 | |
261 void StatusBubbleGtk::InitWidgets() { | |
262 bool ltr = !base::i18n::IsRTL(); | |
263 | |
264 label_.Own(gtk_label_new(NULL)); | |
265 | |
266 padding_ = gtk_alignment_new(0, 0, 1, 1); | |
267 gtk_alignment_set_padding(GTK_ALIGNMENT(padding_), | |
268 kInternalTopBottomPadding, kInternalTopBottomPadding, | |
269 kInternalLeftRightPadding + (ltr ? 0 : kCornerSize), | |
270 kInternalLeftRightPadding + (ltr ? kCornerSize : 0)); | |
271 gtk_container_add(GTK_CONTAINER(padding_), label_.get()); | |
272 gtk_widget_show_all(padding_); | |
273 | |
274 container_.Own(gtk_event_box_new()); | |
275 gtk_widget_set_no_show_all(container_.get(), TRUE); | |
276 gtk_util::ActAsRoundedWindow( | |
277 container_.get(), ui::kGdkWhite, kCornerSize, | |
278 gtk_util::ROUNDED_TOP_RIGHT, | |
279 gtk_util::BORDER_TOP | gtk_util::BORDER_RIGHT); | |
280 gtk_widget_set_name(container_.get(), "status-bubble"); | |
281 gtk_container_add(GTK_CONTAINER(container_.get()), padding_); | |
282 | |
283 // We need to listen for mouse motion events, since a fast-moving pointer may | |
284 // enter our window without us getting any motion events on the browser near | |
285 // enough for us to run away. | |
286 gtk_widget_add_events(container_.get(), GDK_POINTER_MOTION_MASK | | |
287 GDK_ENTER_NOTIFY_MASK); | |
288 g_signal_connect(container_.get(), "motion-notify-event", | |
289 G_CALLBACK(HandleMotionNotifyThunk), this); | |
290 g_signal_connect(container_.get(), "enter-notify-event", | |
291 G_CALLBACK(HandleEnterNotifyThunk), this); | |
292 | |
293 UserChangedTheme(); | |
294 } | |
295 | |
296 void StatusBubbleGtk::UserChangedTheme() { | |
297 if (theme_service_->UsingNativeTheme()) { | |
298 gtk_widget_modify_fg(label_.get(), GTK_STATE_NORMAL, NULL); | |
299 gtk_widget_modify_bg(container_.get(), GTK_STATE_NORMAL, NULL); | |
300 } else { | |
301 // TODO(erg): This is the closest to "text that will look good on a | |
302 // toolbar" that I can find. Maybe in later iterations of the theme system, | |
303 // there will be a better color to pick. | |
304 GdkColor bookmark_text = | |
305 theme_service_->GetGdkColor(ThemeProperties::COLOR_BOOKMARK_TEXT); | |
306 gtk_widget_modify_fg(label_.get(), GTK_STATE_NORMAL, &bookmark_text); | |
307 | |
308 GdkColor toolbar_color = | |
309 theme_service_->GetGdkColor(ThemeProperties::COLOR_TOOLBAR); | |
310 gtk_widget_modify_bg(container_.get(), GTK_STATE_NORMAL, &toolbar_color); | |
311 } | |
312 | |
313 gtk_util::SetRoundedWindowBorderColor(container_.get(), | |
314 theme_service_->GetBorderColor()); | |
315 } | |
316 | |
317 void StatusBubbleGtk::SetFlipHorizontally(bool flip_horizontally) { | |
318 if (flip_horizontally == flip_horizontally_) | |
319 return; | |
320 | |
321 flip_horizontally_ = flip_horizontally; | |
322 | |
323 bool ltr = !base::i18n::IsRTL(); | |
324 bool on_left = (ltr && !flip_horizontally) || (!ltr && flip_horizontally); | |
325 | |
326 gtk_alignment_set_padding(GTK_ALIGNMENT(padding_), | |
327 kInternalTopBottomPadding, kInternalTopBottomPadding, | |
328 kInternalLeftRightPadding + (on_left ? 0 : kCornerSize), | |
329 kInternalLeftRightPadding + (on_left ? kCornerSize : 0)); | |
330 // The rounded window code flips these arguments if we're RTL. | |
331 gtk_util::SetRoundedWindowEdgesAndBorders( | |
332 container_.get(), | |
333 kCornerSize, | |
334 flip_horizontally ? | |
335 gtk_util::ROUNDED_TOP_LEFT : | |
336 gtk_util::ROUNDED_TOP_RIGHT, | |
337 gtk_util::BORDER_TOP | | |
338 (flip_horizontally ? gtk_util::BORDER_LEFT : gtk_util::BORDER_RIGHT)); | |
339 gtk_widget_queue_draw(container_.get()); | |
340 } | |
341 | |
342 void StatusBubbleGtk::ExpandURL() { | |
343 GtkAllocation allocation; | |
344 gtk_widget_get_allocation(label_.get(), &allocation); | |
345 start_width_ = allocation.width; | |
346 expand_animation_.reset(new gfx::SlideAnimation(this)); | |
347 expand_animation_->SetTweenType(gfx::Tween::LINEAR); | |
348 expand_animation_->Show(); | |
349 | |
350 SetStatusTextToURL(); | |
351 } | |
352 | |
353 void StatusBubbleGtk::UpdateLabelSizeRequest() { | |
354 if (!expanded() || !expand_animation_->is_animating()) { | |
355 gtk_widget_set_size_request(label_.get(), -1, -1); | |
356 return; | |
357 } | |
358 | |
359 int new_width = start_width_ + | |
360 (desired_width_ - start_width_) * expand_animation_->GetCurrentValue(); | |
361 gtk_widget_set_size_request(label_.get(), new_width, -1); | |
362 } | |
363 | |
364 // See http://crbug.com/68897 for why we have to handle this event. | |
365 gboolean StatusBubbleGtk::HandleEnterNotify(GtkWidget* sender, | |
366 GdkEventCrossing* event) { | |
367 ignore_next_left_content_ = true; | |
368 MouseMoved(gfx::Point(event->x_root, event->y_root), false); | |
369 return FALSE; | |
370 } | |
371 | |
372 gboolean StatusBubbleGtk::HandleMotionNotify(GtkWidget* sender, | |
373 GdkEventMotion* event) { | |
374 MouseMoved(gfx::Point(event->x_root, event->y_root), false); | |
375 return FALSE; | |
376 } | |
377 | |
378 void StatusBubbleGtk::AnimationEnded(const gfx::Animation* animation) { | |
379 UpdateLabelSizeRequest(); | |
380 } | |
381 | |
382 void StatusBubbleGtk::AnimationProgressed(const gfx::Animation* animation) { | |
383 UpdateLabelSizeRequest(); | |
384 } | |
OLD | NEW |