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