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/generated_credit_card_bubble_views.h" |
| 6 |
| 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/insets.h" |
| 13 #include "ui/gfx/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" |
| 19 |
| 20 namespace autofill { |
| 21 |
| 22 namespace { |
| 23 |
| 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(); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 GeneratedCreditCardBubbleViews::~GeneratedCreditCardBubbleViews() {} |
| 35 |
| 36 void GeneratedCreditCardBubbleViews::Show() { |
| 37 // TODO(dbeam): investigate why this steals focus from the web contents. |
| 38 views::BubbleDelegateView::CreateBubble(this)->Show(); |
| 39 |
| 40 // This bubble doesn't render correctly on Windows without calling |
| 41 // |SizeToContents()|. This must be called after showing the widget. |
| 42 SizeToContents(); |
| 43 } |
| 44 |
| 45 void GeneratedCreditCardBubbleViews::Hide() { |
| 46 GetWidget()->Close(); |
| 47 } |
| 48 |
| 49 bool GeneratedCreditCardBubbleViews::IsHiding() const { |
| 50 return GetWidget() && GetWidget()->IsClosed(); |
| 51 } |
| 52 |
| 53 gfx::Size GeneratedCreditCardBubbleViews::GetPreferredSize() { |
| 54 return gfx::Size( |
| 55 GeneratedCreditCardBubbleView::kContentsWidth, |
| 56 GetHeightForWidth(GeneratedCreditCardBubbleViews::kContentsWidth)); |
| 57 } |
| 58 |
| 59 base::string16 GeneratedCreditCardBubbleViews::GetWindowTitle() const { |
| 60 return controller_ ? controller_->TitleText() : base::string16(); |
| 61 } |
| 62 |
| 63 void GeneratedCreditCardBubbleViews::Init() { |
| 64 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, |
| 65 views::kRelatedControlVerticalSpacing)); |
| 66 |
| 67 const base::string16& contents_text = controller_->ContentsText(); |
| 68 views::StyledLabel* contents = new views::StyledLabel(contents_text, this); |
| 69 |
| 70 const std::vector<TextRange>& text_ranges = controller_->ContentsTextRanges(); |
| 71 for (size_t i = 0; i < text_ranges.size(); ++i) { |
| 72 views::StyledLabel::RangeStyleInfo style; |
| 73 |
| 74 if (text_ranges[i].is_link) |
| 75 style = views::StyledLabel::RangeStyleInfo::CreateForLink(); |
| 76 else |
| 77 style.font_style = gfx::Font::BOLD; |
| 78 |
| 79 contents->AddStyleRange(text_ranges[i].range, style); |
| 80 } |
| 81 |
| 82 AddChildView(contents); |
| 83 } |
| 84 |
| 85 void GeneratedCreditCardBubbleViews::StyledLabelLinkClicked(const ui::Range& r, |
| 86 int event_flags) { |
| 87 if (controller_) |
| 88 controller_->OnLinkClicked(); |
| 89 } |
| 90 |
| 91 // static |
| 92 base::WeakPtr<GeneratedCreditCardBubbleView> |
| 93 GeneratedCreditCardBubbleView::Create( |
| 94 const base::WeakPtr<GeneratedCreditCardBubbleController>& controller) { |
| 95 return (new GeneratedCreditCardBubbleViews(controller))->weak_ptr_factory_. |
| 96 GetWeakPtr(); |
| 97 } |
| 98 |
| 99 GeneratedCreditCardBubbleViews::GeneratedCreditCardBubbleViews( |
| 100 const base::WeakPtr<GeneratedCreditCardBubbleController>& controller) |
| 101 : BubbleDelegateView(GetAnchor(controller), views::BubbleBorder::TOP_RIGHT), |
| 102 controller_(controller), |
| 103 weak_ptr_factory_(this) { |
| 104 gfx::Insets insets = views::BubbleFrameView::GetTitleInsets(); |
| 105 set_margins(gfx::Insets(0, insets.left(), insets.top(), insets.left())); |
| 106 } |
| 107 |
| 108 } // namespace autofill |
OLD | NEW |