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), |
74 controller_(controller) { | 123 controller_(controller) { |
75 if (controller_->display_password()) | 124 if (controller_->display_password()) |
76 CreatePasswordView(); | 125 CreatePasswordView(); |
77 | 126 |
78 help_label_ = new views::StyledLabel(controller_->HelpText(), this); | 127 help_label_ = new views::StyledLabel(controller_->HelpText(), this); |
79 help_label_->SetBaseFontList(controller_->font_list()); | 128 help_label_->SetBaseFontList(controller_->font_list()); |
129 help_label_->SetLineHeight(18); | |
80 views::StyledLabel::RangeStyleInfo default_style; | 130 views::StyledLabel::RangeStyleInfo default_style; |
81 default_style.color = kExplanatoryTextColor; | 131 default_style.color = kExplanatoryTextColor; |
82 help_label_->SetDefaultStyle(default_style); | 132 help_label_->SetDefaultStyle(default_style); |
83 | 133 |
84 views::StyledLabel::RangeStyleInfo link_style = | 134 help_label_->AddStyleRange( |
85 views::StyledLabel::RangeStyleInfo::CreateForLink(); | 135 controller_->HelpTextLinkRange(), |
86 link_style.color = kLinkColor; | 136 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
87 help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style); | |
88 | 137 |
89 help_label_->SetBoundsRect(controller_->help_bounds()); | |
90 help_label_->set_background( | 138 help_label_->set_background( |
91 views::Background::CreateSolidBackground( | 139 views::Background::CreateSolidBackground( |
92 kExplanatoryTextBackgroundColor)); | 140 kExplanatoryTextBackgroundColor)); |
93 help_label_->SetBorder(views::Border::CreateEmptyBorder( | 141 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
94 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 142 controller_->kHelpVerticalPadding - kHelpVerticalOffset, |
95 controller_->kHorizontalPadding, | 143 controller_->kHorizontalPadding, |
96 0, | 144 0, |
97 controller_->kHorizontalPadding)); | 145 controller_->kHorizontalPadding)); |
98 AddChildView(help_label_); | 146 AddChildView(help_label_); |
99 | 147 |
100 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 148 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
101 } | 149 } |
102 | 150 |
103 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 151 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
104 | 152 |
105 void PasswordGenerationPopupViewViews::CreatePasswordView() { | 153 void PasswordGenerationPopupViewViews::CreatePasswordView() { |
106 if (password_view_) | 154 if (password_view_) |
107 return; | 155 return; |
108 | 156 |
109 password_view_ = new PasswordRow(controller_->password(), | 157 password_view_ = new PasswordBox(); |
110 controller_->SuggestedText(), | 158 static_cast<PasswordBox*>(password_view_)->Init( |
111 controller_->font_list(), | 159 controller_->password(), |
112 controller_->kHorizontalPadding); | 160 controller_->SuggestedText(), |
161 controller_->font_list(), | |
162 kItemTextColor, | |
163 controller_->kHorizontalPadding); | |
164 password_view_->SetPosition(gfx::Point(kPopupBorderThickness, | |
165 kPopupBorderThickness)); | |
166 password_view_->SizeToPreferredSize(); | |
113 AddChildView(password_view_); | 167 AddChildView(password_view_); |
114 } | 168 } |
115 | 169 |
170 void PasswordGenerationPopupViewViews::SetBoundsForWidth(int width) { | |
Evan Stade
2014/06/25 00:02:41
nit: does this need to be its own function now? Ca
Garrett Casto
2014/06/25 19:38:14
Done.
| |
171 // Need to leave room for the border. | |
172 int y = kPopupBorderThickness; | |
173 int popup_width = width - 2 * kPopupBorderThickness; | |
174 if (controller_->display_password()) { | |
175 // Currently the UI can change from not offering a password to offering | |
176 // a password (e.g. the user is editing a generated password and deletes | |
177 // it), but it can't change the other way around. | |
178 CreatePasswordView(); | |
179 password_view_->SetBounds( | |
180 kPopupBorderThickness, y, popup_width, kPasswordSectionHeight); | |
181 divider_bounds_ = | |
182 gfx::Rect(kPopupBorderThickness, password_view_->bounds().bottom(), | |
183 popup_width, 1); | |
184 y = divider_bounds_.bottom(); | |
185 } | |
186 help_label_->SetBounds(kPopupBorderThickness, y, popup_width, | |
187 help_label_->GetHeightForWidth(popup_width) + | |
188 help_label_->GetInsets().height()); | |
189 } | |
190 | |
191 int PasswordGenerationPopupViewViews::GetHeight(int width) { | |
192 int y = kPopupBorderThickness; | |
193 if (controller_->display_password()) { | |
194 // Add divider height as well. | |
195 y += kPasswordSectionHeight + 1; | |
196 } | |
197 int popup_width = width - 2 * kPopupBorderThickness; | |
198 y += help_label_->GetHeightForWidth(popup_width) + | |
199 help_label_->GetInsets().height(); | |
200 return y + kPopupBorderThickness; | |
201 } | |
202 | |
116 void PasswordGenerationPopupViewViews::Show() { | 203 void PasswordGenerationPopupViewViews::Show() { |
117 DoShow(); | 204 DoShow(); |
118 } | 205 } |
119 | 206 |
120 void PasswordGenerationPopupViewViews::Hide() { | 207 void PasswordGenerationPopupViewViews::Hide() { |
121 // The controller is no longer valid after it hides us. | 208 // The controller is no longer valid after it hides us. |
122 controller_ = NULL; | 209 controller_ = NULL; |
123 | 210 |
124 DoHide(); | 211 DoHide(); |
125 } | 212 } |
126 | 213 |
127 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { | 214 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 | 215 |
134 DoUpdateBoundsAndRedrawPopup(); | 216 DoUpdateBoundsAndRedrawPopup(); |
135 } | 217 } |
136 | 218 |
137 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { | 219 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { |
138 if (!password_view_) | 220 if (!password_view_) |
139 return; | 221 return; |
140 | 222 |
141 password_view_->set_background( | 223 password_view_->set_background( |
142 views::Background::CreateSolidBackground( | 224 views::Background::CreateSolidBackground( |
143 controller_->password_selected() ? | 225 controller_->password_selected() ? |
144 kHoveredBackgroundColor : | 226 kHoveredBackgroundColor : |
145 kPopupBackground)); | 227 kPopupBackground)); |
146 } | 228 } |
147 | 229 |
148 void PasswordGenerationPopupViewViews::Layout() { | 230 void PasswordGenerationPopupViewViews::Layout() { |
149 if (password_view_) | 231 SetBoundsForWidth(controller_->popup_bounds().width()); |
150 password_view_->SetBoundsRect(controller_->password_bounds()); | |
151 | |
152 help_label_->SetBoundsRect(controller_->help_bounds()); | |
153 } | 232 } |
154 | 233 |
155 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 234 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
156 if (!controller_) | 235 if (!controller_) |
157 return; | 236 return; |
158 | 237 |
159 // Draw border and background. | 238 // Draw border and background. |
160 views::View::OnPaint(canvas); | 239 views::View::OnPaint(canvas); |
161 | 240 |
162 // Divider line needs to be drawn after OnPaint() otherwise the background | 241 // Divider line needs to be drawn after OnPaint() otherwise the background |
163 // will overwrite the divider. | 242 // will overwrite the divider. |
164 if (password_view_) | 243 if (password_view_) |
165 canvas->FillRect(controller_->divider_bounds(), kDividerColor); | 244 canvas->FillRect(divider_bounds_, kDividerColor); |
166 } | 245 } |
167 | 246 |
168 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( | 247 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked( |
169 const gfx::Range& range, int event_flags) { | 248 const gfx::Range& range, int event_flags) { |
170 controller_->OnSavedPasswordsLinkClicked(); | 249 controller_->OnSavedPasswordsLinkClicked(); |
171 } | 250 } |
172 | 251 |
252 bool PasswordGenerationPopupViewViews::IsPointInPasswordBounds( | |
253 const gfx::Point& point) { | |
254 return password_view_->bounds().Contains(point); | |
255 } | |
256 | |
173 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( | 257 PasswordGenerationPopupView* PasswordGenerationPopupView::Create( |
174 PasswordGenerationPopupController* controller) { | 258 PasswordGenerationPopupController* controller) { |
175 views::Widget* observing_widget = | 259 views::Widget* observing_widget = |
176 views::Widget::GetTopLevelWidgetForNativeView( | 260 views::Widget::GetTopLevelWidgetForNativeView( |
177 controller->container_view()); | 261 controller->container_view()); |
178 | 262 |
179 // If the top level widget can't be found, cancel the popup since we can't | 263 // If the top level widget can't be found, cancel the popup since we can't |
180 // fully set it up. | 264 // fully set it up. |
181 if (!observing_widget) | 265 if (!observing_widget) |
182 return NULL; | 266 return NULL; |
183 | 267 |
184 return new PasswordGenerationPopupViewViews(controller, observing_widget); | 268 return new PasswordGenerationPopupViewViews(controller, observing_widget); |
185 } | 269 } |
186 | 270 |
187 } // namespace autofill | 271 } // namespace autofill |
OLD | NEW |