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.
5 #include "chrome/browser/ui/gtk/status_bubble_gtk.h"
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"
29 // Inner padding between the border and the text label.
30 const int kInternalTopBottomPadding
= 1;
31 const int kInternalLeftRightPadding
= 2;
33 // The radius of the edges of our bubble.
34 const int kCornerSize
= 3;
36 // Milliseconds before we hide the status bubble widget when you mouseout.
37 const int kHideDelayMS
= 250;
39 // How close the mouse can get to the infobubble before it starts sliding
41 const int kMousePadding
= 20;
45 StatusBubbleGtk::StatusBubbleGtk(Profile
* profile
)
46 : theme_service_(GtkThemeService::GetFrom(profile
)),
50 flip_horizontally_(false),
52 download_shelf_is_visible_(false),
53 last_mouse_left_content_(false),
54 ignore_next_left_content_(false) {
57 theme_service_
->InitThemesFor(this);
58 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
59 content::Source
<ThemeService
>(theme_service_
));
62 StatusBubbleGtk::~StatusBubbleGtk() {
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
)
72 status_text_
= status_text
;
73 if (!status_text_
.empty())
74 SetStatusTextTo(status_text_
);
75 else if (!url_text_
.empty())
76 SetStatusTextTo(url_text_
);
78 SetStatusTextTo(std::string());
81 void StatusBubbleGtk::SetURL(const GURL
& url
, const std::string
& languages
) {
83 languages_
= languages
;
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_
);
96 void StatusBubbleGtk::SetStatusTextToURL() {
97 GtkWidget
* parent
= gtk_widget_get_parent(container_
.get());
99 // It appears that parent can be NULL (probably only during shutdown).
100 if (!parent
|| !gtk_widget_get_realized(parent
))
103 GtkAllocation allocation
;
104 gtk_widget_get_allocation(parent
, &allocation
);
105 int desired_width
= allocation
.width
;
107 expand_timer_
.Stop();
110 base::TimeDelta::FromMilliseconds(kExpandHoverDelayMS
),
111 this, &StatusBubbleGtk::ExpandURL
);
112 // When not expanded, we limit the size to one third the browser's
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_
);
124 void StatusBubbleGtk::Show() {
125 // If we were going to hide, stop.
128 gtk_widget_show(container_
.get());
129 GdkWindow
* gdk_window
= gtk_widget_get_window(container_
.get());
131 gdk_window_raise(gdk_window
);
134 void StatusBubbleGtk::Hide() {
135 // If we were going to expand the bubble, stop.
136 expand_timer_
.Stop();
137 expand_animation_
.reset();
139 gtk_widget_hide(container_
.get());
142 void StatusBubbleGtk::SetStatusTextTo(const std::string
& status_utf8
) {
143 if (status_utf8
.empty()) {
145 hide_timer_
.Start(FROM_HERE
,
146 base::TimeDelta::FromMilliseconds(kHideDelayMS
),
147 this, &StatusBubbleGtk::Hide
);
149 gtk_label_set_text(GTK_LABEL(label_
.get()), status_utf8
.c_str());
151 gtk_widget_size_request(label_
.get(), &req
);
152 desired_width_
= req
.width
;
154 UpdateLabelSizeRequest();
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);
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;
175 last_mouse_location_
= location
;
176 last_mouse_left_content_
= left_content
;
178 if (!gtk_widget_get_realized(container_
.get()))
181 GtkWidget
* parent
= gtk_widget_get_parent(container_
.get());
182 if (!parent
|| !gtk_widget_get_realized(parent
))
185 int old_y_offset
= y_offset_
;
186 bool old_flip_horizontally
= flip_horizontally_
;
189 SetFlipHorizontally(false);
192 GtkWidget
* toplevel
= gtk_widget_get_toplevel(container_
.get());
193 if (!toplevel
|| !gtk_widget_get_realized(toplevel
))
196 bool ltr
= !base::i18n::IsRTL();
198 GtkRequisition requisition
;
199 gtk_widget_size_request(container_
.get(), &requisition
);
201 GtkAllocation parent_allocation
;
202 gtk_widget_get_allocation(parent
, &parent_allocation
);
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
,
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
;
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);
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
);
240 SetFlipHorizontally(false);
245 if (y_offset_
!= old_y_offset
|| flip_horizontally_
!= old_flip_horizontally
)
246 gtk_widget_queue_resize_no_redraw(parent
);
249 void StatusBubbleGtk::UpdateDownloadShelfVisibility(bool visible
) {
250 download_shelf_is_visible_
= visible
;
253 void StatusBubbleGtk::Observe(int type
,
254 const content::NotificationSource
& source
,
255 const content::NotificationDetails
& details
) {
256 if (type
== chrome::NOTIFICATION_BROWSER_THEME_CHANGED
) {
261 void StatusBubbleGtk::InitWidgets() {
262 bool ltr
= !base::i18n::IsRTL();
264 label_
.Own(gtk_label_new(NULL
));
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_
);
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_
);
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);
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
);
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
);
308 GdkColor toolbar_color
=
309 theme_service_
->GetGdkColor(ThemeProperties::COLOR_TOOLBAR
);
310 gtk_widget_modify_bg(container_
.get(), GTK_STATE_NORMAL
, &toolbar_color
);
313 gtk_util::SetRoundedWindowBorderColor(container_
.get(),
314 theme_service_
->GetBorderColor());
317 void StatusBubbleGtk::SetFlipHorizontally(bool flip_horizontally
) {
318 if (flip_horizontally
== flip_horizontally_
)
321 flip_horizontally_
= flip_horizontally
;
323 bool ltr
= !base::i18n::IsRTL();
324 bool on_left
= (ltr
&& !flip_horizontally
) || (!ltr
&& flip_horizontally
);
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(
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());
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();
350 SetStatusTextToURL();
353 void StatusBubbleGtk::UpdateLabelSizeRequest() {
354 if (!expanded() || !expand_animation_
->is_animating()) {
355 gtk_widget_set_size_request(label_
.get(), -1, -1);
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);
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);
372 gboolean
StatusBubbleGtk::HandleMotionNotify(GtkWidget
* sender
,
373 GdkEventMotion
* event
) {
374 MouseMoved(gfx::Point(event
->x_root
, event
->y_root
), false);
378 void StatusBubbleGtk::AnimationEnded(const gfx::Animation
* animation
) {
379 UpdateLabelSizeRequest();
382 void StatusBubbleGtk::AnimationProgressed(const gfx::Animation
* animation
) {
383 UpdateLabelSizeRequest();