From 03d9d9e6e9849eed24785862d825deab74bfdf08 Mon Sep 17 00:00:00 2001 From: tsrwebgl Date: Thu, 28 Mar 2019 14:22:56 +0000 Subject: [PATCH] Fix width of status bubbles Status bubbles for short URLs now match the width of the URL Tested by mousing over various links to make the status bubble appear. Bug: 936144 Change-Id: Ia6e4735a84e4d02f560b860b93e6dbc2066ff63e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1529187 Commit-Queue: Elly Fong-Jones Reviewed-by: Elly Fong-Jones Cr-Commit-Position: refs/heads/master@{#645300} --- AUTHORS | 1 + .../browser/ui/views/status_bubble_views.cc | 47 +++++++++++++------ chrome/browser/ui/views/status_bubble_views.h | 3 ++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/AUTHORS b/AUTHORS index f6cf6d8d9dfdba..ce32667d5ef5d8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -297,6 +297,7 @@ Georgy Buranov Gergely Nagy Getulio Sánchez Gideon Pyzer +Giovanni Panaro Girish Kumar M Gitanshu Mehndiratta Giuseppe Iuculano diff --git a/chrome/browser/ui/views/status_bubble_views.cc b/chrome/browser/ui/views/status_bubble_views.cc index 8134edc7e15d7d..25b1bbd7950a8b 100644 --- a/chrome/browser/ui/views/status_bubble_views.cc +++ b/chrome/browser/ui/views/status_bubble_views.cc @@ -721,6 +721,14 @@ void StatusBubbleViews::SetBounds(int x, int y, int w, int h) { AvoidMouse(last_mouse_moved_location_); } +int StatusBubbleViews::GetWidthForURL(const base::string16& url_string) { + // Get the width of the elided url + int elided_url_width = gfx::GetStringWidth(url_string, GetFont()); + // Add proper paddings + return elided_url_width + (kShadowThickness * 2) + kTextPositionX + + kTextHorizPadding + 1; +} + void StatusBubbleViews::SetStatus(const base::string16& status_text) { if (size_.IsEmpty()) return; // We have no bounds, don't attempt to show the popup. @@ -760,12 +768,6 @@ void StatusBubbleViews::SetURL(const GURL& url) { return; } - // Reset expansion state only when bubble is completely hidden. - if (view_->state() == StatusView::BUBBLE_HIDDEN) { - is_expanded_ = false; - SetBubbleWidth(GetStandardStatusBubbleWidth()); - } - // Set Elided Text corresponding to the GURL object. int text_width = static_cast(size_.width() - (kShadowThickness * 2) - kTextPositionX - kTextHorizPadding - 1); @@ -776,8 +778,28 @@ void StatusBubbleViews::SetURL(const GURL& url) { // correctly. url_text_ = base::i18n::GetDisplayStringInLTRDirectionality(url_text_); + // Get the width of the URL if the bubble width is the maximum size. + base::string16 full_size_elided_url = + url_formatter::ElideUrl(url, GetFont(), GetMaxStatusBubbleWidth()); + int url_width = GetWidthForURL(full_size_elided_url); + + // Get the width for the url if it is unexpanded. + int unexpanded_width = std::min(url_width, GetStandardStatusBubbleWidth()); + + // Reset expansion state only when bubble is completely hidden. + if (view_->state() == StatusView::BUBBLE_HIDDEN) { + is_expanded_ = false; + url_text_ = url_formatter::ElideUrl(url, GetFont(), unexpanded_width); + SetBubbleWidth(unexpanded_width); + } + if (IsFrameVisible()) { - view_->SetText(url_text_, true); + // If bubble is not expanded & not empty, make it fit properly in the + // unexpanded bubble + if (!is_expanded_ & !url.is_empty()) { + url_text_ = url_formatter::ElideUrl(url, GetFont(), unexpanded_width); + SetBubbleWidth(unexpanded_width); + } CancelExpandTimer(); @@ -793,6 +815,7 @@ void StatusBubbleViews::SetURL(const GURL& url) { expand_timer_factory_.GetWeakPtr()), base::TimeDelta::FromMilliseconds(kExpandHoverDelayMS)); } + view_->SetText(url_text_, true); } } @@ -937,15 +960,11 @@ bool StatusBubbleViews::IsFrameMaximized() { void StatusBubbleViews::ExpandBubble() { // Elide URL to maximum possible size, then check actual length (it may // still be too long to fit) before expanding bubble. - int max_status_bubble_width = GetMaxStatusBubbleWidth(); - const gfx::FontList font_list; - url_text_ = url_formatter::ElideUrl(url_, font_list, max_status_bubble_width); + url_text_ = + url_formatter::ElideUrl(url_, GetFont(), GetMaxStatusBubbleWidth()); int expanded_bubble_width = std::max(GetStandardStatusBubbleWidth(), - std::min(gfx::GetStringWidth(url_text_, font_list) + - (kShadowThickness * 2) + kTextPositionX + - kTextHorizPadding + 1, - max_status_bubble_width)); + std::min(GetWidthForURL(url_text_), GetMaxStatusBubbleWidth())); is_expanded_ = true; expand_view_->StartExpansion(url_text_, size_.width(), expanded_bubble_width); } diff --git a/chrome/browser/ui/views/status_bubble_views.h b/chrome/browser/ui/views/status_bubble_views.h index 60e3dc23b6026e..28bdc3ffc15cda 100644 --- a/chrome/browser/ui/views/status_bubble_views.h +++ b/chrome/browser/ui/views/status_bubble_views.h @@ -58,6 +58,9 @@ class StatusBubbleViews : public StatusBubble { // Set bubble to new width. void SetBubbleWidth(int width); + // Gets the width that a bubble should be for a given string + int GetWidthForURL(const base::string16& url_string); + // Overridden from StatusBubble: void SetStatus(const base::string16& status) override; void SetURL(const GURL& url) override;