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/user_view.h"
9 #include "ash/multi_profile_uma.h"
10 #include "ash/popup_message.h"
11 #include "ash/session/session_state_delegate.h"
12 #include "ash/shell.h"
13 #include "ash/shell_delegate.h"
14 #include "ash/system/tray/system_tray.h"
15 #include "ash/system/tray/system_tray_delegate.h"
16 #include "ash/system/tray/tray_popup_label_button.h"
17 #include "ash/system/tray/tray_popup_label_button_border.h"
18 #include "ash/system/user/button_from_view.h"
19 #include "ash/system/user/config.h"
20 #include "ash/system/user/rounded_image_view.h"
21 #include "ash/system/user/user_card_view.h"
22 #include "components/user_manager/user_info.h"
23 #include "grit/ash_resources.h"
24 #include "grit/ash_strings.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/views/layout/fill_layout.h"
28 #include "ui/views/painter.h"
29 #include "ui/wm/core/shadow_types.h"
36 const int kPublicAccountLogoutButtonBorderImagesNormal
[] = {
37 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
38 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
39 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
40 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
41 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
42 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
43 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
44 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
45 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND
,
48 const int kPublicAccountLogoutButtonBorderImagesHovered
[] = {
49 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
50 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
51 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
52 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
53 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_HOVER_BACKGROUND
,
54 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
55 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
56 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
57 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER
,
60 // When a hover border is used, it is starting this many pixels before the icon
62 const int kTrayUserTileHoverBorderInset
= 10;
64 // Offsetting the popup message relative to the tray menu.
65 const int kPopupMessageOffset
= 25;
67 // Switch to a user with the given |user_index|.
68 void SwitchUser(ash::MultiProfileIndex user_index
) {
69 // Do not switch users when the log screen is presented.
70 if (ash::Shell::GetInstance()
71 ->session_state_delegate()
72 ->IsUserSessionBlocked())
75 DCHECK(user_index
> 0);
76 ash::SessionStateDelegate
* delegate
=
77 ash::Shell::GetInstance()->session_state_delegate();
78 ash::MultiProfileUMA::RecordSwitchActiveUser(
79 ash::MultiProfileUMA::SWITCH_ACTIVE_USER_BY_TRAY
);
80 delegate
->SwitchActiveUser(delegate
->GetUserInfo(user_index
)->GetUserID());
83 class LogoutButton
: public TrayPopupLabelButton
{
85 // If |placeholder| is true, button is used as placeholder. That means that
86 // button is inactive and is not painted, but consume the same ammount of
87 // space, as if it was painted.
88 LogoutButton(views::ButtonListener
* listener
,
89 const base::string16
& text
,
91 : TrayPopupLabelButton(listener
, text
), placeholder_(placeholder
) {
92 SetVisible(!placeholder_
);
95 ~LogoutButton() override
{}
99 DISALLOW_COPY_AND_ASSIGN(LogoutButton
);
102 class UserViewMouseWatcherHost
: public views::MouseWatcherHost
{
104 explicit UserViewMouseWatcherHost(const gfx::Rect
& screen_area
)
105 : screen_area_(screen_area
) {}
106 ~UserViewMouseWatcherHost() override
{}
108 // Implementation of MouseWatcherHost.
109 bool Contains(const gfx::Point
& screen_point
,
110 views::MouseWatcherHost::MouseEventType type
) override
{
111 return screen_area_
.Contains(screen_point
);
115 gfx::Rect screen_area_
;
117 DISALLOW_COPY_AND_ASSIGN(UserViewMouseWatcherHost
);
120 // The menu item view which gets shown when the user clicks in multi profile
121 // mode onto the user item.
122 class AddUserView
: public views::View
{
124 // The |owner| is the view for which this view gets created.
125 AddUserView(ButtonFromView
* owner
);
126 ~AddUserView() override
;
128 // Get the anchor view for a message.
129 views::View
* anchor() { return anchor_
; }
132 // Overridden from views::View.
133 gfx::Size
GetPreferredSize() const override
;
135 // Create the additional client content for this item.
138 // This is the content we create and show.
139 views::View
* add_user_
;
141 // This is the owner view of this item.
142 ButtonFromView
* owner_
;
144 // The anchor view for targetted bubble messages.
145 views::View
* anchor_
;
147 DISALLOW_COPY_AND_ASSIGN(AddUserView
);
150 AddUserView::AddUserView(ButtonFromView
* owner
)
151 : add_user_(NULL
), owner_(owner
), anchor_(NULL
) {
153 owner_
->ForceBorderVisible(true);
156 AddUserView::~AddUserView() {
157 owner_
->ForceBorderVisible(false);
160 gfx::Size
AddUserView::GetPreferredSize() const {
161 return owner_
->bounds().size();
164 void AddUserView::AddContent() {
165 SetLayoutManager(new views::FillLayout());
166 set_background(views::Background::CreateSolidBackground(kBackgroundColor
));
168 add_user_
= new views::View
;
169 add_user_
->SetBorder(views::Border::CreateEmptyBorder(
170 0, kTrayUserTileHoverBorderInset
, 0, 0));
172 add_user_
->SetLayoutManager(new views::BoxLayout(
173 views::BoxLayout::kHorizontal
, 0, 0, kTrayPopupPaddingBetweenItems
));
174 AddChildViewAt(add_user_
, 0);
176 // Add the [+] icon which is also the anchor for messages.
177 RoundedImageView
* icon
= new RoundedImageView(kTrayAvatarCornerRadius
, true);
179 icon
->SetImage(*ui::ResourceBundle::GetSharedInstance()
180 .GetImageNamed(IDR_AURA_UBER_TRAY_ADD_MULTIPROFILE_USER
)
182 gfx::Size(kTrayAvatarSize
, kTrayAvatarSize
));
183 add_user_
->AddChildView(icon
);
185 // Add the command text.
186 views::Label
* command_label
= new views::Label(
187 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT
));
188 command_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
189 add_user_
->AddChildView(command_label
);
194 UserView::UserView(SystemTrayItem
* owner
,
195 user::LoginStatus login
,
196 MultiProfileIndex index
,
197 bool for_detailed_view
)
198 : multiprofile_index_(index
),
199 user_card_view_(NULL
),
201 is_user_card_button_(false),
202 logout_button_(NULL
),
203 add_user_enabled_(true),
204 for_detailed_view_(for_detailed_view
),
205 focus_manager_(NULL
) {
206 CHECK_NE(user::LOGGED_IN_NONE
, login
);
208 // Only the logged in user will have a background. All other users will have
209 // to allow the TrayPopupContainer highlighting the menu line.
210 set_background(views::Background::CreateSolidBackground(
211 login
== user::LOGGED_IN_PUBLIC
? kPublicAccountBackgroundColor
212 : kBackgroundColor
));
214 SetLayoutManager(new views::BoxLayout(
215 views::BoxLayout::kHorizontal
, 0, 0, kTrayPopupPaddingBetweenItems
));
216 // The logout button must be added before the user card so that the user card
217 // can correctly calculate the remaining available width.
218 // Note that only the current multiprofile user gets a button.
219 if (!multiprofile_index_
)
220 AddLogoutButton(login
);
224 UserView::~UserView() {
225 RemoveAddUserMenuOption();
228 void UserView::MouseMovedOutOfHost() {
229 RemoveAddUserMenuOption();
232 TrayUser::TestState
UserView::GetStateForTest() const {
233 if (add_menu_option_
.get()) {
234 return add_user_enabled_
? TrayUser::ACTIVE
: TrayUser::ACTIVE_BUT_DISABLED
;
237 if (!is_user_card_button_
)
238 return TrayUser::SHOWN
;
240 return static_cast<ButtonFromView
*>(user_card_view_
)->is_hovered_for_test()
245 gfx::Rect
UserView::GetBoundsInScreenOfUserButtonForTest() {
246 DCHECK(user_card_view_
);
247 return user_card_view_
->GetBoundsInScreen();
250 gfx::Size
UserView::GetPreferredSize() const {
251 gfx::Size size
= views::View::GetPreferredSize();
252 // Only the active user panel will be forced to a certain height.
253 if (!multiprofile_index_
) {
255 std::max(size
.height(), kTrayPopupItemHeight
+ GetInsets().height()));
260 int UserView::GetHeightForWidth(int width
) const {
261 return GetPreferredSize().height();
264 void UserView::Layout() {
265 gfx::Rect
contents_area(GetContentsBounds());
266 if (user_card_view_
&& logout_button_
) {
267 // Give the logout button the space it requests.
268 gfx::Rect logout_area
= contents_area
;
269 logout_area
.ClampToCenteredSize(logout_button_
->GetPreferredSize());
270 logout_area
.set_x(contents_area
.right() - logout_area
.width());
272 // Give the remaining space to the user card.
273 gfx::Rect user_card_area
= contents_area
;
274 int remaining_width
= contents_area
.width() - logout_area
.width();
275 if (IsMultiProfileSupportedAndUserActive() ||
276 IsMultiAccountSupportedAndUserActive()) {
277 // In multiprofile/multiaccount case |user_card_view_| and
278 // |logout_button_| have to have the same height.
279 int y
= std::min(user_card_area
.y(), logout_area
.y());
280 int height
= std::max(user_card_area
.height(), logout_area
.height());
281 logout_area
.set_y(y
);
282 logout_area
.set_height(height
);
283 user_card_area
.set_y(y
);
284 user_card_area
.set_height(height
);
286 // In multiprofile mode we have also to increase the size of the card by
287 // the size of the border to make it overlap with the logout button.
288 user_card_area
.set_width(std::max(0, remaining_width
+ 1));
290 // To make the logout button symmetrical with the user card we also make
291 // the button longer by the same size the hover area in front of the icon
293 logout_area
.set_width(logout_area
.width() +
294 kTrayUserTileHoverBorderInset
);
296 // In all other modes we have to make sure that there is enough spacing
298 remaining_width
-= kTrayPopupPaddingBetweenItems
;
300 user_card_area
.set_width(remaining_width
);
301 user_card_view_
->SetBoundsRect(user_card_area
);
302 logout_button_
->SetBoundsRect(logout_area
);
303 } else if (user_card_view_
) {
304 user_card_view_
->SetBoundsRect(contents_area
);
305 } else if (logout_button_
) {
306 logout_button_
->SetBoundsRect(contents_area
);
310 void UserView::ButtonPressed(views::Button
* sender
, const ui::Event
& event
) {
311 if (sender
== logout_button_
) {
312 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
313 ash::UMA_STATUS_AREA_SIGN_OUT
);
314 RemoveAddUserMenuOption();
315 Shell::GetInstance()->system_tray_delegate()->SignOut();
316 } else if (sender
== user_card_view_
&& !multiprofile_index_
&&
317 IsMultiAccountSupportedAndUserActive()) {
318 owner_
->TransitionDetailedView();
319 } else if (sender
== user_card_view_
&&
320 IsMultiProfileSupportedAndUserActive()) {
321 if (!multiprofile_index_
) {
322 ToggleAddUserMenuOption();
324 RemoveAddUserMenuOption();
325 SwitchUser(multiprofile_index_
);
326 // Since the user list is about to change the system menu should get
328 owner_
->system_tray()->CloseSystemBubble();
330 } else if (add_menu_option_
.get() &&
331 sender
== add_menu_option_
->GetContentsView()) {
332 RemoveAddUserMenuOption();
333 // Let the user add another account to the session.
334 MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY
);
335 Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
336 owner_
->system_tray()->CloseSystemBubble();
342 void UserView::OnWillChangeFocus(View
* focused_before
, View
* focused_now
) {
344 RemoveAddUserMenuOption();
347 void UserView::OnDidChangeFocus(View
* focused_before
, View
* focused_now
) {
348 // Nothing to do here.
351 void UserView::AddLogoutButton(user::LoginStatus login
) {
352 const base::string16 title
=
353 user::GetLocalizedSignOutStringForStatus(login
, true);
354 TrayPopupLabelButton
* logout_button
=
355 new LogoutButton(this, title
, for_detailed_view_
);
356 logout_button
->SetAccessibleName(title
);
357 logout_button_
= logout_button
;
358 // In public account mode, the logout button border has a custom color.
359 if (login
== user::LOGGED_IN_PUBLIC
) {
360 scoped_ptr
<TrayPopupLabelButtonBorder
> border(
361 new TrayPopupLabelButtonBorder());
362 border
->SetPainter(false,
363 views::Button::STATE_NORMAL
,
364 views::Painter::CreateImageGridPainter(
365 kPublicAccountLogoutButtonBorderImagesNormal
));
366 border
->SetPainter(false,
367 views::Button::STATE_HOVERED
,
368 views::Painter::CreateImageGridPainter(
369 kPublicAccountLogoutButtonBorderImagesHovered
));
370 border
->SetPainter(false,
371 views::Button::STATE_PRESSED
,
372 views::Painter::CreateImageGridPainter(
373 kPublicAccountLogoutButtonBorderImagesHovered
));
374 logout_button_
->SetBorder(border
.Pass());
376 AddChildView(logout_button_
);
379 void UserView::AddUserCard(user::LoginStatus login
) {
380 // Add padding around the panel.
381 SetBorder(views::Border::CreateEmptyBorder(kTrayPopupUserCardVerticalPadding
,
382 kTrayPopupPaddingHorizontal
,
383 kTrayPopupUserCardVerticalPadding
,
384 kTrayPopupPaddingHorizontal
));
386 views::TrayBubbleView
* bubble_view
=
387 owner_
->system_tray()->GetSystemBubble()->bubble_view();
389 bubble_view
->GetMaximumSize().width() -
390 (2 * kTrayPopupPaddingHorizontal
+ kTrayPopupPaddingBetweenItems
);
392 max_card_width
-= logout_button_
->GetPreferredSize().width();
394 new UserCardView(login
, max_card_width
, multiprofile_index_
);
395 // The entry is clickable when no system modal dialog is open and one of the
396 // multi user options is active.
397 bool clickable
= !Shell::GetInstance()->IsSystemModalWindowOpen() &&
398 (IsMultiProfileSupportedAndUserActive() ||
399 IsMultiAccountSupportedAndUserActive());
401 // To allow the border to start before the icon, reduce the size before and
402 // add an inset to the icon to get the spacing.
403 if (!multiprofile_index_
) {
404 SetBorder(views::Border::CreateEmptyBorder(
405 kTrayPopupUserCardVerticalPadding
,
406 kTrayPopupPaddingHorizontal
- kTrayUserTileHoverBorderInset
,
407 kTrayPopupUserCardVerticalPadding
,
408 kTrayPopupPaddingHorizontal
));
409 user_card_view_
->SetBorder(views::Border::CreateEmptyBorder(
410 0, kTrayUserTileHoverBorderInset
, 0, 0));
412 gfx::Insets insets
= gfx::Insets(1, 1, 1, 1);
413 views::View
* contents_view
= user_card_view_
;
414 ButtonFromView
* button
= NULL
;
415 if (!for_detailed_view_
) {
416 if (multiprofile_index_
) {
417 // Since the activation border needs to be drawn around the tile, we
418 // have to put the tile into another view which fills the menu panel,
419 // but keeping the offsets of the content.
420 contents_view
= new views::View();
421 contents_view
->SetBorder(views::Border::CreateEmptyBorder(
422 kTrayPopupUserCardVerticalPadding
,
423 kTrayPopupPaddingHorizontal
,
424 kTrayPopupUserCardVerticalPadding
,
425 kTrayPopupPaddingHorizontal
));
426 contents_view
->SetLayoutManager(new views::FillLayout());
427 SetBorder(views::Border::CreateEmptyBorder(0, 0, 0, 0));
428 contents_view
->AddChildView(user_card_view_
);
429 insets
= gfx::Insets(1, 1, 1, 3);
431 button
= new ButtonFromView(contents_view
,
433 !multiprofile_index_
,
436 // We want user card for detailed view to have exactly the same look
437 // as user card for default view. That's why we wrap it in a button
438 // without click listener and special hover behavior.
439 button
= new ButtonFromView(contents_view
, NULL
, false, insets
);
441 // A click on the button should not trigger a focus change.
442 button
->set_request_focus_on_press(false);
443 user_card_view_
= button
;
444 is_user_card_button_
= true;
446 AddChildViewAt(user_card_view_
, 0);
447 // Card for supervised user can consume more space than currently
448 // available. In that case we should increase system bubble's width.
449 if (login
== user::LOGGED_IN_PUBLIC
)
450 bubble_view
->SetWidth(GetPreferredSize().width());
453 void UserView::ToggleAddUserMenuOption() {
454 if (add_menu_option_
.get()) {
455 RemoveAddUserMenuOption();
459 // Note: We do not need to install a global event handler to delete this
460 // item since it will destroyed automatically before the menu / user menu item
462 add_menu_option_
.reset(new views::Widget
);
463 views::Widget::InitParams params
;
464 params
.type
= views::Widget::InitParams::TYPE_TOOLTIP
;
465 params
.keep_on_top
= true;
466 params
.context
= this->GetWidget()->GetNativeWindow();
467 params
.accept_events
= true;
468 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
469 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
470 add_menu_option_
->Init(params
);
471 add_menu_option_
->SetOpacity(0xFF);
472 add_menu_option_
->GetNativeWindow()->set_owned_by_parent(false);
473 SetShadowType(add_menu_option_
->GetNativeView(), wm::SHADOW_TYPE_NONE
);
475 // Position it below our user card.
476 gfx::Rect bounds
= user_card_view_
->GetBoundsInScreen();
477 bounds
.set_y(bounds
.y() + bounds
.height());
478 add_menu_option_
->SetBounds(bounds
);
481 add_menu_option_
->SetAlwaysOnTop(true);
482 add_menu_option_
->Show();
484 AddUserView
* add_user_view
=
485 new AddUserView(static_cast<ButtonFromView
*>(user_card_view_
));
487 const SessionStateDelegate
* delegate
=
488 Shell::GetInstance()->session_state_delegate();
490 SessionStateDelegate::AddUserError add_user_error
;
491 add_user_enabled_
= delegate
->CanAddUserToMultiProfile(&add_user_error
);
493 ButtonFromView
* button
= new ButtonFromView(add_user_view
,
494 add_user_enabled_
? this : NULL
,
496 gfx::Insets(1, 1, 1, 1));
497 button
->set_request_focus_on_press(false);
498 button
->SetAccessibleName(
499 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT
));
500 button
->ForceBorderVisible(true);
501 add_menu_option_
->SetContentsView(button
);
503 if (add_user_enabled_
) {
504 // We activate the entry automatically if invoked with focus.
505 if (user_card_view_
->HasFocus()) {
506 button
->GetFocusManager()->SetFocusedView(button
);
507 user_card_view_
->GetFocusManager()->SetFocusedView(button
);
510 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
512 switch (add_user_error
) {
513 case SessionStateDelegate::ADD_USER_ERROR_NOT_ALLOWED_PRIMARY_USER
:
514 message_id
= IDS_ASH_STATUS_TRAY_MESSAGE_NOT_ALLOWED_PRIMARY_USER
;
516 case SessionStateDelegate::ADD_USER_ERROR_MAXIMUM_USERS_REACHED
:
517 message_id
= IDS_ASH_STATUS_TRAY_MESSAGE_CANNOT_ADD_USER
;
519 case SessionStateDelegate::ADD_USER_ERROR_OUT_OF_USERS
:
520 message_id
= IDS_ASH_STATUS_TRAY_MESSAGE_OUT_OF_USERS
;
523 NOTREACHED() << "Unknown adding user error " << add_user_error
;
526 popup_message_
.reset(new PopupMessage(
527 bundle
.GetLocalizedString(IDS_ASH_STATUS_TRAY_CAPTION_CANNOT_ADD_USER
),
528 bundle
.GetLocalizedString(message_id
),
529 PopupMessage::ICON_WARNING
,
530 add_user_view
->anchor(),
531 views::BubbleBorder::TOP_LEFT
,
532 gfx::Size(parent()->bounds().width() - kPopupMessageOffset
, 0),
533 2 * kPopupMessageOffset
));
535 // Find the screen area which encloses both elements and sets then a mouse
536 // watcher which will close the "menu".
537 gfx::Rect area
= user_card_view_
->GetBoundsInScreen();
538 area
.set_height(2 * area
.height());
539 mouse_watcher_
.reset(
540 new views::MouseWatcher(new UserViewMouseWatcherHost(area
), this));
541 mouse_watcher_
->Start();
542 // Install a listener to focus changes so that we can remove the card when
543 // the focus gets changed. When called through the destruction of the bubble,
544 // the FocusManager cannot be determined anymore and we remember it here.
545 focus_manager_
= user_card_view_
->GetFocusManager();
546 focus_manager_
->AddFocusChangeListener(this);
549 void UserView::RemoveAddUserMenuOption() {
550 if (!add_menu_option_
.get())
552 focus_manager_
->RemoveFocusChangeListener(this);
553 focus_manager_
= NULL
;
554 if (user_card_view_
->GetFocusManager())
555 user_card_view_
->GetFocusManager()->ClearFocus();
556 popup_message_
.reset();
557 mouse_watcher_
.reset();
558 add_menu_option_
.reset();