OLD | NEW |
---|---|
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, | |
43 const SkColor& text_color) { | |
35 views::BoxLayout* box_layout = new views::BoxLayout( | 44 views::BoxLayout* box_layout = new views::BoxLayout( |
36 views::BoxLayout::kHorizontal, horizontal_border, 0, 0); | 45 views::BoxLayout::kVertical, 0, 13, 5); |
37 box_layout->set_main_axis_alignment( | 46 box_layout->set_main_axis_alignment( |
38 views::BoxLayout::MAIN_AXIS_ALIGNMENT_FILL); | 47 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
39 SetLayoutManager(box_layout); | 48 SetLayoutManager(box_layout); |
40 | 49 |
41 password_label_ = new views::Label(password, font_list); | 50 views::Label* suggestion_label = new views::Label( |
42 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 51 suggestion_text, font_list.DeriveWithStyle(gfx::Font::BOLD)); |
43 AddChildView(password_label_); | 52 suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
53 suggestion_label->SetEnabledColor(text_color); | |
54 AddChildView(suggestion_label); | |
44 | 55 |
45 suggestion_label_ = new views::Label(suggestion, font_list); | 56 views::Label* password_label = |
46 suggestion_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 57 new views::Label(generated_password, font_list); |
47 suggestion_label_->SetEnabledColor( | 58 password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
48 PasswordGenerationPopupView::kExplanatoryTextColor); | 59 password_label->SetEnabledColor(text_color); |
49 AddChildView(suggestion_label_); | 60 AddChildView(password_label); |
50 } | 61 } |
51 virtual ~PasswordRow() {} | |
52 | 62 |
53 // views::View: | 63 // views::View: |
54 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { | 64 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { |
55 // Send events to the parent view for handling. | 65 // Send events to the parent view for handling. |
56 return false; | 66 return false; |
57 } | 67 } |
58 | 68 |
59 private: | 69 private: |
60 // Child views. Not owned. | 70 DISALLOW_COPY_AND_ASSIGN(PasswordTextBox); |
61 views::Label* suggestion_label_; | 71 }; |
62 views::Label* password_label_; | |
63 | 72 |
64 DISALLOW_COPY_AND_ASSIGN(PasswordRow); | 73 // Class that shows the generated password and associated UI (currently a key |
74 // image and some explanatory text). | |
75 class PasswordBox : public views::View { | |
76 public: | |
77 PasswordBox() {} | |
78 virtual ~PasswordBox() {} | |
79 | |
80 // |password| is the generated password, |suggestion| is the text prompting | |
81 // the user to select the password, |font_list| is the font used for all the | |
82 // text, and |horizontal_border| is the horizontal whitespace between the | |
83 // parent element and this object. | |
84 void Init(const base::string16& password, | |
85 const base::string16& suggestion, | |
86 const gfx::FontList& font_list, | |
87 const SkColor& text_color, | |
88 int horizontal_border) { | |
89 views::BoxLayout* box_layout = new views::BoxLayout( | |
90 views::BoxLayout::kHorizontal, horizontal_border, 0, 15); | |
91 box_layout->set_main_axis_alignment( | |
92 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | |
93 SetLayoutManager(box_layout); | |
94 | |
95 views::ImageView* key_image = new views::ImageView(); | |
96 key_image->SetImage( | |
97 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
98 IDR_GENERATE_PASSWORD_KEY).ToImageSkia()); | |
99 AddChildView(key_image); | |
100 | |
101 PasswordTextBox* password_text_box = new PasswordTextBox(); | |
102 password_text_box->Init(suggestion, password, font_list, text_color); | |
103 AddChildView(password_text_box); | |
104 } | |
105 | |
106 // views::View: | |
107 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { | |
108 // Send events to the parent view for handling. | |
109 return false; | |
110 } | |
111 | |
112 private: | |
113 DISALLOW_COPY_AND_ASSIGN(PasswordBox); | |
65 }; | 114 }; |
66 | 115 |
67 } // namespace | 116 } // namespace |
68 | 117 |
69 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( | 118 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( |
70 PasswordGenerationPopupController* controller, | 119 PasswordGenerationPopupController* controller, |
71 views::Widget* observing_widget) | 120 views::Widget* observing_widget) |
72 : AutofillPopupBaseView(controller, observing_widget), | 121 : AutofillPopupBaseView(controller, observing_widget), |
73 password_view_(NULL), | 122 password_view_(NULL), |
123 font_list_(ResourceBundle::GetSharedInstance().GetFontList( | |
124 ResourceBundle::SmallFont)), | |
74 controller_(controller) { | 125 controller_(controller) { |
75 if (controller_->display_password()) | 126 if (controller_->display_password()) |
76 CreatePasswordView(); | 127 CreatePasswordView(); |
77 | 128 |
78 help_label_ = new views::StyledLabel(controller_->HelpText(), this); | 129 help_label_ = new views::StyledLabel(controller_->HelpText(), this); |
79 help_label_->SetBaseFontList(controller_->font_list()); | 130 help_label_->SetBaseFontList(font_list_); |
131 help_label_->SetLineHeightMultiplier(1.15); | |
80 views::StyledLabel::RangeStyleInfo default_style; | 132 views::StyledLabel::RangeStyleInfo default_style; |
81 default_style.color = kExplanatoryTextColor; | 133 default_style.color = kExplanatoryTextColor; |
82 help_label_->SetDefaultStyle(default_style); | 134 help_label_->SetDefaultStyle(default_style); |
83 | 135 |
84 views::StyledLabel::RangeStyleInfo link_style = | 136 help_label_->AddStyleRange( |
85 views::StyledLabel::RangeStyleInfo::CreateForLink(); | 137 controller_->HelpTextLinkRange(), |
86 link_style.color = kLinkColor; | 138 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
87 help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style); | |
88 | 139 |
89 help_label_->SetBoundsRect(controller_->help_bounds()); | |
90 help_label_->set_background( | 140 help_label_->set_background( |
91 views::Background::CreateSolidBackground( | 141 views::Background::CreateSolidBackground( |
92 kExplanatoryTextBackgroundColor)); | 142 kExplanatoryTextBackgroundColor)); |
93 help_label_->SetBorder(views::Border::CreateEmptyBorder( | 143 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
94 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 144 controller_->kHelpVerticalPadding - kHelpVerticalOffset, |
95 controller_->kHorizontalPadding, | 145 controller_->kHorizontalPadding, |
96 0, | 146 0, |
97 controller_->kHorizontalPadding)); | 147 controller_->kHorizontalPadding)); |
98 AddChildView(help_label_); | 148 AddChildView(help_label_); |
99 | 149 |
100 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 150 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
101 } | 151 } |
102 | 152 |
103 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 153 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
104 | 154 |
105 void PasswordGenerationPopupViewViews::CreatePasswordView() { | 155 void PasswordGenerationPopupViewViews::CreatePasswordView() { |
106 if (password_view_) | 156 if (password_view_) |
107 return; | 157 return; |
108 | 158 |
109 password_view_ = new PasswordRow(controller_->password(), | 159 password_view_ = new PasswordBox(); |
110 controller_->SuggestedText(), | 160 static_cast<PasswordBox*>(password_view_)->Init( |
Evan Stade
2014/06/26 00:06:04
nit: prefer avoiding this cast. Perhaps Init() can
Garrett Casto
2014/06/26 01:48:53
The problem is that password_view_ is currently ju
Evan Stade
2014/06/26 03:28:08
I understand that, but you could do:
PasswordBox*
Garrett Casto
2014/07/14 06:18:39
Ah, misunderstood your second suggestion. Thanks f
| |
111 controller_->font_list(), | 161 controller_->password(), |
112 controller_->kHorizontalPadding); | 162 controller_->SuggestedText(), |
163 font_list_, | |
164 kItemTextColor, | |
Evan Stade
2014/06/26 00:06:04
Since this is a constant and PasswordBox is only u
Garrett Casto
2014/06/26 01:48:53
Done, though I had to make kItemTextColor (and fri
Evan Stade
2014/06/26 03:28:08
ah, I see
| |
165 controller_->kHorizontalPadding); | |
Evan Stade
2014/06/26 00:06:04
nit: kind of weird to refer to a static via an ins
Garrett Casto
2014/06/26 01:48:53
Done.
| |
166 password_view_->SetPosition(gfx::Point(kPopupBorderThickness, | |
167 kPopupBorderThickness)); | |
168 password_view_->SizeToPreferredSize(); | |
113 AddChildView(password_view_); | 169 AddChildView(password_view_); |
114 } | 170 } |
115 | 171 |
172 gfx::Size PasswordGenerationPopupViewViews::GetBounds() { | |
Evan Stade
2014/06/26 00:06:04
hmm, from this angle, this method name is a little
Garrett Casto
2014/06/26 01:48:53
Done.
| |
173 int height = kPopupBorderThickness; | |
174 if (controller_->display_password()) { | |
175 // Add divider height as well. | |
176 height += kPasswordSectionHeight + 1; | |
177 } | |
178 int width = controller_->GetMinimumWidth(); | |
179 int popup_width = width - 2 * kPopupBorderThickness; | |
180 height += help_label_->GetHeightForWidth(popup_width) + | |
181 help_label_->GetInsets().height(); | |
182 return gfx::Size(width, height + 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 | 197 |
134 DoUpdateBoundsAndRedrawPopup(); | 198 DoUpdateBoundsAndRedrawPopup(); |
135 } | 199 } |
136 | 200 |
137 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { | 201 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { |
138 if (!password_view_) | 202 if (!password_view_) |
139 return; | 203 return; |
140 | 204 |
141 password_view_->set_background( | 205 password_view_->set_background( |
142 views::Background::CreateSolidBackground( | 206 views::Background::CreateSolidBackground( |
143 controller_->password_selected() ? | 207 controller_->password_selected() ? |
144 kHoveredBackgroundColor : | 208 kHoveredBackgroundColor : |
145 kPopupBackground)); | 209 kPopupBackground)); |
146 } | 210 } |
147 | 211 |
148 void PasswordGenerationPopupViewViews::Layout() { | 212 void PasswordGenerationPopupViewViews::Layout() { |
149 if (password_view_) | 213 // Need to leave room for the border. |
150 password_view_->SetBoundsRect(controller_->password_bounds()); | 214 int y = kPopupBorderThickness; |
151 | 215 int popup_width = bounds().width() - 2 * kPopupBorderThickness; |
152 help_label_->SetBoundsRect(controller_->help_bounds()); | 216 if (controller_->display_password()) { |
217 // Currently the UI can change from not offering a password to offering | |
218 // a password (e.g. the user is editing a generated password and deletes | |
219 // it), but it can't change the other way around. | |
220 CreatePasswordView(); | |
221 password_view_->SetBounds( | |
222 kPopupBorderThickness, y, popup_width, kPasswordSectionHeight); | |
223 divider_bounds_ = | |
224 gfx::Rect(kPopupBorderThickness, password_view_->bounds().bottom(), | |
225 popup_width, 1); | |
226 y = divider_bounds_.bottom(); | |
227 } | |
Evan Stade
2014/06/26 00:06:04
nit: \n
Garrett Casto
2014/06/26 01:48:53
Done.
| |
228 help_label_->SetBounds(kPopupBorderThickness, y, popup_width, | |
229 help_label_->GetHeightForWidth(popup_width) + | |
230 help_label_->GetInsets().height()); | |
153 } | 231 } |
154 | 232 |
155 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 233 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
156 if (!controller_) | 234 if (!controller_) |
157 return; | 235 return; |
158 | 236 |
159 // Draw border and background. | 237 // Draw border and background. |
160 views::View::OnPaint(canvas); | 238 views::View::OnPaint(canvas); |
161 | 239 |
162 // Divider line needs to be drawn after OnPaint() otherwise the background | 240 // Divider line needs to be drawn after OnPaint() otherwise the background |
163 // will overwrite the divider. | 241 // will overwrite the divider. |
164 if (password_view_) | 242 if (password_view_) |
165 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | 243 canvas->FillRect(divider_bounds_, kDividerColor); |
166 } | 244 } |
167 | 245 |
168 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( | 246 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( |
169 const gfx::Range& range, int event_flags) { | 247 const gfx::Range& range, int event_flags) { |
170 controller_->OnSavedPasswordsLinkClicked(); | 248 controller_->OnSavedPasswordsLinkClicked(); |
171 } | 249 } |
172 | 250 |
251 bool PasswordGenerationPopupViewViews::IsPointInPasswordBounds( | |
252 const gfx::Point& point) { | |
253 return password_view_->bounds().Contains(point); | |
254 } | |
255 | |
173 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( | 256 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( |
174 PasswordGenerationPopupController* controller) { | 257 PasswordGenerationPopupController* controller) { |
175 views::Widget* observing_widget = | 258 views::Widget* observing_widget = |
176 views::Widget::GetTopLevelWidgetForNativeView( | 259 views::Widget::GetTopLevelWidgetForNativeView( |
177 controller->container_view()); | 260 controller->container_view()); |
178 | 261 |
179 // If the top level widget can't be found, cancel the popup since we can't | 262 // If the top level widget can't be found, cancel the popup since we can't |
180 // fully set it up. | 263 // fully set it up. |
181 if (!observing_widget) | 264 if (!observing_widget) |
182 return NULL; | 265 return NULL; |
183 | 266 |
184 return new PasswordGenerationPopupViewViews(controller, observing_widget); | 267 return new PasswordGenerationPopupViewViews(controller, observing_widget); |
185 } | 268 } |
186 | 269 |
187 } // namespace autofill | 270 } // namespace autofill |
OLD | NEW |