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" | |
8 #include "base/macros.h" | |
9 #include "ui/gfx/geometry/point.h" | 7 #include "ui/gfx/geometry/point.h" |
10 #include "ui/gfx/geometry/rect.h" | 8 #include "ui/gfx/geometry/rect.h" |
11 #include "ui/gfx/geometry/size.h" | 9 #include "ui/gfx/geometry/size.h" |
12 #include "ui/gfx/text_constants.h" | |
13 #include "ui/views/bubble/bubble_border.h" | 10 #include "ui/views/bubble/bubble_border.h" |
14 #include "ui/views/bubble/bubble_frame_view.h" | 11 #include "ui/views/bubble/bubble_frame_view.h" |
15 #include "ui/views/controls/combobox/combobox.h" | |
16 #include "ui/views/controls/label.h" | 12 #include "ui/views/controls/label.h" |
17 #include "ui/views/layout/fill_layout.h" | 13 #include "ui/views/layout/fill_layout.h" |
18 #include "ui/views/layout/layout_constants.h" | |
19 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
20 | 15 |
21 namespace autofill { | 16 namespace views { |
22 | 17 |
23 namespace { | 18 namespace { |
24 | 19 |
25 // The visible width of bubble borders (differs from the actual width) in px. | 20 // The visible width of bubble borders (differs from the actual width) in px. |
26 const int kBubbleBorderVisibleWidth = 1; | 21 const int kBubbleBorderVisibleWidth = 1; |
27 | 22 |
28 // The margin between the content of the error bubble and its border. | 23 // The margin between the content of the error bubble and its border. |
29 const int kInfoBubbleHorizontalMargin = 14; | 24 const int kInfoBubbleHorizontalMargin = 14; |
30 const int kInfoBubbleVerticalMargin = 12; | 25 const int kInfoBubbleVerticalMargin = 12; |
31 | 26 |
32 } // namespace | 27 } // namespace |
33 | 28 |
34 class InfoBubbleFrame : public views::BubbleFrameView { | 29 class InfoBubbleFrame : public BubbleFrameView { |
35 public: | 30 public: |
36 explicit InfoBubbleFrame(const gfx::Insets& content_margins) | 31 explicit InfoBubbleFrame(const gfx::Insets& content_margins) |
37 : views::BubbleFrameView(gfx::Insets(), content_margins) {} | 32 : BubbleFrameView(gfx::Insets(), content_margins) {} |
38 ~InfoBubbleFrame() override {} | 33 ~InfoBubbleFrame() override {} |
39 | 34 |
40 gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override { | 35 gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override { |
41 return available_bounds_; | 36 return available_bounds_; |
42 } | 37 } |
43 | 38 |
44 void set_available_bounds(const gfx::Rect& available_bounds) { | 39 void set_available_bounds(const gfx::Rect& available_bounds) { |
45 available_bounds_ = available_bounds; | 40 available_bounds_ = available_bounds; |
46 } | 41 } |
47 | 42 |
48 private: | 43 private: |
49 // Bounds that this frame should try to keep bubbles within (screen coords). | 44 // Bounds that this frame should try to keep bubbles within (screen coords). |
50 gfx::Rect available_bounds_; | 45 gfx::Rect available_bounds_; |
51 | 46 |
52 DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame); | 47 DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame); |
53 }; | 48 }; |
54 | 49 |
55 InfoBubble::InfoBubble(views::View* anchor, | 50 InfoBubble::InfoBubble(View* anchor, const base::string16& message) |
56 const base::string16& message) | 51 : anchor_(anchor), frame_(nullptr), preferred_width_(0) { |
57 : anchor_(anchor), | |
58 frame_(NULL), | |
59 align_to_anchor_edge_(false), | |
60 preferred_width_(233), | |
61 show_above_anchor_(false) { | |
62 DCHECK(anchor_); | 52 DCHECK(anchor_); |
63 SetAnchorView(anchor_); | 53 SetAnchorView(anchor_); |
64 | 54 |
65 set_margins(gfx::Insets(kInfoBubbleVerticalMargin, | 55 set_margins( |
66 kInfoBubbleHorizontalMargin, | 56 gfx::Insets(kInfoBubbleVerticalMargin, kInfoBubbleHorizontalMargin)); |
67 kInfoBubbleVerticalMargin, | |
68 kInfoBubbleHorizontalMargin)); | |
69 set_can_activate(false); | 57 set_can_activate(false); |
70 | 58 |
71 SetLayoutManager(new views::FillLayout); | 59 SetLayoutManager(new FillLayout); |
72 views::Label* label = new views::Label(message); | 60 Label* label = new Label(message); |
73 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 61 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
74 label->SetMultiLine(true); | 62 label->SetMultiLine(true); |
75 AddChildView(label); | 63 AddChildView(label); |
76 } | 64 } |
77 | 65 |
78 InfoBubble::~InfoBubble() {} | 66 InfoBubble::~InfoBubble() {} |
79 | 67 |
80 void InfoBubble::Show() { | 68 void InfoBubble::Show() { |
81 // TODO(dbeam): currently we assume that combobox menus always show downward | 69 widget_ = BubbleDialogDelegateView::CreateBubble(this); |
82 // (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 | |
84 // this might happen and adjust |show_above_anchor_| accordingly. This is not | |
85 // that big of deal because it rarely happens in practice. | |
86 if (show_above_anchor_) | |
87 set_arrow(views::BubbleBorder::vertical_mirror(arrow())); | |
88 | |
89 widget_ = views::BubbleDialogDelegateView::CreateBubble(this); | |
90 | |
91 if (align_to_anchor_edge_) { | |
92 // 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. | |
94 frame_->bubble_border()->set_arrow(arrow()); | |
95 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | |
96 } | |
97 | 70 |
98 UpdatePosition(); | 71 UpdatePosition(); |
99 } | 72 } |
100 | 73 |
101 void InfoBubble::Hide() { | 74 void InfoBubble::Hide() { |
102 views::Widget* widget = GetWidget(); | 75 Widget* widget = GetWidget(); |
103 if (widget && !widget->IsClosed()) | 76 if (widget && !widget->IsClosed()) |
104 widget->Close(); | 77 widget->Close(); |
105 } | 78 } |
106 | 79 |
107 void InfoBubble::UpdatePosition() { | 80 NonClientFrameView* InfoBubble::CreateNonClientFrameView(Widget* widget) { |
108 if (!widget_) | |
109 return; | |
110 | |
111 if (!anchor_->GetVisibleBounds().IsEmpty()) { | |
112 SizeToContents(); | |
113 widget_->SetVisibilityChangedAnimationsEnabled(true); | |
114 widget_->ShowInactive(); | |
115 } else { | |
116 widget_->SetVisibilityChangedAnimationsEnabled(false); | |
117 widget_->Hide(); | |
118 } | |
119 } | |
120 | |
121 views::NonClientFrameView* InfoBubble::CreateNonClientFrameView( | |
122 views::Widget* widget) { | |
123 DCHECK(!frame_); | 81 DCHECK(!frame_); |
124 frame_ = new InfoBubbleFrame(margins()); | 82 frame_ = new InfoBubbleFrame(margins()); |
125 frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen()); | 83 frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen()); |
126 frame_->SetBubbleBorder(std::unique_ptr<views::BubbleBorder>( | 84 frame_->SetBubbleBorder(std::unique_ptr<BubbleBorder>( |
127 new views::BubbleBorder(arrow(), shadow(), color()))); | 85 new BubbleBorder(arrow(), shadow(), color()))); |
128 return frame_; | 86 return frame_; |
129 } | 87 } |
130 | 88 |
131 gfx::Size InfoBubble::GetPreferredSize() const { | 89 gfx::Size InfoBubble::GetPreferredSize() const { |
| 90 if (preferred_width_ == 0) |
| 91 return BubbleDialogDelegateView::GetPreferredSize(); |
| 92 |
132 int pref_width = preferred_width_; | 93 int pref_width = preferred_width_; |
133 pref_width -= frame_->GetInsets().width(); | 94 pref_width -= frame_->GetInsets().width(); |
134 pref_width -= 2 * kBubbleBorderVisibleWidth; | 95 pref_width -= 2 * kBubbleBorderVisibleWidth; |
135 return gfx::Size(pref_width, GetHeightForWidth(pref_width)); | 96 return gfx::Size(pref_width, GetHeightForWidth(pref_width)); |
136 } | 97 } |
137 | 98 |
138 void InfoBubble::OnWidgetDestroyed(views::Widget* widget) { | 99 void InfoBubble::OnWidgetDestroyed(Widget* widget) { |
139 if (widget == widget_) | 100 if (widget == widget_) |
140 widget_ = NULL; | 101 widget_ = nullptr; |
141 } | 102 } |
142 | 103 |
143 void InfoBubble::OnWidgetBoundsChanged(views::Widget* widget, | 104 void InfoBubble::OnWidgetBoundsChanged(Widget* widget, |
144 const gfx::Rect& new_bounds) { | 105 const gfx::Rect& new_bounds) { |
145 views::BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds); | 106 BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds); |
146 if (anchor_widget() == widget) | 107 if (anchor_widget() == widget) |
147 frame_->set_available_bounds(widget->GetWindowBoundsInScreen()); | 108 frame_->set_available_bounds(widget->GetWindowBoundsInScreen()); |
148 } | 109 } |
149 | 110 |
150 int InfoBubble::GetDialogButtons() const { | 111 int InfoBubble::GetDialogButtons() const { |
151 return ui::DIALOG_BUTTON_NONE; | 112 return ui::DIALOG_BUTTON_NONE; |
152 } | 113 } |
153 | 114 |
154 } // namespace autofill | 115 void InfoBubble::UpdatePosition() { |
| 116 if (!widget_) |
| 117 return; |
| 118 |
| 119 if (!anchor_->GetVisibleBounds().IsEmpty()) { |
| 120 SizeToContents(); |
| 121 widget_->SetVisibilityChangedAnimationsEnabled(true); |
| 122 widget_->ShowInactive(); |
| 123 } else { |
| 124 widget_->SetVisibilityChangedAnimationsEnabled(false); |
| 125 widget_->Hide(); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace views |
OLD | NEW |