tray_user.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 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/tray_user.h"
6
7#include <algorithm>
8#include <climits>
9#include <vector>
10
11#include "ash/ash_switches.h"
12#include "ash/popup_message.h"
13#include "ash/root_window_controller.h"
14#include "ash/session_state_delegate.h"
15#include "ash/shelf/shelf_layout_manager.h"
16#include "ash/shell.h"
17#include "ash/shell_delegate.h"
18#include "ash/system/tray/system_tray.h"
19#include "ash/system/tray/system_tray_delegate.h"
20#include "ash/system/tray/system_tray_notifier.h"
21#include "ash/system/tray/tray_constants.h"
22#include "ash/system/tray/tray_item_view.h"
23#include "ash/system/tray/tray_popup_label_button.h"
24#include "ash/system/tray/tray_popup_label_button_border.h"
25#include "ash/system/tray/tray_utils.h"
26#include "base/i18n/rtl.h"
27#include "base/logging.h"
28#include "base/memory/scoped_vector.h"
29#include "base/strings/string16.h"
30#include "base/strings/string_util.h"
31#include "base/strings/utf_string_conversions.h"
32#include "grit/ash_resources.h"
33#include "grit/ash_strings.h"
34#include "skia/ext/image_operations.h"
35#include "third_party/skia/include/core/SkCanvas.h"
36#include "third_party/skia/include/core/SkPaint.h"
37#include "third_party/skia/include/core/SkPath.h"
38#include "ui/aura/window.h"
39#include "ui/base/l10n/l10n_util.h"
40#include "ui/base/range/range.h"
41#include "ui/base/resource/resource_bundle.h"
42#include "ui/base/text/text_elider.h"
43#include "ui/gfx/canvas.h"
44#include "ui/gfx/font.h"
45#include "ui/gfx/image/image.h"
46#include "ui/gfx/image/image_skia_operations.h"
47#include "ui/gfx/insets.h"
48#include "ui/gfx/rect.h"
49#include "ui/gfx/render_text.h"
50#include "ui/gfx/size.h"
51#include "ui/gfx/skia_util.h"
52#include "ui/views/border.h"
53#include "ui/views/bubble/tray_bubble_view.h"
54#include "ui/views/controls/button/button.h"
55#include "ui/views/controls/button/custom_button.h"
56#include "ui/views/controls/image_view.h"
57#include "ui/views/controls/label.h"
58#include "ui/views/controls/link.h"
59#include "ui/views/controls/link_listener.h"
60#include "ui/views/corewm/shadow_types.h"
61#include "ui/views/layout/box_layout.h"
62#include "ui/views/layout/fill_layout.h"
63#include "ui/views/mouse_watcher.h"
64#include "ui/views/painter.h"
65#include "ui/views/view.h"
66#include "ui/views/widget/widget.h"
67
68namespace {
69
70const int kUserDetailsVerticalPadding = 5;
71const int kUserCardVerticalPadding = 10;
72const int kInactiveUserCardVerticalPadding = 4;
73const int kProfileRoundedCornerRadius = 2;
74const int kUserIconSize = 27;
75const int kUserIconLargeSize = 32;
76const int kUserIconLargeCornerRadius = 2;
77const int kUserLabelToIconPadding = 5;
78
79// When a hover border is used, it is starting this many pixels before the icon
80// position.
81const int kTrayUserTileHoverBorderInset = 10;
82
83// The border color of the user button.
84const SkColor kBorderColor = 0xffdcdcdc;
85
86// The invisible word joiner character, used as a marker to indicate the start
87// and end of the user's display name in the public account user card's text.
88const char16 kDisplayNameMark[] = { 0x2060, 0 };
89
90const int kPublicAccountLogoutButtonBorderImagesNormal[] = {
91    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
92    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
93    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
94    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
95    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
96    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
97    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
98    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
99    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND,
100};
101
102const int kPublicAccountLogoutButtonBorderImagesHovered[] = {
103    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
104    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
105    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
106    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
107    IDR_AURA_TRAY_POPUP_LABEL_BUTTON_HOVER_BACKGROUND,
108    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
109    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
110    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
111    IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER,
112};
113
114// Offsetting the popup message relative to the tray menu.
115const int kPopupMessageOffset = 25;
116
117}  // namespace
118
119namespace ash {
120namespace internal {
121
122namespace tray {
123
124// A custom image view with rounded edges.
125class RoundedImageView : public views::View {
126 public:
127  // Constructs a new rounded image view with rounded corners of radius
128  // |corner_radius|. If |active_user| is set, the icon will be drawn in
129  // full colors - otherwise it will fade into the background.
130  RoundedImageView(int corner_radius, bool active_user);
131  virtual ~RoundedImageView();
132
133  // Set the image that should be displayed. The image contents is copied to the
134  // receiver's image.
135  void SetImage(const gfx::ImageSkia& img, const gfx::Size& size);
136
137  // Set the radii of the corners independantly.
138  void SetCornerRadii(int top_left,
139                      int top_right,
140                      int bottom_right,
141                      int bottom_left);
142
143 private:
144  // Overridden from views::View.
145  virtual gfx::Size GetPreferredSize() OVERRIDE;
146  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
147
148  gfx::ImageSkia image_;
149  gfx::ImageSkia resized_;
150  gfx::Size image_size_;
151  int corner_radius_[4];
152
153  // True if the given user is the active user and the icon should get
154  // painted as active.
155  bool active_user_;
156
157  DISALLOW_COPY_AND_ASSIGN(RoundedImageView);
158};
159
160// The user details shown in public account mode. This is essentially a label
161// but with custom painting code as the text is styled with multiple colors and
162// contains a link.
163class PublicAccountUserDetails : public views::View,
164                                 public views::LinkListener {
165 public:
166  PublicAccountUserDetails(SystemTrayItem* owner, int used_width);
167  virtual ~PublicAccountUserDetails();
168
169 private:
170  // Overridden from views::View.
171  virtual void Layout() OVERRIDE;
172  virtual gfx::Size GetPreferredSize() OVERRIDE;
173  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
174
175  // Overridden from views::LinkListener.
176  virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
177
178  // Calculate a preferred size that ensures the label text and the following
179  // link do not wrap over more than three lines in total for aesthetic reasons
180  // if possible.
181  void CalculatePreferredSize(SystemTrayItem* owner, int used_width);
182
183  base::string16 text_;
184  views::Link* learn_more_;
185  gfx::Size preferred_size_;
186  ScopedVector<gfx::RenderText> lines_;
187
188  DISALLOW_COPY_AND_ASSIGN(PublicAccountUserDetails);
189};
190
191// The button which holds the user card in case of multi profile.
192class UserCard : public views::CustomButton {
193 public:
194  UserCard(views::ButtonListener* listener, bool active_user);
195  virtual ~UserCard();
196
197  // Called when the border should remain even in the non highlighted state.
198  void ForceBorderVisible(bool show);
199
200  // Overridden from views::View
201  virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
202  virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
203
204  // Check if the item is hovered.
205  bool is_hovered_for_test() {return button_hovered_; }
206
207 private:
208  // Change the hover/active state of the "button" when the status changes.
209  void ShowActive();
210
211  // True if this is the active user.
212  bool is_active_user_;
213
214  // True if button is hovered.
215  bool button_hovered_;
216
217  // True if the border should be visible.
218  bool show_border_;
219
220  DISALLOW_COPY_AND_ASSIGN(UserCard);
221};
222
223class UserViewMouseWatcherHost : public views::MouseWatcherHost {
224public:
225 explicit UserViewMouseWatcherHost(const gfx::Rect& screen_area)
226     : screen_area_(screen_area) {}
227 virtual ~UserViewMouseWatcherHost() {}
228
229 // Implementation of MouseWatcherHost.
230 virtual bool Contains(const gfx::Point& screen_point,
231                       views::MouseWatcherHost::MouseEventType type) OVERRIDE {
232   return screen_area_.Contains(screen_point);
233 }
234
235private:
236 gfx::Rect screen_area_;
237
238 DISALLOW_COPY_AND_ASSIGN(UserViewMouseWatcherHost);
239};
240
241// The view of a user item.
242class UserView : public views::View,
243                 public views::ButtonListener,
244                 public views::MouseWatcherListener {
245 public:
246  UserView(SystemTrayItem* owner,
247           ash::user::LoginStatus login,
248           MultiProfileIndex index);
249  virtual ~UserView();
250
251  // Overridden from MouseWatcherListener:
252  virtual void MouseMovedOutOfHost() OVERRIDE;
253
254  TrayUser::TestState GetStateForTest() const;
255  gfx::Rect GetBoundsInScreenOfUserButtonForTest();
256
257 private:
258  // Overridden from views::View.
259  virtual gfx::Size GetPreferredSize() OVERRIDE;
260  virtual int GetHeightForWidth(int width) OVERRIDE;
261  virtual void Layout() OVERRIDE;
262
263  // Overridden from views::ButtonListener.
264  virtual void ButtonPressed(views::Button* sender,
265                             const ui::Event& event) OVERRIDE;
266
267  void AddLogoutButton(ash::user::LoginStatus login);
268  void AddUserCard(SystemTrayItem* owner, ash::user::LoginStatus login);
269
270  // Create a user icon representation for the user card.
271  views::View* CreateIconForUserCard(ash::user::LoginStatus login);
272
273  // Create the additional user card content for the retail logged in mode.
274  void AddLoggedInRetailModeUserCardContent();
275
276  // Create the additional user card content for the public mode.
277  void AddLoggedInPublicModeUserCardContent(SystemTrayItem* owner);
278
279  // Create the menu option to add another user. If |disabled| is set the user
280  // cannot actively click on the item.
281  void ToggleAddUserMenuOption();
282
283  MultiProfileIndex multiprofile_index_;
284  // The view of the user card.
285  views::View* user_card_view_;
286
287  // This is the owner system tray item of this view.
288  SystemTrayItem* owner_;
289
290  // True if |user_card_view_| is a |UserView| - otherwise it is only a
291  // |views::View|.
292  bool is_user_card_;
293  views::View* logout_button_;
294  scoped_ptr<ash::PopupMessage> popup_message_;
295  scoped_ptr<views::Widget> add_menu_option_;
296
297  // True when the add user panel is visible but not activatable.
298  bool add_user_visible_but_disabled_;
299
300  // The mouse watcher which takes care of out of window hover events.
301  scoped_ptr<views::MouseWatcher> mouse_watcher_;
302
303  DISALLOW_COPY_AND_ASSIGN(UserView);
304};
305
306// The menu item view which gets shown when the user clicks in multi profile
307// mode onto the user item.
308class AddUserView : public views::CustomButton,
309                    public views::ButtonListener {
310 public:
311  // The |owner| is the view for which this view gets created. The |listener|
312  // will get notified when this item gets clicked.
313  AddUserView(UserCard* owner, views::ButtonListener* listener);
314  virtual ~AddUserView();
315
316  // Get the anchor view for a message.
317  views::View* anchor() { return anchor_; }
318
319  // Overridden from views::ButtonListener.
320  virtual void ButtonPressed(views::Button* sender,
321                             const ui::Event& event) OVERRIDE;
322
323 private:
324  // Overridden from views::View.
325  virtual gfx::Size GetPreferredSize() OVERRIDE;
326  virtual int GetHeightForWidth(int width) OVERRIDE;
327  virtual void Layout() OVERRIDE;
328
329  // Create the additional client content for this item.
330  void AddContent();
331
332  // This is the content we create and show.
333  views::View* add_user_;
334
335  // This listener will get informed when someone clicks on this button.
336  views::ButtonListener* listener_;
337
338  // This is the owner view of this item.
339  UserCard* owner_;
340
341  // The anchor view for targetted bubble messages.
342  views::View* anchor_;
343
344  DISALLOW_COPY_AND_ASSIGN(AddUserView);
345};
346
347RoundedImageView::RoundedImageView(int corner_radius, bool active_user)
348    : active_user_(active_user) {
349  for (int i = 0; i < 4; ++i)
350    corner_radius_[i] = corner_radius;
351}
352
353RoundedImageView::~RoundedImageView() {}
354
355void RoundedImageView::SetImage(const gfx::ImageSkia& img,
356                                const gfx::Size& size) {
357  image_ = img;
358  image_size_ = size;
359
360  // Try to get the best image quality for the avatar.
361  resized_ = gfx::ImageSkiaOperations::CreateResizedImage(image_,
362      skia::ImageOperations::RESIZE_BEST, size);
363  if (GetWidget() && visible()) {
364    PreferredSizeChanged();
365    SchedulePaint();
366  }
367}
368
369void RoundedImageView::SetCornerRadii(int top_left,
370                                      int top_right,
371                                      int bottom_right,
372                                      int bottom_left) {
373  corner_radius_[0] = top_left;
374  corner_radius_[1] = top_right;
375  corner_radius_[2] = bottom_right;
376  corner_radius_[3] = bottom_left;
377}
378
379gfx::Size RoundedImageView::GetPreferredSize() {
380  return gfx::Size(image_size_.width() + GetInsets().width(),
381                   image_size_.height() + GetInsets().height());
382}
383
384void RoundedImageView::OnPaint(gfx::Canvas* canvas) {
385  View::OnPaint(canvas);
386  gfx::Rect image_bounds(size());
387  image_bounds.ClampToCenteredSize(GetPreferredSize());
388  image_bounds.Inset(GetInsets());
389  const SkScalar kRadius[8] = {
390    SkIntToScalar(corner_radius_[0]),
391    SkIntToScalar(corner_radius_[0]),
392    SkIntToScalar(corner_radius_[1]),
393    SkIntToScalar(corner_radius_[1]),
394    SkIntToScalar(corner_radius_[2]),
395    SkIntToScalar(corner_radius_[2]),
396    SkIntToScalar(corner_radius_[3]),
397    SkIntToScalar(corner_radius_[3])
398  };
399  SkPath path;
400  path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius);
401  SkPaint paint;
402  paint.setAntiAlias(true);
403  paint.setXfermodeMode(active_user_ ? SkXfermode::kSrcOver_Mode :
404                                       SkXfermode::kLuminosity_Mode);
405  canvas->DrawImageInPath(resized_, image_bounds.x(), image_bounds.y(),
406                          path, paint);
407}
408
409PublicAccountUserDetails::PublicAccountUserDetails(SystemTrayItem* owner,
410                                                   int used_width)
411    : learn_more_(NULL) {
412  const int inner_padding =
413      kTrayPopupPaddingHorizontal - kTrayPopupPaddingBetweenItems;
414  const bool rtl = base::i18n::IsRTL();
415  set_border(views::Border::CreateEmptyBorder(
416      kUserDetailsVerticalPadding, rtl ? 0 : inner_padding,
417      kUserDetailsVerticalPadding, rtl ? inner_padding : 0));
418
419  // Retrieve the user's display name and wrap it with markers.
420  // Note that since this is a public account it always has to be the primary
421  // user.
422  base::string16 display_name =
423      ash::Shell::GetInstance()->session_state_delegate()->
424          GetUserDisplayName(0);
425  RemoveChars(display_name, kDisplayNameMark, &display_name);
426  display_name = kDisplayNameMark[0] + display_name + kDisplayNameMark[0];
427  // Retrieve the domain managing the device and wrap it with markers.
428  base::string16 domain = UTF8ToUTF16(
429      ash::Shell::GetInstance()->system_tray_delegate()->GetEnterpriseDomain());
430  RemoveChars(domain, kDisplayNameMark, &domain);
431  base::i18n::WrapStringWithLTRFormatting(&domain);
432  // Retrieve the label text, inserting the display name and domain.
433  text_ = l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_PUBLIC_LABEL,
434                                     display_name, domain);
435
436  learn_more_ = new views::Link(l10n_util::GetStringUTF16(IDS_ASH_LEARN_MORE));
437  learn_more_->SetUnderline(false);
438  learn_more_->set_listener(this);
439  AddChildView(learn_more_);
440
441  CalculatePreferredSize(owner, used_width);
442}
443
444PublicAccountUserDetails::~PublicAccountUserDetails() {}
445
446void PublicAccountUserDetails::Layout() {
447  lines_.clear();
448  const gfx::Rect contents_area = GetContentsBounds();
449  if (contents_area.IsEmpty())
450    return;
451
452  // Word-wrap the label text.
453  const gfx::Font font;
454  std::vector<base::string16> lines;
455  ui::ElideRectangleText(text_, font, contents_area.width(),
456                         contents_area.height(), ui::ELIDE_LONG_WORDS, &lines);
457  // Loop through the lines, creating a renderer for each.
458  gfx::Point position = contents_area.origin();
459  ui::Range display_name(ui::Range::InvalidRange());
460  for (std::vector<base::string16>::const_iterator it = lines.begin();
461       it != lines.end(); ++it) {
462    gfx::RenderText* line = gfx::RenderText::CreateInstance();
463    line->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_UI);
464    line->SetText(*it);
465    const gfx::Size size(contents_area.width(), line->GetStringSize().height());
466    line->SetDisplayRect(gfx::Rect(position, size));
467    position.set_y(position.y() + size.height());
468
469    // Set the default text color for the line.
470    line->SetColor(kPublicAccountUserCardTextColor);
471
472    // If a range of the line contains the user's display name, apply a custom
473    // text color to it.
474    if (display_name.is_empty())
475      display_name.set_start(it->find(kDisplayNameMark));
476    if (!display_name.is_empty()) {
477      display_name.set_end(
478          it->find(kDisplayNameMark, display_name.start() + 1));
479      ui::Range line_range(0, it->size());
480      line->ApplyColor(kPublicAccountUserCardNameColor,
481                       display_name.Intersect(line_range));
482      // Update the range for the next line.
483      if (display_name.end() >= line_range.end())
484        display_name.set_start(0);
485      else
486        display_name = ui::Range::InvalidRange();
487    }
488
489    lines_.push_back(line);
490  }
491
492  // Position the link after the label text, separated by a space. If it does
493  // not fit onto the last line of the text, wrap the link onto its own line.
494  const gfx::Size last_line_size = lines_.back()->GetStringSize();
495  const int space_width = font.GetStringWidth(ASCIIToUTF16(" "));
496  const gfx::Size link_size = learn_more_->GetPreferredSize();
497  if (contents_area.width() - last_line_size.width() >=
498      space_width + link_size.width()) {
499    position.set_x(position.x() + last_line_size.width() + space_width);
500    position.set_y(position.y() - last_line_size.height());
501  }
502  position.set_y(position.y() - learn_more_->GetInsets().top());
503  gfx::Rect learn_more_bounds(position, link_size);
504  learn_more_bounds.Intersect(contents_area);
505  if (base::i18n::IsRTL()) {
506    const gfx::Insets insets = GetInsets();
507    learn_more_bounds.Offset(insets.right() - insets.left(), 0);
508  }
509  learn_more_->SetBoundsRect(learn_more_bounds);
510}
511
512gfx::Size PublicAccountUserDetails::GetPreferredSize() {
513  return preferred_size_;
514}
515
516void PublicAccountUserDetails::OnPaint(gfx::Canvas* canvas) {
517  for (ScopedVector<gfx::RenderText>::const_iterator it = lines_.begin();
518       it != lines_.end(); ++it) {
519    (*it)->Draw(canvas);
520  }
521  views::View::OnPaint(canvas);
522}
523
524void PublicAccountUserDetails::LinkClicked(views::Link* source,
525                                           int event_flags) {
526  DCHECK_EQ(source, learn_more_);
527  ash::Shell::GetInstance()->system_tray_delegate()->ShowPublicAccountInfo();
528}
529
530void PublicAccountUserDetails::CalculatePreferredSize(SystemTrayItem* owner,
531                                                      int used_width) {
532  const gfx::Font font;
533  const gfx::Size link_size = learn_more_->GetPreferredSize();
534  const int space_width = font.GetStringWidth(ASCIIToUTF16(" "));
535  const gfx::Insets insets = GetInsets();
536  views::TrayBubbleView* bubble_view =
537      owner->system_tray()->GetSystemBubble()->bubble_view();
538  int min_width = std::max(
539      link_size.width(),
540      bubble_view->GetPreferredSize().width() - (used_width + insets.width()));
541  int max_width = std::min(
542      font.GetStringWidth(text_) + space_width + link_size.width(),
543      bubble_view->GetMaximumSize().width() - (used_width + insets.width()));
544  // Do a binary search for the minimum width that ensures no more than three
545  // lines are needed. The lower bound is the minimum of the current bubble
546  // width and the width of the link (as no wrapping is permitted inside the
547  // link). The upper bound is the maximum of the largest allowed bubble width
548  // and the sum of the label text and link widths when put on a single line.
549  std::vector<base::string16> lines;
550  while (min_width < max_width) {
551    lines.clear();
552    const int width = (min_width + max_width) / 2;
553    const bool too_narrow = ui::ElideRectangleText(
554        text_, font, width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines) != 0;
555    int line_count = lines.size();
556    if (!too_narrow && line_count == 3 &&
557            width - font.GetStringWidth(lines.back()) <=
558            space_width + link_size.width()) {
559      ++line_count;
560    }
561    if (too_narrow || line_count > 3)
562      min_width = width + 1;
563    else
564      max_width = width;
565  }
566
567  // Calculate the corresponding height and set the preferred size.
568  lines.clear();
569  ui::ElideRectangleText(
570      text_, font, min_width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines);
571  int line_count = lines.size();
572  if (min_width - font.GetStringWidth(lines.back()) <=
573      space_width + link_size.width()) {
574    ++line_count;
575  }
576  const int line_height = font.GetHeight();
577  const int link_extra_height = std::max(
578      link_size.height() - learn_more_->GetInsets().top() - line_height, 0);
579  preferred_size_ = gfx::Size(
580      min_width + insets.width(),
581      line_count * line_height + link_extra_height + insets.height());
582
583  bubble_view->SetWidth(preferred_size_.width() + used_width);
584}
585
586UserCard::UserCard(views::ButtonListener* listener, bool active_user)
587    : CustomButton(listener),
588      is_active_user_(active_user),
589      button_hovered_(false),
590      show_border_(false) {
591  if (is_active_user_) {
592    set_background(
593        views::Background::CreateSolidBackground(kBackgroundColor));
594    ShowActive();
595  }
596}
597
598UserCard::~UserCard() {}
599
600void UserCard::ForceBorderVisible(bool show) {
601  show_border_ = show;
602  ShowActive();
603}
604
605void UserCard::OnMouseEntered(const ui::MouseEvent& event) {
606  if (is_active_user_) {
607    button_hovered_ = true;
608    background()->SetNativeControlColor(kHoverBackgroundColor);
609    ShowActive();
610  }
611}
612
613void UserCard::OnMouseExited(const ui::MouseEvent& event) {
614  if (is_active_user_) {
615    button_hovered_ = false;
616    background()->SetNativeControlColor(kBackgroundColor);
617    ShowActive();
618  }
619}
620
621void UserCard::ShowActive() {
622  int width = button_hovered_ || show_border_ ? 1 : 0;
623  set_border(views::Border::CreateSolidSidedBorder(width, width, width, 1,
624                                                   kBorderColor));
625  SchedulePaint();
626}
627
628UserView::UserView(SystemTrayItem* owner,
629                   ash::user::LoginStatus login,
630                   MultiProfileIndex index)
631    : multiprofile_index_(index),
632      user_card_view_(NULL),
633      owner_(owner),
634      is_user_card_(false),
635      logout_button_(NULL),
636      add_user_visible_but_disabled_(false) {
637  CHECK_NE(ash::user::LOGGED_IN_NONE, login);
638  if (!index) {
639    // Only the logged in user will have a background. All other users will have
640    // to allow the TrayPopupContainer highlighting the menu line.
641    set_background(views::Background::CreateSolidBackground(
642        login == ash::user::LOGGED_IN_PUBLIC ? kPublicAccountBackgroundColor :
643                                               kBackgroundColor));
644  }
645  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
646                                        kTrayPopupPaddingBetweenItems));
647  // The logout button must be added before the user card so that the user card
648  // can correctly calculate the remaining available width.
649  // Note that only the current multiprofile user gets a button.
650  AddLogoutButton(!multiprofile_index_ ? login : ash::user::LOGGED_IN_LOCKED);
651  AddUserCard(owner, login);
652}
653
654UserView::~UserView() {}
655
656void UserView::MouseMovedOutOfHost() {
657  popup_message_.reset();
658  mouse_watcher_.reset();
659  add_menu_option_.reset();
660}
661
662TrayUser::TestState UserView::GetStateForTest() const {
663  if (add_menu_option_.get()) {
664    return add_user_visible_but_disabled_ ? TrayUser::ACTIVE_BUT_DISABLED :
665                                            TrayUser::ACTIVE;
666  }
667
668  if (!is_user_card_)
669    return TrayUser::SHOWN;
670
671  return static_cast<UserCard*>(user_card_view_)->is_hovered_for_test() ?
672      TrayUser::HOVERED : TrayUser::SHOWN;
673}
674
675gfx::Rect UserView::GetBoundsInScreenOfUserButtonForTest() {
676  DCHECK(user_card_view_);
677  return user_card_view_->GetBoundsInScreen();
678}
679
680gfx::Size UserView::GetPreferredSize() {
681  gfx::Size size = views::View::GetPreferredSize();
682  // Only the active user panel will be forced to a certain height.
683  if (!multiprofile_index_) {
684    size.set_height(std::max(size.height(),
685                             kTrayPopupItemHeight + GetInsets().height()));
686  }
687  return size;
688}
689
690int UserView::GetHeightForWidth(int width) {
691  return GetPreferredSize().height();
692}
693
694void UserView::Layout() {
695  gfx::Rect contents_area(GetContentsBounds());
696  if (user_card_view_ && logout_button_) {
697    // Give the logout button the space it requests.
698    gfx::Rect logout_area = contents_area;
699    logout_area.ClampToCenteredSize(logout_button_->GetPreferredSize());
700    logout_area.set_x(contents_area.right() - logout_area.width());
701
702    // Give the remaining space to the user card.
703    gfx::Rect user_card_area = contents_area;
704    int remaining_width = contents_area.width() - logout_area.width();
705    if (ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
706      // In multiprofile case |user_card_view_| and |logout_button_| have to
707      // have the same height.
708      int y = std::min(user_card_area.y(), logout_area.y());
709      int height = std::max(user_card_area.height(), logout_area.height());
710      logout_area.set_y(y);
711      logout_area.set_height(height);
712      user_card_area.set_y(y);
713      user_card_area.set_height(height);
714
715      // In multiprofile mode we have also to increase the size of the card by
716      // the size of the border to make it overlap with the logout button.
717      user_card_area.set_width(std::max(0, remaining_width + 1));
718
719      // To make the logout button symmetrical with the user card we also make
720      // the button longer by the same size the hover area in front of the icon
721      // got inset.
722      logout_area.set_width(logout_area.width() +
723                            kTrayUserTileHoverBorderInset);
724    } else {
725      // In all other modes we have to make sure that there is enough spacing
726      // between the two.
727      remaining_width -= kTrayPopupPaddingBetweenItems;
728    }
729    user_card_area.set_width(remaining_width);
730    user_card_view_->SetBoundsRect(user_card_area);
731    logout_button_->SetBoundsRect(logout_area);
732  } else if (user_card_view_) {
733    user_card_view_->SetBoundsRect(contents_area);
734  } else if (logout_button_) {
735    logout_button_->SetBoundsRect(contents_area);
736  }
737}
738
739void UserView::ButtonPressed(views::Button* sender, const ui::Event& event) {
740  if (sender == logout_button_) {
741    ash::Shell::GetInstance()->system_tray_delegate()->SignOut();
742  } else if (sender == user_card_view_ &&
743             ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
744    if (!multiprofile_index_) {
745      ToggleAddUserMenuOption();
746    } else {
747      ash::SessionStateDelegate* delegate =
748          ash::Shell::GetInstance()->session_state_delegate();
749      delegate->SwitchActiveUser(delegate->GetUserEmail(multiprofile_index_));
750      // Since the user list is about to change the system menu should get
751      // closed.
752      owner_->system_tray()->CloseSystemBubble();
753    }
754  } else if (add_menu_option_.get() &&
755             sender == add_menu_option_->GetContentsView()) {
756    // Let the user add another account to the session.
757    ash::Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
758  } else {
759    NOTREACHED();
760  }
761}
762
763void UserView::AddLogoutButton(ash::user::LoginStatus login) {
764  const base::string16 title = ash::user::GetLocalizedSignOutStringForStatus(
765      login, true);
766  TrayPopupLabelButton* logout_button = new TrayPopupLabelButton(this, title);
767  logout_button->SetAccessibleName(title);
768  logout_button_ = logout_button;
769  // In public account mode, the logout button border has a custom color.
770  if (login == ash::user::LOGGED_IN_PUBLIC) {
771    TrayPopupLabelButtonBorder* border =
772        static_cast<TrayPopupLabelButtonBorder*>(logout_button_->border());
773    border->SetPainter(false, views::Button::STATE_NORMAL,
774                       views::Painter::CreateImageGridPainter(
775                           kPublicAccountLogoutButtonBorderImagesNormal));
776    border->SetPainter(false, views::Button::STATE_HOVERED,
777                       views::Painter::CreateImageGridPainter(
778                           kPublicAccountLogoutButtonBorderImagesHovered));
779    border->SetPainter(false, views::Button::STATE_PRESSED,
780                       views::Painter::CreateImageGridPainter(
781                           kPublicAccountLogoutButtonBorderImagesHovered));
782  }
783  AddChildView(logout_button_);
784}
785
786void UserView::AddUserCard(SystemTrayItem* owner,
787                           ash::user::LoginStatus login) {
788  // Add padding around the panel.
789  set_border(views::Border::CreateEmptyBorder(
790      kUserCardVerticalPadding, kTrayPopupPaddingHorizontal,
791      kUserCardVerticalPadding, kTrayPopupPaddingHorizontal));
792
793  if (ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled() &&
794      login != ash::user::LOGGED_IN_RETAIL_MODE) {
795    user_card_view_ = new UserCard(this, multiprofile_index_ == 0);
796    is_user_card_ = true;
797  } else {
798    user_card_view_ = new views::View();
799    is_user_card_ = false;
800  }
801
802  user_card_view_->SetLayoutManager(new views::BoxLayout(
803      views::BoxLayout::kHorizontal, 0, 0 , kTrayPopupPaddingBetweenItems));
804  AddChildViewAt(user_card_view_, 0);
805
806  if (login == ash::user::LOGGED_IN_RETAIL_MODE) {
807    AddLoggedInRetailModeUserCardContent();
808    return;
809  }
810
811  // The entire user card should trigger hover (the inner items get disabled).
812  user_card_view_->SetEnabled(true);
813  user_card_view_->set_notify_enter_exit_on_child(true);
814
815  if (login == ash::user::LOGGED_IN_PUBLIC) {
816    AddLoggedInPublicModeUserCardContent(owner);
817    return;
818  }
819
820  views::View* icon = CreateIconForUserCard(login);
821  user_card_view_->AddChildView(icon);
822
823  // To allow the border to start before the icon, reduce the size before and
824  // add an inset to the icon to get the spacing.
825  if (multiprofile_index_ == 0 &&
826      ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
827    icon->set_border(views::Border::CreateEmptyBorder(
828        0, kTrayUserTileHoverBorderInset, 0, 0));
829    set_border(views::Border::CreateEmptyBorder(
830        kUserCardVerticalPadding,
831        kTrayPopupPaddingHorizontal - kTrayUserTileHoverBorderInset,
832        kUserCardVerticalPadding,
833        kTrayPopupPaddingHorizontal));
834  }
835  ash::SessionStateDelegate* delegate =
836      ash::Shell::GetInstance()->session_state_delegate();
837  views::Label* username = NULL;
838  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
839  if (!multiprofile_index_) {
840    base::string16 user_name_string =
841        login == ash::user::LOGGED_IN_GUEST ?
842            bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_GUEST_LABEL) :
843            delegate->GetUserDisplayName(multiprofile_index_);
844    if (!user_name_string.empty()) {
845      username = new views::Label(user_name_string);
846      username->SetHorizontalAlignment(gfx::ALIGN_LEFT);
847    }
848  }
849
850  views::Label* additional = NULL;
851  if (login != ash::user::LOGGED_IN_GUEST) {
852    base::string16 user_email_string =
853        login == ash::user::LOGGED_IN_LOCALLY_MANAGED ?
854            bundle.GetLocalizedString(
855                IDS_ASH_STATUS_TRAY_LOCALLY_MANAGED_LABEL) :
856            UTF8ToUTF16(delegate->GetUserEmail(multiprofile_index_));
857    if (!user_email_string.empty()) {
858      additional = new views::Label(user_email_string);
859      additional->SetFont(bundle.GetFont(ui::ResourceBundle::SmallFont));
860      additional->SetHorizontalAlignment(gfx::ALIGN_LEFT);
861    }
862  }
863
864  // Adjust text properties dependent on if it is an active or inactive user.
865  if (multiprofile_index_) {
866    // Fade the text of non active users to 50%.
867    SkColor text_color = additional->enabled_color();
868    text_color = SkColorSetA(text_color, SkColorGetA(text_color) / 2);
869    if (additional)
870      additional->SetDisabledColor(text_color);
871    if (username)
872      username->SetDisabledColor(text_color);
873  }
874
875  if (additional && username) {
876    views::View* details = new views::View;
877    details->SetLayoutManager(new views::BoxLayout(
878        views::BoxLayout::kVertical, 0, kUserDetailsVerticalPadding, 0));
879    details->AddChildView(username);
880    details->AddChildView(additional);
881    user_card_view_->AddChildView(details);
882  } else {
883    if (username)
884      user_card_view_->AddChildView(username);
885    if (additional)
886      user_card_view_->AddChildView(additional);
887  }
888}
889
890views::View* UserView::CreateIconForUserCard(ash::user::LoginStatus login) {
891  RoundedImageView* icon = new RoundedImageView(kProfileRoundedCornerRadius,
892                                                multiprofile_index_ == 0);
893  icon->SetEnabled(false);
894  if (login == ash::user::LOGGED_IN_GUEST) {
895    icon->SetImage(*ui::ResourceBundle::GetSharedInstance().
896        GetImageNamed(IDR_AURA_UBER_TRAY_GUEST_ICON).ToImageSkia(),
897        gfx::Size(kUserIconSize, kUserIconSize));
898  } else {
899    icon->SetImage(
900        ash::Shell::GetInstance()->session_state_delegate()->
901            GetUserImage(multiprofile_index_),
902        gfx::Size(kUserIconSize, kUserIconSize));
903  }
904  return icon;
905}
906
907void UserView::AddLoggedInRetailModeUserCardContent() {
908  views::Label* details = new views::Label;
909  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
910  details->SetText(
911      bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_KIOSK_LABEL));
912  details->set_border(views::Border::CreateEmptyBorder(0, 4, 0, 1));
913  details->SetHorizontalAlignment(gfx::ALIGN_LEFT);
914  user_card_view_->AddChildView(details);
915}
916
917void UserView::AddLoggedInPublicModeUserCardContent(SystemTrayItem* owner) {
918  user_card_view_->AddChildView(
919      CreateIconForUserCard(ash::user::LOGGED_IN_PUBLIC));
920  user_card_view_->AddChildView(new PublicAccountUserDetails(
921      owner, GetPreferredSize().width() + kTrayPopupPaddingBetweenItems));
922}
923
924void UserView::ToggleAddUserMenuOption() {
925  if (add_menu_option_.get()) {
926    popup_message_.reset();
927    mouse_watcher_.reset();
928    add_menu_option_.reset();
929    return;
930  }
931
932  // Note: We do not need to install a global event handler to delete this
933  // item since it will destroyed automatically before the menu / user menu item
934  // gets destroyed..
935  const SessionStateDelegate* session_state_delegate =
936      ash::Shell::GetInstance()->session_state_delegate();
937  add_user_visible_but_disabled_ =
938      session_state_delegate->NumberOfLoggedInUsers() >=
939          session_state_delegate->GetMaximumNumberOfLoggedInUsers();
940  add_menu_option_.reset(new views::Widget);
941  views::Widget::InitParams params;
942  params.type = views::Widget::InitParams::TYPE_TOOLTIP;
943  params.keep_on_top = true;
944  params.context = this->GetWidget()->GetNativeWindow();
945  params.accept_events = true;
946  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
947  params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
948  add_menu_option_->Init(params);
949  add_menu_option_->SetOpacity(0xFF);
950  add_menu_option_->GetNativeWindow()->set_owned_by_parent(false);
951  SetShadowType(add_menu_option_->GetNativeView(),
952                views::corewm::SHADOW_TYPE_NONE);
953
954  // Position it below our user card.
955  gfx::Rect bounds = user_card_view_->GetBoundsInScreen();
956  bounds.set_y(bounds.y() + bounds.height());
957  add_menu_option_->SetBounds(bounds);
958
959  // Show the content.
960  AddUserView* add_user_view = new AddUserView(
961      static_cast<UserCard*>(user_card_view_), this);
962  add_menu_option_->SetContentsView(add_user_view);
963  add_menu_option_->SetAlwaysOnTop(true);
964  add_menu_option_->Show();
965  if (add_user_visible_but_disabled_) {
966    ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
967    popup_message_.reset(new PopupMessage(
968        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_CAPTION_CANNOT_ADD_USER),
969        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_MESSAGE_CANNOT_ADD_USER),
970        PopupMessage::ICON_WARNING,
971        add_user_view->anchor(),
972        views::BubbleBorder::TOP_LEFT,
973        gfx::Size(parent()->bounds().width() - kPopupMessageOffset, 0),
974        2 * kPopupMessageOffset));
975  }
976  // Find the screen area which encloses both elements and sets then a mouse
977  // watcher which will close the "menu".
978  gfx::Rect area = user_card_view_->GetBoundsInScreen();
979  area.set_height(2 * area.height());
980  mouse_watcher_.reset(new views::MouseWatcher(
981      new UserViewMouseWatcherHost(area),
982      this));
983  mouse_watcher_->Start();
984}
985
986AddUserView::AddUserView(UserCard* owner, views::ButtonListener* listener)
987    : CustomButton(listener_),
988      add_user_(NULL),
989      listener_(listener),
990      owner_(owner),
991      anchor_(NULL) {
992  AddContent();
993  owner_->ForceBorderVisible(true);
994}
995
996AddUserView::~AddUserView() {
997  owner_->ForceBorderVisible(false);
998}
999
1000gfx::Size AddUserView::GetPreferredSize() {
1001  return owner_->bounds().size();
1002}
1003
1004int AddUserView::GetHeightForWidth(int width) {
1005  return owner_->bounds().size().height();
1006}
1007
1008void AddUserView::Layout() {
1009  gfx::Rect contents_area(GetContentsBounds());
1010  add_user_->SetBoundsRect(contents_area);
1011}
1012
1013void AddUserView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1014  if (add_user_ == sender)
1015    listener_->ButtonPressed(this, event);
1016  else
1017    NOTREACHED();
1018}
1019
1020void AddUserView::AddContent() {
1021  set_notify_enter_exit_on_child(true);
1022
1023  const SessionStateDelegate* delegate =
1024      ash::Shell::GetInstance()->session_state_delegate();
1025  bool enable = delegate->NumberOfLoggedInUsers() <
1026                    delegate->GetMaximumNumberOfLoggedInUsers();
1027
1028  SetLayoutManager(new views::FillLayout());
1029  set_background(views::Background::CreateSolidBackground(kBackgroundColor));
1030
1031  // Add padding around the panel.
1032  set_border(views::Border::CreateSolidBorder(1, kBorderColor));
1033
1034  add_user_ = new UserCard(this, enable);
1035  add_user_->set_border(views::Border::CreateEmptyBorder(
1036      kUserCardVerticalPadding,
1037      kTrayPopupPaddingHorizontal- kTrayUserTileHoverBorderInset,
1038      kUserCardVerticalPadding,
1039      kTrayPopupPaddingHorizontal- kTrayUserTileHoverBorderInset));
1040
1041  add_user_->SetLayoutManager(new views::BoxLayout(
1042      views::BoxLayout::kHorizontal, 0, 0 , kTrayPopupPaddingBetweenItems));
1043  AddChildViewAt(add_user_, 0);
1044
1045  // Add the [+] icon which is also the anchor for messages.
1046  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
1047  RoundedImageView* icon = new RoundedImageView(kProfileRoundedCornerRadius,
1048                                                true);
1049  anchor_ = icon;
1050  icon->SetImage(*ui::ResourceBundle::GetSharedInstance().
1051      GetImageNamed(IDR_AURA_UBER_TRAY_ADD_MULTIPROFILE_USER).ToImageSkia(),
1052      gfx::Size(kUserIconSize, kUserIconSize));
1053  add_user_->AddChildView(icon);
1054
1055  // Add the command text.
1056  views::Label* command_label = new views::Label(
1057      bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT));
1058  command_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1059  add_user_->AddChildView(command_label);
1060}
1061
1062}  // namespace tray
1063
1064TrayUser::TrayUser(SystemTray* system_tray, MultiProfileIndex index)
1065    : SystemTrayItem(system_tray),
1066      multiprofile_index_(index),
1067      user_(NULL),
1068      layout_view_(NULL),
1069      avatar_(NULL),
1070      label_(NULL),
1071      separator_shown_(false) {
1072  Shell::GetInstance()->system_tray_notifier()->AddUserObserver(this);
1073}
1074
1075TrayUser::~TrayUser() {
1076  Shell::GetInstance()->system_tray_notifier()->RemoveUserObserver(this);
1077}
1078
1079TrayUser::TestState TrayUser::GetStateForTest() const {
1080  if (separator_shown_)
1081    return SEPARATOR;
1082  if (!user_)
1083    return HIDDEN;
1084  return user_->GetStateForTest();
1085}
1086
1087gfx::Rect TrayUser::GetUserPanelBoundsInScreenForTest() const {
1088  DCHECK(user_);
1089  return user_->GetBoundsInScreenOfUserButtonForTest();
1090}
1091
1092views::View* TrayUser::CreateTrayView(user::LoginStatus status) {
1093  CHECK(layout_view_ == NULL);
1094  // Only the current user gets an icon. All other users will only be
1095  // represented in the tray menu.
1096  if (multiprofile_index_)
1097    return NULL;
1098
1099  layout_view_ = new views::View();
1100  layout_view_->SetLayoutManager(
1101      new views::BoxLayout(views::BoxLayout::kHorizontal,
1102                           0, 0, kUserLabelToIconPadding));
1103  UpdateAfterLoginStatusChange(status);
1104  return layout_view_;
1105}
1106
1107views::View* TrayUser::CreateDefaultView(user::LoginStatus status) {
1108  if (status == user::LOGGED_IN_NONE)
1109    return NULL;
1110
1111  CHECK(user_ == NULL);
1112
1113  const SessionStateDelegate* session_state_delegate =
1114      ash::Shell::GetInstance()->session_state_delegate();
1115  int logged_in_users = session_state_delegate->NumberOfLoggedInUsers();
1116
1117  // If there are multiple users logged in, the users will be separated from the
1118  // rest of the menu by a separator.
1119  if (multiprofile_index_ ==
1120          session_state_delegate->GetMaximumNumberOfLoggedInUsers() &&
1121      logged_in_users > 1) {
1122    separator_shown_ = true;
1123    return new views::View();
1124  }
1125
1126  // Do not show more UserView's then there are logged in users.
1127  if (multiprofile_index_ >= logged_in_users)
1128    return NULL;
1129
1130  user_ = new tray::UserView(this, status, multiprofile_index_);
1131  return user_;
1132}
1133
1134views::View* TrayUser::CreateDetailedView(user::LoginStatus status) {
1135  return NULL;
1136}
1137
1138void TrayUser::DestroyTrayView() {
1139  layout_view_ = NULL;
1140  avatar_ = NULL;
1141  label_ = NULL;
1142  separator_shown_ = false;
1143}
1144
1145void TrayUser::DestroyDefaultView() {
1146  user_ = NULL;
1147}
1148
1149void TrayUser::DestroyDetailedView() {
1150}
1151
1152void TrayUser::UpdateAfterLoginStatusChange(user::LoginStatus status) {
1153  // Only the active user is represented in the tray.
1154  if (!layout_view_)
1155    return;
1156  bool need_label = false;
1157  bool need_avatar = false;
1158  switch (status) {
1159    case user::LOGGED_IN_LOCKED:
1160    case user::LOGGED_IN_USER:
1161    case user::LOGGED_IN_OWNER:
1162    case user::LOGGED_IN_PUBLIC:
1163      need_avatar = true;
1164      break;
1165    case user::LOGGED_IN_LOCALLY_MANAGED:
1166      need_avatar = true;
1167      need_label = true;
1168      break;
1169    case user::LOGGED_IN_GUEST:
1170      need_label = true;
1171      break;
1172    case user::LOGGED_IN_RETAIL_MODE:
1173    case user::LOGGED_IN_KIOSK_APP:
1174    case user::LOGGED_IN_NONE:
1175      break;
1176  }
1177
1178  if ((need_avatar != (avatar_ != NULL)) ||
1179      (need_label != (label_ != NULL))) {
1180    layout_view_->RemoveAllChildViews(true);
1181    if (need_label) {
1182      label_ = new views::Label;
1183      SetupLabelForTray(label_);
1184      layout_view_->AddChildView(label_);
1185    } else {
1186      label_ = NULL;
1187    }
1188    if (need_avatar) {
1189      avatar_ = new tray::RoundedImageView(kProfileRoundedCornerRadius, true);
1190      layout_view_->AddChildView(avatar_);
1191    } else {
1192      avatar_ = NULL;
1193    }
1194  }
1195
1196  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
1197  if (status == user::LOGGED_IN_LOCALLY_MANAGED) {
1198    label_->SetText(
1199        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOCALLY_MANAGED_LABEL));
1200  } else if (status == user::LOGGED_IN_GUEST) {
1201    label_->SetText(bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_GUEST_LABEL));
1202  }
1203
1204  if (avatar_ && ash::switches::UseAlternateShelfLayout()) {
1205    avatar_->SetCornerRadii(0,
1206                            kUserIconLargeCornerRadius,
1207                            kUserIconLargeCornerRadius,
1208                            0);
1209    avatar_->set_border(NULL);
1210  }
1211  UpdateAvatarImage(status);
1212
1213  // Update layout after setting label_ and avatar_ with new login status.
1214  if (Shell::GetPrimaryRootWindowController()->shelf())
1215    UpdateAfterShelfAlignmentChange(
1216        Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
1217            GetAlignment());
1218}
1219
1220void TrayUser::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
1221  // Inactive users won't have a layout.
1222  if (!layout_view_)
1223    return;
1224  if (alignment == SHELF_ALIGNMENT_BOTTOM ||
1225      alignment == SHELF_ALIGNMENT_TOP) {
1226    if (avatar_) {
1227      if (ash::switches::UseAlternateShelfLayout()) {
1228        avatar_->set_border(NULL);
1229        avatar_->SetCornerRadii(0,
1230                                kUserIconLargeCornerRadius,
1231                                kUserIconLargeCornerRadius,
1232                                0);
1233      } else {
1234        avatar_->set_border(views::Border::CreateEmptyBorder(
1235            0, kTrayImageItemHorizontalPaddingBottomAlignment + 2,
1236            0, kTrayImageItemHorizontalPaddingBottomAlignment));
1237      }
1238    }
1239    if (label_) {
1240      label_->set_border(views::Border::CreateEmptyBorder(
1241          0, kTrayLabelItemHorizontalPaddingBottomAlignment,
1242          0, kTrayLabelItemHorizontalPaddingBottomAlignment));
1243    }
1244    layout_view_->SetLayoutManager(
1245        new views::BoxLayout(views::BoxLayout::kHorizontal,
1246                             0, 0, kUserLabelToIconPadding));
1247  } else {
1248    if (avatar_) {
1249      if (ash::switches::UseAlternateShelfLayout()) {
1250        avatar_->set_border(NULL);
1251        avatar_->SetCornerRadii(0,
1252                                0,
1253                                kUserIconLargeCornerRadius,
1254                                kUserIconLargeCornerRadius);
1255      } else {
1256        SetTrayImageItemBorder(avatar_, alignment);
1257      }
1258    }
1259    if (label_) {
1260      label_->set_border(views::Border::CreateEmptyBorder(
1261          kTrayLabelItemVerticalPaddingVeriticalAlignment,
1262          kTrayLabelItemHorizontalPaddingBottomAlignment,
1263          kTrayLabelItemVerticalPaddingVeriticalAlignment,
1264          kTrayLabelItemHorizontalPaddingBottomAlignment));
1265    }
1266    layout_view_->SetLayoutManager(
1267        new views::BoxLayout(views::BoxLayout::kVertical,
1268                             0, 0, kUserLabelToIconPadding));
1269  }
1270}
1271
1272void TrayUser::OnUserUpdate() {
1273  UpdateAvatarImage(Shell::GetInstance()->system_tray_delegate()->
1274      GetUserLoginStatus());
1275}
1276
1277void TrayUser::UpdateAvatarImage(user::LoginStatus status) {
1278  if (!avatar_)
1279    return;
1280
1281  int icon_size = ash::switches::UseAlternateShelfLayout() ?
1282      kUserIconLargeSize : kUserIconSize;
1283
1284  avatar_->SetImage(
1285      ash::Shell::GetInstance()->session_state_delegate()->GetUserImage(
1286          multiprofile_index_),
1287      gfx::Size(icon_size, icon_size));
1288}
1289
1290}  // namespace internal
1291}  // namespace ash
1292