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) { |
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, 10, 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 suggestion_label->SetEnabledColor( |
| 53 PasswordGenerationPopupViewViews::kItemTextColor); |
| 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( |
49 AddChildView(suggestion_label_); | 60 PasswordGenerationPopupViewViews::kItemTextColor); |
| 61 AddChildView(password_label); |
50 } | 62 } |
51 virtual ~PasswordRow() {} | |
52 | 63 |
53 // views::View: | 64 // views::View: |
54 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { | 65 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { |
55 // Send events to the parent view for handling. | 66 // Send events to the parent view for handling. |
56 return false; | 67 return false; |
57 } | 68 } |
58 | 69 |
59 private: | 70 private: |
60 // Child views. Not owned. | 71 DISALLOW_COPY_AND_ASSIGN(PasswordTextBox); |
61 views::Label* suggestion_label_; | |
62 views::Label* password_label_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(PasswordRow); | |
65 }; | 72 }; |
66 | 73 |
67 } // namespace | 74 } // namespace |
68 | 75 |
| 76 // Class that shows the generated password and associated UI (currently a key |
| 77 // image and some explanatory text). |
| 78 class PasswordGenerationPopupViewViews::PasswordBox : public views::View { |
| 79 public: |
| 80 PasswordBox() {} |
| 81 virtual ~PasswordBox() {} |
| 82 |
| 83 // |password| is the generated password, |suggestion| is the text prompting |
| 84 // the user to select the password, and |font_list| is the font used for all |
| 85 // the text. |
| 86 void Init(const base::string16& password, |
| 87 const base::string16& suggestion, |
| 88 const gfx::FontList& font_list) { |
| 89 views::BoxLayout* box_layout = new views::BoxLayout( |
| 90 views::BoxLayout::kHorizontal, |
| 91 PasswordGenerationPopupController::kHorizontalPadding, |
| 92 0, |
| 93 15); |
| 94 box_layout->set_main_axis_alignment( |
| 95 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 96 SetLayoutManager(box_layout); |
| 97 |
| 98 views::ImageView* key_image = new views::ImageView(); |
| 99 key_image->SetImage( |
| 100 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 101 IDR_GENERATE_PASSWORD_KEY).ToImageSkia()); |
| 102 AddChildView(key_image); |
| 103 |
| 104 PasswordTextBox* password_text_box = new PasswordTextBox(); |
| 105 password_text_box->Init(suggestion, password, font_list); |
| 106 AddChildView(password_text_box); |
| 107 } |
| 108 |
| 109 // views::View: |
| 110 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE { |
| 111 // Send events to the parent view for handling. |
| 112 return false; |
| 113 } |
| 114 |
| 115 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(PasswordBox); |
| 117 }; |
| 118 |
69 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( | 119 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( |
70 PasswordGenerationPopupController* controller, | 120 PasswordGenerationPopupController* controller, |
71 views::Widget* observing_widget) | 121 views::Widget* observing_widget) |
72 : AutofillPopupBaseView(controller, observing_widget), | 122 : AutofillPopupBaseView(controller, observing_widget), |
73 password_view_(NULL), | 123 password_view_(NULL), |
| 124 font_list_(ResourceBundle::GetSharedInstance().GetFontList( |
| 125 ResourceBundle::SmallFont)), |
74 controller_(controller) { | 126 controller_(controller) { |
75 if (controller_->display_password()) | 127 if (controller_->display_password()) |
76 CreatePasswordView(); | 128 CreatePasswordView(); |
77 | 129 |
78 help_label_ = new views::StyledLabel(controller_->HelpText(), this); | 130 help_label_ = new views::StyledLabel(controller_->HelpText(), this); |
79 help_label_->SetBaseFontList(controller_->font_list()); | 131 help_label_->SetBaseFontList(font_list_); |
| 132 help_label_->SetLineHeight(20); |
80 views::StyledLabel::RangeStyleInfo default_style; | 133 views::StyledLabel::RangeStyleInfo default_style; |
81 default_style.color = kExplanatoryTextColor; | 134 default_style.color = kExplanatoryTextColor; |
82 help_label_->SetDefaultStyle(default_style); | 135 help_label_->SetDefaultStyle(default_style); |
83 | 136 |
84 views::StyledLabel::RangeStyleInfo link_style = | 137 help_label_->AddStyleRange( |
85 views::StyledLabel::RangeStyleInfo::CreateForLink(); | 138 controller_->HelpTextLinkRange(), |
86 link_style.color = kLinkColor; | 139 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
87 help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style); | |
88 | 140 |
89 help_label_->SetBoundsRect(controller_->help_bounds()); | |
90 help_label_->set_background( | 141 help_label_->set_background( |
91 views::Background::CreateSolidBackground( | 142 views::Background::CreateSolidBackground( |
92 kExplanatoryTextBackgroundColor)); | 143 kExplanatoryTextBackgroundColor)); |
93 help_label_->SetBorder(views::Border::CreateEmptyBorder( | 144 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
94 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 145 PasswordGenerationPopupController::kHelpVerticalPadding - |
95 controller_->kHorizontalPadding, | 146 kHelpVerticalOffset, |
96 0, | 147 PasswordGenerationPopupController::kHorizontalPadding, |
97 controller_->kHorizontalPadding)); | 148 PasswordGenerationPopupController::kHelpVerticalPadding - |
| 149 kHelpVerticalOffset, |
| 150 PasswordGenerationPopupController::kHorizontalPadding)); |
98 AddChildView(help_label_); | 151 AddChildView(help_label_); |
99 | 152 |
100 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 153 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
101 } | 154 } |
102 | 155 |
103 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 156 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
104 | 157 |
105 void PasswordGenerationPopupViewViews::CreatePasswordView() { | 158 void PasswordGenerationPopupViewViews::CreatePasswordView() { |
106 if (password_view_) | 159 if (password_view_) |
107 return; | 160 return; |
108 | 161 |
109 password_view_ = new PasswordRow(controller_->password(), | 162 password_view_ = new PasswordBox(); |
110 controller_->SuggestedText(), | 163 password_view_->Init(controller_->password(), |
111 controller_->font_list(), | 164 controller_->SuggestedText(), |
112 controller_->kHorizontalPadding); | 165 font_list_); |
| 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::GetPreferredSizeOfPasswordView() { |
| 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 return gfx::Size(width, height + kPopupBorderThickness); |
| 182 } |
| 183 |
116 void PasswordGenerationPopupViewViews::Show() { | 184 void PasswordGenerationPopupViewViews::Show() { |
117 DoShow(); | 185 DoShow(); |
118 } | 186 } |
119 | 187 |
120 void PasswordGenerationPopupViewViews::Hide() { | 188 void PasswordGenerationPopupViewViews::Hide() { |
121 // The controller is no longer valid after it hides us. | 189 // The controller is no longer valid after it hides us. |
122 controller_ = NULL; | 190 controller_ = NULL; |
123 | 191 |
124 DoHide(); | 192 DoHide(); |
125 } | 193 } |
126 | 194 |
127 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { | 195 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 | 196 |
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 // Need to leave room for the border. |
150 password_view_->SetBoundsRect(controller_->password_bounds()); | 213 int y = kPopupBorderThickness; |
| 214 int popup_width = bounds().width() - 2 * kPopupBorderThickness; |
| 215 if (controller_->display_password()) { |
| 216 // Currently the UI can change from not offering a password to offering |
| 217 // a password (e.g. the user is editing a generated password and deletes |
| 218 // it), but it can't change the other way around. |
| 219 CreatePasswordView(); |
| 220 password_view_->SetBounds( |
| 221 kPopupBorderThickness, y, popup_width, kPasswordSectionHeight); |
| 222 divider_bounds_ = |
| 223 gfx::Rect(kPopupBorderThickness, password_view_->bounds().bottom(), |
| 224 popup_width, 1); |
| 225 y = divider_bounds_.bottom(); |
| 226 } |
151 | 227 |
152 help_label_->SetBoundsRect(controller_->help_bounds()); | 228 help_label_->SetBounds(kPopupBorderThickness, y, popup_width, |
| 229 help_label_->GetHeightForWidth(popup_width)); |
153 } | 230 } |
154 | 231 |
155 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 232 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
156 if (!controller_) | 233 if (!controller_) |
157 return; | 234 return; |
158 | 235 |
159 // Draw border and background. | 236 // Draw border and background. |
160 views::View::OnPaint(canvas); | 237 views::View::OnPaint(canvas); |
161 | 238 |
162 // Divider line needs to be drawn after OnPaint() otherwise the background | 239 // Divider line needs to be drawn after OnPaint() otherwise the background |
163 // will overwrite the divider. | 240 // will overwrite the divider. |
164 if (password_view_) | 241 if (password_view_) |
165 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | 242 canvas->FillRect(divider_bounds_, kDividerColor); |
166 } | 243 } |
167 | 244 |
168 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( | 245 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( |
169 const gfx::Range& range, int event_flags) { | 246 const gfx::Range& range, int event_flags) { |
170 controller_->OnSavedPasswordsLinkClicked(); | 247 controller_->OnSavedPasswordsLinkClicked(); |
171 } | 248 } |
172 | 249 |
| 250 bool PasswordGenerationPopupViewViews::IsPointInPasswordBounds( |
| 251 const gfx::Point& point) { |
| 252 return password_view_->bounds().Contains(point); |
| 253 } |
| 254 |
173 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( | 255 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( |
174 PasswordGenerationPopupController* controller) { | 256 PasswordGenerationPopupController* controller) { |
175 views::Widget* observing_widget = | 257 views::Widget* observing_widget = |
176 views::Widget::GetTopLevelWidgetForNativeView( | 258 views::Widget::GetTopLevelWidgetForNativeView( |
177 controller->container_view()); | 259 controller->container_view()); |
178 | 260 |
179 // If the top level widget can't be found, cancel the popup since we can't | 261 // If the top level widget can't be found, cancel the popup since we can't |
180 // fully set it up. | 262 // fully set it up. |
181 if (!observing_widget) | 263 if (!observing_widget) |
182 return NULL; | 264 return NULL; |
183 | 265 |
184 return new PasswordGenerationPopupViewViews(controller, observing_widget); | 266 return new PasswordGenerationPopupViewViews(controller, observing_widget); |
185 } | 267 } |
186 | 268 |
187 } // namespace autofill | 269 } // namespace autofill |
OLD | NEW |