Cleanup: json_schema_test.js now lives in extensions/test/data.
[chromium-blink-merge.git] / ash / system / user / accounts_detailed_view.cc
blob97f66910442c002afc4ff5dc753f948897debeb0
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"
7 #include <vector>
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"
29 namespace ash {
30 namespace tray {
32 namespace {
34 const int kAccountsViewVerticalPadding = 12;
35 const int kPrimaryAccountColumnSetID = 0;
36 const int kSecondaryAccountColumnSetID = 1;
37 const int kPaddingBetweenAccounts = 20;
39 } // namespace
41 AccountsDetailedView::AccountsDetailedView(TrayUser* owner,
42 user::LoginStatus login_status)
43 : TrayDetailsView(owner),
44 delegate_(NULL),
45 account_list_(NULL),
46 add_account_button_(NULL),
47 add_user_button_(NULL) {
48 std::string user_id = Shell::GetInstance()
49 ->session_state_delegate()
50 ->GetUserInfo(0)
51 ->GetUserID();
52 delegate_ =
53 Shell::GetInstance()->system_tray_delegate()->GetUserAccountsDelegate(
54 user_id);
55 delegate_->AddObserver(this);
56 AddHeader(login_status);
57 CreateScrollableList();
58 AddAccountList();
59 AddAddAccountButton();
60 AddFooter();
63 AccountsDetailedView::~AccountsDetailedView() {
64 delegate_->RemoveObserver(this);
67 void AccountsDetailedView::OnViewClicked(views::View* sender) {
68 if (sender == footer()->content())
69 TransitionToDefaultView();
70 else if (sender == add_account_button_)
71 delegate_->LaunchAddAccountDialog();
72 else
73 NOTREACHED();
76 void AccountsDetailedView::ButtonPressed(views::Button* sender,
77 const ui::Event& event) {
78 std::map<views::View*, std::string>::iterator it =
79 delete_button_to_account_id_.find(sender);
80 if (it != delete_button_to_account_id_.end()) {
81 delegate_->DeleteAccount(it->second);
82 } else if (add_user_button_ && add_user_button_ == sender) {
83 MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY);
84 Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
85 owner()->system_tray()->CloseSystemBubble();
86 } else {
87 NOTREACHED();
91 void AccountsDetailedView::AccountListChanged() { UpdateAccountList(); }
93 void AccountsDetailedView::AddHeader(user::LoginStatus login_status) {
94 views::View* user_view_container = new views::View;
95 user_view_container->SetLayoutManager(
96 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
97 user_view_container->SetBorder(
98 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor));
99 user_view_container->AddChildView(
100 new tray::UserView(owner(), login_status, 0, true));
101 AddChildView(user_view_container);
104 void AccountsDetailedView::AddAccountList() {
105 scroll_content()->SetBorder(
106 views::Border::CreateEmptyBorder(kAccountsViewVerticalPadding,
107 kTrayPopupPaddingHorizontal,
108 kAccountsViewVerticalPadding,
109 kTrayPopupPaddingHorizontal));
110 views::Label* account_list_title = new views::Label(
111 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCOUNT_LIST_TITLE));
112 account_list_title->SetEnabledColor(SkColorSetARGB(0x7f, 0, 0, 0));
113 account_list_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
114 scroll_content()->AddChildView(account_list_title);
115 account_list_ = new views::View();
116 UpdateAccountList();
117 scroll_content()->AddChildView(account_list_);
120 void AccountsDetailedView::AddAddAccountButton() {
121 SessionStateDelegate* session_state_delegate =
122 Shell::GetInstance()->session_state_delegate();
123 HoverHighlightView* add_account_button = new HoverHighlightView(this);
124 const user_manager::UserInfo* user_info =
125 session_state_delegate->GetUserInfo(0);
126 base::string16 user_name = user_info->GetGivenName();
127 if (user_name.empty())
128 user_name = user_info->GetDisplayName();
129 if (user_name.empty())
130 user_name = base::ASCIIToUTF16(user_info->GetEmail());
131 add_account_button->AddLabel(
132 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_ADD_ACCOUNT_LABEL,
133 user_name),
134 gfx::ALIGN_CENTER, false /* highlight */);
135 AddChildView(add_account_button);
136 add_account_button_ = add_account_button;
139 void AccountsDetailedView::AddFooter() {
140 CreateSpecialRow(IDS_ASH_STATUS_TRAY_ACCOUNTS_TITLE, this);
141 if (!IsMultiProfileSupportedAndUserActive())
142 return;
143 TrayPopupHeaderButton* add_user_button =
144 new TrayPopupHeaderButton(this,
145 IDR_AURA_UBER_TRAY_ADD_PROFILE,
146 IDR_AURA_UBER_TRAY_ADD_PROFILE,
147 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER,
148 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER,
149 IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT);
150 add_user_button->SetTooltipText(
151 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT));
152 footer()->AddButton(add_user_button);
153 add_user_button_ = add_user_button;
156 void AccountsDetailedView::UpdateAccountList() {
157 // Clear existing view.
158 delete_button_to_account_id_.clear();
159 account_list_->RemoveAllChildViews(true);
161 // Configuring layout manager.
162 views::GridLayout* layout = new views::GridLayout(account_list_);
163 account_list_->SetLayoutManager(layout);
164 views::ColumnSet* primary_account_row =
165 layout->AddColumnSet(kPrimaryAccountColumnSetID);
166 primary_account_row->AddColumn(views::GridLayout::LEADING,
167 views::GridLayout::CENTER,
168 1.0,
169 views::GridLayout::USE_PREF,
172 views::ColumnSet* secondary_account_row =
173 layout->AddColumnSet(kSecondaryAccountColumnSetID);
174 secondary_account_row->AddColumn(views::GridLayout::FILL,
175 views::GridLayout::CENTER,
176 1.0,
177 views::GridLayout::USE_PREF,
180 secondary_account_row->AddPaddingColumn(0.0, kTrayPopupPaddingBetweenItems);
181 secondary_account_row->AddColumn(views::GridLayout::FILL,
182 views::GridLayout::CENTER,
183 0.0,
184 views::GridLayout::USE_PREF,
188 // Adding primary account.
189 layout->AddPaddingRow(0.0, kPaddingBetweenAccounts);
190 layout->StartRow(0.0, kPrimaryAccountColumnSetID);
191 const std::string& primary_account = delegate_->GetPrimaryAccountId();
192 views::Label* primary_account_label =
193 new views::Label(l10n_util::GetStringFUTF16(
194 IDS_ASH_STATUS_TRAY_PRIMARY_ACCOUNT_LABEL,
195 base::ASCIIToUTF16(
196 delegate_->GetAccountDisplayName(primary_account))));
197 layout->AddView(primary_account_label);
199 // Adding secondary accounts.
200 const std::vector<std::string>& secondary_accounts =
201 delegate_->GetSecondaryAccountIds();
202 for (size_t i = 0; i < secondary_accounts.size(); ++i) {
203 layout->AddPaddingRow(0.0, kPaddingBetweenAccounts);
204 layout->StartRow(0.0, kSecondaryAccountColumnSetID);
205 const std::string& account_id = secondary_accounts[i];
206 views::Label* account_label = new views::Label(
207 base::ASCIIToUTF16(delegate_->GetAccountDisplayName(account_id)));
208 account_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
209 layout->AddView(account_label);
210 views::View* delete_button = CreateDeleteButton();
211 delete_button_to_account_id_[delete_button] = account_id;
212 layout->AddView(delete_button);
215 scroll_content()->SizeToPreferredSize();
216 scroller()->Layout();
219 views::View* AccountsDetailedView::CreateDeleteButton() {
220 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
221 views::ImageButton* delete_button = new views::ImageButton(this);
222 delete_button->SetImage(
223 views::Button::STATE_NORMAL,
224 rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT).ToImageSkia());
225 delete_button->SetImage(
226 views::Button::STATE_HOVERED,
227 rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER).ToImageSkia());
228 delete_button->SetImage(
229 views::Button::STATE_PRESSED,
230 rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER).ToImageSkia());
231 return delete_button;
234 } // namespace tray
235 } // namespace ash