15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/views/autofill/password_generation_popup_view_views.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string16.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/ui/autofill/popup_constants.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "grit/theme_resources.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/base/resource/resource_bundle.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/canvas.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/background.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/border.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/views/controls/image_view.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/label.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/styled_label.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/layout/box_layout.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/widget/widget.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace autofill {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The amount of whitespace that is present when there is no padding. Used
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to get the proper spacing in the help section.
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst int kHelpVerticalOffset = 5;
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Wrapper around just the text portions of the generation UI (password and
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// prompting text).
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass PasswordTextBox : public views::View {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PasswordTextBox() {}
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~PasswordTextBox() {}
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |suggestion_text| prompts the user to select the password,
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |generated_password| is the generated password, and |font_list| is the font
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // used for all text in this class.
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Init(const base::string16& suggestion_text,
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            const base::string16& generated_password,
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            const gfx::FontList& font_list) {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::BoxLayout* box_layout = new views::BoxLayout(
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        views::BoxLayout::kVertical, 0, 12, 5);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    box_layout->set_main_axis_alignment(
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SetLayoutManager(box_layout);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    views::Label* suggestion_label = new views::Label(
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        suggestion_text, font_list.DeriveWithStyle(gfx::Font::BOLD));
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    suggestion_label->SetEnabledColor(
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        PasswordGenerationPopupView::kPasswordTextColor);
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AddChildView(suggestion_label);
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    views::Label* password_label =
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        new views::Label(generated_password, font_list);
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    password_label->SetEnabledColor(
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        PasswordGenerationPopupView::kPasswordTextColor);
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AddChildView(password_label);
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // views::View:
6446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
6546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Send events to the parent view for handling.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(PasswordTextBox);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Class that shows the generated password and associated UI (currently a key
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// image and some explanatory text).
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass PasswordGenerationPopupViewViews::PasswordBox : public views::View {
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PasswordBox() {}
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~PasswordBox() {}
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |password| is the generated password, |suggestion| is the text prompting
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // the user to select the password, and |font_list| is the font used for all
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // the text.
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Init(const base::string16& password,
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            const base::string16& suggestion,
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            const gfx::FontList& font_list) {
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    views::BoxLayout* box_layout = new views::BoxLayout(
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        views::BoxLayout::kHorizontal,
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        PasswordGenerationPopupController::kHorizontalPadding,
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        0,
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        15);
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    box_layout->set_main_axis_alignment(
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    SetLayoutManager(box_layout);
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    views::ImageView* key_image = new views::ImageView();
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    key_image->SetImage(
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        ui::ResourceBundle::GetSharedInstance().GetImageNamed(
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            IDR_GENERATE_PASSWORD_KEY).ToImageSkia());
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AddChildView(key_image);
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    PasswordTextBox* password_text_box = new PasswordTextBox();
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    password_text_box->Init(suggestion, password, font_list);
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AddChildView(password_text_box);
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // views::View:
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Send events to the parent view for handling.
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return false;
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(PasswordBox);
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews(
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PasswordGenerationPopupController* controller,
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::Widget* observing_widget)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : AutofillPopupBaseView(controller, observing_widget),
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      password_view_(NULL),
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      font_list_(ResourceBundle::GetSharedInstance().GetFontList(
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          ResourceBundle::SmallFont)),
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller_(controller) {
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (controller_->display_password())
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CreatePasswordView();
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  help_label_ = new views::StyledLabel(controller_->HelpText(), this);
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  help_label_->SetBaseFontList(font_list_);
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  help_label_->SetLineHeight(20);
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::StyledLabel::RangeStyleInfo default_style;
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  default_style.color = kExplanatoryTextColor;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  help_label_->SetDefaultStyle(default_style);
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  views::StyledLabel::RangeStyleInfo link_style =
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      views::StyledLabel::RangeStyleInfo::CreateForLink();
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  link_style.disable_line_wrapping = false;
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  help_label_->AddStyleRange(controller_->HelpTextLinkRange(), link_style);
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  help_label_->set_background(
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      views::Background::CreateSolidBackground(
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          kExplanatoryTextBackgroundColor));
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  help_label_->SetBorder(views::Border::CreateEmptyBorder(
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      PasswordGenerationPopupController::kHelpVerticalPadding -
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      kHelpVerticalOffset,
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      PasswordGenerationPopupController::kHorizontalPadding,
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      PasswordGenerationPopupController::kHelpVerticalPadding -
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      kHelpVerticalOffset,
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      PasswordGenerationPopupController::kHorizontalPadding));
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddChildView(help_label_);
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  set_background(views::Background::CreateSolidBackground(kPopupBackground));
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordGenerationPopupViewViews::~PasswordGenerationPopupViewViews() {}
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::CreatePasswordView() {
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (password_view_)
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  password_view_ = new PasswordBox();
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  password_view_->Init(controller_->password(),
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       controller_->SuggestedText(),
165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       font_list_);
166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  password_view_->SetPosition(gfx::Point(kPopupBorderThickness,
167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                         kPopupBorderThickness));
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  password_view_->SizeToPreferredSize();
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddChildView(password_view_);
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
172116680a4aac90f2aa7413d9095a592090648e557Ben Murdochgfx::Size PasswordGenerationPopupViewViews::GetPreferredSizeOfPasswordView() {
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int height = kPopupBorderThickness;
174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (controller_->display_password()) {
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Add divider height as well.
17603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    height +=
17703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        PasswordGenerationPopupController::kPopupPasswordSectionHeight + 1;
178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int width = controller_->GetMinimumWidth();
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int popup_width = width - 2 * kPopupBorderThickness;
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  height += help_label_->GetHeightForWidth(popup_width);
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return gfx::Size(width, height + kPopupBorderThickness);
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::Show() {
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DoShow();
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::Hide() {
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The controller is no longer valid after it hides us.
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller_ = NULL;
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DoHide();
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::UpdateBoundsAndRedrawPopup() {
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DoUpdateBoundsAndRedrawPopup();
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::PasswordSelectionUpdated() {
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!password_view_)
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  password_view_->set_background(
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      views::Background::CreateSolidBackground(
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          controller_->password_selected() ?
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          kHoveredBackgroundColor :
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          kPopupBackground));
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::Layout() {
212116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Need to leave room for the border.
213116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y = kPopupBorderThickness;
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int popup_width = bounds().width() - 2 * kPopupBorderThickness;
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (controller_->display_password()) {
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Currently the UI can change from not offering a password to offering
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // a password (e.g. the user is editing a generated password and deletes
218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // it), but it can't change the other way around.
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    CreatePasswordView();
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    password_view_->SetBounds(
22103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        kPopupBorderThickness,
22203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        y,
22303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        popup_width,
22403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        PasswordGenerationPopupController::kPopupPasswordSectionHeight);
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    divider_bounds_ =
226116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        gfx::Rect(kPopupBorderThickness, password_view_->bounds().bottom(),
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  popup_width, 1);
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    y = divider_bounds_.bottom();
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  help_label_->SetBounds(kPopupBorderThickness, y, popup_width,
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         help_label_->GetHeightForWidth(popup_width));
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::OnPaint(gfx::Canvas* canvas) {
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!controller_)
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Draw border and background.
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::View::OnPaint(canvas);
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Divider line needs to be drawn after OnPaint() otherwise the background
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // will overwrite the divider.
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (password_view_)
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    canvas->FillRect(divider_bounds_, kDividerColor);
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordGenerationPopupViewViews::StyledLabelLinkClicked(
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Range& range, int event_flags) {
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller_->OnSavedPasswordsLinkClicked();
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool PasswordGenerationPopupViewViews::IsPointInPasswordBounds(
254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const gfx::Point& point) {
2556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (password_view_)
2566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return password_view_->bounds().Contains(point);
2576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return false;
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordGenerationPopupView* PasswordGenerationPopupView::Create(
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PasswordGenerationPopupController* controller) {
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::Widget* observing_widget =
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      views::Widget::GetTopLevelWidgetForNativeView(
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          controller->container_view());
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the top level widget can't be found, cancel the popup since we can't
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // fully set it up.
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!observing_widget)
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new PasswordGenerationPopupViewViews(controller, observing_widget);
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace autofill
275