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

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: Address comments Created 3 years, 6 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
« no previous file with comments | « ash/login/ui/login_password_view.h ('k') | ash/login/ui/login_password_view_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/image_view.h"
14 #include "ui/views/controls/separator.h"
15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/layout/fill_layout.h"
18
19 namespace ash {
20 namespace {
21
22 // Width/height of the submit button.
23 constexpr int kSubmitButtonWidthDp = 20;
24 constexpr int kSubmitButtonHeightDp = 20;
25
26 // Width/height of the password inputfield.
27 constexpr int kPasswordInputWidthDp = 184;
28 constexpr int kPasswordInputHeightDp = 40;
29
30 // Total width of the password view.
31 constexpr int kPasswordTotalWidthDp = 204;
32
33 // Distance between the last password dot and the submit arrow/button.
34 constexpr int kDistanceBetweenPasswordAndSubmitDp = 0;
35
36 const char* kLoginPasswordViewName = "LoginPasswordView";
37
38 } // namespace
39
40 LoginPasswordView::TestApi::TestApi(LoginPasswordView* view) : view_(view) {}
41
42 LoginPasswordView::TestApi::~TestApi() = default;
43
44 views::View* LoginPasswordView::TestApi::textfield() const {
45 return view_->textfield_;
46 }
47
48 views::View* LoginPasswordView::TestApi::submit_button() const {
49 return view_->submit_button_;
50 }
51
52 LoginPasswordView::LoginPasswordView(const OnPasswordSubmit& on_submit)
53 : on_submit_(on_submit) {
54 DCHECK(on_submit_);
55 auto* root_layout = new views::BoxLayout(views::BoxLayout::kVertical);
56 root_layout->set_main_axis_alignment(
57 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
58 SetLayoutManager(root_layout);
59
60 auto* row = new views::View();
61 AddChildView(row);
62 auto* layout =
63 new views::BoxLayout(views::BoxLayout::kHorizontal, gfx::Insets(),
64 kDistanceBetweenPasswordAndSubmitDp);
65 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
66 row->SetLayoutManager(layout);
67
68 // Password textfield. We control the textfield size by sizing the parent
69 // view, as the textfield will expand to fill it.
70 auto* textfield_sizer = new views::View();
71 textfield_sizer->SetLayoutManager(new SizeRangeLayout(
72 gfx::Size(kPasswordInputWidthDp, kPasswordInputHeightDp)));
73 textfield_ = new views::Textfield();
74 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
75 // TODO(jdufault): Real placeholder text.
76 textfield_->set_placeholder_text(base::ASCIIToUTF16("Password (FIXME)"));
77 textfield_->SetBorder(nullptr);
78
79 textfield_sizer->AddChildView(textfield_);
80 row->AddChildView(textfield_sizer);
81
82 // Submit button.
83 auto* submit_icon = new views::ImageView();
84 submit_icon->SetPreferredSize(
85 gfx::Size(kSubmitButtonWidthDp, kSubmitButtonHeightDp));
86 // TODO(jdufault): Use the real icon.
87 submit_icon->SetImage(
88 gfx::CreateVectorIcon(kSystemMenuArrowRightIcon, SK_ColorBLUE));
89 submit_button_ = new ash::tray::ButtonFromView(
90 submit_icon, this, TrayPopupInkDropStyle::HOST_CENTERED);
91 row->AddChildView(submit_button_);
92
93 // Separator on bottom.
94 AddChildView(new views::Separator());
95
96 // Make sure the textfield always starts with focus.
97 textfield_->RequestFocus();
98 }
99
100 LoginPasswordView::~LoginPasswordView() = default;
101
102 const char* LoginPasswordView::GetClassName() const {
103 return kLoginPasswordViewName;
104 }
105
106 gfx::Size LoginPasswordView::CalculatePreferredSize() const {
107 gfx::Size size = views::View::CalculatePreferredSize();
108 size.set_width(kPasswordTotalWidthDp);
109 return size;
110 }
111
112 bool LoginPasswordView::OnKeyPressed(const ui::KeyEvent& event) {
113 if (event.key_code() == ui::KeyboardCode::VKEY_RETURN) {
114 SubmitPassword();
115 return true;
116 }
117
118 return false;
119 }
120
121 void LoginPasswordView::ButtonPressed(views::Button* sender,
122 const ui::Event& event) {
123 DCHECK_EQ(sender, submit_button_);
124 SubmitPassword();
125 }
126
127 void LoginPasswordView::SubmitPassword() {
128 on_submit_.Run(textfield_->text());
129 textfield_->SetText(base::string16());
130 }
131
132 } // namespace ash
OLDNEW
« no previous file with comments | « ash/login/ui/login_password_view.h ('k') | ash/login/ui/login_password_view_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698