BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / views / status_bubble_views.cc
blob52115f4c1a9d49d230909513e56a7a1e9cb323c0
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/views/status_bubble_views.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/i18n/rtl.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "chrome/browser/themes/theme_properties.h"
17 #include "components/url_formatter/elide_url.h"
18 #include "components/url_formatter/url_formatter.h"
19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "third_party/skia/include/core/SkRRect.h"
21 #include "ui/base/theme_provider.h"
22 #include "ui/gfx/animation/animation_delegate.h"
23 #include "ui/gfx/animation/linear_animation.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/font_list.h"
26 #include "ui/gfx/geometry/point.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/gfx/skia_util.h"
30 #include "ui/gfx/text_elider.h"
31 #include "ui/gfx/text_utils.h"
32 #include "ui/native_theme/native_theme.h"
33 #include "ui/views/controls/scrollbar/native_scroll_bar.h"
34 #include "ui/views/widget/root_view.h"
35 #include "ui/views/widget/widget.h"
36 #include "url/gurl.h"
38 #if defined(USE_ASH)
39 #include "ash/wm/window_state.h"
40 #endif
42 // The alpha and color of the bubble's shadow.
43 static const SkColor kShadowColor = SkColorSetARGB(30, 0, 0, 0);
45 // The roundedness of the edges of our bubble.
46 static const int kBubbleCornerRadius = 4;
48 // How close the mouse can get to the infobubble before it starts sliding
49 // off-screen.
50 static const int kMousePadding = 20;
52 // The horizontal offset of the text within the status bubble, not including the
53 // outer shadow ring.
54 static const int kTextPositionX = 3;
56 // The minimum horizontal space between the (right) end of the text and the edge
57 // of the status bubble, not including the outer shadow ring.
58 static const int kTextHorizPadding = 1;
60 // Delays before we start hiding or showing the bubble after we receive a
61 // show or hide request.
62 static const int kShowDelay = 80;
63 static const int kHideDelay = 250;
65 // How long each fade should last for.
66 static const int kShowFadeDurationMS = 120;
67 static const int kHideFadeDurationMS = 200;
68 static const int kFramerate = 25;
70 // How long each expansion step should take.
71 static const int kMinExpansionStepDurationMS = 20;
72 static const int kMaxExpansionStepDurationMS = 150;
75 // StatusBubbleViews::StatusViewAnimation --------------------------------------
76 class StatusBubbleViews::StatusViewAnimation : public gfx::LinearAnimation,
77 public gfx::AnimationDelegate {
78 public:
79 StatusViewAnimation(StatusView* status_view,
80 double opacity_start,
81 double opacity_end);
82 ~StatusViewAnimation() override;
84 double GetCurrentOpacity();
86 private:
87 // gfx::LinearAnimation:
88 void AnimateToState(double state) override;
90 // gfx::AnimationDelegate:
91 void AnimationEnded(const Animation* animation) override;
93 StatusView* status_view_;
95 // Start and end opacities for the current transition - note that as a
96 // fade-in can easily turn into a fade out, opacity_start_ is sometimes
97 // a value between 0 and 1.
98 double opacity_start_;
99 double opacity_end_;
101 DISALLOW_COPY_AND_ASSIGN(StatusViewAnimation);
105 // StatusBubbleViews::StatusView -----------------------------------------------
107 // StatusView manages the display of the bubble, applying text changes and
108 // fading in or out the bubble as required.
109 class StatusBubbleViews::StatusView : public views::View {
110 public:
111 // The bubble can be in one of many states:
112 enum BubbleState {
113 BUBBLE_HIDDEN, // Entirely BUBBLE_HIDDEN.
114 BUBBLE_HIDING_FADE, // In a fade-out transition.
115 BUBBLE_HIDING_TIMER, // Waiting before a fade-out.
116 BUBBLE_SHOWING_TIMER, // Waiting before a fade-in.
117 BUBBLE_SHOWING_FADE, // In a fade-in transition.
118 BUBBLE_SHOWN // Fully visible.
121 enum BubbleStyle {
122 STYLE_BOTTOM,
123 STYLE_FLOATING,
124 STYLE_STANDARD,
125 STYLE_STANDARD_RIGHT
128 StatusView(views::Widget* popup,
129 ui::ThemeProvider* theme_provider);
130 ~StatusView() override;
132 // Set the bubble text to a certain value, hides the bubble if text is
133 // an empty string. Trigger animation sequence to display if
134 // |should_animate_open|.
135 void SetText(const base::string16& text, bool should_animate_open);
137 BubbleState state() const { return state_; }
138 BubbleStyle style() const { return style_; }
139 void SetStyle(BubbleStyle style);
141 // Show the bubble instantly.
142 void Show();
144 // Hide the bubble instantly.
145 void Hide();
147 // Resets any timers we have. Typically called when the user moves a
148 // mouse.
149 void ResetTimer();
151 // This call backs the StatusView in order to fade the bubble in and out.
152 void SetOpacity(double opacity);
154 // Depending on the state of the bubble this will either hide the popup or
155 // not.
156 void OnAnimationEnded();
158 private:
159 class InitialTimer;
161 // Manage the timers that control the delay before a fade begins or ends.
162 void StartTimer(base::TimeDelta time);
163 void OnTimer();
164 void CancelTimer();
165 void RestartTimer(base::TimeDelta delay);
167 // Manage the fades and starting and stopping the animations correctly.
168 void StartFade(double start, double end, int duration);
169 void StartHiding();
170 void StartShowing();
172 // views::View:
173 const char* GetClassName() const override;
174 void OnPaint(gfx::Canvas* canvas) override;
176 BubbleState state_;
177 BubbleStyle style_;
179 scoped_ptr<StatusViewAnimation> animation_;
181 // Handle to the widget that contains us.
182 views::Widget* popup_;
184 // The currently-displayed text.
185 base::string16 text_;
187 // Holds the theme provider of the frame that created us.
188 ui::ThemeProvider* theme_service_;
190 base::WeakPtrFactory<StatusBubbleViews::StatusView> timer_factory_;
192 DISALLOW_COPY_AND_ASSIGN(StatusView);
195 StatusBubbleViews::StatusView::StatusView(views::Widget* popup,
196 ui::ThemeProvider* theme_provider)
197 : state_(BUBBLE_HIDDEN),
198 style_(STYLE_STANDARD),
199 animation_(new StatusViewAnimation(this, 0, 0)),
200 popup_(popup),
201 theme_service_(theme_provider),
202 timer_factory_(this) {
205 StatusBubbleViews::StatusView::~StatusView() {
206 animation_->Stop();
207 CancelTimer();
210 void StatusBubbleViews::StatusView::SetText(const base::string16& text,
211 bool should_animate_open) {
212 if (text.empty()) {
213 // The string was empty.
214 StartHiding();
215 } else {
216 // We want to show the string.
217 if (text != text_) {
218 text_ = text;
219 SchedulePaint();
221 if (should_animate_open)
222 StartShowing();
226 void StatusBubbleViews::StatusView::Show() {
227 animation_->Stop();
228 CancelTimer();
229 SetOpacity(1.0);
230 popup_->ShowInactive();
231 state_ = BUBBLE_SHOWN;
234 void StatusBubbleViews::StatusView::Hide() {
235 animation_->Stop();
236 CancelTimer();
237 SetOpacity(0.0);
238 text_.clear();
239 popup_->Hide();
240 state_ = BUBBLE_HIDDEN;
243 void StatusBubbleViews::StatusView::StartTimer(base::TimeDelta time) {
244 if (timer_factory_.HasWeakPtrs())
245 timer_factory_.InvalidateWeakPtrs();
247 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
248 FROM_HERE, base::Bind(&StatusBubbleViews::StatusView::OnTimer,
249 timer_factory_.GetWeakPtr()),
250 time);
253 void StatusBubbleViews::StatusView::OnTimer() {
254 if (state_ == BUBBLE_HIDING_TIMER) {
255 state_ = BUBBLE_HIDING_FADE;
256 StartFade(1.0, 0.0, kHideFadeDurationMS);
257 } else if (state_ == BUBBLE_SHOWING_TIMER) {
258 state_ = BUBBLE_SHOWING_FADE;
259 StartFade(0.0, 1.0, kShowFadeDurationMS);
263 void StatusBubbleViews::StatusView::CancelTimer() {
264 if (timer_factory_.HasWeakPtrs())
265 timer_factory_.InvalidateWeakPtrs();
268 void StatusBubbleViews::StatusView::RestartTimer(base::TimeDelta delay) {
269 CancelTimer();
270 StartTimer(delay);
273 void StatusBubbleViews::StatusView::ResetTimer() {
274 if (state_ == BUBBLE_SHOWING_TIMER) {
275 // We hadn't yet begun showing anything when we received a new request
276 // for something to show, so we start from scratch.
277 RestartTimer(base::TimeDelta::FromMilliseconds(kShowDelay));
281 void StatusBubbleViews::StatusView::StartFade(double start,
282 double end,
283 int duration) {
284 animation_.reset(new StatusViewAnimation(this, start, end));
286 // This will also reset the currently-occurring animation.
287 animation_->SetDuration(duration);
288 animation_->Start();
291 void StatusBubbleViews::StatusView::StartHiding() {
292 if (state_ == BUBBLE_SHOWN) {
293 state_ = BUBBLE_HIDING_TIMER;
294 StartTimer(base::TimeDelta::FromMilliseconds(kHideDelay));
295 } else if (state_ == BUBBLE_SHOWING_TIMER) {
296 state_ = BUBBLE_HIDDEN;
297 popup_->Hide();
298 CancelTimer();
299 } else if (state_ == BUBBLE_SHOWING_FADE) {
300 state_ = BUBBLE_HIDING_FADE;
301 // Figure out where we are in the current fade.
302 double current_opacity = animation_->GetCurrentOpacity();
304 // Start a fade in the opposite direction.
305 StartFade(current_opacity, 0.0,
306 static_cast<int>(kHideFadeDurationMS * current_opacity));
310 void StatusBubbleViews::StatusView::StartShowing() {
311 if (state_ == BUBBLE_HIDDEN) {
312 popup_->ShowInactive();
313 state_ = BUBBLE_SHOWING_TIMER;
314 StartTimer(base::TimeDelta::FromMilliseconds(kShowDelay));
315 } else if (state_ == BUBBLE_HIDING_TIMER) {
316 state_ = BUBBLE_SHOWN;
317 CancelTimer();
318 } else if (state_ == BUBBLE_HIDING_FADE) {
319 // We're partway through a fade.
320 state_ = BUBBLE_SHOWING_FADE;
322 // Figure out where we are in the current fade.
323 double current_opacity = animation_->GetCurrentOpacity();
325 // Start a fade in the opposite direction.
326 StartFade(current_opacity, 1.0,
327 static_cast<int>(kShowFadeDurationMS * current_opacity));
328 } else if (state_ == BUBBLE_SHOWING_TIMER) {
329 // We hadn't yet begun showing anything when we received a new request
330 // for something to show, so we start from scratch.
331 ResetTimer();
335 void StatusBubbleViews::StatusView::SetOpacity(double opacity) {
336 popup_->SetOpacity(static_cast<unsigned char>(opacity * 255));
339 void StatusBubbleViews::StatusView::SetStyle(BubbleStyle style) {
340 if (style_ != style) {
341 style_ = style;
342 SchedulePaint();
346 void StatusBubbleViews::StatusView::OnAnimationEnded() {
347 if (state_ == BUBBLE_HIDING_FADE) {
348 state_ = BUBBLE_HIDDEN;
349 popup_->Hide();
350 } else if (state_ == BUBBLE_SHOWING_FADE) {
351 state_ = BUBBLE_SHOWN;
355 const char* StatusBubbleViews::StatusView::GetClassName() const {
356 return "StatusBubbleViews::StatusView";
359 void StatusBubbleViews::StatusView::OnPaint(gfx::Canvas* canvas) {
360 SkPaint paint;
361 paint.setStyle(SkPaint::kFill_Style);
362 paint.setAntiAlias(true);
363 SkColor toolbar_color = theme_service_->GetColor(
364 ThemeProperties::COLOR_TOOLBAR);
365 paint.setColor(toolbar_color);
367 gfx::Rect popup_bounds = popup_->GetWindowBoundsInScreen();
369 SkScalar rad[8] = {};
371 // Top Edges - if the bubble is in its bottom position (sticking downwards),
372 // then we square the top edges. Otherwise, we square the edges based on the
373 // position of the bubble within the window (the bubble is positioned in the
374 // southeast corner in RTL and in the southwest corner in LTR).
375 if (style_ != STYLE_BOTTOM) {
376 if (base::i18n::IsRTL() != (style_ == STYLE_STANDARD_RIGHT)) {
377 // The text is RtL or the bubble is on the right side (but not both).
379 // Top Left corner.
380 rad[0] = kBubbleCornerRadius;
381 rad[1] = kBubbleCornerRadius;
382 } else {
383 // Top Right corner.
384 rad[2] = kBubbleCornerRadius;
385 rad[3] = kBubbleCornerRadius;
389 // Bottom edges - Keep these squared off if the bubble is in its standard
390 // position (sticking upward).
391 if (style_ != STYLE_STANDARD && style_ != STYLE_STANDARD_RIGHT) {
392 // Bottom Right Corner.
393 rad[4] = kBubbleCornerRadius;
394 rad[5] = kBubbleCornerRadius;
396 // Bottom Left Corner.
397 rad[6] = kBubbleCornerRadius;
398 rad[7] = kBubbleCornerRadius;
401 // Draw the bubble's shadow.
402 int width = popup_bounds.width();
403 int height = popup_bounds.height();
404 gfx::Rect rect(gfx::Rect(popup_bounds.size()));
405 SkPaint shadow_paint;
406 shadow_paint.setAntiAlias(true);
407 shadow_paint.setColor(kShadowColor);
409 SkRRect rrect;
410 rrect.setRectRadii(RectToSkRect(rect), (const SkVector*)rad);
411 canvas->sk_canvas()->drawRRect(rrect, shadow_paint);
413 const int shadow_size = 2 * kShadowThickness;
414 // Draw the bubble.
415 rect.SetRect(SkIntToScalar(kShadowThickness), SkIntToScalar(kShadowThickness),
416 SkIntToScalar(width - shadow_size),
417 SkIntToScalar(height - shadow_size));
418 rrect.setRectRadii(RectToSkRect(rect), (const SkVector*)rad);
419 canvas->sk_canvas()->drawRRect(rrect, paint);
421 // Draw highlight text and then the text body. In order to make sure the text
422 // is aligned to the right on RTL UIs, we mirror the text bounds if the
423 // locale is RTL.
424 const gfx::FontList font_list;
425 int text_width =
426 std::min(gfx::GetStringWidth(text_, font_list),
427 width - shadow_size - kTextPositionX - kTextHorizPadding);
428 int text_height = height - shadow_size;
429 gfx::Rect body_bounds(kShadowThickness + kTextPositionX,
430 kShadowThickness,
431 std::max(0, text_width),
432 std::max(0, text_height));
433 body_bounds.set_x(GetMirroredXForRect(body_bounds));
434 SkColor text_color =
435 theme_service_->GetColor(ThemeProperties::COLOR_STATUS_BAR_TEXT);
436 canvas->DrawStringRect(text_, font_list, text_color, body_bounds);
440 // StatusBubbleViews::StatusViewAnimation --------------------------------------
442 StatusBubbleViews::StatusViewAnimation::StatusViewAnimation(
443 StatusView* status_view,
444 double opacity_start,
445 double opacity_end)
446 : gfx::LinearAnimation(kFramerate, this),
447 status_view_(status_view),
448 opacity_start_(opacity_start),
449 opacity_end_(opacity_end) {
452 StatusBubbleViews::StatusViewAnimation::~StatusViewAnimation() {
453 // Remove ourself as a delegate so that we don't get notified when
454 // animations end as a result of destruction.
455 set_delegate(NULL);
458 double StatusBubbleViews::StatusViewAnimation::GetCurrentOpacity() {
459 return opacity_start_ + (opacity_end_ - opacity_start_) *
460 gfx::LinearAnimation::GetCurrentValue();
463 void StatusBubbleViews::StatusViewAnimation::AnimateToState(double state) {
464 status_view_->SetOpacity(GetCurrentOpacity());
467 void StatusBubbleViews::StatusViewAnimation::AnimationEnded(
468 const gfx::Animation* animation) {
469 status_view_->SetOpacity(opacity_end_);
470 status_view_->OnAnimationEnded();
473 // StatusBubbleViews::StatusViewExpander ---------------------------------------
475 // Manages the expansion and contraction of the status bubble as it accommodates
476 // URLs too long to fit in the standard bubble. Changes are passed through the
477 // StatusView to paint.
478 class StatusBubbleViews::StatusViewExpander : public gfx::LinearAnimation,
479 public gfx::AnimationDelegate {
480 public:
481 StatusViewExpander(StatusBubbleViews* status_bubble,
482 StatusView* status_view)
483 : gfx::LinearAnimation(kFramerate, this),
484 status_bubble_(status_bubble),
485 status_view_(status_view),
486 expansion_start_(0),
487 expansion_end_(0) {
490 // Manage the expansion of the bubble.
491 void StartExpansion(const base::string16& expanded_text,
492 int current_width,
493 int expansion_end);
495 // Set width of fully expanded bubble.
496 void SetExpandedWidth(int expanded_width);
498 private:
499 // Animation functions.
500 int GetCurrentBubbleWidth();
501 void SetBubbleWidth(int width);
502 void AnimateToState(double state) override;
503 void AnimationEnded(const gfx::Animation* animation) override;
505 // Manager that owns us.
506 StatusBubbleViews* status_bubble_;
508 // Change the bounds and text of this view.
509 StatusView* status_view_;
511 // Text elided (if needed) to fit maximum status bar width.
512 base::string16 expanded_text_;
514 // Widths at expansion start and end.
515 int expansion_start_;
516 int expansion_end_;
519 void StatusBubbleViews::StatusViewExpander::AnimateToState(double state) {
520 SetBubbleWidth(GetCurrentBubbleWidth());
523 void StatusBubbleViews::StatusViewExpander::AnimationEnded(
524 const gfx::Animation* animation) {
525 SetBubbleWidth(expansion_end_);
526 status_view_->SetText(expanded_text_, false);
529 void StatusBubbleViews::StatusViewExpander::StartExpansion(
530 const base::string16& expanded_text,
531 int expansion_start,
532 int expansion_end) {
533 expanded_text_ = expanded_text;
534 expansion_start_ = expansion_start;
535 expansion_end_ = expansion_end;
536 int min_duration = std::max(kMinExpansionStepDurationMS,
537 static_cast<int>(kMaxExpansionStepDurationMS *
538 (expansion_end - expansion_start) / 100.0));
539 SetDuration(std::min(kMaxExpansionStepDurationMS, min_duration));
540 Start();
543 int StatusBubbleViews::StatusViewExpander::GetCurrentBubbleWidth() {
544 return static_cast<int>(expansion_start_ +
545 (expansion_end_ - expansion_start_) *
546 gfx::LinearAnimation::GetCurrentValue());
549 void StatusBubbleViews::StatusViewExpander::SetBubbleWidth(int width) {
550 status_bubble_->SetBubbleWidth(width);
551 status_view_->SchedulePaint();
555 // StatusBubbleViews -----------------------------------------------------------
557 const int StatusBubbleViews::kShadowThickness = 1;
559 StatusBubbleViews::StatusBubbleViews(views::View* base_view)
560 : contains_mouse_(false),
561 offset_(0),
562 base_view_(base_view),
563 view_(NULL),
564 download_shelf_is_visible_(false),
565 is_expanded_(false),
566 expand_timer_factory_(this) {
567 expand_view_.reset();
570 StatusBubbleViews::~StatusBubbleViews() {
571 CancelExpandTimer();
572 if (popup_.get())
573 popup_->CloseNow();
576 void StatusBubbleViews::Init() {
577 if (!popup_.get()) {
578 popup_.reset(new views::Widget);
579 views::Widget* frame = base_view_->GetWidget();
580 if (!view_)
581 view_ = new StatusView(popup_.get(), frame->GetThemeProvider());
582 if (!expand_view_.get())
583 expand_view_.reset(new StatusViewExpander(this, view_));
584 // On Windows use TYPE_MENU to ensure that this window uses the software
585 // compositor which avoids the UI thread blocking issue during command
586 // buffer creation. We can revert this change once http://crbug.com/125248
587 // is fixed.
588 #if defined(OS_WIN)
589 views::Widget::InitParams params(views::Widget::InitParams::TYPE_MENU);
590 // The menu style assumes a top most window. We don't want that in this
591 // case.
592 params.keep_on_top = false;
593 #else
594 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
595 #endif
596 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
597 params.accept_events = false;
598 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
599 params.parent = frame->GetNativeView();
600 params.context = frame->GetNativeWindow();
601 popup_->Init(params);
602 // We do our own animation and don't want any from the system.
603 popup_->SetVisibilityChangedAnimationsEnabled(false);
604 popup_->SetOpacity(0x00);
605 popup_->SetContentsView(view_);
606 #if defined(USE_ASH)
607 ash::wm::GetWindowState(popup_->GetNativeWindow())->
608 set_ignored_by_shelf(true);
609 #endif
610 RepositionPopup();
614 void StatusBubbleViews::Reposition() {
615 // In restored mode, the client area has a client edge between it and the
616 // frame.
617 int overlap = kShadowThickness;
618 int height = GetPreferredSize().height();
619 int base_view_height = base_view()->bounds().height();
620 gfx::Point origin(-overlap, base_view_height - height + overlap);
621 SetBounds(origin.x(), origin.y(), base_view()->bounds().width() / 3, height);
624 void StatusBubbleViews::RepositionPopup() {
625 if (popup_.get()) {
626 gfx::Point top_left;
627 views::View::ConvertPointToScreen(base_view_, &top_left);
629 popup_->SetBounds(gfx::Rect(top_left.x() + position_.x(),
630 top_left.y() + position_.y(),
631 size_.width(), size_.height()));
635 gfx::Size StatusBubbleViews::GetPreferredSize() {
636 return gfx::Size(0, gfx::FontList().GetHeight() + kTotalVerticalPadding);
639 void StatusBubbleViews::SetBounds(int x, int y, int w, int h) {
640 original_position_.SetPoint(x, y);
641 position_.SetPoint(base_view_->GetMirroredXWithWidthInView(x, w), y);
642 size_.SetSize(w, h);
643 RepositionPopup();
644 if (popup_.get() && contains_mouse_)
645 AvoidMouse(last_mouse_moved_location_);
648 void StatusBubbleViews::SetStatus(const base::string16& status_text) {
649 if (size_.IsEmpty())
650 return; // We have no bounds, don't attempt to show the popup.
652 if (status_text_ == status_text && !status_text.empty())
653 return;
655 if (!IsFrameVisible())
656 return; // Don't show anything if the parent isn't visible.
658 Init();
659 status_text_ = status_text;
660 if (!status_text_.empty()) {
661 view_->SetText(status_text, true);
662 view_->Show();
663 } else if (!url_text_.empty()) {
664 view_->SetText(url_text_, true);
665 } else {
666 view_->SetText(base::string16(), true);
670 void StatusBubbleViews::SetURL(const GURL& url, const std::string& languages) {
671 url_ = url;
672 languages_ = languages;
673 if (size_.IsEmpty())
674 return; // We have no bounds, don't attempt to show the popup.
676 Init();
678 // If we want to clear a displayed URL but there is a status still to
679 // display, display that status instead.
680 if (url.is_empty() && !status_text_.empty()) {
681 url_text_ = base::string16();
682 if (IsFrameVisible())
683 view_->SetText(status_text_, true);
684 return;
687 // Reset expansion state only when bubble is completely hidden.
688 if (view_->state() == StatusView::BUBBLE_HIDDEN) {
689 is_expanded_ = false;
690 SetBubbleWidth(GetStandardStatusBubbleWidth());
693 // Set Elided Text corresponding to the GURL object.
694 gfx::Rect popup_bounds = popup_->GetWindowBoundsInScreen();
695 int text_width = static_cast<int>(popup_bounds.width() -
696 (kShadowThickness * 2) - kTextPositionX - kTextHorizPadding - 1);
697 url_text_ =
698 url_formatter::ElideUrl(url, gfx::FontList(), text_width, languages);
700 // An URL is always treated as a left-to-right string. On right-to-left UIs
701 // we need to explicitly mark the URL as LTR to make sure it is displayed
702 // correctly.
703 url_text_ = base::i18n::GetDisplayStringInLTRDirectionality(url_text_);
705 if (IsFrameVisible()) {
706 view_->SetText(url_text_, true);
708 CancelExpandTimer();
710 // If bubble is already in expanded state, shift to adjust to new text
711 // size (shrinking or expanding). Otherwise delay.
712 if (is_expanded_ && !url.is_empty()) {
713 ExpandBubble();
714 } else if (url_formatter::FormatUrl(url, languages).length() >
715 url_text_.length()) {
716 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
717 FROM_HERE, base::Bind(&StatusBubbleViews::ExpandBubble,
718 expand_timer_factory_.GetWeakPtr()),
719 base::TimeDelta::FromMilliseconds(kExpandHoverDelayMS));
724 void StatusBubbleViews::Hide() {
725 status_text_ = base::string16();
726 url_text_ = base::string16();
727 if (view_)
728 view_->Hide();
731 void StatusBubbleViews::MouseMoved(const gfx::Point& location,
732 bool left_content) {
733 contains_mouse_ = !left_content;
734 if (left_content) {
735 RepositionPopup();
736 return;
738 last_mouse_moved_location_ = location;
740 if (view_) {
741 view_->ResetTimer();
743 if (view_->state() != StatusView::BUBBLE_HIDDEN &&
744 view_->state() != StatusView::BUBBLE_HIDING_FADE &&
745 view_->state() != StatusView::BUBBLE_HIDING_TIMER) {
746 AvoidMouse(location);
751 void StatusBubbleViews::UpdateDownloadShelfVisibility(bool visible) {
752 download_shelf_is_visible_ = visible;
755 void StatusBubbleViews::AvoidMouse(const gfx::Point& location) {
756 // Get the position of the frame.
757 gfx::Point top_left;
758 views::View::ConvertPointToScreen(base_view_, &top_left);
759 // Border included.
760 int window_width = base_view_->GetLocalBounds().width();
762 // Get the cursor position relative to the popup.
763 gfx::Point relative_location = location;
764 if (base::i18n::IsRTL()) {
765 int top_right_x = top_left.x() + window_width;
766 relative_location.set_x(top_right_x - relative_location.x());
767 } else {
768 relative_location.set_x(
769 relative_location.x() - (top_left.x() + position_.x()));
771 relative_location.set_y(
772 relative_location.y() - (top_left.y() + position_.y()));
774 // If the mouse is in a position where we think it would move the
775 // status bubble, figure out where and how the bubble should be moved.
776 if (relative_location.y() > -kMousePadding &&
777 relative_location.x() < size_.width() + kMousePadding) {
778 int offset = kMousePadding + relative_location.y();
780 // Make the movement non-linear.
781 offset = offset * offset / kMousePadding;
783 // When the mouse is entering from the right, we want the offset to be
784 // scaled by how horizontally far away the cursor is from the bubble.
785 if (relative_location.x() > size_.width()) {
786 offset = static_cast<int>(static_cast<float>(offset) * (
787 static_cast<float>(kMousePadding -
788 (relative_location.x() - size_.width())) /
789 static_cast<float>(kMousePadding)));
792 // Cap the offset and change the visual presentation of the bubble
793 // depending on where it ends up (so that rounded corners square off
794 // and mate to the edges of the tab content).
795 if (offset >= size_.height() - kShadowThickness * 2) {
796 offset = size_.height() - kShadowThickness * 2;
797 view_->SetStyle(StatusView::STYLE_BOTTOM);
798 } else if (offset > kBubbleCornerRadius / 2 - kShadowThickness) {
799 view_->SetStyle(StatusView::STYLE_FLOATING);
800 } else {
801 view_->SetStyle(StatusView::STYLE_STANDARD);
804 // Check if the bubble sticks out from the monitor or will obscure
805 // download shelf.
806 gfx::NativeView window = base_view_->GetWidget()->GetNativeView();
807 gfx::Rect monitor_rect = gfx::Screen::GetScreenFor(window)->
808 GetDisplayNearestWindow(window).work_area();
809 const int bubble_bottom_y = top_left.y() + position_.y() + size_.height();
811 if (bubble_bottom_y + offset > monitor_rect.height() ||
812 (download_shelf_is_visible_ &&
813 (view_->style() == StatusView::STYLE_FLOATING ||
814 view_->style() == StatusView::STYLE_BOTTOM))) {
815 // The offset is still too large. Move the bubble to the right and reset
816 // Y offset_ to zero.
817 view_->SetStyle(StatusView::STYLE_STANDARD_RIGHT);
818 offset_ = 0;
820 // Subtract border width + bubble width.
821 int right_position_x = window_width - (position_.x() + size_.width());
822 popup_->SetBounds(gfx::Rect(top_left.x() + right_position_x,
823 top_left.y() + position_.y(),
824 size_.width(), size_.height()));
825 } else {
826 offset_ = offset;
827 popup_->SetBounds(gfx::Rect(top_left.x() + position_.x(),
828 top_left.y() + position_.y() + offset_,
829 size_.width(), size_.height()));
831 } else if (offset_ != 0 ||
832 view_->style() == StatusView::STYLE_STANDARD_RIGHT) {
833 offset_ = 0;
834 view_->SetStyle(StatusView::STYLE_STANDARD);
835 popup_->SetBounds(gfx::Rect(top_left.x() + position_.x(),
836 top_left.y() + position_.y(),
837 size_.width(), size_.height()));
841 bool StatusBubbleViews::IsFrameVisible() {
842 views::Widget* frame = base_view_->GetWidget();
843 if (!frame->IsVisible())
844 return false;
846 views::Widget* window = frame->GetTopLevelWidget();
847 return !window || !window->IsMinimized();
850 bool StatusBubbleViews::IsFrameMaximized() {
851 views::Widget* frame = base_view_->GetWidget();
852 views::Widget* window = frame->GetTopLevelWidget();
853 return window && window->IsMaximized();
856 void StatusBubbleViews::ExpandBubble() {
857 // Elide URL to maximum possible size, then check actual length (it may
858 // still be too long to fit) before expanding bubble.
859 gfx::Rect popup_bounds = popup_->GetWindowBoundsInScreen();
860 int max_status_bubble_width = GetMaxStatusBubbleWidth();
861 const gfx::FontList font_list;
862 url_text_ = url_formatter::ElideUrl(url_, font_list, max_status_bubble_width,
863 languages_);
864 int expanded_bubble_width =
865 std::max(GetStandardStatusBubbleWidth(),
866 std::min(gfx::GetStringWidth(url_text_, font_list) +
867 (kShadowThickness * 2) + kTextPositionX +
868 kTextHorizPadding + 1,
869 max_status_bubble_width));
870 is_expanded_ = true;
871 expand_view_->StartExpansion(url_text_, popup_bounds.width(),
872 expanded_bubble_width);
875 int StatusBubbleViews::GetStandardStatusBubbleWidth() {
876 return base_view_->bounds().width() / 3;
879 int StatusBubbleViews::GetMaxStatusBubbleWidth() {
880 const ui::NativeTheme* theme = base_view_->GetNativeTheme();
881 return static_cast<int>(std::max(0, base_view_->bounds().width() -
882 (kShadowThickness * 2) - kTextPositionX - kTextHorizPadding - 1 -
883 views::NativeScrollBar::GetVerticalScrollBarWidth(theme)));
886 void StatusBubbleViews::SetBubbleWidth(int width) {
887 size_.set_width(width);
888 SetBounds(original_position_.x(), original_position_.y(),
889 size_.width(), size_.height());
892 void StatusBubbleViews::CancelExpandTimer() {
893 if (expand_timer_factory_.HasWeakPtrs())
894 expand_timer_factory_.InvalidateWeakPtrs();