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/layout/box_layout.h" | |
14 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
15 | 16 |
16 namespace autofill { | 17 namespace autofill { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); | 21 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5); |
21 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x93, 0x93, 0x93); | 22 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x7F, 0x7F, 0x7F); |
22 const SkColor kDividerColor = SkColorSetRGB(0xE7, 0xE7, 0xE7); | 23 const SkColor kDividerColor = SkColorSetRGB(0xE9, 0xE9, 0xE9); |
23 | |
24 // This is the amount of vertical whitespace that is left above and below the | |
25 // password when it is highlighted. | |
26 const int kPasswordVerticalInset = 7; | |
27 | 24 |
28 // 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 |
29 // to get the proper spacing in the help section. | 26 // to get the proper spacing in the help section. |
30 const int kHelpVerticalOffset = 3; | 27 const int kHelpVerticalOffset = 3; |
31 | 28 |
29 // Class that shows the password and the suggestion side-by-side. | |
30 class PasswordRow : public views::View { | |
31 public: | |
32 PasswordRow(const base::string16& password, | |
33 const base::string16& suggestion, | |
34 const gfx::FontList& font_list, | |
35 int horizontal_border) { | |
36 set_clip_insets(gfx::Insets( | |
37 PasswordGenerationPopupView::kPasswordVerticalInset, 0, | |
38 PasswordGenerationPopupView::kPasswordVerticalInset, 0)); | |
39 views::BoxLayout* box_layout = new views::BoxLayout( | |
40 views::BoxLayout::kHorizontal, horizontal_border, 0, 0); | |
41 box_layout->set_spread_blank_space(true); | |
42 SetLayoutManager(box_layout); | |
43 | |
44 password_label_ = new views::Label(password, font_list); | |
45 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
46 AddChildView(password_label_); | |
47 | |
48 suggestion_label_ = new views::Label(suggestion, font_list); | |
49 suggestion_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
50 suggestion_label_->SetEnabledColor(kExplanatoryTextColor); | |
51 AddChildView(suggestion_label_); | |
52 } | |
53 virtual ~PasswordRow() {} | |
54 | |
55 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE { | |
56 // Have parent do event handling. | |
57 return false; | |
58 } | |
59 | |
60 private: | |
61 // Child views. Not owned. | |
62 views::Label* suggestion_label_; | |
63 views::Label* password_label_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(PasswordRow); | |
66 }; | |
67 | |
32 } // namespace | 68 } // namespace |
33 | 69 |
34 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( | 70 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( |
35 PasswordGenerationPopupController* controller, | 71 PasswordGenerationPopupController* controller, |
36 views::Widget* observing_widget) | 72 views::Widget* observing_widget) |
37 : AutofillPopupBaseView(controller, observing_widget), | 73 : AutofillPopupBaseView(controller, observing_widget), |
74 password_view_(NULL), | |
38 controller_(controller) { | 75 controller_(controller) { |
39 password_label_ = new views::Label(controller->password()); | 76 if (controller_->display_password()) |
40 password_label_->SetBoundsRect(controller->password_bounds()); | 77 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 | 78 |
48 base::string16 help_text = controller->HelpText(); | 79 help_label_ = |
49 base::string16 learn_more_link_text = controller->LearnMoreLink(); | 80 new views::StyledLabel(controller_->HelpText(), this); |
Evan Stade
2014/02/12 23:10:57
doesn't need to wrap
Garrett Casto
2014/02/12 23:38:51
Done.
| |
50 views::StyledLabel* help_label = | 81 help_label_->SetBaseFontList(controller_->font_list()); |
51 new views::StyledLabel(help_text + learn_more_link_text, this); | |
52 views::StyledLabel::RangeStyleInfo default_style; | 82 views::StyledLabel::RangeStyleInfo default_style; |
53 default_style.color = kExplanatoryTextColor; | 83 default_style.color = kExplanatoryTextColor; |
54 help_label->SetDefaultStyle(default_style); | 84 help_label_->SetDefaultStyle(default_style); |
55 help_label->AddStyleRange( | 85 help_label_->AddStyleRange( |
56 gfx::Range(help_text.size(), | 86 controller_->HelpTextLinkRange(), |
57 help_text.size() + learn_more_link_text.size()), | |
58 views::StyledLabel::RangeStyleInfo::CreateForLink()); | 87 views::StyledLabel::RangeStyleInfo::CreateForLink()); |
59 help_label->SetBoundsRect(controller->help_bounds()); | 88 |
60 help_label->set_background( | 89 help_label_->SetBoundsRect(controller_->help_bounds()); |
90 help_label_->set_background( | |
61 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); | 91 views::Background::CreateSolidBackground(kExplanatoryTextBackground)); |
62 help_label->SetBorder(views::Border::CreateEmptyBorder( | 92 help_label_->SetBorder(views::Border::CreateEmptyBorder( |
63 controller_->kHelpVerticalPadding - kHelpVerticalOffset, | 93 controller_->kHelpVerticalPadding - kHelpVerticalOffset, |
64 controller_->kHorizontalPadding, | 94 controller_->kHorizontalPadding, |
65 0, | 95 0, |
66 controller_->kHorizontalPadding)); | 96 controller_->kHorizontalPadding)); |
67 AddChildView(help_label); | 97 AddChildView(help_label_); |
68 | 98 |
69 set_background(views::Background::CreateSolidBackground(kPopupBackground)); | 99 set_background(views::Background::CreateSolidBackground(kPopupBackground)); |
70 } | 100 } |
71 | 101 |
72 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} | 102 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {} |
73 | 103 |
104 void PasswordGenerationPopupViewViews::CreatePasswordView() { | |
105 if (password_view_) | |
106 return; | |
107 | |
108 password_view_ = | |
109 new PasswordRow(controller_->password(), | |
Evan Stade
2014/02/12 23:10:57
don't need to wrap
Garrett Casto
2014/02/12 23:38:51
Done.
| |
110 controller_->SuggestedText(), | |
111 controller_->font_list(), | |
112 controller_->kHorizontalPadding); | |
113 AddChildView(password_view_); | |
114 } | |
115 | |
74 void PasswordGenerationPopupViewViews::Show() { | 116 void PasswordGenerationPopupViewViews::Show() { |
75 DoShow(); | 117 DoShow(); |
76 } | 118 } |
77 | 119 |
78 void PasswordGenerationPopupViewViews::Hide() { | 120 void PasswordGenerationPopupViewViews::Hide() { |
79 // The controller is no longer valid after it hides us. | 121 // The controller is no longer valid after it hides us. |
80 controller_ = NULL; | 122 controller_ = NULL; |
81 | 123 |
82 DoHide(); | 124 DoHide(); |
83 } | 125 } |
84 | 126 |
85 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() { | 127 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 | |
86 DoUpdateBoundsAndRedrawPopup(); | 134 DoUpdateBoundsAndRedrawPopup(); |
87 } | 135 } |
88 | 136 |
137 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() { | |
138 if (!password_view_) | |
139 return; | |
140 | |
141 password_view_->set_background( | |
142 views::Background::CreateSolidBackground( | |
143 controller_->password_selected() ? | |
144 kHoveredBackgroundColor : | |
145 kPopupBackground)); | |
146 } | |
147 | |
148 void PasswordGenerationPopupViewViews::Layout() { | |
149 if (password_view_) | |
150 password_view_->SetBoundsRect(controller_->password_bounds()); | |
151 | |
152 help_label_->SetBoundsRect(controller_->help_bounds()); | |
153 } | |
154 | |
89 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 155 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
90 if (!controller_) | 156 if (!controller_) |
91 return; | 157 return; |
92 | 158 |
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. | 159 // Draw border and background. |
102 views::View::OnPaint(canvas); | 160 views::View::OnPaint(canvas); |
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 |