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/new_credit_card_bubble_views.h" |
| 6 |
| 7 #include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h" |
| 8 #include "chrome/browser/ui/browser_finder.h" |
| 9 #include "chrome/browser/ui/host_desktop.h" |
| 10 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 11 #include "chrome/browser/ui/views/toolbar_view.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/image_view.h" |
| 16 #include "ui/views/controls/link.h" |
| 17 #include "ui/views/layout/box_layout.h" |
| 18 #include "ui/views/layout/layout_constants.h" |
| 19 #include "ui/views/view.h" |
| 20 #include "ui/views/widget/widget.h" |
| 21 |
| 22 namespace autofill { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Get the view this bubble will be anchored to via |controller|. |
| 27 views::View* GetAnchor(NewCreditCardBubbleController* controller) { |
| 28 Browser* browser = chrome::FindTabbedBrowser(controller->profile(), false, |
| 29 chrome::GetActiveDesktop()); |
| 30 if (!browser) |
| 31 return NULL; |
| 32 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 33 return browser_view->GetToolbarView()->app_menu(); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 NewCreditCardBubbleViews::~NewCreditCardBubbleViews() { |
| 39 controller_->OnBubbleDestroyed(); |
| 40 } |
| 41 |
| 42 void NewCreditCardBubbleViews::Show() { |
| 43 // TODO(dbeam): investigate why this steals focus from the web contents. |
| 44 views::BubbleDelegateView::CreateBubble(this)->Show(); |
| 45 |
| 46 // This bubble doesn't render correctly on Windows without calling |
| 47 // |SizeToContents()|. This must be called after showing the widget. |
| 48 SizeToContents(); |
| 49 } |
| 50 |
| 51 void NewCreditCardBubbleViews::Hide() { |
| 52 GetWidget()->Close(); |
| 53 } |
| 54 |
| 55 gfx::Size NewCreditCardBubbleViews::GetPreferredSize() { |
| 56 return gfx::Size( |
| 57 NewCreditCardBubbleView::kContentsWidth, |
| 58 GetHeightForWidth(NewCreditCardBubbleView::kContentsWidth)); |
| 59 } |
| 60 |
| 61 void NewCreditCardBubbleViews::Init() { |
| 62 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, |
| 63 views::kRelatedControlVerticalSpacing)); |
| 64 |
| 65 views::View* card_container = new views::View(); |
| 66 card_container->SetLayoutManager( |
| 67 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 10)); |
| 68 |
| 69 views::View* card_desc_view = new views::View(); |
| 70 card_desc_view->SetLayoutManager( |
| 71 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 10)); |
| 72 |
| 73 views::ImageView* card_icon = new views::ImageView(); |
| 74 const CreditCardDescription& card_desc = controller_->CardDescription(); |
| 75 card_icon->SetImage(card_desc.icon.AsImageSkia()); |
| 76 card_desc_view->AddChildView(card_icon); |
| 77 |
| 78 views::Label* card_name = new views::Label(card_desc.name); |
| 79 card_name->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 80 card_desc_view->AddChildView(card_name); |
| 81 card_container->AddChildView(card_desc_view); |
| 82 |
| 83 views::Label* desc = new views::Label(card_desc.description); |
| 84 desc->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 85 desc->SetMultiLine(true); |
| 86 card_container->AddChildView(desc); |
| 87 |
| 88 AddChildView(card_container); |
| 89 |
| 90 views::Link* link = new views::Link(controller_->LinkText()); |
| 91 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 92 link->set_listener(this); |
| 93 AddChildView(link); |
| 94 } |
| 95 |
| 96 base::string16 NewCreditCardBubbleViews::GetWindowTitle() const { |
| 97 return controller_->TitleText(); |
| 98 } |
| 99 |
| 100 void NewCreditCardBubbleViews::LinkClicked(views::Link* source, |
| 101 int event_flags) { |
| 102 controller_->OnLinkClicked(); |
| 103 } |
| 104 |
| 105 // static |
| 106 base::WeakPtr<NewCreditCardBubbleView> NewCreditCardBubbleView::Create( |
| 107 NewCreditCardBubbleController* controller) { |
| 108 NewCreditCardBubbleViews* bubble = new NewCreditCardBubbleViews(controller); |
| 109 return bubble->weak_ptr_factory_.GetWeakPtr(); |
| 110 } |
| 111 |
| 112 NewCreditCardBubbleViews::NewCreditCardBubbleViews( |
| 113 NewCreditCardBubbleController* controller) |
| 114 : BubbleDelegateView(GetAnchor(controller), views::BubbleBorder::TOP_RIGHT), |
| 115 controller_(controller), |
| 116 weak_ptr_factory_(this) { |
| 117 gfx::Insets insets = views::BubbleFrameView::GetTitleInsets(); |
| 118 set_margins(gfx::Insets(0, insets.left(), insets.top(), insets.left())); |
| 119 } |
| 120 |
| 121 } // namespace autofill |
OLD | NEW |