Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / autofill / generated_credit_card_bubble_views.cc
blob94406f7471fe01ef904cc091984a3087b3643998
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"
20 namespace autofill {
22 namespace {
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();
32 } // namespace
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.
41 SizeToContents();
44 void GeneratedCreditCardBubbleViews::Hide() {
45 GetWidget()->Close();
48 bool GeneratedCreditCardBubbleViews::IsHiding() const {
49 return GetWidget() && GetWidget()->IsClosed();
52 gfx::Size GeneratedCreditCardBubbleViews::GetPreferredSize() const {
53 return gfx::Size(
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();
75 else
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,
85 int event_flags) {
86 if (controller_)
87 controller_->OnLinkClicked();
90 // static
91 base::WeakPtr<GeneratedCreditCardBubbleView>
92 GeneratedCreditCardBubbleView::Create(
93 const base::WeakPtr<GeneratedCreditCardBubbleController>& controller) {
94 return (new GeneratedCreditCardBubbleViews(controller))->weak_ptr_factory_.
95 GetWeakPtr();
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