Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: chrome/browser/ui/views/autofill/password_generation_popup_view_views.cc

Issue 342833002: [Password Generation] Update Aura UI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: UI changes Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/autofill/password_generation_popup_view_views. h" 5 #include "chrome/browser/ui/views/autofill/password_generation_popup_view_views. h"
6 6
7 #include "base/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h" 8 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
9 #include "chrome/browser/ui/autofill/popup_constants.h"
10 #include "grit/theme_resources.h"
11 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
10 #include "ui/views/background.h" 13 #include "ui/views/background.h"
11 #include "ui/views/border.h" 14 #include "ui/views/border.h"
15 #include "ui/views/controls/image_view.h"
12 #include "ui/views/controls/label.h" 16 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/styled_label.h" 17 #include "ui/views/controls/styled_label.h"
14 #include "ui/views/layout/box_layout.h" 18 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/widget/widget.h" 19 #include "ui/views/widget/widget.h"
16 20
17 namespace autofill { 21 namespace autofill {
18 22
19 namespace { 23 namespace {
20 24
21 // The amount of whitespace that is present when there is no padding. Used 25 // The amount of whitespace that is present when there is no padding. Used
22 // to get the proper spacing in the help section. 26 // to get the proper spacing in the help section.
23 const int kHelpVerticalOffset = 3; 27 const int kHelpVerticalOffset = 5;
28 const int kPasswordSectionHeight = 62;
24 29
25 // Class that shows the password and the suggestion side-by-side. 30 // Wrapper around just the text portions of the generation UI (password and
26 class PasswordRow : public views::View { 31 // prompting text).
32 class PasswordTextBox : public views::View {
27 public: 33 public:
28 PasswordRow(const base::string16& password, 34 PasswordTextBox() {}
29 const base::string16& suggestion, 35 virtual ~PasswordTextBox() {}
30 const gfx::FontList& font_list, 36
31 int horizontal_border) { 37 // |suggestion_text| prompts the user to select the password,
32 set_clip_insets(gfx::Insets( 38 // |generated_password| is the generated password, and |font_list| is the font
33 PasswordGenerationPopupView::kPasswordVerticalInset, 0, 39 // used for all text in this class.
34 PasswordGenerationPopupView::kPasswordVerticalInset, 0)); 40 void Init(const base::string16& suggestion_text,
41 const base::string16& generated_password,
42 const gfx::FontList& font_list) {
35 views::BoxLayout* box_layout = new views::BoxLayout( 43 views::BoxLayout* box_layout = new views::BoxLayout(
36 views::BoxLayout::kHorizontal, horizontal_border, 0, 0); 44 views::BoxLayout::kVertical, 0, 13, 5);
37 box_layout->set_main_axis_alignment( 45 box_layout->set_main_axis_alignment(
38 views::BoxLayout::MAIN_AXIS_ALIGNMENT_FILL); 46 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
39 SetLayoutManager(box_layout); 47 SetLayoutManager(box_layout);
40 48
41 password_label_ = new views::Label(password, font_list); 49 views::Label* suggestion_label = new views::Label(
42 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 50 suggestion_text, font_list.DeriveWithStyle(gfx::Font::BOLD));
43 AddChildView(password_label_); 51 suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
52 AddChildView(suggestion_label);
44 53
45 suggestion_label_ = new views::Label(suggestion, font_list); 54 views::Label* password_label =
46 suggestion_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); 55 new views::Label(generated_password, font_list);
47 suggestion_label_->SetEnabledColor( 56 password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
48 PasswordGenerationPopupView::kExplanatoryTextColor); 57 AddChildView(password_label);
49 AddChildView(suggestion_label_);
50 } 58 }
51 virtual ~PasswordRow() {}
52 59
53 // views::View: 60 // views::View:
54 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { 61 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
55 // Send events to the parent view for handling. 62 // Send events to the parent view for handling.
56 return false; 63 return false;
57 } 64 }
58 65
59 private: 66 private:
60 // Child views. Not owned. 67 DISALLOW_COPY_AND_ASSIGN(PasswordTextBox);
61 views::Label* suggestion_label_; 68 };
62 views::Label* password_label_;
63 69
64 DISALLOW_COPY_AND_ASSIGN(PasswordRow); 70 // Class that shows the generated password and associated UI (currently a key
71 // image and some explanatory text).
72 class PasswordBox : public views::View {
73 public:
74 PasswordBox() {}
75 virtual ~PasswordBox() {}
76
77 // |password| is the generated password, |suggestion| is the text prompting
78 // the user to select the password, |font_list| is the font used for all the
79 // text, and |horizontal_border| is the horizontal whitespace between the
80 // parent element and this object.
81 void Init(const base::string16& password,
82 const base::string16& suggestion,
83 const gfx::FontList& font_list,
84 int horizontal_border) {
85 views::BoxLayout* box_layout = new views::BoxLayout(
86 views::BoxLayout::kHorizontal, horizontal_border, 0, 15);
87 box_layout->set_main_axis_alignment(
88 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
89 SetLayoutManager(box_layout);
90
91 views::ImageView* key_image = new views::ImageView();
92 key_image->SetImage(
93 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
94 IDR_GENERATE_PASSWORD_KEY).ToImageSkia());
95 AddChildView(key_image);
96
97 PasswordTextBox* password_text_box = new PasswordTextBox();
98 password_text_box->Init(suggestion, password, font_list);
99 AddChildView(password_text_box);
100 }
101
102 // views::View:
103 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
104 // Send events to the parent view for handling.
105 return false;
106 }
107
108 private:
109 DISALLOW_COPY_AND_ASSIGN(PasswordBox);
65 }; 110 };
66 111
67 } // namespace 112 } // namespace
68 113
69 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( 114 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews(
70 PasswordGenerationPopupController* controller, 115 PasswordGenerationPopupController* controller,
71 views::Widget* observing_widget) 116 views::Widget* observing_widget)
72 : AutofillPopupBaseView(controller, observing_widget), 117 : AutofillPopupBaseView(controller, observing_widget),
73 password_view_(NULL), 118 password_view_(NULL),
74 controller_(controller) { 119 controller_(controller) {
75 if (controller_->display_password()) 120 if (controller_->display_password())
76 CreatePasswordView(); 121 CreatePasswordView();
77 122
78 help_label_ = new views::StyledLabel(controller_->HelpText(), this); 123 help_label_ = new views::StyledLabel(controller_->HelpText(), this);
79 help_label_->SetBaseFontList(controller_->font_list()); 124 help_label_->SetBaseFontList(controller_->font_list());
125 help_label_->SetInterlineSpacing(3);
80 views::StyledLabel::RangeStyleInfo default_style; 126 views::StyledLabel::RangeStyleInfo default_style;
81 default_style.color = kExplanatoryTextColor; 127 default_style.color = kExplanatoryTextColor;
82 help_label_->SetDefaultStyle(default_style); 128 help_label_->SetDefaultStyle(default_style);
83 129
84 views::StyledLabel::RangeStyleInfo link_style = 130 help_label_->AddStyleRange(
85 views::StyledLabel::RangeStyleInfo::CreateForLink(); 131 controller_->HelpTextLinkRange(),
86 link_style.color = kLinkColor; 132 views::StyledLabel::RangeStyleInfo::CreateForLink());
87 help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style);
88 133
89 help_label_->SetBoundsRect(controller_->help_bounds());
90 help_label_->set_background( 134 help_label_->set_background(
91 views::Background::CreateSolidBackground( 135 views::Background::CreateSolidBackground(
92 kExplanatoryTextBackgroundColor)); 136 kExplanatoryTextBackgroundColor));
93 help_label_->SetBorder(views::Border::CreateEmptyBorder( 137 help_label_->SetBorder(views::Border::CreateEmptyBorder(
94 controller_->kHelpVerticalPadding - kHelpVerticalOffset, 138 controller_->kHelpVerticalPadding - kHelpVerticalOffset,
95 controller_->kHorizontalPadding, 139 controller_->kHorizontalPadding,
96 0, 140 0,
97 controller_->kHorizontalPadding)); 141 controller_->kHorizontalPadding));
98 AddChildView(help_label_); 142 AddChildView(help_label_);
99
100 set_background(views::Background::CreateSolidBackground(kPopupBackground));
101 } 143 }
102 144
103 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} 145 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {}
104 146
105 void PasswordGenerationPopupViewViews::CreatePasswordView() { 147 void PasswordGenerationPopupViewViews::CreatePasswordView() {
106 if (password_view_) 148 if (password_view_)
107 return; 149 return;
108 150
109 password_view_ = new PasswordRow(controller_->password(), 151 password_view_ = new PasswordBox();
110 controller_->SuggestedText(), 152 static_cast<PasswordBox*>(password_view_)->Init(
111 controller_->font_list(), 153 controller_->password(),
112 controller_->kHorizontalPadding); 154 controller_->SuggestedText(),
155 controller_->font_list(),
156 controller_->kHorizontalPadding);
157 password_view_->SetPosition(gfx::Point(kPopupBorderThickness,
158 kPopupBorderThickness));
159 password_view_->SizeToPreferredSize();
113 AddChildView(password_view_); 160 AddChildView(password_view_);
114 } 161 }
115 162
163 int PasswordGenerationPopupViewViews::SetBoundsForWidth(int width) {
164 // Need to leave room for the border.
165 int y = kPopupBorderThickness;
166 int popup_width = width - 2 * kPopupBorderThickness;
167 if (controller_->display_password()) {
168 // Currently the UI can change from not offering a password to offering
169 // a password (e.g. the user is editing a generated password and deletes
170 // it), but it can't change the other way around.
171 CreatePasswordView();
172 password_view_->SetBounds(
173 kPopupBorderThickness, y, popup_width, kPasswordSectionHeight);
174 divider_bounds_ =
175 gfx::Rect(kPopupBorderThickness, password_view_->bounds().bottom(),
176 popup_width, 1);
177 y = divider_bounds_.bottom();
178 }
179 help_label_->SetBounds(kPopupBorderThickness, y, popup_width,
180 help_label_->GetHeightForWidth(popup_width) +
181 help_label_->GetInsets().height());
182 return help_label_->bounds().bottom() + kPopupBorderThickness;
183 }
184
116 void PasswordGenerationPopupViewViews::Show() { 185 void PasswordGenerationPopupViewViews::Show() {
117 DoShow(); 186 DoShow();
118 } 187 }
119 188
120 void PasswordGenerationPopupViewViews::Hide() { 189 void PasswordGenerationPopupViewViews::Hide() {
121 // The controller is no longer valid after it hides us. 190 // The controller is no longer valid after it hides us.
122 controller_ = NULL; 191 controller_ = NULL;
123 192
124 DoHide(); 193 DoHide();
125 } 194 }
126 195
127 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { 196 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() {
128 // Currently the UI can change from not offering a password to offering
129 // a password (e.g. the user is editing a generated password and deletes it),
130 // but it can't change the other way around.
131 if (controller_->display_password())
132 CreatePasswordView();
133
134 DoUpdateBoundsAndRedrawPopup(); 197 DoUpdateBoundsAndRedrawPopup();
135 } 198 }
136 199
137 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { 200 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() {
138 if (!password_view_) 201 if (!password_view_)
139 return; 202 return;
140 203
141 password_view_->set_background( 204 password_view_->set_background(
142 views::Background::CreateSolidBackground( 205 views::Background::CreateSolidBackground(
143 controller_->password_selected() ? 206 controller_->password_selected() ?
144 kHoveredBackgroundColor : 207 kHoveredBackgroundColor :
145 kPopupBackground)); 208 kPopupBackground));
146 } 209 }
147 210
148 void PasswordGenerationPopupViewViews::Layout() { 211 void PasswordGenerationPopupViewViews::Layout() {
149 if (password_view_) 212 SetBoundsForWidth(controller_->popup_bounds().width());
150 password_view_->SetBoundsRect(controller_->password_bounds());
151
152 help_label_->SetBoundsRect(controller_->help_bounds());
153 } 213 }
154 214
155 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { 215 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) {
156 if (!controller_) 216 if (!controller_)
157 return; 217 return;
158 218
159 // Draw border and background. 219 // Draw border and background.
160 views::View::OnPaint(canvas); 220 views::View::OnPaint(canvas);
161 221
162 // Divider line needs to be drawn after OnPaint() otherwise the background 222 // Divider line needs to be drawn after OnPaint() otherwise the background
163 // will overwrite the divider. 223 // will overwrite the divider.
164 if (password_view_) 224 if (password_view_)
165 canvas->FillRect(controller_->divider_bounds(), kDividerColor); 225 canvas->FillRect(divider_bounds_, kDividerColor);
166 } 226 }
167 227
168 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( 228 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked(
169 const gfx::Range& range, int event_flags) { 229 const gfx::Range& range, int event_flags) {
170 controller_->OnSavedPasswordsLinkClicked(); 230 controller_->OnSavedPasswordsLinkClicked();
171 } 231 }
172 232
233 bool PasswordGenerationPopupViewViews::IsPointInPasswordBounds(
234 const gfx::Point& point) {
235 return password_view_->bounds().Contains(point);
236 }
237
173 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( 238 PasswordGenerationPopupView* PasswordGenerationPopupView::Create(
174 PasswordGenerationPopupController* controller) { 239 PasswordGenerationPopupController* controller) {
175 views::Widget* observing_widget = 240 views::Widget* observing_widget =
176 views::Widget::GetTopLevelWidgetForNativeView( 241 views::Widget::GetTopLevelWidgetForNativeView(
177 controller->container_view()); 242 controller->container_view());
178 243
179 // If the top level widget can't be found, cancel the popup since we can't 244 // If the top level widget can't be found, cancel the popup since we can't
180 // fully set it up. 245 // fully set it up.
181 if (!observing_widget) 246 if (!observing_widget)
182 return NULL; 247 return NULL;
183 248
184 return new PasswordGenerationPopupViewViews(controller, observing_widget); 249 return new PasswordGenerationPopupViewViews(controller, observing_widget);
185 } 250 }
186 251
187 } // namespace autofill 252 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698