Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / passwords / manage_password_item_view.cc
blob92e7ad63fe5622e338721cce83688369a9859a1b
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.
5 #include "chrome/browser/ui/views/passwords/manage_password_item_view.h"
7 #include "grit/generated_resources.h"
8 #include "grit/ui_resources.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/views/controls/button/button.h"
11 #include "ui/views/controls/button/label_button.h"
12 #include "ui/views/layout/grid_layout.h"
13 #include "ui/views/layout/layout_constants.h"
15 ManagePasswordItemView::ManagePasswordItemView(
16 ManagePasswordsBubbleModel* manage_passwords_bubble_model,
17 autofill::PasswordForm password_form,
18 int field_1_width,
19 int field_2_width)
20 : manage_passwords_bubble_model_(manage_passwords_bubble_model),
21 password_form_(password_form),
22 delete_password_(false),
23 field_1_width_(field_1_width),
24 field_2_width_(field_2_width) {
25 views::GridLayout* layout = new views::GridLayout(this);
26 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
27 SetLayoutManager(layout);
29 const int column_set_save_id = 0;
30 views::ColumnSet* column_set_save = layout->AddColumnSet(column_set_save_id);
31 column_set_save->AddPaddingColumn(0, views::kItemLabelSpacing);
32 column_set_save->AddColumn(
33 views::GridLayout::FILL, views::GridLayout::FILL, 0,
34 views::GridLayout::FIXED, field_1_width_, field_1_width_);
35 column_set_save->AddPaddingColumn(0, views::kItemLabelSpacing);
36 column_set_save->AddColumn(
37 views::GridLayout::FILL, views::GridLayout::FILL, 1,
38 views::GridLayout::USE_PREF, field_2_width_, field_2_width_);
39 column_set_save->AddPaddingColumn(0, views::kItemLabelSpacing);
41 const int column_set_manage_id = 1;
42 views::ColumnSet* column_set_manage =
43 layout->AddColumnSet(column_set_manage_id);
44 column_set_manage->AddPaddingColumn(0, views::kItemLabelSpacing);
45 column_set_manage->AddColumn(
46 views::GridLayout::FILL, views::GridLayout::FILL, 0,
47 views::GridLayout::FIXED, field_1_width_, field_1_width_);
48 column_set_manage->AddPaddingColumn(0, views::kItemLabelSpacing);
49 column_set_manage->AddColumn(
50 views::GridLayout::FILL, views::GridLayout::FILL, 1,
51 views::GridLayout::USE_PREF, field_2_width_, field_2_width_);
52 column_set_manage->AddColumn(views::GridLayout::TRAILING,
53 views::GridLayout::FILL, 0, views::GridLayout::USE_PREF, 0, 0);
55 if (manage_passwords_bubble_model_->manage_passwords_bubble_state() !=
56 ManagePasswordsBubbleModel::PASSWORD_TO_BE_SAVED)
57 layout->StartRow(0, column_set_manage_id);
58 else
59 layout->StartRow(0, column_set_save_id);
61 label_1_ = new views::Label(password_form_.username_value);
62 label_1_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
64 label_2_ =
65 new views::Link(GetPasswordDisplayString(password_form_.password_value));
66 label_2_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
67 label_2_->set_listener(this);
68 label_2_->SetFocusable(false);
69 label_2_->SetEnabled(false);
70 label_2_->SetUnderline(false);
72 delete_button_ = new views::LabelButton(this, base::string16());
73 delete_button_->SetStyle(views::Button::STYLE_TEXTBUTTON);
74 delete_button_->SetImage(views::Button::STATE_NORMAL,
75 *rb->GetImageSkiaNamed(IDR_CLOSE_2));
76 const int delete_button_height = delete_button_->GetPreferredSize().height();
78 layout->AddView(label_1_, 1, 1,
79 views::GridLayout::FILL, views::GridLayout::FILL,
80 -1, delete_button_height);
81 layout->AddView(label_2_, 1, 1,
82 views::GridLayout::FILL, views::GridLayout::FILL,
83 -1, delete_button_height);
85 if (manage_passwords_bubble_model_->manage_passwords_bubble_state() !=
86 ManagePasswordsBubbleModel::PASSWORD_TO_BE_SAVED) {
87 layout->AddView(delete_button_, 1, 1,
88 views::GridLayout::FILL, views::GridLayout::FILL,
89 -1, delete_button_height);
93 // static
94 base::string16 ManagePasswordItemView::GetPasswordDisplayString(
95 const base::string16& password) {
96 const wchar_t kPasswordBullet = 0x2022;
97 const size_t kMaxPasswordChar = 22;
98 return base::string16(std::min(password.length(), kMaxPasswordChar),
99 kPasswordBullet);
102 ManagePasswordItemView::~ManagePasswordItemView() {
103 if (delete_password_)
104 manage_passwords_bubble_model_->DeleteFromBestMatches(password_form_);
107 void ManagePasswordItemView::Refresh() {
108 if (delete_password_) {
109 label_1_->SetText(l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_DELETED));
110 label_2_->SetText(l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_UNDO));
111 label_2_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
112 label_2_->SetEnabled(true);
113 delete_button_->SetVisible(false);
114 manage_passwords_bubble_model_->OnPasswordAction(password_form_, true);
115 } else {
116 label_1_->SetText(password_form_.username_value);
117 label_2_->SetText(GetPasswordDisplayString(password_form_.password_value));
118 label_2_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
119 label_2_->SetEnabled(false);
120 delete_button_->SetVisible(true);
121 manage_passwords_bubble_model_->OnPasswordAction(password_form_, false);
125 void ManagePasswordItemView::ButtonPressed(views::Button* sender,
126 const ui::Event& event) {
127 DCHECK_EQ(delete_button_, sender);
128 delete_password_ = true;
129 Refresh();
132 void ManagePasswordItemView::LinkClicked(views::Link* source,
133 int event_flags) {
134 DCHECK_EQ(source, label_2_);
135 delete_password_ = false;
136 Refresh();