Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: ash/login/ui/login_password_view.cc

Issue 2896533002: cros: Simple password view for lock. Adds test support. (Closed)
Patch Set: Initial upload Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "ash/login/ui/login_password_view.h"
6
7 #include "ash/resources/vector_icons/vector_icons.h"
8 #include "ash/system/tray/size_range_layout.h"
9 #include "ash/system/user/button_from_view.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/gfx/paint_vector_icon.h"
12 #include "ui/views/border.h"
13 #include "ui/views/controls/button/button.h"
14 #include "ui/views/controls/image_view.h"
15 #include "ui/views/controls/separator.h"
16 #include "ui/views/controls/textfield/textfield.h"
17 #include "ui/views/layout/box_layout.h"
18 #include "ui/views/layout/fill_layout.h"
19
20 namespace ash {
21 namespace {
22
23 // Width/height of the submit button.
24 const int kSubmitButtonWidthDp = 20;
xiyuan 2017/05/24 20:11:02 nit: const -> constexpr ?
jdufault 2017/06/07 18:09:13 Done.
25 const int kSubmitButtonHeightDp = 20;
26
27 // Width/height of the password inputfield.
28 const int kPasswordInputWidthDp = 184;
29 const int kPasswordInputHeightDp = 40;
30
31 // Total width of the password view.
32 const int kPasswordTotalWidthDp = 204;
33
34 // Distance between the last password dot and the submit arrow/button.
35 const int kDistanceBetweenPasswordAndSubmitDp = 0;
36
37 const char* kLoginPasswordViewName = "LoginPasswordView";
38
39 } // namespace
40
41 LoginPasswordView::TestApi::TestApi(LoginPasswordView* view) : view_(view) {}
42
43 LoginPasswordView::TestApi::~TestApi() = default;
44
45 views::View* LoginPasswordView::TestApi::textfield() const {
46 return view_->textfield_;
47 }
48
49 views::View* LoginPasswordView::TestApi::submit_button() const {
50 return view_->submit_button_;
51 }
52
53 LoginPasswordView::LoginPasswordView(const OnPasswordSubmit& on_submit)
54 : on_submit_(on_submit) {
55 auto* root_layout =
56 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
57 root_layout->set_main_axis_alignment(
58 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
59 SetLayoutManager(root_layout);
60
61 {
62 auto* row = new views::View();
63 AddChildView(row);
64 auto* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
65 kDistanceBetweenPasswordAndSubmitDp);
66 layout->set_main_axis_alignment(
67 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
68 row->SetLayoutManager(layout);
69
70 // Password textfield. We place the actual textfield inside of an empty
71 // fixed-size view so we can precisely control the textfield sizing.
72 auto* textfield_sizer = new views::View();
73 textfield_sizer->SetLayoutManager(new SizeRangeLayout(
74 gfx::Size(kPasswordInputWidthDp, kPasswordInputHeightDp)));
75 textfield_ = new views::Textfield();
76 textfield_->SetLayoutManager(new views::FillLayout());
xiyuan 2017/05/24 20:11:02 Should this be set to |textfield_sizer | instead?
jdufault 2017/06/07 18:09:13 textfield_sizer has a SizeRangeLayout set on it al
xiyuan 2017/06/07 18:19:59 Can you elaboarate why |textfield_| needs a layout
jdufault 2017/06/08 00:11:34 Looks like the |textfield_| layout manager is not
77 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
78 // TODO(jdufault): Real placeholder text.
79 textfield_->set_placeholder_text(base::ASCIIToUTF16("Password (FIXME)"));
80 textfield_->SetBorder(nullptr);
81
82 textfield_sizer->AddChildView(textfield_);
83 row->AddChildView(textfield_sizer);
84
85 // Submit button.
86 auto* submit_icon = new views::ImageView();
87 submit_icon->set_preferred_size(
88 gfx::Size(kSubmitButtonWidthDp, kSubmitButtonHeightDp));
89 // TODO(jdufuault): Use the real icon.
90 submit_icon->SetImage(
91 gfx::CreateVectorIcon(kSystemMenuArrowRightIcon, SK_ColorBLUE));
92 submit_button_ = new ash::tray::ButtonFromView(
93 submit_icon, this, TrayPopupInkDropStyle::HOST_CENTERED);
94 row->AddChildView(submit_button_);
95 }
96
97 // Separator on bottom.
98 AddChildView(new views::Separator());
99
100 // Make sure the textfield always starts with focus.
101 textfield_->RequestFocus();
102 }
103
104 LoginPasswordView::~LoginPasswordView() = default;
105
106 const char* LoginPasswordView::GetClassName() const {
107 return kLoginPasswordViewName;
108 }
109
110 gfx::Size LoginPasswordView::GetPreferredSize() const {
111 gfx::Size size = views::View::GetPreferredSize();
112 size.set_width(kPasswordTotalWidthDp);
113 return size;
114 }
115
116 bool LoginPasswordView::OnKeyPressed(const ui::KeyEvent& event) {
117 if (event.key_code() == ui::KeyboardCode::VKEY_RETURN) {
118 SubmitPassword();
119 return true;
120 }
121
122 return false;
123 }
124
125 void LoginPasswordView::ButtonPressed(views::Button* sender,
126 const ui::Event& event) {
127 SubmitPassword();
xiyuan 2017/05/24 20:11:02 nit: DCHCKE_EQ(sender, submit_button_)
jdufault 2017/06/07 18:09:13 Done.
128 }
129
130 void LoginPasswordView::SubmitPassword() {
131 if (on_submit_)
132 on_submit_.Run(textfield_->text());
133 textfield_->SetText(base::string16());
134 textfield_->SchedulePaint();
xiyuan 2017/05/24 20:11:02 SchedulePaint() is probably not needed since SetTe
jdufault 2017/06/07 18:09:13 Done.
135 }
136
137 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698