diff options
author | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-02 19:23:03 +0000 |
---|---|---|
committer | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-02 19:23:03 +0000 |
commit | 955be75d7b55716fd4a1eba787b9311be20b8ab8 (patch) | |
tree | be11a0e3e744ca1e6522126754a6d71dd046cd57 /chrome | |
parent | 13805a61dfe72b4ed668940ba3a5a18f12f0529c (diff) | |
download | chromium_src-955be75d7b55716fd4a1eba787b9311be20b8ab8.zip chromium_src-955be75d7b55716fd4a1eba787b9311be20b8ab8.tar.gz chromium_src-955be75d7b55716fd4a1eba787b9311be20b8ab8.tar.bz2 |
Change status bubble so that it expands to accommodate URL's that are abridged in the standard width.
BUG= http://crbug.com/1455
TEST= Mouse over a link which is abridged in the status bubble. Hover for 2 seconds. Link should expand to show as much as possible without extending out of the view in which it is contained.
Review URL: http://codereview.chromium.org/146043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19829 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/cocoa/status_bubble_mac.h | 1 | ||||
-rw-r--r-- | chrome/browser/cocoa/status_bubble_mac.mm | 4 | ||||
-rw-r--r-- | chrome/browser/gtk/status_bubble_gtk.h | 4 | ||||
-rw-r--r-- | chrome/browser/status_bubble.h | 3 | ||||
-rw-r--r-- | chrome/browser/views/status_bubble_views.cc | 182 | ||||
-rw-r--r-- | chrome/browser/views/status_bubble_views.h | 38 |
6 files changed, 223 insertions, 9 deletions
diff --git a/chrome/browser/cocoa/status_bubble_mac.h b/chrome/browser/cocoa/status_bubble_mac.h index b6843c0..35b5dc5 100644 --- a/chrome/browser/cocoa/status_bubble_mac.h +++ b/chrome/browser/cocoa/status_bubble_mac.h @@ -25,6 +25,7 @@ class StatusBubbleMac : public StatusBubble { virtual void Hide(); virtual void MouseMoved(); virtual void UpdateDownloadShelfVisibility(bool visible); + virtual void SetBubbleWidth(int width); private: friend class StatusBubbleMacTest; diff --git a/chrome/browser/cocoa/status_bubble_mac.mm b/chrome/browser/cocoa/status_bubble_mac.mm index 160b086..e2d6c78 100644 --- a/chrome/browser/cocoa/status_bubble_mac.mm +++ b/chrome/browser/cocoa/status_bubble_mac.mm @@ -192,6 +192,10 @@ void StatusBubbleMac::UpdateDownloadShelfVisibility(bool visible) { NOTIMPLEMENTED(); } +void StatusBubbleMac::SetBubbleWidth(int width) { + NOTIMPLEMENTED(); +} + void StatusBubbleMac::Create() { if (window_) return; diff --git a/chrome/browser/gtk/status_bubble_gtk.h b/chrome/browser/gtk/status_bubble_gtk.h index 006efd0..1a5ca85 100644 --- a/chrome/browser/gtk/status_bubble_gtk.h +++ b/chrome/browser/gtk/status_bubble_gtk.h @@ -36,6 +36,10 @@ class StatusBubbleGtk : public StatusBubble { // the download shelf, when it is visible. virtual void UpdateDownloadShelfVisibility(bool visible) { } + virtual void SetBubbleWidth(int width) { } + + void SetStatus(const std::string& status_utf8); + // Top of the widget hierarchy for a StatusBubble. This top level widget is // guarenteed to have its gtk_widget_name set to "status-bubble" for // identification. diff --git a/chrome/browser/status_bubble.h b/chrome/browser/status_bubble.h index f5ecda4..446e9c3 100644 --- a/chrome/browser/status_bubble.h +++ b/chrome/browser/status_bubble.h @@ -44,6 +44,9 @@ class StatusBubble { // This is used by to ensure that the status bubble does not obscure // the download shelf, when it is visible. virtual void UpdateDownloadShelfVisibility(bool visible) = 0; + + // Allow StatusView animation to set width of StatusBubble. + virtual void SetBubbleWidth(int width) = 0; }; #endif // #ifndef CHROME_BROWSER_STATUS_BUBBLE_H_ diff --git a/chrome/browser/views/status_bubble_views.cc b/chrome/browser/views/status_bubble_views.cc index ecaba94..5afa4fb 100644 --- a/chrome/browser/views/status_bubble_views.cc +++ b/chrome/browser/views/status_bubble_views.cc @@ -28,6 +28,7 @@ #include "views/widget/root_view.h" #include "views/widget/widget.h" #if defined(OS_WIN) +#include "views/controls/scrollbar/native_scroll_bar.h" #include "views/widget/widget_win.h" #endif @@ -53,7 +54,7 @@ static const int kTextPositionX = 3; // The minimum horizontal space between the (right) end of the text and the edge // of the status bubble, not including the outer shadow ring, or a 1 px gap we -// leave so we can shit all the text by 1 px to produce a "highlight" effect. +// leave so we can shift all the text by 1 px to produce a "highlight" effect. static const int kTextHorizPadding = 1; // Delays before we start hiding or showing the bubble after we receive a @@ -66,6 +67,9 @@ static const int kShowFadeDurationMS = 120; static const int kHideFadeDurationMS = 200; static const int kFramerate = 25; +// How long each expansion step should take. +static const int kExpansionStepDurationMS = 150; + // View ----------------------------------------------------------------------- // StatusView manages the display of the bubble, applying text changes and // fading in or out the bubble as required. @@ -113,6 +117,11 @@ class StatusBubbleViews::StatusView : public views::Label, // Set the bubble text to a certain value, hides the bubble if text is // an empty string. + void SetTextAndAnimate(const std::wstring& text); + + // Set the bubble text to a certain value without triggering animation + // sequence. Called by the StatusViewExpander after bubble has been + // fully expanded. void SetText(const std::wstring& text); BubbleStage GetState() const { return stage_; } @@ -178,6 +187,12 @@ class StatusBubbleViews::StatusView : public views::Label, }; void StatusBubbleViews::StatusView::SetText(const std::wstring& text) { + text_ = text; + SchedulePaint(); +} + +void StatusBubbleViews::StatusView::SetTextAndAnimate( + const std::wstring& text) { if (text.empty()) { // The string was empty. StartHiding(); @@ -317,7 +332,6 @@ void StatusBubbleViews::StatusView::AnimateToState(double state) { void StatusBubbleViews::StatusView::AnimationEnded( const Animation* animation) { SetOpacity(opacity_end_); - if (stage_ == BUBBLE_HIDING_FADE) { stage_ = BUBBLE_HIDDEN; popup_->Hide(); @@ -453,6 +467,83 @@ void StatusBubbleViews::StatusView::Paint(gfx::Canvas* canvas) { body_bounds.height()); } + +// StatusViewExpander --------------------------------------------------------- +// StatusViewExpander manages the expansion and contraction of the status +// bubble as it accommodates URL's too long to fit in the standard bubble. +// Changes are passed through to the StatusView to paint. +class StatusBubbleViews::StatusViewExpander : public Animation, + public AnimationDelegate { + public: + StatusViewExpander(StatusBubble* status_bubble, StatusView* status_view) + : Animation(kFramerate, this), + status_bubble_(status_bubble), + status_view_(status_view), + expansion_start_(0), + expansion_end_(0) { + } + + // Manage the expansion of the bubble. + void StartExpansion(std::wstring expanded_text, int current_width, + int expansion_end); + + // Set width of fully expanded bubble. + void SetExpandedWidth(int expanded_width); + + private: + // Animation functions. + int GetCurrentBubbleWidth(); + void SetBubbleWidth(int width); + void AnimateToState(double state); + void AnimationEnded(const Animation* animation); + + // We are changing the bounds and text of this view. + StatusView* status_view_; + + // Manager that owns us. + StatusBubble* status_bubble_; + + // The currently displayed text. + std::wstring text_; + + // Text elided to fit maximum possible status bar width. + std::wstring expanded_text_; + + // Widths at expansion start and end. + int expansion_start_; + int expansion_end_; +}; + +void StatusBubbleViews::StatusViewExpander::AnimateToState(double state) { + SetBubbleWidth(GetCurrentBubbleWidth()); +} + +void StatusBubbleViews::StatusViewExpander::AnimationEnded( + const Animation* animation) { + SetBubbleWidth(expansion_end_); + status_view_->SetText(expanded_text_); +} + +void StatusBubbleViews::StatusViewExpander::StartExpansion( + std::wstring expanded_text, int expansion_start, + int expansion_end) { + expanded_text_ = expanded_text; + expansion_start_ = expansion_start; + expansion_end_ = expansion_end; + SetDuration(kExpansionStepDurationMS); + Start(); +} + +int StatusBubbleViews::StatusViewExpander::GetCurrentBubbleWidth() { + return static_cast<int>(expansion_start_ + + (expansion_end_ - expansion_start_) * Animation::GetCurrentValue()); +} + +void StatusBubbleViews::StatusViewExpander::SetBubbleWidth(int width) { + status_bubble_->SetBubbleWidth(width); + status_view_->SchedulePaint(); +} + // StatusBubble --------------------------------------------------------------- const int StatusBubbleViews::kShadowThickness = 1; @@ -463,10 +554,13 @@ StatusBubbleViews::StatusBubbleViews(views::Widget* frame) opacity_(0), frame_(frame), view_(NULL), - download_shelf_is_visible_(false) { + download_shelf_is_visible_(false), + is_expanded_(false), + expand_timer_factory_(this) { } StatusBubbleViews::~StatusBubbleViews() { + CancelExpandTimer(); if (popup_.get()) popup_->CloseNow(); } @@ -479,6 +573,8 @@ void StatusBubbleViews::Init() { if (!view_) view_ = new StatusView(this, popup, frame_->GetThemeProvider()); + if (!expand_view_) + expand_view_ = new StatusViewExpander(this, view_); popup->set_window_style(WS_POPUP); popup->set_window_ex_style(WS_EX_LAYERED | WS_EX_TOOLWINDOW | @@ -535,26 +631,34 @@ void StatusBubbleViews::SetStatus(const std::wstring& status_text) { Init(); status_text_ = status_text; if (!status_text_.empty()) { - view_->SetText(status_text); + view_->SetTextAndAnimate(status_text); view_->Show(); } else if (!url_text_.empty()) { - view_->SetText(url_text_); + view_->SetTextAndAnimate(url_text_); } else { - view_->SetText(std::wstring()); + view_->SetTextAndAnimate(std::wstring()); } } void StatusBubbleViews::SetURL(const GURL& url, const std::wstring& languages) { + languages_ = languages; + url_ = url; Init(); // If we want to clear a displayed URL but there is a status still to // display, display that status instead. if (url.is_empty() && !status_text_.empty()) { url_text_ = std::wstring(); - view_->SetText(status_text_); + view_->SetTextAndAnimate(status_text_); return; } + // Reset expansion state only when bubble is completely hidden. + if (view_->GetState() == StatusView::BUBBLE_HIDDEN) { + is_expanded_ = false; + SetBubbleWidth(GetStandardStatusBubbleWidth()); + } + // Set Elided Text corresponding to the GURL object. gfx::Rect popup_bounds; popup_->GetBounds(&popup_bounds, true); @@ -563,13 +667,30 @@ void StatusBubbleViews::SetURL(const GURL& url, const std::wstring& languages) { url_text_ = gfx::ElideUrl(url, view_->Label::GetFont(), text_width, languages); + url_parse::Parsed parsed; + std::wstring original_url_text_ = + net::FormatUrl(url, languages, true, true, &parsed, NULL); + // An URL is always treated as a left-to-right string. On right-to-left UIs // we need to explicitly mark the URL as LTR to make sure it is displayed // correctly. if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT && !url_text_.empty()) l10n_util::WrapStringWithLTRFormatting(&url_text_); - view_->SetText(url_text_); + + view_->SetTextAndAnimate(url_text_); + + CancelExpandTimer(); + + // If bubble is already in expanded state, shift immediately to adjust to + // new text size (shrinking or expanding). Otherwise delay for + // kExpandHoverDelay ms. + if (is_expanded_ && !url.is_empty()) + ExpandBubble(); + else if (original_url_text_.length() > url_text_.length()) + MessageLoop::current()->PostDelayedTask(FROM_HERE, + expand_timer_factory_.NewRunnableMethod( + &StatusBubbleViews::ExpandBubble), kExpandHoverDelay); } void StatusBubbleViews::Hide() { @@ -675,7 +796,7 @@ void StatusBubbleViews::AvoidMouse() { view_->SetStyle(StatusView::STYLE_STANDARD_RIGHT); offset_ = 0; - // Substract border width + bubble width. + // Subtract border width + bubble width. int right_position_x = window_width - (position_.x() + size_.width()); popup_->SetBounds(gfx::Rect(top_left.x() + right_position_x, top_left.y() + position_.y(), @@ -695,3 +816,46 @@ void StatusBubbleViews::AvoidMouse() { size_.width(), size_.height())); } } + +void StatusBubbleViews::ExpandBubble() { + // Elide url to maximum possible size, then check actual length (it may + // still be too long to fit) before expanding bubble. + gfx::Rect popup_bounds; + popup_->GetBounds(&popup_bounds, true); + int max_status_bubble_width = GetMaxStatusBubbleWidth(); + url_text_ = gfx::ElideUrl(url_, view_->Label::GetFont(), + max_status_bubble_width, languages_); + int expanded_bubble_width = + std::max(GetStandardStatusBubbleWidth(), + std::min(view_->Label::GetFont().GetStringWidth(url_text_) + + (kShadowThickness * 2) + kTextPositionX + kTextHorizPadding + 1, + max_status_bubble_width)); + is_expanded_ = true; + expand_view_->StartExpansion(url_text_, popup_bounds.width(), + expanded_bubble_width); +} + +int StatusBubbleViews::GetStandardStatusBubbleWidth() { + gfx::Rect frame_bounds; + frame_->GetBounds(&frame_bounds, false); + return frame_bounds.width() / 3; +} + +int StatusBubbleViews::GetMaxStatusBubbleWidth() { + gfx::Rect frame_bounds; + frame_->GetBounds(&frame_bounds, false); + return static_cast<int>(frame_bounds.width() - (kShadowThickness * 2) - + kTextPositionX - kTextHorizPadding - 1 - + views::NativeScrollBar::GetVerticalScrollBarWidth()); +} + +void StatusBubbleViews::SetBubbleWidth(int width) { + size_.set_width(width); + SetBounds(position_.x(), position_.y(), size_.width(), size_.height()); +} + +void StatusBubbleViews::CancelExpandTimer() { + if (!expand_timer_factory_.empty()) + expand_timer_factory_.RevokeAll(); +} + diff --git a/chrome/browser/views/status_bubble_views.h b/chrome/browser/views/status_bubble_views.h index 5adb3e0..6c37615 100644 --- a/chrome/browser/views/status_bubble_views.h +++ b/chrome/browser/views/status_bubble_views.h @@ -8,7 +8,9 @@ #include "base/gfx/rect.h" #include "base/logging.h" #include "base/scoped_ptr.h" +#include "base/task.h" #include "chrome/browser/status_bubble.h" +#include "googleurl/src/gurl.h" class GURL; namespace views { @@ -26,6 +28,9 @@ class StatusBubbleViews : public StatusBubble { // The combined vertical padding above and below the text. static const int kTotalVerticalPadding = 7; + // On hover, expand status bubble to accommodate long URL after this delay. + static const int kExpandHoverDelay = 2000; + explicit StatusBubbleViews(views::Widget* frame); ~StatusBubbleViews(); @@ -40,6 +45,9 @@ class StatusBubbleViews : public StatusBubble { // Set the bounds of the bubble relative to the browser window. void SetBounds(int x, int y, int w, int h); + // Set bubble to new width; for animation of expansion. + void SetBubbleWidth(int width); + // Overridden from StatusBubble: virtual void SetStatus(const std::wstring& status); virtual void SetURL(const GURL& url, const std::wstring& languages); @@ -49,6 +57,7 @@ class StatusBubbleViews : public StatusBubble { private: class StatusView; + class StatusViewExpander; // Initializes the popup and view. void Init(); @@ -57,12 +66,34 @@ class StatusBubbleViews : public StatusBubble { // users to see links in the region normally occupied by the status bubble. void AvoidMouse(); + // Expand bubble size to accommodate an abridged URL. + void ExpandBubble(); + + // Cancel all the expansions waiting in the timer. + void CancelExpandTimer(); + + // Get the standard width for a status bubble in the current frame size. + int GetStandardStatusBubbleWidth(); + + // Get the maximum possible width for a status bubble in the current + // frame size. + int GetMaxStatusBubbleWidth(); + // The status text we want to display when there are no URLs to display. std::wstring status_text_; // The url we want to display when there is no status text to display. + // This string may be elided if the URL is too long to fit in status bubble. std::wstring url_text_; + // The original url. We need to keep this around to we can re-elide it to + // dynamically fit the bubble if we need to expand it to show a url that + // has been cut off. + GURL url_; + + // Keep this around so we can elide the original url when we expand it. + std::wstring languages_; + // Position relative to the parent window. gfx::Point position_; gfx::Size size_; @@ -77,10 +108,17 @@ class StatusBubbleViews : public StatusBubble { views::Widget* frame_; StatusView* view_; + StatusViewExpander* expand_view_; // If the download shelf is visible, do not obscure it. bool download_shelf_is_visible_; + // Is the bubble expanded? If so, change size immediately. + bool is_expanded_; + + // Times expansion of status bubble when URL is too long for standard width. + ScopedRunnableMethodFactory<StatusBubbleViews> expand_timer_factory_; + DISALLOW_COPY_AND_ASSIGN(StatusBubbleViews); }; |