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/autofill/password_generation_popup_controller_impl.h " | 5 #include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h " |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
27 #include "grit/google_chrome_strings.h" | 27 #include "grit/google_chrome_strings.h" |
28 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
29 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
30 #include "ui/events/keycodes/keyboard_codes.h" | 30 #include "ui/events/keycodes/keyboard_codes.h" |
31 #include "ui/gfx/rect_conversions.h" | 31 #include "ui/gfx/rect_conversions.h" |
32 #include "ui/gfx/text_utils.h" | 32 #include "ui/gfx/text_utils.h" |
33 | 33 |
34 namespace autofill { | 34 namespace autofill { |
35 | 35 |
36 namespace { | |
37 // The amount of additional space that a link takes up as it has a border if | |
38 // it's focused. TODO(gcasto): Does OSX do this as well? | |
39 const int kLinkBorderSize = 1; | |
40 const int kPasswordSectionHeight = 62; | |
41 } | |
42 | |
36 base::WeakPtr<PasswordGenerationPopupControllerImpl> | 43 base::WeakPtr<PasswordGenerationPopupControllerImpl> |
37 PasswordGenerationPopupControllerImpl::GetOrCreate( | 44 PasswordGenerationPopupControllerImpl::GetOrCreate( |
38 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous, | 45 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous, |
39 const gfx::RectF& bounds, | 46 const gfx::RectF& bounds, |
40 const PasswordForm& form, | 47 const PasswordForm& form, |
41 int max_length, | 48 int max_length, |
42 password_manager::PasswordManager* password_manager, | 49 password_manager::PasswordManager* password_manager, |
43 PasswordGenerationPopupObserver* observer, | 50 PasswordGenerationPopupObserver* observer, |
44 content::WebContents* web_contents, | 51 content::WebContents* web_contents, |
45 gfx::NativeView container_view) { | 52 gfx::NativeView container_view) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 web_contents()->GetRenderViewHost()->Send( | 159 web_contents()->GetRenderViewHost()->Send( |
153 new AutofillMsg_GeneratedPasswordAccepted( | 160 new AutofillMsg_GeneratedPasswordAccepted( |
154 web_contents()->GetRenderViewHost()->GetRoutingID(), | 161 web_contents()->GetRenderViewHost()->GetRoutingID(), |
155 current_password_)); | 162 current_password_)); |
156 password_manager_->SetFormHasGeneratedPassword(form_); | 163 password_manager_->SetFormHasGeneratedPassword(form_); |
157 Hide(); | 164 Hide(); |
158 } | 165 } |
159 | 166 |
160 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { | 167 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { |
161 // Minimum width in pixels. | 168 // Minimum width in pixels. |
162 const int minimum_required_width = 300; | 169 const int minimum_required_width = 350; |
163 | 170 |
164 // If the width of the field is longer than the minimum, use that instead. | 171 // If the width of the field is longer than the minimum, use that instead. |
165 int width = std::max(minimum_required_width, | 172 int width = std::max(minimum_required_width, |
166 controller_common_.RoundedElementBounds().width()); | 173 controller_common_.RoundedElementBounds().width()); |
167 | 174 |
168 if (display_password_) { | 175 if (display_password_) { |
169 // Make sure that the width will always be large enough to display the | 176 // Make sure that the width will always be large enough to display the |
170 // password and suggestion on one line. | 177 // password and suggestion on one line. |
171 width = std::max(width, | 178 width = std::max(width, |
172 gfx::GetStringWidth(current_password_ + SuggestedText(), | 179 gfx::GetStringWidth(current_password_ + SuggestedText(), |
173 font_list_) + 2 * kHorizontalPadding); | 180 font_list_) + 2 * kHorizontalPadding); |
174 } | 181 } |
175 | 182 |
176 return width; | 183 return width; |
177 } | 184 } |
178 | 185 |
179 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { | 186 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { |
Evan Stade
2014/06/19 00:16:35
size calculations belong in view implementations.
Garrett Casto
2014/06/20 23:47:00
One problem with this is that you need to know the
Evan Stade
2014/06/23 18:20:40
what platform are you using for the study? cros? C
Garrett Casto
2014/06/24 01:15:11
I actually don't know if it's CrOS or Windows. I t
| |
180 // Note that this wrapping isn't exactly what the popup will do. It shouldn't | 187 // Calculating the height of the help text is complicated by the fact that |
181 // line break in the middle of the link, but as long as the link isn't longer | 188 // we won't add a line break in the middle of a link. Note that this currently |
182 // than given width this shouldn't affect the height calculated here. The | 189 // doesn't appropriately wrap at word boundries either, but that is less of |
183 // default width should be wide enough to prevent this from being an issue. | 190 // a problem at the moment given the size of the link and it's placement on |
191 // the line. | |
192 // TODO(gcasto): I really wish that there was a better cross platform way of | |
193 // calculating this, but it's not clear that there is. | |
194 // gfx::ElideRectangleText() is close, but it doesn't understand embedded | |
195 // links. | |
184 int total_length = gfx::GetStringWidth(HelpText(), font_list_); | 196 int total_length = gfx::GetStringWidth(HelpText(), font_list_); |
185 int usable_width = width - 2 * kHorizontalPadding; | 197 int before_link_length = |
198 gfx::GetStringWidth(HelpText().substr(0, HelpTextLinkRange().start()), | |
199 font_list_); | |
200 int after_link_length = | |
201 gfx::GetStringWidth(HelpText().substr(0, HelpTextLinkRange().end()), | |
202 font_list_); | |
203 // Even though the link will only be on one line, this is how the usable width | |
204 // is calculated on StyledLabel. | |
205 int usable_width = | |
206 width - 2 * kHorizontalPadding - 2 * kLinkBorderSize; | |
207 int num_lines; | |
208 if (before_link_length / usable_width != after_link_length / usable_width) { | |
209 // Link would be wrapped, line break before the link starts. | |
210 int link_and_after_length = | |
211 gfx::GetStringWidth(HelpText().substr(HelpTextLinkRange().start()), | |
212 font_list_); | |
213 num_lines = (before_link_length/usable_width + | |
214 link_and_after_length/usable_width + | |
215 2); | |
216 } else { | |
217 // Link shouldn't cross a line break. | |
218 num_lines = total_length/usable_width + 1; | |
219 } | |
220 | |
186 int text_height = | 221 int text_height = |
187 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * | 222 ((num_lines-1) * |
Evan Stade
2014/06/19 00:16:35
spaces around binary operators
Garrett Casto
2014/06/20 23:47:00
Done.
| |
188 font_list_.GetFontSize(); | 223 (font_list_.GetHeight() + |
189 int help_section_height = text_height + 2 * kHelpVerticalPadding; | 224 PasswordGenerationPopupView::kHelpSectionAdditionalSpacing)) + |
225 font_list_.GetBaseline(); | |
226 // The vertical padding on the last line should be from the base of the | |
227 // characters not the lowest level of a character, so subtract the descent. | |
228 int font_descent = font_list_.GetHeight() - font_list_.GetBaseline(); | |
229 int help_section_height = | |
230 text_height + 2 * kHelpVerticalPadding - font_descent; | |
190 | 231 |
191 int password_section_height = 0; | 232 int password_section_height = 0; |
192 if (display_password_) { | 233 if (display_password_) { |
193 password_section_height = | 234 // Minimum width is wide enought that we shouldn't have to do any text |
194 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding; | 235 // wrapping in this section. |
236 password_section_height = kPasswordSectionHeight; | |
195 } | 237 } |
196 | 238 |
197 return (2 * kPopupBorderThickness + | 239 return (2 * kPopupBorderThickness + |
198 help_section_height + | 240 help_section_height + |
199 password_section_height); | 241 password_section_height); |
200 } | 242 } |
201 | 243 |
202 void PasswordGenerationPopupControllerImpl::CalculateBounds() { | 244 void PasswordGenerationPopupControllerImpl::CalculateBounds() { |
203 int popup_width = GetDesiredWidth(); | 245 int popup_width = GetDesiredWidth(); |
204 int popup_height = GetDesiredHeight(popup_width); | 246 int popup_height = GetDesiredHeight(popup_width); |
205 | 247 |
206 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); | 248 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); |
207 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness; | 249 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness; |
208 | 250 |
209 // Calculate the bounds for the rest of the elements given the bounds of | 251 // Calculate the bounds for the rest of the elements given the bounds of |
210 // the popup. | 252 // the popup. |
211 if (display_password_) { | 253 if (display_password_) { |
212 password_bounds_ = gfx::Rect( | 254 password_bounds_ = gfx::Rect( |
213 kPopupBorderThickness, | 255 kPopupBorderThickness, |
214 kPopupBorderThickness, | 256 kPopupBorderThickness, |
215 sub_view_width, | 257 sub_view_width, |
216 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding); | 258 kPasswordSectionHeight); |
217 | 259 |
218 divider_bounds_ = gfx::Rect(kPopupBorderThickness, | 260 divider_bounds_ = gfx::Rect(kPopupBorderThickness, |
219 password_bounds_.bottom(), | 261 password_bounds_.bottom(), |
220 sub_view_width, | 262 sub_view_width, |
221 1 /* divider heigth*/); | 263 1 /* divider heigth*/); |
222 } else { | 264 } else { |
223 password_bounds_ = gfx::Rect(); | 265 password_bounds_ = gfx::Rect(); |
224 divider_bounds_ = gfx::Rect(); | 266 divider_bounds_ = gfx::Rect(); |
225 } | 267 } |
226 | 268 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 | 390 |
349 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { | 391 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { |
350 return help_text_; | 392 return help_text_; |
351 } | 393 } |
352 | 394 |
353 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { | 395 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { |
354 return link_range_; | 396 return link_range_; |
355 } | 397 } |
356 | 398 |
357 } // namespace autofill | 399 } // namespace autofill |
OLD | NEW |