Chromium Code Reviews| 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/debug/stack_trace.h" | |
| 7 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 8 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h" | 9 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 9 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 10 #include "ui/views/background.h" | 12 #include "ui/views/background.h" |
| 11 #include "ui/views/border.h" | 13 #include "ui/views/border.h" |
| 12 #include "ui/views/controls/label.h" | 14 #include "ui/views/controls/label.h" |
| 13 #include "ui/views/controls/styled_label.h" | 15 #include "ui/views/controls/styled_label.h" |
| 14 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 15 | 17 |
| 16 namespace autofill { | 18 namespace autofill { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); | 22 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); |
| 21 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x93, 0x93, 0x93); | 23 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x7F, 0x7F, 0x7F); |
| 22 const SkColor kDividerColor = SkColorSetRGB(0xE7, 0xE7, 0xE7); | 24 const SkColor kDividerColor = SkColorSetRGB(0xE9, 0xE9, 0xE9); |
| 23 | 25 |
| 24 // This is the amount of vertical whitespace that is left above and below the | 26 // This is the amount of vertical whitespace that is left above and below the |
| 25 // password when it is highlighted. | 27 // password when it is highlighted. |
| 26 const int kPasswordVerticalInset = 7; | 28 const int kPasswordVerticalInset = 7; |
| 27 | 29 |
| 28 // The amount of whitespace that is present when there is no padding. Used | 30 // The amount of whitespace that is present when there is no padding. Used |
| 29 // to get the proper spacing in the help section. | 31 // to get the proper spacing in the help section. |
| 30 const int kHelpVerticalOffset = 3; | 32 const int kHelpVerticalOffset = 3; |
| 31 | 33 |
| 34 // Class that shows the password and the suggestion side-by-side. | |
| 35 class PasswordLabel : public views::View { | |
| 36 public: | |
| 37 PasswordLabel(const base::string16& password, | |
| 38 const base::string16& suggestion, | |
| 39 const gfx::Rect& bounds, | |
| 40 const gfx::FontList& font_list, | |
| 41 int horizontal_border) { | |
| 42 views::Label* password_label = new views::Label(password, font_list); | |
| 43 password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 44 password_label->SetBoundsRect(bounds); | |
| 45 password_label->SetBorder( | |
| 46 views::Border::CreateEmptyBorder(0, horizontal_border, 0, 0)); | |
| 47 AddChildView(password_label); | |
| 48 | |
| 49 views::Label* suggestion_label = new views::Label(suggestion, font_list); | |
| 50 suggestion_label->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
| 51 suggestion_label->SetBoundsRect(bounds); | |
|
Evan Stade
2014/02/03 18:49:15
set child bounds in the layout() function, either
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 52 suggestion_label->SetBorder( | |
| 53 views::Border::CreateEmptyBorder(0, 0, 0, horizontal_border)); | |
| 54 suggestion_label->SetEnabledColor(kExplanatoryTextColor); | |
| 55 AddChildView(suggestion_label); | |
| 56 | |
| 57 SetBoundsRect(bounds); | |
|
Evan Stade
2014/02/03 18:49:15
same applies here, only the parent view is respons
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 58 } | |
| 59 virtual ~PasswordLabel() {} | |
| 60 | |
| 61 virtual views::View* GetEventHandlerForRect(const gfx::Rect& rect) OVERRIDE { | |
| 62 // Event handling is done by AutofillPopupBaseView. | |
| 63 return parent(); | |
|
Evan Stade
2014/02/03 18:49:15
I think the normal way of doing this is
virtual b
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 DISALLOW_COPY_AND_ASSIGN(PasswordLabel); | |
| 68 }; | |
| 69 | |
| 32 } // namespace | 70 } // namespace |
| 33 | 71 |
| 34 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( | 72 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( |
| 35 PasswordGenerationPopupController* controller, | 73 PasswordGenerationPopupController* controller, |
| 36 views::Widget* observing_widget) | 74 views::Widget* observing_widget) |
| 37 : AutofillPopupBaseView(controller, observing_widget), | 75 : AutofillPopupBaseView(controller, observing_widget), |
| 76 password_view_(NULL), | |
| 38 controller_(controller) { | 77 controller_(controller) { |
| 39 password_label_ = new views::Label(controller->password()); | 78 if (controller_->display_password()) |
| 40 password_label_->SetBoundsRect(controller->password_bounds()); | 79 CreatePasswordView(); |
| 41 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 42 password_label_->set_clip_insets(gfx::Insets( | |
| 43 kPasswordVerticalInset, 0, kPasswordVerticalInset, 0)); | |
| 44 password_label_->SetBorder(views::Border::CreateEmptyBorder( | |
| 45 0, controller_->kHorizontalPadding, 0, controller_->kHorizontalPadding)); | |
| 46 AddChildView(password_label_); | |
| 47 | 80 |
| 48 base::string16 help_text = controller->HelpText(); | 81 base::string16 help_text = controller_->HelpText(); |
| 49 base::string16 learn_more_link_text = controller->LearnMoreLink(); | 82 base::string16 saved_passwords_link_text = controller_->SavedPasswordsLink(); |
| 50 views::StyledLabel* help_label = | 83 help_label_ = |
| 51 new views::StyledLabel(help_text + learn_more_link_text, this); | 84 new views::StyledLabel(help_text + saved_passwords_link_text, this); |
| 85 help_label_->SetFontList(controller_->font_list()); | |
| 52 views::StyledLabel::RangeStyleInfo default_style; | 86 views::StyledLabel::RangeStyleInfo default_style; |
| 53 default_style.color = kExplanatoryTextColor; | 87 default_style.color = kExplanatoryTextColor; |
| 54 help_label->SetDefaultStyle(default_style); | 88 help_label_->SetDefaultStyle(default_style); |
| 55 help_label->AddStyleRange( | 89 |
| 90 views::StyledLabel::RangeStyleInfo link_style = | |
| 91 views::StyledLabel::RangeStyleInfo::CreateForLink(); | |
| 92 help_label_->AddStyleRange( | |
| 56 gfx::Range(help_text.size(), | 93 gfx::Range(help_text.size(), |
| 57 help_text.size() + learn_more_link_text.size()), | 94 help_text.size() + saved_passwords_link_text.size()), |
| 58 views::StyledLabel::RangeStyleInfo::CreateForLink()); | 95 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
| 59 help_label->SetBoundsRect(controller->help_bounds()); | 96 help_label_->SetBoundsRect(controller_->help_bounds()); |
|
Evan Stade
2014/02/03 18:49:15
ditto
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 60 help_label->set_background( | 97 help_label_->set_background( |
| 61 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); | 98 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); |
| 62 help_label->SetBorder(views::Border::CreateEmptyBorder( | 99 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
| 63 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 100 controller_->kHelpVerticalPadding - kHelpVerticalOffset, |
| 64 controller_->kHorizontalPadding, | 101 controller_->kHorizontalPadding, |
| 65 0, | 102 0, |
| 66 controller_->kHorizontalPadding)); | 103 controller_->kHorizontalPadding)); |
| 67 AddChildView(help_label); | 104 AddChildView(help_label_); |
| 68 | 105 |
| 69 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 106 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
| 70 } | 107 } |
| 71 | 108 |
| 72 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 109 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
| 73 | 110 |
| 111 void PasswordGenerationPopupViewViews::CreatePasswordView() { | |
| 112 password_view_ = | |
| 113 new PasswordLabel(controller_->password(), | |
| 114 controller_->SuggestedText(), | |
| 115 controller_->password_bounds(), | |
| 116 controller_->font_list(), | |
| 117 controller_->kHorizontalPadding); | |
| 118 password_view_->set_clip_insets(gfx::Insets( | |
| 119 kPasswordVerticalInset, 0, kPasswordVerticalInset, 0)); | |
| 120 AddChildView(password_view_); | |
| 121 } | |
| 122 | |
| 74 void PasswordGenerationPopupViewViews::Show() { | 123 void PasswordGenerationPopupViewViews::Show() { |
| 75 DoShow(); | 124 DoShow(); |
| 76 } | 125 } |
| 77 | 126 |
| 78 void PasswordGenerationPopupViewViews::Hide() { | 127 void PasswordGenerationPopupViewViews::Hide() { |
| 79 // The controller is no longer valid after it hides us. | 128 // The controller is no longer valid after it hides us. |
| 80 controller_ = NULL; | 129 controller_ = NULL; |
| 81 | 130 |
| 82 DoHide(); | 131 DoHide(); |
| 83 } | 132 } |
| 84 | 133 |
| 85 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { | 134 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { |
| 135 // Currently the UI can change from not offering a password to offering | |
| 136 // a password (e.g. the user is editing a generated password and deletes it), | |
| 137 // but it can't change the other way around. | |
| 138 if (controller_->display_password()) { | |
| 139 CreatePasswordView(); | |
| 140 help_label_->SetBoundsRect(controller_->help_bounds()); | |
| 141 } | |
| 86 DoUpdateBoundsAndRedrawPopup(); | 142 DoUpdateBoundsAndRedrawPopup(); |
| 87 } | 143 } |
| 88 | 144 |
| 89 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 145 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
| 90 if (!controller_) | 146 if (!controller_) |
| 91 return; | 147 return; |
| 92 | 148 |
| 93 if (controller_->password_selected()) { | 149 if (password_view_) { |
| 94 password_label_->set_background( | 150 if (controller_->password_selected()) { |
| 95 views::Background::CreateSolidBackground(kHoveredBackgroundColor)); | 151 password_view_->set_background( |
|
Evan Stade
2014/02/03 18:49:15
i feel like this should happen when the password s
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 96 } else { | 152 views::Background::CreateSolidBackground(kHoveredBackgroundColor)); |
| 97 password_label_->set_background( | 153 } else { |
| 98 views::Background::CreateSolidBackground(kPopupBackground)); | 154 password_view_->set_background( |
| 155 views::Background::CreateSolidBackground(kPopupBackground)); | |
| 156 } | |
| 99 } | 157 } |
| 100 | 158 |
| 101 // Draw border and background. | 159 // Draw border and background. |
| 102 views::View::OnPaint(canvas); | 160 views::View::OnPaint(canvas); |
|
Evan Stade
2014/02/03 18:49:15
you can probably always call this first, then if (
Garrett Casto
2014/02/04 17:54:57
N/A I think
| |
| 103 | 161 |
| 104 // Divider line needs to be drawn after OnPaint() otherwise the background | 162 // Divider line needs to be drawn after OnPaint() otherwise the background |
| 105 // will overwrite the divider. | 163 // will overwrite the divider. |
| 106 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | 164 if (password_view_) |
| 165 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | |
| 107 } | 166 } |
| 108 | 167 |
| 109 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( | 168 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( |
| 110 const gfx::Range& range, int event_flags) { | 169 const gfx::Range& range, int event_flags) { |
| 111 controller_->OnHelpLinkClicked(); | 170 controller_->OnSavedPasswordsLinkClicked(); |
| 112 } | 171 } |
| 113 | 172 |
| 114 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( | 173 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( |
| 115 PasswordGenerationPopupController* controller) { | 174 PasswordGenerationPopupController* controller) { |
| 116 views::Widget* observing_widget = | 175 views::Widget* observing_widget = |
| 117 views::Widget::GetTopLevelWidgetForNativeView( | 176 views::Widget::GetTopLevelWidgetForNativeView( |
| 118 controller->container_view()); | 177 controller->container_view()); |
| 119 | 178 |
| 120 // If the top level widget can't be found, cancel the popup since we can't | 179 // If the top level widget can't be found, cancel the popup since we can't |
| 121 // fully set it up. | 180 // fully set it up. |
| 122 if (!observing_widget) | 181 if (!observing_widget) |
| 123 return NULL; | 182 return NULL; |
| 124 | 183 |
| 125 return new PasswordGenerationPopupViewViews(controller, observing_widget); | 184 return new PasswordGenerationPopupViewViews(controller, observing_widget); |
| 126 } | 185 } |
| 127 | 186 |
| 128 } // namespace autofill | 187 } // namespace autofill |
| OLD | NEW |