OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ash/system/user/accounts_detailed_view.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 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 "grit/ash_resources.h" |
| 21 #include "grit/ui_resources.h" |
| 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/views/border.h" |
| 24 #include "ui/views/layout/box_layout.h" |
| 25 #include "ui/views/layout/grid_layout.h" |
| 26 |
| 27 namespace ash { |
| 28 namespace tray { |
| 29 |
| 30 namespace { |
| 31 |
| 32 const int kAccountsViewVerticalPadding = 12; |
| 33 const int kPrimaryAccountColumnSetID = 0; |
| 34 const int kSecondaryAccountColumnSetID = 1; |
| 35 const int kPaddingBetweenAccounts = 20; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 AccountsDetailedView::AccountsDetailedView(TrayUser* owner, |
| 40 user::LoginStatus login_status) |
| 41 : TrayDetailsView(owner), |
| 42 delegate_(NULL), |
| 43 account_list_(NULL), |
| 44 add_account_button_(NULL), |
| 45 add_user_button_(NULL) { |
| 46 std::string user_id = |
| 47 Shell::GetInstance()->session_state_delegate()->GetUserID(0); |
| 48 delegate_ = |
| 49 Shell::GetInstance()->system_tray_delegate()->GetUserAccountsDelegate( |
| 50 user_id); |
| 51 delegate_->AddObserver(this); |
| 52 AddHeader(login_status); |
| 53 CreateScrollableList(); |
| 54 AddAccountList(); |
| 55 AddAddAccountButton(); |
| 56 AddFooter(); |
| 57 } |
| 58 |
| 59 AccountsDetailedView::~AccountsDetailedView() { |
| 60 delegate_->RemoveObserver(this); |
| 61 } |
| 62 |
| 63 void AccountsDetailedView::OnViewClicked(views::View* sender) { |
| 64 if (sender == footer()->content()) |
| 65 TransitionToDefaultView(); |
| 66 else if (sender == add_account_button_) |
| 67 delegate_->LaunchAddAccountDialog(); |
| 68 else |
| 69 NOTREACHED(); |
| 70 } |
| 71 |
| 72 void AccountsDetailedView::ButtonPressed(views::Button* sender, |
| 73 const ui::Event& event) { |
| 74 std::map<views::View*, std::string>::iterator it = |
| 75 delete_button_to_account_id_.find(sender); |
| 76 if (it != delete_button_to_account_id_.end()) { |
| 77 delegate_->DeleteAccount(it->second); |
| 78 } else if (add_user_button_ && add_user_button_ == sender) { |
| 79 MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY); |
| 80 Shell::GetInstance()->system_tray_delegate()->ShowUserLogin(); |
| 81 owner()->system_tray()->CloseSystemBubble(); |
| 82 } else { |
| 83 NOTREACHED(); |
| 84 } |
| 85 } |
| 86 |
| 87 void AccountsDetailedView::AccountListChanged() { UpdateAccountList(); } |
| 88 |
| 89 void AccountsDetailedView::AddHeader(user::LoginStatus login_status) { |
| 90 views::View* user_view_container = new views::View; |
| 91 user_view_container->SetLayoutManager( |
| 92 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| 93 user_view_container->SetBorder( |
| 94 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor)); |
| 95 user_view_container->AddChildView( |
| 96 new tray::UserView(owner(), login_status, 0, true)); |
| 97 AddChildView(user_view_container); |
| 98 } |
| 99 |
| 100 void AccountsDetailedView::AddAccountList() { |
| 101 scroll_content()->SetBorder( |
| 102 views::Border::CreateEmptyBorder(kAccountsViewVerticalPadding, |
| 103 kTrayPopupPaddingHorizontal, |
| 104 kAccountsViewVerticalPadding, |
| 105 kTrayPopupPaddingHorizontal)); |
| 106 views::Label* account_list_title = new views::Label( |
| 107 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCOUNT_LIST_TITLE)); |
| 108 account_list_title->SetEnabledColor(SkColorSetARGB(0x7f, 0, 0, 0)); |
| 109 account_list_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 110 scroll_content()->AddChildView(account_list_title); |
| 111 account_list_ = new views::View(); |
| 112 UpdateAccountList(); |
| 113 scroll_content()->AddChildView(account_list_); |
| 114 } |
| 115 |
| 116 void AccountsDetailedView::AddAddAccountButton() { |
| 117 SessionStateDelegate* session_state_delegate = |
| 118 Shell::GetInstance()->session_state_delegate(); |
| 119 HoverHighlightView* add_account_button = new HoverHighlightView(this); |
| 120 base::string16 user_name = session_state_delegate->GetUserGivenName(0); |
| 121 if (user_name.empty()) |
| 122 user_name = session_state_delegate->GetUserDisplayName(0); |
| 123 if (user_name.empty()) |
| 124 user_name = base::ASCIIToUTF16(session_state_delegate->GetUserEmail(0)); |
| 125 add_account_button->AddLabel( |
| 126 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_ADD_ACCOUNT_LABEL, |
| 127 user_name), |
| 128 gfx::ALIGN_CENTER, |
| 129 gfx::Font::NORMAL); |
| 130 AddChildView(add_account_button); |
| 131 add_account_button_ = add_account_button; |
| 132 } |
| 133 |
| 134 void AccountsDetailedView::AddFooter() { |
| 135 CreateSpecialRow(IDS_ASH_STATUS_TRAY_ACCOUNTS_TITLE, this); |
| 136 if (!IsMultiProfileSupportedAndUserActive()) |
| 137 return; |
| 138 TrayPopupHeaderButton* add_user_button = |
| 139 new TrayPopupHeaderButton(this, |
| 140 IDR_AURA_UBER_TRAY_NETWORK_INFO, |
| 141 IDR_AURA_UBER_TRAY_NETWORK_INFO, |
| 142 IDR_AURA_UBER_TRAY_NETWORK_INFO_HOVER, |
| 143 IDR_AURA_UBER_TRAY_NETWORK_INFO_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; |
| 149 } |
| 150 |
| 151 void AccountsDetailedView::UpdateAccountList() { |
| 152 // Clear existing view. |
| 153 delete_button_to_account_id_.clear(); |
| 154 account_list_->RemoveAllChildViews(true); |
| 155 |
| 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::BASELINE, |
| 163 1.0, |
| 164 views::GridLayout::USE_PREF, |
| 165 0, |
| 166 0); |
| 167 views::ColumnSet* secondary_account_row = |
| 168 layout->AddColumnSet(kSecondaryAccountColumnSetID); |
| 169 secondary_account_row->AddColumn(views::GridLayout::FILL, |
| 170 views::GridLayout::BASELINE, |
| 171 1.0, |
| 172 views::GridLayout::USE_PREF, |
| 173 0, |
| 174 0); |
| 175 secondary_account_row->AddPaddingColumn(0.0, kTrayPopupPaddingBetweenItems); |
| 176 secondary_account_row->AddColumn(views::GridLayout::FILL, |
| 177 views::GridLayout::BASELINE, |
| 178 0.0, |
| 179 views::GridLayout::USE_PREF, |
| 180 0, |
| 181 0); |
| 182 |
| 183 // Adding primary account. |
| 184 layout->AddPaddingRow(0.0, kPaddingBetweenAccounts); |
| 185 layout->StartRow(0.0, kPrimaryAccountColumnSetID); |
| 186 const std::string& primary_account = delegate_->GetPrimaryAccount(); |
| 187 views::Label* primary_account_label = |
| 188 new views::Label(l10n_util::GetStringFUTF16( |
| 189 IDS_ASH_STATUS_TRAY_PRIMARY_ACCOUNT_LABEL, |
| 190 base::ASCIIToUTF16( |
| 191 delegate_->GetAccountDisplayName(primary_account)))); |
| 192 layout->AddView(primary_account_label); |
| 193 |
| 194 // Adding secondary accounts. |
| 195 const std::vector<std::string>& secondary_accounts = |
| 196 delegate_->GetSecondaryAccountsList(); |
| 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); |
| 208 } |
| 209 |
| 210 scroll_content()->SizeToPreferredSize(); |
| 211 scroller()->Layout(); |
| 212 } |
| 213 |
| 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(views::Button::STATE_NORMAL, |
| 218 rb.GetImageNamed(IDR_CLOSE_2).ToImageSkia()); |
| 219 delete_button->SetImage(views::Button::STATE_HOVERED, |
| 220 rb.GetImageNamed(IDR_CLOSE_2_H).ToImageSkia()); |
| 221 delete_button->SetImage(views::Button::STATE_PRESSED, |
| 222 rb.GetImageNamed(IDR_CLOSE_2_P).ToImageSkia()); |
| 223 return delete_button; |
| 224 } |
| 225 |
| 226 } // namespace tray |
| 227 } // namespace ash |
OLD | NEW |