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 "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 #include "ui/views/background.h" | 10 #include "ui/views/background.h" |
11 #include "ui/views/border.h" | 11 #include "ui/views/border.h" |
12 #include "ui/views/controls/label.h" | 12 #include "ui/views/controls/label.h" |
13 #include "ui/views/controls/styled_label.h" | 13 #include "ui/views/controls/styled_label.h" |
14 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
15 | 15 |
16 namespace autofill { | 16 namespace autofill { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); | 20 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); |
21 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x93, 0x93, 0x93); | 21 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x7F, 0x7F, 0x7F); |
22 const SkColor kDividerColor = SkColorSetRGB(0xE7, 0xE7, 0xE7); | 22 const SkColor kDividerColor = SkColorSetRGB(0xE9, 0xE9, 0xE9); |
23 | 23 |
24 // This is the amount of vertical whitespace that is left above and below the | 24 // This is the amount of vertical whitespace that is left above and below the |
25 // password when it is highlighted. | 25 // password when it is highlighted. |
26 const int kPasswordVerticalInset = 7; | 26 const int kPasswordVerticalInset = 7; |
27 | 27 |
28 // The amount of whitespace that is present when there is no padding. Used | 28 // The amount of whitespace that is present when there is no padding. Used |
29 // to get the proper spacing in the help section. | 29 // to get the proper spacing in the help section. |
30 const int kHelpVerticalOffset = 3; | 30 const int kHelpVerticalOffset = 3; |
31 | 31 |
32 // Class that shows the password and the suggestion side-by-side. | |
33 class PasswordLabel : public views::View { | |
Evan Stade
2014/02/06 16:13:52
think it's a little weird to have a class called F
Garrett Casto
2014/02/06 18:39:57
Done.
| |
34 public: | |
35 PasswordLabel(const base::string16& password, | |
36 const base::string16& suggestion, | |
37 const gfx::FontList& font_list, | |
38 int horizontal_border) { | |
39 password_label_ = new views::Label(password, font_list); | |
40 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
41 password_label_->SetBorder( | |
42 views::Border::CreateEmptyBorder(0, horizontal_border, 0, 0)); | |
Evan Stade
2014/02/06 16:13:52
one wonders why you place the inset on password_la
Garrett Casto
2014/02/06 18:39:57
Leftover from an earlier time. Changed.
| |
43 AddChildView(password_label_); | |
44 | |
45 suggestion_label_ = new views::Label(suggestion, font_list); | |
46 suggestion_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
47 suggestion_label_->SetBorder( | |
48 views::Border::CreateEmptyBorder(0, 0, 0, horizontal_border)); | |
49 suggestion_label_->SetEnabledColor(kExplanatoryTextColor); | |
50 AddChildView(suggestion_label_); | |
51 } | |
52 virtual ~PasswordLabel() {} | |
53 | |
54 virtual void Layout() OVERRIDE { | |
55 password_label_->SetBoundsRect(bounds()); | |
Evan Stade
2014/02/06 16:13:52
technically I think you want GetContentsBounds(),
Garrett Casto
2014/02/06 18:39:57
Done.
| |
56 suggestion_label_->SetBoundsRect(bounds()); | |
Evan Stade
2014/02/06 16:13:52
how is it these two labels never overlap? I see on
Garrett Casto
2014/02/06 18:39:57
I thought about this, but wouldn't a box layout br
Evan Stade
2014/02/07 21:45:48
no, box layouts flip appropriately in rtl
Garrett Casto
2014/02/10 11:02:44
Interesting. I had looked through the implementati
| |
57 } | |
58 | |
59 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE { | |
60 // Have parent do event handling. | |
61 return false; | |
62 } | |
63 | |
64 private: | |
65 // Child views. Not owned. | |
66 views::Label* suggestion_label_; | |
67 views::Label* password_label_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(PasswordLabel); | |
70 }; | |
71 | |
32 } // namespace | 72 } // namespace |
33 | 73 |
34 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( | 74 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( |
35 PasswordGenerationPopupController* controller, | 75 PasswordGenerationPopupController* controller, |
36 views::Widget* observing_widget) | 76 views::Widget* observing_widget) |
37 : AutofillPopupBaseView(controller, observing_widget), | 77 : AutofillPopupBaseView(controller, observing_widget), |
78 password_view_(NULL), | |
38 controller_(controller) { | 79 controller_(controller) { |
39 password_label_ = new views::Label(controller->password()); | 80 if (controller_->display_password()) |
40 password_label_->SetBoundsRect(controller->password_bounds()); | 81 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 | 82 |
48 base::string16 help_text = controller->HelpText(); | 83 base::string16 help_text = controller_->HelpText(); |
49 base::string16 learn_more_link_text = controller->LearnMoreLink(); | 84 base::string16 saved_passwords_link_text = controller_->SavedPasswordsLink(); |
50 views::StyledLabel* help_label = | 85 help_label_ = |
51 new views::StyledLabel(help_text + learn_more_link_text, this); | 86 new views::StyledLabel(help_text + saved_passwords_link_text, this); |
87 help_label_->SetFontList(controller_->font_list()); | |
52 views::StyledLabel::RangeStyleInfo default_style; | 88 views::StyledLabel::RangeStyleInfo default_style; |
53 default_style.color = kExplanatoryTextColor; | 89 default_style.color = kExplanatoryTextColor; |
54 help_label->SetDefaultStyle(default_style); | 90 help_label_->SetDefaultStyle(default_style); |
55 help_label->AddStyleRange( | 91 |
92 views::StyledLabel::RangeStyleInfo link_style = | |
93 views::StyledLabel::RangeStyleInfo::CreateForLink(); | |
94 help_label_->AddStyleRange( | |
56 gfx::Range(help_text.size(), | 95 gfx::Range(help_text.size(), |
57 help_text.size() + learn_more_link_text.size()), | 96 help_text.size() + saved_passwords_link_text.size()), |
58 views::StyledLabel::RangeStyleInfo::CreateForLink()); | 97 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
59 help_label->SetBoundsRect(controller->help_bounds()); | 98 help_label_->SetBoundsRect(controller_->help_bounds()); |
60 help_label->set_background( | 99 help_label_->set_background( |
61 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); | 100 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); |
62 help_label->SetBorder(views::Border::CreateEmptyBorder( | 101 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
63 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 102 controller_->kHelpVerticalPadding - kHelpVerticalOffset, |
64 controller_->kHorizontalPadding, | 103 controller_->kHorizontalPadding, |
65 0, | 104 0, |
66 controller_->kHorizontalPadding)); | 105 controller_->kHorizontalPadding)); |
67 AddChildView(help_label); | 106 AddChildView(help_label_); |
68 | 107 |
69 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 108 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
70 } | 109 } |
71 | 110 |
72 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 111 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
73 | 112 |
113 void PasswordGenerationPopupViewViews::CreatePasswordView() { | |
114 if (password_view_) | |
115 return; | |
116 | |
117 password_view_ = | |
118 new PasswordLabel(controller_->password(), | |
119 controller_->SuggestedText(), | |
120 controller_->font_list(), | |
121 controller_->kHorizontalPadding); | |
122 password_view_->set_clip_insets(gfx::Insets( | |
Evan Stade
2014/02/06 16:13:52
i feel like you should be able to do this by setti
Garrett Casto
2014/02/06 18:39:57
As far as I could tell, this was the only way to s
| |
123 kPasswordVerticalInset, 0, kPasswordVerticalInset, 0)); | |
124 AddChildView(password_view_); | |
125 } | |
126 | |
74 void PasswordGenerationPopupViewViews::Show() { | 127 void PasswordGenerationPopupViewViews::Show() { |
75 DoShow(); | 128 DoShow(); |
76 } | 129 } |
77 | 130 |
78 void PasswordGenerationPopupViewViews::Hide() { | 131 void PasswordGenerationPopupViewViews::Hide() { |
79 // The controller is no longer valid after it hides us. | 132 // The controller is no longer valid after it hides us. |
80 controller_ = NULL; | 133 controller_ = NULL; |
81 | 134 |
82 DoHide(); | 135 DoHide(); |
83 } | 136 } |
84 | 137 |
85 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { | 138 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { |
139 // Currently the UI can change from not offering a password to offering | |
140 // a password (e.g. the user is editing a generated password and deletes it), | |
141 // but it can't change the other way around. | |
142 if (controller_->display_password()) { | |
Evan Stade
2014/02/06 16:13:52
nit: no curlies
Garrett Casto
2014/02/06 18:39:57
Done.
| |
143 CreatePasswordView(); | |
144 } | |
86 DoUpdateBoundsAndRedrawPopup(); | 145 DoUpdateBoundsAndRedrawPopup(); |
87 } | 146 } |
88 | 147 |
148 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { | |
149 if (password_view_) { | |
Evan Stade
2014/02/06 16:13:52
if (!password_view_)
return;
password_view_->se
Garrett Casto
2014/02/06 18:39:57
I actually already did that and forgot to upload.
| |
150 if (controller_->password_selected()) { | |
151 password_view_->set_background( | |
152 views::Background::CreateSolidBackground(kHoveredBackgroundColor)); | |
153 } else { | |
154 password_view_->set_background( | |
155 views::Background::CreateSolidBackground(kPopupBackground)); | |
156 } | |
157 } | |
158 } | |
159 | |
160 void PasswordGenerationPopupViewViews::Layout() { | |
161 if (password_view_) | |
162 password_view_->SetBoundsRect(controller_->password_bounds()); | |
Evan Stade
2014/02/06 16:13:52
\n
Garrett Casto
2014/02/06 18:39:57
Done.
| |
163 help_label_->SetBoundsRect(controller_->help_bounds()); | |
164 } | |
165 | |
89 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 166 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
90 if (!controller_) | 167 if (!controller_) |
91 return; | 168 return; |
92 | 169 |
93 if (controller_->password_selected()) { | |
94 password_label_->set_background( | |
95 views::Background::CreateSolidBackground(kHoveredBackgroundColor)); | |
96 } else { | |
97 password_label_->set_background( | |
98 views::Background::CreateSolidBackground(kPopupBackground)); | |
99 } | |
100 | |
101 // Draw border and background. | 170 // Draw border and background. |
102 views::View::OnPaint(canvas); | 171 views::View::OnPaint(canvas); |
103 | 172 |
104 // Divider line needs to be drawn after OnPaint() otherwise the background | 173 // Divider line needs to be drawn after OnPaint() otherwise the background |
105 // will overwrite the divider. | 174 // will overwrite the divider. |
106 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | 175 if (password_view_) |
176 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | |
107 } | 177 } |
108 | 178 |
109 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( | 179 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( |
110 const gfx::Range& range, int event_flags) { | 180 const gfx::Range& range, int event_flags) { |
111 controller_->OnHelpLinkClicked(); | 181 controller_->OnSavedPasswordsLinkClicked(); |
112 } | 182 } |
113 | 183 |
114 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( | 184 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( |
115 PasswordGenerationPopupController* controller) { | 185 PasswordGenerationPopupController* controller) { |
116 views::Widget* observing_widget = | 186 views::Widget* observing_widget = |
117 views::Widget::GetTopLevelWidgetForNativeView( | 187 views::Widget::GetTopLevelWidgetForNativeView( |
118 controller->container_view()); | 188 controller->container_view()); |
119 | 189 |
120 // If the top level widget can't be found, cancel the popup since we can't | 190 // If the top level widget can't be found, cancel the popup since we can't |
121 // fully set it up. | 191 // fully set it up. |
122 if (!observing_widget) | 192 if (!observing_widget) |
123 return NULL; | 193 return NULL; |
124 | 194 |
125 return new PasswordGenerationPopupViewViews(controller, observing_widget); | 195 return new PasswordGenerationPopupViewViews(controller, observing_widget); |
126 } | 196 } |
127 | 197 |
128 } // namespace autofill | 198 } // namespace autofill |
OLD | NEW |