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 "ash/system/user/accounts_detailed_view.h"
9 #include "ash/multi_profile_uma.h"
10 #include "ash/shell.h"
11 #include "ash/system/tray/fixed_sized_scroll_view.h"
12 #include "ash/system/tray/hover_highlight_view.h"
13 #include "ash/system/tray/system_tray.h"
14 #include "ash/system/tray/system_tray_delegate.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_popup_header_button.h"
17 #include "ash/system/user/config.h"
18 #include "ash/system/user/tray_user.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "components/user_manager/user_info.h"
21 #include "grit/ash_resources.h"
22 #include "grit/ash_strings.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/views/border.h"
26 #include "ui/views/layout/box_layout.h"
27 #include "ui/views/layout/grid_layout.h"
34 const int kAccountsViewVerticalPadding
= 12;
35 const int kPrimaryAccountColumnSetID
= 0;
36 const int kSecondaryAccountColumnSetID
= 1;
37 const int kPaddingBetweenAccounts
= 20;
41 AccountsDetailedView::AccountsDetailedView(TrayUser
* owner
,
42 user::LoginStatus login_status
,
43 UserAccountsDelegate
* delegate
)
44 : TrayDetailsView(owner
),
46 account_list_(nullptr),
47 add_account_button_(nullptr),
48 add_user_button_(nullptr) {
50 delegate_
->AddObserver(this);
51 AddHeader(login_status
);
52 CreateScrollableList();
54 AddAddAccountButton();
58 AccountsDetailedView::~AccountsDetailedView() {
59 delegate_
->RemoveObserver(this);
62 void AccountsDetailedView::OnViewClicked(views::View
* sender
) {
63 if (sender
== footer()->content())
64 TransitionToDefaultView();
65 else if (sender
== add_account_button_
)
66 delegate_
->LaunchAddAccountDialog();
71 void AccountsDetailedView::ButtonPressed(views::Button
* sender
,
72 const ui::Event
& event
) {
73 std::map
<views::View
*, std::string
>::iterator it
=
74 delete_button_to_account_id_
.find(sender
);
75 if (it
!= delete_button_to_account_id_
.end()) {
76 delegate_
->DeleteAccount(it
->second
);
77 } else if (add_user_button_
&& add_user_button_
== sender
) {
78 MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY
);
79 Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
80 owner()->system_tray()->CloseSystemBubble();
86 void AccountsDetailedView::AccountListChanged() { UpdateAccountList(); }
88 void AccountsDetailedView::AddHeader(user::LoginStatus login_status
) {
89 views::View
* user_view_container
= new views::View
;
90 user_view_container
->SetLayoutManager(
91 new views::BoxLayout(views::BoxLayout::kVertical
, 0, 0, 0));
92 user_view_container
->SetBorder(
93 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor
));
94 user_view_container
->AddChildView(
95 new tray::UserView(owner(), login_status
, 0, true));
96 AddChildView(user_view_container
);
99 void AccountsDetailedView::AddAccountList() {
100 scroll_content()->SetBorder(
101 views::Border::CreateEmptyBorder(kAccountsViewVerticalPadding
,
102 kTrayPopupPaddingHorizontal
,
103 kAccountsViewVerticalPadding
,
104 kTrayPopupPaddingHorizontal
));
105 views::Label
* account_list_title
= new views::Label(
106 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCOUNT_LIST_TITLE
));
107 account_list_title
->SetEnabledColor(SkColorSetARGB(0x7f, 0, 0, 0));
108 account_list_title
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
109 scroll_content()->AddChildView(account_list_title
);
110 account_list_
= new views::View();
112 scroll_content()->AddChildView(account_list_
);
115 void AccountsDetailedView::AddAddAccountButton() {
116 SessionStateDelegate
* session_state_delegate
=
117 Shell::GetInstance()->session_state_delegate();
118 HoverHighlightView
* add_account_button
= new HoverHighlightView(this);
119 const user_manager::UserInfo
* user_info
=
120 session_state_delegate
->GetUserInfo(0);
121 base::string16 user_name
= user_info
->GetGivenName();
122 if (user_name
.empty())
123 user_name
= user_info
->GetDisplayName();
124 if (user_name
.empty())
125 user_name
= base::ASCIIToUTF16(user_info
->GetEmail());
126 add_account_button
->AddLabel(
127 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_ADD_ACCOUNT_LABEL
,
129 gfx::ALIGN_CENTER
, false /* highlight */);
130 AddChildView(add_account_button
);
131 add_account_button_
= add_account_button
;
134 void AccountsDetailedView::AddFooter() {
135 CreateSpecialRow(IDS_ASH_STATUS_TRAY_ACCOUNTS_TITLE
, this);
136 if (!IsMultiProfileSupportedAndUserActive())
138 TrayPopupHeaderButton
* add_user_button
=
139 new TrayPopupHeaderButton(this,
140 IDR_AURA_UBER_TRAY_ADD_PROFILE
,
141 IDR_AURA_UBER_TRAY_ADD_PROFILE
,
142 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER
,
143 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER
,
144 IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT
);
145 add_user_button
->SetTooltipText(
146 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT
));
147 footer()->AddButton(add_user_button
);
148 add_user_button_
= add_user_button
;
151 void AccountsDetailedView::UpdateAccountList() {
152 // Clear existing view.
153 delete_button_to_account_id_
.clear();
154 account_list_
->RemoveAllChildViews(true);
156 // Configuring layout manager.
157 views::GridLayout
* layout
= new views::GridLayout(account_list_
);
158 account_list_
->SetLayoutManager(layout
);
159 views::ColumnSet
* primary_account_row
=
160 layout
->AddColumnSet(kPrimaryAccountColumnSetID
);
161 primary_account_row
->AddColumn(views::GridLayout::LEADING
,
162 views::GridLayout::CENTER
,
164 views::GridLayout::USE_PREF
,
167 views::ColumnSet
* secondary_account_row
=
168 layout
->AddColumnSet(kSecondaryAccountColumnSetID
);
169 secondary_account_row
->AddColumn(views::GridLayout::FILL
,
170 views::GridLayout::CENTER
,
172 views::GridLayout::USE_PREF
,
175 secondary_account_row
->AddPaddingColumn(0.0, kTrayPopupPaddingBetweenItems
);
176 secondary_account_row
->AddColumn(views::GridLayout::FILL
,
177 views::GridLayout::CENTER
,
179 views::GridLayout::USE_PREF
,
183 // Adding primary account.
184 layout
->AddPaddingRow(0.0, kPaddingBetweenAccounts
);
185 layout
->StartRow(0.0, kPrimaryAccountColumnSetID
);
186 const std::string
& primary_account
= delegate_
->GetPrimaryAccountId();
187 views::Label
* primary_account_label
=
188 new views::Label(l10n_util::GetStringFUTF16(
189 IDS_ASH_STATUS_TRAY_PRIMARY_ACCOUNT_LABEL
,
191 delegate_
->GetAccountDisplayName(primary_account
))));
192 layout
->AddView(primary_account_label
);
194 // Adding secondary accounts.
195 const std::vector
<std::string
>& secondary_accounts
=
196 delegate_
->GetSecondaryAccountIds();
197 for (size_t i
= 0; i
< secondary_accounts
.size(); ++i
) {
198 layout
->AddPaddingRow(0.0, kPaddingBetweenAccounts
);
199 layout
->StartRow(0.0, kSecondaryAccountColumnSetID
);
200 const std::string
& account_id
= secondary_accounts
[i
];
201 views::Label
* account_label
= new views::Label(
202 base::ASCIIToUTF16(delegate_
->GetAccountDisplayName(account_id
)));
203 account_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
204 layout
->AddView(account_label
);
205 views::View
* delete_button
= CreateDeleteButton();
206 delete_button_to_account_id_
[delete_button
] = account_id
;
207 layout
->AddView(delete_button
);
210 scroll_content()->SizeToPreferredSize();
211 scroller()->Layout();
214 views::View
* AccountsDetailedView::CreateDeleteButton() {
215 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
216 views::ImageButton
* delete_button
= new views::ImageButton(this);
217 delete_button
->SetImage(
218 views::Button::STATE_NORMAL
,
219 rb
.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT
).ToImageSkia());
220 delete_button
->SetImage(
221 views::Button::STATE_HOVERED
,
222 rb
.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER
).ToImageSkia());
223 delete_button
->SetImage(
224 views::Button::STATE_PRESSED
,
225 rb
.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER
).ToImageSkia());
226 return delete_button
;