1 // Copyright 2013 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 "ui/views/bubble/info_bubble.h"
6 
7 #include <memory>
8 
9 #include "ui/gfx/geometry/point.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/gfx/geometry/size.h"
12 #include "ui/views/bubble/bubble_border.h"
13 #include "ui/views/bubble/bubble_frame_view.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/fill_layout.h"
16 #include "ui/views/layout/layout_provider.h"
17 #include "ui/views/widget/widget.h"
18 
19 namespace views {
20 
21 namespace {
22 
23 // The visible width of bubble borders (differs from the actual width) in px.
24 constexpr int kBubbleBorderVisibleWidth = 1;
25 
26 }  // namespace
27 
28 class InfoBubbleFrame : public BubbleFrameView {
29  public:
InfoBubbleFrame(const gfx::Insets & content_margins)30   explicit InfoBubbleFrame(const gfx::Insets& content_margins)
31       : BubbleFrameView(gfx::Insets(), content_margins) {}
32   ~InfoBubbleFrame() override = default;
33 
GetAvailableScreenBounds(const gfx::Rect & rect) const34   gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override {
35     return available_bounds_;
36   }
37 
set_available_bounds(const gfx::Rect & available_bounds)38   void set_available_bounds(const gfx::Rect& available_bounds) {
39     available_bounds_ = available_bounds;
40   }
41 
42  private:
43   // Bounds that this frame should try to keep bubbles within (screen coords).
44   gfx::Rect available_bounds_;
45 
46   DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame);
47 };
48 
InfoBubble(View * anchor,const base::string16 & message)49 InfoBubble::InfoBubble(View* anchor, const base::string16& message)
50     : anchor_(anchor), frame_(nullptr), preferred_width_(0) {
51   DCHECK(anchor_);
52   SetAnchorView(anchor_);
53 
54   DialogDelegate::SetButtons(ui::DIALOG_BUTTON_NONE);
55 
56   set_margins(LayoutProvider::Get()->GetInsetsMetric(
57       InsetsMetric::INSETS_TOOLTIP_BUBBLE));
58   SetCanActivate(false);
59 
60   SetLayoutManager(std::make_unique<FillLayout>());
61   Label* label = new Label(message);
62   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
63   label->SetMultiLine(true);
64   AddChildView(label);
65 }
66 
67 InfoBubble::~InfoBubble() = default;
68 
Show()69 void InfoBubble::Show() {
70   widget_ = BubbleDialogDelegateView::CreateBubble(this);
71 
72   UpdatePosition();
73 }
74 
Hide()75 void InfoBubble::Hide() {
76   Widget* widget = GetWidget();
77   if (widget && !widget->IsClosed())
78     widget->Close();
79 }
80 
CreateNonClientFrameView(Widget * widget)81 NonClientFrameView* InfoBubble::CreateNonClientFrameView(Widget* widget) {
82   DCHECK(!frame_);
83   frame_ = new InfoBubbleFrame(margins());
84   frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen());
85   frame_->SetBubbleBorder(
86       std::make_unique<BubbleBorder>(arrow(), GetShadow(), color()));
87   return frame_;
88 }
89 
CalculatePreferredSize() const90 gfx::Size InfoBubble::CalculatePreferredSize() const {
91   if (preferred_width_ == 0)
92     return BubbleDialogDelegateView::CalculatePreferredSize();
93 
94   int pref_width = preferred_width_;
95   pref_width -= frame_->GetInsets().width();
96   pref_width -= 2 * kBubbleBorderVisibleWidth;
97   return gfx::Size(pref_width, GetHeightForWidth(pref_width));
98 }
99 
OnWidgetDestroyed(Widget * widget)100 void InfoBubble::OnWidgetDestroyed(Widget* widget) {
101   if (widget == widget_)
102     widget_ = nullptr;
103 }
104 
OnWidgetBoundsChanged(Widget * widget,const gfx::Rect & new_bounds)105 void InfoBubble::OnWidgetBoundsChanged(Widget* widget,
106                                        const gfx::Rect& new_bounds) {
107   BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds);
108   if (anchor_widget() == widget)
109     frame_->set_available_bounds(widget->GetWindowBoundsInScreen());
110 }
111 
UpdatePosition()112 void InfoBubble::UpdatePosition() {
113   if (!widget_)
114     return;
115 
116   if (!anchor_->GetVisibleBounds().IsEmpty()) {
117     SizeToContents();
118     widget_->SetVisibilityChangedAnimationsEnabled(true);
119     widget_->ShowInactive();
120   } else {
121     widget_->SetVisibilityChangedAnimationsEnabled(false);
122     widget_->Hide();
123   }
124 }
125 
126 BEGIN_METADATA(InfoBubble)
127 METADATA_PARENT_CLASS(BubbleDialogDelegateView)
128 END_METADATA()
129 
130 }  // namespace views
131