Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / ui / views / autofill / password_generation_popup_view_views.cc
blob33272c1da37ccadb90a18ff2e324e36c03da5397
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/autofill/password_generation_popup_view_views.h"
7 #include "base/strings/string16.h"
8 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/styled_label.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/widget/widget.h"
17 namespace autofill {
19 namespace {
21 const SkColor kExplanatoryTextBackground = SkColorSetRGB(0xF5, 0xF5, 0xF5);
22 const SkColor kExplanatoryTextColor = SkColorSetRGB(0x66, 0x66, 0x66);
23 const SkColor kDividerColor = SkColorSetRGB(0xE9, 0xE9, 0xE9);
24 const SkColor kLinkColor = SkColorSetRGB(0x55, 0x59, 0xFE);
26 // The amount of whitespace that is present when there is no padding. Used
27 // to get the proper spacing in the help section.
28 const int kHelpVerticalOffset = 3;
30 // Class that shows the password and the suggestion side-by-side.
31 class PasswordRow : public views::View {
32 public:
33 PasswordRow(const base::string16& password,
34 const base::string16& suggestion,
35 const gfx::FontList& font_list,
36 int horizontal_border) {
37 set_clip_insets(gfx::Insets(
38 PasswordGenerationPopupView::kPasswordVerticalInset, 0,
39 PasswordGenerationPopupView::kPasswordVerticalInset, 0));
40 views::BoxLayout* box_layout = new views::BoxLayout(
41 views::BoxLayout::kHorizontal, horizontal_border, 0, 0);
42 box_layout->set_spread_blank_space(true);
43 SetLayoutManager(box_layout);
45 password_label_ = new views::Label(password, font_list);
46 password_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
47 AddChildView(password_label_);
49 suggestion_label_ = new views::Label(suggestion, font_list);
50 suggestion_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
51 suggestion_label_->SetEnabledColor(kExplanatoryTextColor);
52 AddChildView(suggestion_label_);
54 virtual ~PasswordRow() {}
56 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE {
57 // Have parent do event handling.
58 return false;
61 private:
62 // Child views. Not owned.
63 views::Label* suggestion_label_;
64 views::Label* password_label_;
66 DISALLOW_COPY_AND_ASSIGN(PasswordRow);
69 } // namespace
71 PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews(
72 PasswordGenerationPopupController* controller,
73 views::Widget* observing_widget)
74 : AutofillPopupBaseView(controller, observing_widget),
75 password_view_(NULL),
76 controller_(controller) {
77 if (controller_->display_password())
78 CreatePasswordView();
80 help_label_ = new views::StyledLabel(controller_->HelpText(), this);
81 help_label_->SetBaseFontList(controller_->font_list());
82 views::StyledLabel::RangeStyleInfo default_style;
83 default_style.color = kExplanatoryTextColor;
84 help_label_->SetDefaultStyle(default_style);
86 views::StyledLabel::RangeStyleInfo link_style =
87 views::StyledLabel::RangeStyleInfo::CreateForLink();
88 link_style.color = kLinkColor;
89 help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style);
91 help_label_->SetBoundsRect(controller_->help_bounds());
92 help_label_->set_background(
93 views::Background::CreateSolidBackground(kExplanatoryTextBackground));
94 help_label_->SetBorder(views::Border::CreateEmptyBorder(
95 controller_->kHelpVerticalPadding - kHelpVerticalOffset,
96 controller_->kHorizontalPadding,
98 controller_->kHorizontalPadding));
99 AddChildView(help_label_);
101 set_background(views::Background::CreateSolidBackground(kPopupBackground));
104 PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {}
106 void PasswordGenerationPopupViewViews::CreatePasswordView() {
107 if (password_view_)
108 return;
110 password_view_ = new PasswordRow(controller_->password(),
111 controller_->SuggestedText(),
112 controller_->font_list(),
113 controller_->kHorizontalPadding);
114 AddChildView(password_view_);
117 void PasswordGenerationPopupViewViews::Show() {
118 DoShow();
121 void PasswordGenerationPopupViewViews::Hide() {
122 // The controller is no longer valid after it hides us.
123 controller_ = NULL;
125 DoHide();
128 void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() {
129 // Currently the UI can change from not offering a password to offering
130 // a password (e.g. the user is editing a generated password and deletes it),
131 // but it can't change the other way around.
132 if (controller_->display_password())
133 CreatePasswordView();
135 DoUpdateBoundsAndRedrawPopup();
138 void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() {
139 if (!password_view_)
140 return;
142 password_view_->set_background(
143 views::Background::CreateSolidBackground(
144 controller_->password_selected() ?
145 kHoveredBackgroundColor :
146 kPopupBackground));
149 void PasswordGenerationPopupViewViews::Layout() {
150 if (password_view_)
151 password_view_->SetBoundsRect(controller_->password_bounds());
153 help_label_->SetBoundsRect(controller_->help_bounds());
156 void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) {
157 if (!controller_)
158 return;
160 // Draw border and background.
161 views::View::OnPaint(canvas);
163 // Divider line needs to be drawn after OnPaint() otherwise the background
164 // will overwrite the divider.
165 if (password_view_)
166 canvas->FillRect(controller_->divider_bounds(), kDividerColor);
169 void PasswordGenerationPopupViewViews::StyledLabelLinkClicked(
170 const gfx::Range& range, int event_flags) {
171 controller_->OnSavedPasswordsLinkClicked();
174 PasswordGenerationPopupView* PasswordGenerationPopupView::Create(
175 PasswordGenerationPopupController* controller) {
176 views::Widget* observing_widget =
177 views::Widget::GetTopLevelWidgetForNativeView(
178 controller->container_view());
180 // If the top level widget can't be found, cancel the popup since we can't
181 // fully set it up.
182 if (!observing_widget)
183 return NULL;
185 return new PasswordGenerationPopupViewViews(controller, observing_widget);
188 } // namespace autofill