Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / system / user / accounts_detailed_view.cc
bloba89cfa1c71056c92c2109f504c74a5cda4565ebf
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 "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/views/border.h"
25 #include "ui/views/layout/box_layout.h"
26 #include "ui/views/layout/grid_layout.h"
28 namespace ash {
29 namespace tray {
31 namespace {
33 const int kAccountsViewVerticalPadding = 12;
34 const int kPrimaryAccountColumnSetID = 0;
35 const int kSecondaryAccountColumnSetID = 1;
36 const int kPaddingBetweenAccounts = 20;
38 } // namespace
40 AccountsDetailedView::AccountsDetailedView(TrayUser* owner,
41 user::LoginStatus login_status)
42 : TrayDetailsView(owner),
43 delegate_(NULL),
44 account_list_(NULL),
45 add_account_button_(NULL),
46 add_user_button_(NULL) {
47 std::string user_id = Shell::GetInstance()
48 ->session_state_delegate()
49 ->GetUserInfo(0)
50 ->GetUserID();
51 delegate_ =
52 Shell::GetInstance()->system_tray_delegate()->GetUserAccountsDelegate(
53 user_id);
54 delegate_->AddObserver(this);
55 AddHeader(login_status);
56 CreateScrollableList();
57 AddAccountList();
58 AddAddAccountButton();
59 AddFooter();
62 AccountsDetailedView::~AccountsDetailedView() {
63 delegate_->RemoveObserver(this);
66 void AccountsDetailedView::OnViewClicked(views::View* sender) {
67 if (sender == footer()->content())
68 TransitionToDefaultView();
69 else if (sender == add_account_button_)
70 delegate_->LaunchAddAccountDialog();
71 else
72 NOTREACHED();
75 void AccountsDetailedView::ButtonPressed(views::Button* sender,
76 const ui::Event& event) {
77 std::map<views::View*, std::string>::iterator it =
78 delete_button_to_account_id_.find(sender);
79 if (it != delete_button_to_account_id_.end()) {
80 delegate_->DeleteAccount(it->second);
81 } else if (add_user_button_ && add_user_button_ == sender) {
82 MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY);
83 Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
84 owner()->system_tray()->CloseSystemBubble();
85 } else {
86 NOTREACHED();
90 void AccountsDetailedView::AccountListChanged() { UpdateAccountList(); }
92 void AccountsDetailedView::AddHeader(user::LoginStatus login_status) {
93 views::View* user_view_container = new views::View;
94 user_view_container->SetLayoutManager(
95 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
96 user_view_container->SetBorder(
97 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor));
98 user_view_container->AddChildView(
99 new tray::UserView(owner(), login_status, 0, true));
100 AddChildView(user_view_container);
103 void AccountsDetailedView::AddAccountList() {
104 scroll_content()->SetBorder(
105 views::Border::CreateEmptyBorder(kAccountsViewVerticalPadding,
106 kTrayPopupPaddingHorizontal,
107 kAccountsViewVerticalPadding,
108 kTrayPopupPaddingHorizontal));
109 views::Label* account_list_title = new views::Label(
110 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCOUNT_LIST_TITLE));
111 account_list_title->SetEnabledColor(SkColorSetARGB(0x7f, 0, 0, 0));
112 account_list_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
113 scroll_content()->AddChildView(account_list_title);
114 account_list_ = new views::View();
115 UpdateAccountList();
116 scroll_content()->AddChildView(account_list_);
119 void AccountsDetailedView::AddAddAccountButton() {
120 SessionStateDelegate* session_state_delegate =
121 Shell::GetInstance()->session_state_delegate();
122 HoverHighlightView* add_account_button = new HoverHighlightView(this);
123 const user_manager::UserInfo* user_info =
124 session_state_delegate->GetUserInfo(0);
125 base::string16 user_name = user_info->GetGivenName();
126 if (user_name.empty())
127 user_name = user_info->GetDisplayName();
128 if (user_name.empty())
129 user_name = base::ASCIIToUTF16(user_info->GetEmail());
130 add_account_button->AddLabel(
131 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_ADD_ACCOUNT_LABEL,
132 user_name),
133 gfx::ALIGN_CENTER,
134 gfx::Font::NORMAL);
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