OLD | NEW |
---|---|
(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; | |
James Cook
2017/06/08 17:26:31
optional: Pixel constants are generally assumed to
jdufault
2017/06/08 23:12:32
I've been confused in the past by the unit type so
| |
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 auto* root_layout = new views::BoxLayout(views::BoxLayout::kVertical); | |
55 root_layout->set_main_axis_alignment( | |
56 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
57 SetLayoutManager(root_layout); | |
58 | |
59 { | |
James Cook
2017/06/08 17:26:31
Do you need a new scope here?
jdufault
2017/06/08 23:12:32
Done.
| |
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( | |
66 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
67 row->SetLayoutManager(layout); | |
68 | |
69 // Password textfield. We control the textfield size by sizing the parent | |
70 // view, as the textfield will expand to fill it. | |
71 auto* textfield_sizer = new views::View(); | |
72 textfield_sizer->SetLayoutManager(new SizeRangeLayout( | |
73 gfx::Size(kPasswordInputWidthDp, kPasswordInputHeightDp))); | |
74 textfield_ = new views::Textfield(); | |
75 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | |
76 // TODO(jdufault): Real placeholder text. | |
77 textfield_->set_placeholder_text(base::ASCIIToUTF16("Password (FIXME)")); | |
78 textfield_->SetBorder(nullptr); | |
79 | |
80 textfield_sizer->AddChildView(textfield_); | |
81 row->AddChildView(textfield_sizer); | |
82 | |
83 // Submit button. | |
84 auto* submit_icon = new views::ImageView(); | |
85 submit_icon->SetPreferredSize( | |
86 gfx::Size(kSubmitButtonWidthDp, kSubmitButtonHeightDp)); | |
87 // TODO(jdufault): Use the real icon. | |
88 submit_icon->SetImage( | |
89 gfx::CreateVectorIcon(kSystemMenuArrowRightIcon, SK_ColorBLUE)); | |
90 submit_button_ = new ash::tray::ButtonFromView( | |
91 submit_icon, this, TrayPopupInkDropStyle::HOST_CENTERED); | |
92 row->AddChildView(submit_button_); | |
93 } | |
94 | |
95 // Separator on bottom. | |
96 AddChildView(new views::Separator()); | |
97 | |
98 // Make sure the textfield always starts with focus. | |
99 textfield_->RequestFocus(); | |
100 } | |
101 | |
102 LoginPasswordView::~LoginPasswordView() = default; | |
103 | |
104 const char* LoginPasswordView::GetClassName() const { | |
105 return kLoginPasswordViewName; | |
106 } | |
107 | |
108 gfx::Size LoginPasswordView::CalculatePreferredSize() const { | |
109 gfx::Size size = views::View::CalculatePreferredSize(); | |
110 size.set_width(kPasswordTotalWidthDp); | |
111 return size; | |
112 } | |
113 | |
114 bool LoginPasswordView::OnKeyPressed(const ui::KeyEvent& event) { | |
115 if (event.key_code() == ui::KeyboardCode::VKEY_RETURN) { | |
116 SubmitPassword(); | |
117 return true; | |
118 } | |
119 | |
120 return false; | |
121 } | |
122 | |
123 void LoginPasswordView::ButtonPressed(views::Button* sender, | |
124 const ui::Event& event) { | |
125 DCHECK_EQ(sender, submit_button_); | |
126 SubmitPassword(); | |
127 } | |
128 | |
129 void LoginPasswordView::SubmitPassword() { | |
130 if (on_submit_) | |
131 on_submit_.Run(textfield_->text()); | |
132 textfield_->SetText(base::string16()); | |
133 } | |
134 | |
135 } // namespace ash | |
OLD | NEW |