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/autofill_credit_card_bubble_views.h" | 5 #include "chrome/browser/ui/views/autofill/autofill_credit_card_bubble_views.h" |
6 | 6 |
7 #include "chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.h" | 7 #include "chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_finder.h" | 9 #include "chrome/browser/ui/browser_finder.h" |
10 #include "chrome/browser/ui/views/frame/browser_view.h" | 10 #include "chrome/browser/ui/views/frame/browser_view.h" |
11 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 11 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
12 #include "chrome/browser/ui/views/toolbar_view.h" | |
12 #include "ui/gfx/font.h" | 13 #include "ui/gfx/font.h" |
13 #include "ui/gfx/insets.h" | 14 #include "ui/gfx/insets.h" |
14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
15 #include "ui/views/bubble/bubble_frame_view.h" | 16 #include "ui/views/bubble/bubble_frame_view.h" |
17 #include "ui/views/controls/image_view.h" | |
16 #include "ui/views/controls/link.h" | 18 #include "ui/views/controls/link.h" |
17 #include "ui/views/controls/styled_label.h" | 19 #include "ui/views/controls/styled_label.h" |
18 #include "ui/views/layout/box_layout.h" | 20 #include "ui/views/layout/box_layout.h" |
19 #include "ui/views/layout/layout_constants.h" | 21 #include "ui/views/layout/layout_constants.h" |
20 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
21 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
22 | 24 |
23 namespace autofill { | 25 namespace autofill { |
24 | 26 |
25 AutofillCreditCardBubbleViews::~AutofillCreditCardBubbleViews() {} | 27 namespace { |
28 | |
29 views::View* GetAnchor( | |
30 const base::WeakPtr<AutofillCreditCardBubbleController>& controller) { | |
31 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser( | |
32 chrome::FindBrowserWithWebContents(controller->web_contents())); | |
33 return controller->AnchorToSettingsMenu() ? | |
34 browser_view->GetToolbarView()->app_menu() : | |
35 browser_view->GetLocationBarView()->autofill_credit_card_view(); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 AutofillCreditCardBubbleViews::~AutofillCreditCardBubbleViews() { | |
41 if (controller_) | |
42 controller_->OnBubbleDestroyed(); | |
43 } | |
26 | 44 |
27 void AutofillCreditCardBubbleViews::Show() { | 45 void AutofillCreditCardBubbleViews::Show() { |
28 // TODO(dbeam): investigate why this steals focus from the web contents. | 46 // TODO(dbeam): investigate why this steals focus from the web contents. |
29 views::BubbleDelegateView::CreateBubble(this); | 47 views::BubbleDelegateView::CreateBubble(this); |
30 | |
31 GetWidget()->Show(); | 48 GetWidget()->Show(); |
32 SizeToContents(); | 49 SizeToContents(); |
33 } | 50 } |
34 | 51 |
35 void AutofillCreditCardBubbleViews::Hide() { | 52 void AutofillCreditCardBubbleViews::Hide() { |
36 GetWidget()->Close(); | 53 GetWidget()->Close(); |
37 } | 54 } |
38 | 55 |
39 bool AutofillCreditCardBubbleViews::IsHiding() const { | 56 bool AutofillCreditCardBubbleViews::IsHiding() const { |
40 return GetWidget() && GetWidget()->IsClosed(); | 57 return GetWidget() && GetWidget()->IsClosed(); |
41 } | 58 } |
42 | 59 |
43 string16 AutofillCreditCardBubbleViews::GetWindowTitle() const { | 60 base::string16 AutofillCreditCardBubbleViews::GetWindowTitle() const { |
44 return controller_->BubbleTitle(); | 61 return controller_->TitleText(); |
45 } | 62 } |
46 | 63 |
47 void AutofillCreditCardBubbleViews::Init() { | 64 void AutofillCreditCardBubbleViews::Init() { |
48 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, | 65 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, |
49 views::kRelatedControlVerticalSpacing)); | 66 views::kRelatedControlVerticalSpacing)); |
50 | 67 |
51 views::StyledLabel::RangeStyleInfo bold; | 68 const base::string16& header_text = controller_->HeaderText(); |
52 bold.font_style = gfx::Font::BOLD; | 69 if (!header_text.empty()) { |
53 const std::vector<ui::Range>& ranges = controller_->BubbleTextRanges(); | 70 views::StyledLabel* header = new views::StyledLabel(header_text, this); |
71 const std::vector<TextRange>& text_ranges = controller_->HeaderTextRanges(); | |
72 for (size_t i = 0; i < text_ranges.size(); ++i) { | |
73 views::StyledLabel::RangeStyleInfo range_style; | |
74 if (text_ranges[i].is_link) | |
75 range_style = views::StyledLabel::RangeStyleInfo::CreateForLink(); | |
76 else | |
77 range_style.font_style = gfx::Font::BOLD; | |
78 header->AddStyleRange(text_ranges[i].range, range_style); | |
79 } | |
80 AddChildView(header); | |
81 } | |
54 | 82 |
55 views::StyledLabel* contents = | 83 const CreditCardDescription* card_desc = controller_->Description(); |
56 new views::StyledLabel(controller_->BubbleText(), NULL); | 84 if (card_desc) { |
57 for (size_t i = 0; i < ranges.size(); ++i) { | 85 views::View* card_container = new views::View(); |
58 contents->AddStyleRange(ranges[i], bold); | 86 card_container->SetLayoutManager( |
87 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 10)); | |
88 | |
89 views::View* title_row = new views::View(); | |
Evan Stade
2013/08/02 16:29:45
not really a title... the title is blank. Calling
Dan Beam
2013/08/06 02:41:35
Done.
| |
90 title_row->SetLayoutManager( | |
91 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 10)); | |
92 | |
93 views::ImageView* card_icon = new views::ImageView(); | |
94 card_icon->SetImage(card_desc->icon.AsImageSkia()); | |
95 title_row->AddChildView(card_icon); | |
96 | |
97 views::Label* title = new views::Label(card_desc->title); | |
Evan Stade
2013/08/02 16:29:45
ditto on CardDescription::title
Dan Beam
2013/08/06 02:41:35
Done.
| |
98 title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
99 title_row->AddChildView(title); | |
100 card_container->AddChildView(title_row); | |
101 | |
102 views::Label* desc = new views::Label(card_desc->description); | |
103 desc->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
104 desc->SetMultiLine(true); | |
105 card_container->AddChildView(desc); | |
106 | |
107 AddChildView(card_container); | |
59 } | 108 } |
60 AddChildView(contents); | |
61 | 109 |
62 views::Link* link = new views::Link(); | 110 const base::string16& link_text = controller_->LinkText(); |
63 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 111 if (!link_text.empty()) { |
64 link->SetText(controller_->LinkText()); | 112 views::Link* link = new views::Link(link_text); |
65 link->set_listener(this); | 113 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
66 AddChildView(link); | 114 link->set_listener(this); |
115 AddChildView(link); | |
116 } | |
67 } | 117 } |
68 | 118 |
69 gfx::Size AutofillCreditCardBubbleViews::GetPreferredSize() { | 119 gfx::Size AutofillCreditCardBubbleViews::GetPreferredSize() { |
70 return gfx::Size( | 120 return gfx::Size( |
71 AutofillCreditCardBubbleViews::kContentWidth, | 121 AutofillCreditCardBubbleViews::kContentWidth, |
72 GetHeightForWidth(AutofillCreditCardBubble::kContentWidth)); | 122 GetHeightForWidth(AutofillCreditCardBubble::kContentWidth)); |
73 } | 123 } |
74 | 124 |
75 void AutofillCreditCardBubbleViews::LinkClicked(views::Link* source, | 125 void AutofillCreditCardBubbleViews::LinkClicked(views::Link* source, |
76 int event_flags) { | 126 int event_flags) { |
77 if (controller_) | 127 if (controller_) |
78 controller_->OnLinkClicked(); | 128 controller_->OnLinkClicked(); |
79 } | 129 } |
80 | 130 |
131 void AutofillCreditCardBubbleViews::StyledLabelLinkClicked(const ui::Range& r, | |
132 int event_flags) { | |
133 if (controller_) | |
134 controller_->OnLinkClicked(); | |
135 } | |
136 | |
81 // static | 137 // static |
82 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubble::Create( | 138 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubble::Create( |
83 const base::WeakPtr<AutofillCreditCardBubbleController>& controller) { | 139 const base::WeakPtr<AutofillCreditCardBubbleController>& controller) { |
84 AutofillCreditCardBubbleViews* bubble = | 140 AutofillCreditCardBubbleViews* bubble = |
85 new AutofillCreditCardBubbleViews(controller); | 141 new AutofillCreditCardBubbleViews(controller); |
86 return bubble->weak_ptr_factory_.GetWeakPtr(); | 142 return bubble->weak_ptr_factory_.GetWeakPtr(); |
87 } | 143 } |
88 | 144 |
89 AutofillCreditCardBubbleViews::AutofillCreditCardBubbleViews( | 145 AutofillCreditCardBubbleViews::AutofillCreditCardBubbleViews( |
90 const base::WeakPtr<AutofillCreditCardBubbleController>& controller) | 146 const base::WeakPtr<AutofillCreditCardBubbleController>& controller) |
91 : BubbleDelegateView(BrowserView::GetBrowserViewForBrowser( | 147 : BubbleDelegateView(GetAnchor(controller), views::BubbleBorder::TOP_RIGHT), |
92 chrome::FindBrowserWithWebContents(controller->web_contents()))-> | |
93 GetLocationBarView()->autofill_credit_card_view(), | |
94 views::BubbleBorder::TOP_RIGHT), | |
95 controller_(controller), | 148 controller_(controller), |
96 weak_ptr_factory_(this) { | 149 weak_ptr_factory_(this) { |
97 // Match bookmarks bubble view's anchor view insets and margins. | 150 // Match bookmarks bubble view's margins. |
98 set_anchor_view_insets(gfx::Insets(7, 0, 7, 0)); | 151 bool title_empty = controller_->TitleText().empty(); |
Evan Stade
2013/08/02 16:29:45
yea, controller_->TitleText() vs. controller_->Des
Dan Beam
2013/08/06 02:41:35
Done.
| |
99 set_margins(gfx::Insets(0, 19, 18, 18)); | 152 set_margins(gfx::Insets(title_empty ? 18 : 0, 19, 18, 18)); |
100 set_move_with_anchor(true); | |
101 } | 153 } |
102 | 154 |
103 } // namespace autofill | 155 } // namespace autofill |
OLD | NEW |