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