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"
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"
39 #include "ash/wm/window_state.h"
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
50 static const int kMousePadding
= 20;
52 // The horizontal offset of the text within the status bubble, not including the
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
{
79 StatusViewAnimation(StatusView
* status_view
,
82 ~StatusViewAnimation() override
;
84 double GetCurrentOpacity();
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_
;
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
{
111 // The bubble can be in one of many states:
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.
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.
144 // Hide the bubble instantly.
147 // Resets any timers we have. Typically called when the user moves a
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
156 void OnAnimationEnded();
161 // Manage the timers that control the delay before a fade begins or ends.
162 void StartTimer(base::TimeDelta time
);
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
);
173 const char* GetClassName() const override
;
174 void OnPaint(gfx::Canvas
* canvas
) override
;
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)),
201 theme_service_(theme_provider
),
202 timer_factory_(this) {
205 StatusBubbleViews::StatusView::~StatusView() {
210 void StatusBubbleViews::StatusView::SetText(const base::string16
& text
,
211 bool should_animate_open
) {
213 // The string was empty.
216 // We want to show the string.
221 if (should_animate_open
)
226 void StatusBubbleViews::StatusView::Show() {
230 popup_
->ShowInactive();
231 state_
= BUBBLE_SHOWN
;
234 void StatusBubbleViews::StatusView::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()),
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
) {
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
,
284 animation_
.reset(new StatusViewAnimation(this, start
, end
));
286 // This will also reset the currently-occurring animation.
287 animation_
->SetDuration(duration
);
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
;
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
;
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.
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
) {
346 void StatusBubbleViews::StatusView::OnAnimationEnded() {
347 if (state_
== BUBBLE_HIDING_FADE
) {
348 state_
= BUBBLE_HIDDEN
;
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
) {
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).
380 rad
[0] = kBubbleCornerRadius
;
381 rad
[1] = kBubbleCornerRadius
;
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
);
410 rrect
.setRectRadii(RectToSkRect(rect
), (const SkVector
*)rad
);
411 canvas
->sk_canvas()->drawRRect(rrect
, shadow_paint
);
413 const int shadow_size
= 2 * kShadowThickness
;
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
424 const gfx::FontList font_list
;
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
,
431 std::max(0, text_width
),
432 std::max(0, text_height
));
433 body_bounds
.set_x(GetMirroredXForRect(body_bounds
));
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
,
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.
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
{
481 StatusViewExpander(StatusBubbleViews
* status_bubble
,
482 StatusView
* status_view
)
483 : gfx::LinearAnimation(kFramerate
, this),
484 status_bubble_(status_bubble
),
485 status_view_(status_view
),
490 // Manage the expansion of the bubble.
491 void StartExpansion(const base::string16
& expanded_text
,
495 // Set width of fully expanded bubble.
496 void SetExpandedWidth(int expanded_width
);
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_
;
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
,
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
));
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),
562 base_view_(base_view
),
564 download_shelf_is_visible_(false),
566 expand_timer_factory_(this) {
567 expand_view_
.reset();
570 StatusBubbleViews::~StatusBubbleViews() {
576 void StatusBubbleViews::Init() {
578 popup_
.reset(new views::Widget
);
579 views::Widget
* frame
= base_view_
->GetWidget();
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
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
592 params
.keep_on_top
= false;
594 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
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_
);
607 ash::wm::GetWindowState(popup_
->GetNativeWindow())->
608 set_ignored_by_shelf(true);
614 void StatusBubbleViews::Reposition() {
615 // In restored mode, the client area has a client edge between it and the
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() {
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
);
644 if (popup_
.get() && contains_mouse_
)
645 AvoidMouse(last_mouse_moved_location_
);
648 void StatusBubbleViews::SetStatus(const base::string16
& status_text
) {
650 return; // We have no bounds, don't attempt to show the popup.
652 if (status_text_
== status_text
&& !status_text
.empty())
655 if (!IsFrameVisible())
656 return; // Don't show anything if the parent isn't visible.
659 status_text_
= status_text
;
660 if (!status_text_
.empty()) {
661 view_
->SetText(status_text
, true);
663 } else if (!url_text_
.empty()) {
664 view_
->SetText(url_text_
, true);
666 view_
->SetText(base::string16(), true);
670 void StatusBubbleViews::SetURL(const GURL
& url
, const std::string
& languages
) {
672 languages_
= languages
;
674 return; // We have no bounds, don't attempt to show the popup.
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);
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);
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
703 url_text_
= base::i18n::GetDisplayStringInLTRDirectionality(url_text_
);
705 if (IsFrameVisible()) {
706 view_
->SetText(url_text_
, true);
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()) {
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();
731 void StatusBubbleViews::MouseMoved(const gfx::Point
& location
,
733 contains_mouse_
= !left_content
;
738 last_mouse_moved_location_
= location
;
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.
758 views::View::ConvertPointToScreen(base_view_
, &top_left
);
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());
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
);
801 view_
->SetStyle(StatusView::STYLE_STANDARD
);
804 // Check if the bubble sticks out from the monitor or will obscure
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
);
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()));
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
) {
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())
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
,
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
));
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();