| 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/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" | |
| 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 views::BubbleDelegateView::CreateBubble(this)->Show(); | |
| 38 | |
| 39 // This bubble doesn't render correctly on Windows without calling | |
| 40 // |SizeToContents()|. This must be called after showing the widget. | |
| 41 SizeToContents(); | |
| 42 } | |
| 43 | |
| 44 void GeneratedCreditCardBubbleViews::Hide() { | |
| 45 GetWidget()->Close(); | |
| 46 } | |
| 47 | |
| 48 bool GeneratedCreditCardBubbleViews::IsHiding() const { | |
| 49 return GetWidget() && GetWidget()->IsClosed(); | |
| 50 } | |
| 51 | |
| 52 gfx::Size GeneratedCreditCardBubbleViews::GetPreferredSize() const { | |
| 53 return gfx::Size( | |
| 54 GeneratedCreditCardBubbleView::kContentsWidth, | |
| 55 GetHeightForWidth(GeneratedCreditCardBubbleViews::kContentsWidth)); | |
| 56 } | |
| 57 | |
| 58 base::string16 GeneratedCreditCardBubbleViews::GetWindowTitle() const { | |
| 59 return controller_ ? controller_->TitleText() : base::string16(); | |
| 60 } | |
| 61 | |
| 62 void GeneratedCreditCardBubbleViews::Init() { | |
| 63 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, | |
| 64 views::kRelatedControlVerticalSpacing)); | |
| 65 | |
| 66 const base::string16& contents_text = controller_->ContentsText(); | |
| 67 views::StyledLabel* contents = new views::StyledLabel(contents_text, this); | |
| 68 | |
| 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; | |
| 72 | |
| 73 if (text_ranges[i].is_link) | |
| 74 style = views::StyledLabel::RangeStyleInfo::CreateForLink(); | |
| 75 else | |
| 76 style.font_style = gfx::Font::BOLD; | |
| 77 | |
| 78 contents->AddStyleRange(text_ranges[i].range, style); | |
| 79 } | |
| 80 | |
| 81 AddChildView(contents); | |
| 82 } | |
| 83 | |
| 84 void GeneratedCreditCardBubbleViews::StyledLabelLinkClicked(const gfx::Range& r, | |
| 85 int event_flags) { | |
| 86 if (controller_) | |
| 87 controller_->OnLinkClicked(); | |
| 88 } | |
| 89 | |
| 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(); | |
| 96 } | |
| 97 | |
| 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())); | |
| 105 } | |
| 106 | |
| 107 } // namespace autofill | |
| OLD | NEW |