OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/autofill/info_bubble.h" | 5 #include "ui/views/bubble/info_bubble.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
msw
2017/02/15 21:06:38
nit: remove
vasilii
2017/02/16 14:28:06
Done.
| |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
msw
2017/02/15 21:06:38
nit: remove
vasilii
2017/02/16 14:28:06
Done.
| |
9 #include "ui/gfx/geometry/point.h" | 9 #include "ui/gfx/geometry/point.h" |
10 #include "ui/gfx/geometry/rect.h" | 10 #include "ui/gfx/geometry/rect.h" |
11 #include "ui/gfx/geometry/size.h" | 11 #include "ui/gfx/geometry/size.h" |
12 #include "ui/gfx/text_constants.h" | 12 #include "ui/gfx/text_constants.h" |
msw
2017/02/15 21:06:38
nit: remove?
vasilii
2017/02/16 14:28:06
Done.
| |
13 #include "ui/views/bubble/bubble_border.h" | 13 #include "ui/views/bubble/bubble_border.h" |
14 #include "ui/views/bubble/bubble_frame_view.h" | 14 #include "ui/views/bubble/bubble_frame_view.h" |
15 #include "ui/views/controls/combobox/combobox.h" | 15 #include "ui/views/controls/combobox/combobox.h" |
msw
2017/02/15 21:06:38
nit: remove
vasilii
2017/02/16 14:28:06
Done.
| |
16 #include "ui/views/controls/label.h" | 16 #include "ui/views/controls/label.h" |
17 #include "ui/views/layout/fill_layout.h" | 17 #include "ui/views/layout/fill_layout.h" |
18 #include "ui/views/layout/layout_constants.h" | 18 #include "ui/views/layout/layout_constants.h" |
msw
2017/02/15 21:06:38
nit: remove?
vasilii
2017/02/16 14:28:06
Done.
| |
19 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
20 | 20 |
21 namespace autofill { | 21 namespace views { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // The visible width of bubble borders (differs from the actual width) in px. | 25 // The visible width of bubble borders (differs from the actual width) in px. |
26 const int kBubbleBorderVisibleWidth = 1; | 26 const int kBubbleBorderVisibleWidth = 1; |
27 | 27 |
28 // The margin between the content of the error bubble and its border. | 28 // The margin between the content of the error bubble and its border. |
29 const int kInfoBubbleHorizontalMargin = 14; | 29 const int kInfoBubbleHorizontalMargin = 14; |
30 const int kInfoBubbleVerticalMargin = 12; | 30 const int kInfoBubbleVerticalMargin = 12; |
31 | 31 |
32 gfx::Insets GetPreferredInsets(const View* view) { | |
msw
2017/02/15 21:06:38
nit: add a comment or inline in the one caller
vasilii
2017/02/16 14:28:06
Acknowledged.
| |
33 gfx::Size pref_size = view->GetPreferredSize(); | |
34 gfx::Rect local_bounds = view->GetLocalBounds(); | |
35 gfx::Point origin = local_bounds.CenterPoint(); | |
msw
2017/02/15 21:06:38
I'm confused by this function; what is it trying t
vasilii
2017/02/16 14:28:06
Looks like the function does nothing. The insets a
| |
36 origin.Offset(-pref_size.width() / 2, -pref_size.height() / 2); | |
37 return gfx::Insets(origin.y(), origin.x(), | |
38 local_bounds.bottom() - (origin.y() + pref_size.height()), | |
39 local_bounds.right() - (origin.x() + pref_size.width())); | |
40 } | |
41 | |
32 } // namespace | 42 } // namespace |
33 | 43 |
34 class InfoBubbleFrame : public views::BubbleFrameView { | 44 class InfoBubbleFrame : public BubbleFrameView { |
35 public: | 45 public: |
36 explicit InfoBubbleFrame(const gfx::Insets& content_margins) | 46 explicit InfoBubbleFrame(const gfx::Insets& content_margins) |
37 : views::BubbleFrameView(gfx::Insets(), content_margins) {} | 47 : BubbleFrameView(gfx::Insets(), content_margins) {} |
38 ~InfoBubbleFrame() override {} | 48 ~InfoBubbleFrame() override {} |
39 | 49 |
40 gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override { | 50 gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override { |
41 return available_bounds_; | 51 return available_bounds_; |
42 } | 52 } |
43 | 53 |
44 void set_available_bounds(const gfx::Rect& available_bounds) { | 54 void set_available_bounds(const gfx::Rect& available_bounds) { |
45 available_bounds_ = available_bounds; | 55 available_bounds_ = available_bounds; |
46 } | 56 } |
47 | 57 |
48 private: | 58 private: |
49 // Bounds that this frame should try to keep bubbles within (screen coords). | 59 // Bounds that this frame should try to keep bubbles within (screen coords). |
50 gfx::Rect available_bounds_; | 60 gfx::Rect available_bounds_; |
51 | 61 |
52 DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame); | 62 DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame); |
53 }; | 63 }; |
54 | 64 |
55 InfoBubble::InfoBubble(views::View* anchor, | 65 InfoBubble::InfoBubble(View* anchor, const base::string16& message) |
56 const base::string16& message) | |
57 : anchor_(anchor), | 66 : anchor_(anchor), |
58 frame_(NULL), | 67 frame_(NULL), |
59 align_to_anchor_edge_(false), | 68 align_to_anchor_edge_(false), |
60 preferred_width_(233), | 69 preferred_width_(0), |
61 show_above_anchor_(false) { | 70 show_above_anchor_(false) { |
62 DCHECK(anchor_); | 71 DCHECK(anchor_); |
63 SetAnchorView(anchor_); | 72 SetAnchorView(anchor_); |
64 | 73 |
65 set_margins(gfx::Insets(kInfoBubbleVerticalMargin, | 74 set_margins( |
66 kInfoBubbleHorizontalMargin, | 75 gfx::Insets(kInfoBubbleVerticalMargin, kInfoBubbleHorizontalMargin, |
msw
2017/02/15 21:06:38
nit: use two-arg ctor
vasilii
2017/02/16 14:28:06
Done.
| |
67 kInfoBubbleVerticalMargin, | 76 kInfoBubbleVerticalMargin, kInfoBubbleHorizontalMargin)); |
68 kInfoBubbleHorizontalMargin)); | |
69 set_can_activate(false); | 77 set_can_activate(false); |
70 | 78 |
71 SetLayoutManager(new views::FillLayout); | 79 SetLayoutManager(new FillLayout); |
72 views::Label* label = new views::Label(message); | 80 Label* label = new Label(message); |
73 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 81 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
74 label->SetMultiLine(true); | 82 label->SetMultiLine(true); |
75 AddChildView(label); | 83 AddChildView(label); |
76 } | 84 } |
77 | 85 |
78 InfoBubble::~InfoBubble() {} | 86 InfoBubble::~InfoBubble() {} |
79 | 87 |
80 void InfoBubble::Show() { | 88 void InfoBubble::Show() { |
81 // TODO(dbeam): currently we assume that combobox menus always show downward | 89 // TODO(dbeam): currently we assume that combobox menus always show downward |
msw
2017/02/15 21:06:38
nit: update if appropriate (not specific to combob
vasilii
2017/02/16 14:28:06
Done.
| |
82 // (which isn't true). If the invalid combobox is low enough on the screen, | 90 // (which isn't true). If the invalid combobox is low enough on the screen, |
83 // its menu will actually show upward and obscure the bubble. Figure out when | 91 // its menu will actually show upward and obscure the bubble. Figure out when |
84 // this might happen and adjust |show_above_anchor_| accordingly. This is not | 92 // this might happen and adjust |show_above_anchor_| accordingly. This is not |
85 // that big of deal because it rarely happens in practice. | 93 // that big of deal because it rarely happens in practice. |
86 if (show_above_anchor_) | 94 if (show_above_anchor_) |
87 set_arrow(views::BubbleBorder::vertical_mirror(arrow())); | 95 set_arrow(BubbleBorder::vertical_mirror(arrow())); |
88 | 96 |
89 widget_ = views::BubbleDialogDelegateView::CreateBubble(this); | 97 widget_ = BubbleDialogDelegateView::CreateBubble(this); |
90 | 98 |
91 if (align_to_anchor_edge_) { | 99 if (align_to_anchor_edge_) { |
92 // The frame adjusts its arrow before the bubble's alignment can be changed. | 100 // The frame adjusts its arrow before the bubble's alignment can be changed. |
93 // Set the created bubble border back to the original arrow and re-adjust. | 101 // Set the created bubble border back to the original arrow and re-adjust. |
94 frame_->bubble_border()->set_arrow(arrow()); | 102 frame_->bubble_border()->set_arrow(arrow()); |
95 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | 103 SetAlignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
96 } | 104 } |
97 | 105 |
98 UpdatePosition(); | 106 UpdatePosition(); |
99 } | 107 } |
100 | 108 |
101 void InfoBubble::Hide() { | 109 void InfoBubble::Hide() { |
102 views::Widget* widget = GetWidget(); | 110 Widget* widget = GetWidget(); |
103 if (widget && !widget->IsClosed()) | 111 if (widget && !widget->IsClosed()) |
104 widget->Close(); | 112 widget->Close(); |
105 } | 113 } |
106 | 114 |
107 void InfoBubble::UpdatePosition() { | 115 void InfoBubble::UpdatePosition() { |
108 if (!widget_) | 116 if (!widget_) |
109 return; | 117 return; |
110 | 118 |
111 if (!anchor_->GetVisibleBounds().IsEmpty()) { | 119 if (!anchor_->GetVisibleBounds().IsEmpty()) { |
112 SizeToContents(); | 120 SizeToContents(); |
113 widget_->SetVisibilityChangedAnimationsEnabled(true); | 121 widget_->SetVisibilityChangedAnimationsEnabled(true); |
114 widget_->ShowInactive(); | 122 widget_->ShowInactive(); |
115 } else { | 123 } else { |
116 widget_->SetVisibilityChangedAnimationsEnabled(false); | 124 widget_->SetVisibilityChangedAnimationsEnabled(false); |
117 widget_->Hide(); | 125 widget_->Hide(); |
118 } | 126 } |
119 } | 127 } |
120 | 128 |
121 views::NonClientFrameView* InfoBubble::CreateNonClientFrameView( | 129 NonClientFrameView* InfoBubble::CreateNonClientFrameView(Widget* widget) { |
122 views::Widget* widget) { | |
123 DCHECK(!frame_); | 130 DCHECK(!frame_); |
124 frame_ = new InfoBubbleFrame(margins()); | 131 frame_ = new InfoBubbleFrame(margins()); |
125 frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen()); | 132 frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen()); |
126 frame_->SetBubbleBorder(std::unique_ptr<views::BubbleBorder>( | 133 frame_->SetBubbleBorder(std::unique_ptr<BubbleBorder>( |
127 new views::BubbleBorder(arrow(), shadow(), color()))); | 134 new BubbleBorder(arrow(), shadow(), color()))); |
128 return frame_; | 135 return frame_; |
129 } | 136 } |
130 | 137 |
131 gfx::Size InfoBubble::GetPreferredSize() const { | 138 gfx::Size InfoBubble::GetPreferredSize() const { |
139 if (!preferred_width_) | |
msw
2017/02/15 21:06:38
nit: if |preferred_width_ <= 0| (or ==)
vasilii
2017/02/16 14:28:06
Done.
| |
140 return BubbleDialogDelegateView::GetPreferredSize(); | |
141 | |
132 int pref_width = preferred_width_; | 142 int pref_width = preferred_width_; |
133 pref_width -= frame_->GetInsets().width(); | 143 pref_width -= frame_->GetInsets().width(); |
134 pref_width -= 2 * kBubbleBorderVisibleWidth; | 144 pref_width -= 2 * kBubbleBorderVisibleWidth; |
135 return gfx::Size(pref_width, GetHeightForWidth(pref_width)); | 145 return gfx::Size(pref_width, GetHeightForWidth(pref_width)); |
136 } | 146 } |
137 | 147 |
138 void InfoBubble::OnWidgetDestroyed(views::Widget* widget) { | 148 void InfoBubble::OnWidgetDestroyed(Widget* widget) { |
139 if (widget == widget_) | 149 if (widget == widget_) |
140 widget_ = NULL; | 150 widget_ = NULL; |
141 } | 151 } |
142 | 152 |
143 void InfoBubble::OnWidgetBoundsChanged(views::Widget* widget, | 153 void InfoBubble::OnWidgetBoundsChanged(Widget* widget, |
144 const gfx::Rect& new_bounds) { | 154 const gfx::Rect& new_bounds) { |
145 views::BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds); | 155 BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds); |
146 if (anchor_widget() == widget) | 156 if (anchor_widget() == widget) |
147 frame_->set_available_bounds(widget->GetWindowBoundsInScreen()); | 157 frame_->set_available_bounds(widget->GetWindowBoundsInScreen()); |
148 } | 158 } |
149 | 159 |
150 int InfoBubble::GetDialogButtons() const { | 160 int InfoBubble::GetDialogButtons() const { |
151 return ui::DIALOG_BUTTON_NONE; | 161 return ui::DIALOG_BUTTON_NONE; |
152 } | 162 } |
153 | 163 |
154 } // namespace autofill | 164 gfx::Rect InfoBubble::GetAnchorRect() const { |
165 gfx::Rect bounds = BubbleDialogDelegateView::GetAnchorRect(); | |
166 bounds.Inset(GetPreferredInsets(anchor())); | |
167 return bounds; | |
168 } | |
169 | |
170 } // namespace views | |
OLD | NEW |