OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/passwords/credentials_selection_view.h" |
| 6 |
| 7 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" |
| 8 #include "ui/base/models/simple_combobox_model.h" |
| 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/views/controls/button/button.h" |
| 11 #include "ui/views/controls/combobox/combobox.h" |
| 12 #include "ui/views/controls/label.h" |
| 13 #include "ui/views/layout/fill_layout.h" |
| 14 #include "ui/views/layout/grid_layout.h" |
| 15 #include "ui/views/layout/layout_constants.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 views::Label* GeneratePasswordLabel(const autofill::PasswordForm& form) { |
| 20 views::Label* label = new views::Label(form.password_value); |
| 21 label->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList( |
| 22 ui::ResourceBundle::SmallFont)); |
| 23 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 24 label->SetObscured(true); |
| 25 return label; |
| 26 } |
| 27 |
| 28 views::Combobox* GenerateUsernameCombobox( |
| 29 const std::vector<const autofill::PasswordForm*>& forms) { |
| 30 std::vector<base::string16> usernames; |
| 31 for (const auto* form : forms) |
| 32 usernames.push_back(form->username_value); |
| 33 |
| 34 views::Combobox* combobox = |
| 35 new views::Combobox(new ui::SimpleComboboxModel(usernames)); |
| 36 return combobox; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 CredentialsSelectionView::CredentialsSelectionView( |
| 42 ManagePasswordsBubbleModel* manage_passwords_bubble_model, |
| 43 const std::vector<const autofill::PasswordForm*>& password_forms) |
| 44 : password_forms_(password_forms) { |
| 45 DCHECK(!password_forms.empty()); |
| 46 |
| 47 // Layout. |
| 48 views::GridLayout* layout = new views::GridLayout(this); |
| 49 SetLayoutManager(layout); |
| 50 |
| 51 // ColumnSet. |
| 52 int column_set_id = 0; |
| 53 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id); |
| 54 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); |
| 55 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 2, |
| 56 views::GridLayout::USE_PREF, 0, 0); |
| 57 column_set->AddPaddingColumn(0, |
| 58 views::kUnrelatedControlLargeHorizontalSpacing); |
| 59 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 60 views::GridLayout::USE_PREF, 0, 0); |
| 61 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); |
| 62 |
| 63 // The username combobox and password label. |
| 64 layout->StartRowWithPadding(0, column_set_id, 0, |
| 65 views::kRelatedControlVerticalSpacing); |
| 66 combobox_ = GenerateUsernameCombobox( |
| 67 manage_passwords_bubble_model->local_credentials().get()); |
| 68 combobox_->set_listener(this); |
| 69 layout->AddView(combobox_); |
| 70 label_ = GeneratePasswordLabel(*password_forms[0]); |
| 71 layout->AddView(label_); |
| 72 |
| 73 GetLayoutManager()->Layout(this); |
| 74 } |
| 75 |
| 76 const autofill::PasswordForm* |
| 77 CredentialsSelectionView::GetSelectedCredentials() { |
| 78 DCHECK_EQ(password_forms_.size(), |
| 79 static_cast<size_t>(combobox_->model()->GetItemCount())); |
| 80 return password_forms_[combobox_->selected_index()]; |
| 81 } |
| 82 |
| 83 void CredentialsSelectionView::OnPerformAction(views::Combobox* source) { |
| 84 label_->SetText(GetSelectedCredentials()->password_value); |
| 85 } |
OLD | NEW |