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.
5 #include "chrome/browser/ui/views/autofill/generated_credit_card_bubble_views.h"
7 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/views/frame/browser_view.h"
10 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
11 #include "ui/gfx/font.h"
12 #include "ui/gfx/geometry/insets.h"
13 #include "ui/gfx/geometry/size.h"
14 #include "ui/views/bubble/bubble_frame_view.h"
15 #include "ui/views/controls/styled_label.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/layout/layout_constants.h"
18 #include "ui/views/widget/widget.h"
24 // Get the view this bubble will be anchored to via |controller|.
25 views::View
* GetAnchor(
26 const base::WeakPtr
<GeneratedCreditCardBubbleController
>& controller
) {
27 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(
28 chrome::FindBrowserWithWebContents(controller
->web_contents()));
29 return browser_view
->GetLocationBarView()->generated_credit_card_view();
34 GeneratedCreditCardBubbleViews::~GeneratedCreditCardBubbleViews() {}
36 void GeneratedCreditCardBubbleViews::Show() {
37 views::BubbleDelegateView::CreateBubble(this)->Show();
39 // This bubble doesn't render correctly on Windows without calling
40 // |SizeToContents()|. This must be called after showing the widget.
44 void GeneratedCreditCardBubbleViews::Hide() {
48 bool GeneratedCreditCardBubbleViews::IsHiding() const {
49 return GetWidget() && GetWidget()->IsClosed();
52 gfx::Size
GeneratedCreditCardBubbleViews::GetPreferredSize() const {
54 GeneratedCreditCardBubbleView::kContentsWidth
,
55 GetHeightForWidth(GeneratedCreditCardBubbleViews::kContentsWidth
));
58 base::string16
GeneratedCreditCardBubbleViews::GetWindowTitle() const {
59 return controller_
? controller_
->TitleText() : base::string16();
62 void GeneratedCreditCardBubbleViews::Init() {
63 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical
, 0, 0,
64 views::kRelatedControlVerticalSpacing
));
66 const base::string16
& contents_text
= controller_
->ContentsText();
67 views::StyledLabel
* contents
= new views::StyledLabel(contents_text
, this);
69 const std::vector
<TextRange
>& text_ranges
= controller_
->ContentsTextRanges();
70 for (size_t i
= 0; i
< text_ranges
.size(); ++i
) {
71 views::StyledLabel::RangeStyleInfo style
;
73 if (text_ranges
[i
].is_link
)
74 style
= views::StyledLabel::RangeStyleInfo::CreateForLink();
76 style
.font_style
= gfx::Font::BOLD
;
78 contents
->AddStyleRange(text_ranges
[i
].range
, style
);
81 AddChildView(contents
);
84 void GeneratedCreditCardBubbleViews::StyledLabelLinkClicked(const gfx::Range
& r
,
87 controller_
->OnLinkClicked();
91 base::WeakPtr
<GeneratedCreditCardBubbleView
>
92 GeneratedCreditCardBubbleView::Create(
93 const base::WeakPtr
<GeneratedCreditCardBubbleController
>& controller
) {
94 return (new GeneratedCreditCardBubbleViews(controller
))->weak_ptr_factory_
.
98 GeneratedCreditCardBubbleViews::GeneratedCreditCardBubbleViews(
99 const base::WeakPtr
<GeneratedCreditCardBubbleController
>& controller
)
100 : BubbleDelegateView(GetAnchor(controller
), views::BubbleBorder::TOP_RIGHT
),
101 controller_(controller
),
102 weak_ptr_factory_(this) {
103 gfx::Insets insets
= views::BubbleFrameView::GetTitleInsets();
104 set_margins(gfx::Insets(0, insets
.left(), insets
.top(), insets
.left()));
107 } // namespace autofill