tray_user.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/resource/resource_bundle.h"
41#include "ui/gfx/canvas.h"
42#include "ui/gfx/font.h"
43#include "ui/gfx/image/image.h"
44#include "ui/gfx/image/image_skia_operations.h"
45#include "ui/gfx/insets.h"
46#include "ui/gfx/range/range.h"
47#include "ui/gfx/rect.h"
48#include "ui/gfx/render_text.h"
49#include "ui/gfx/size.h"
50#include "ui/gfx/skia_util.h"
51#include "ui/gfx/text_elider.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  gfx::ElideRectangleText(text_, font, contents_area.width(),
456                          contents_area.height(), gfx::ELIDE_LONG_WORDS,
457                          &lines);
458  // Loop through the lines, creating a renderer for each.
459  gfx::Point position = contents_area.origin();
460  gfx::Range display_name(gfx::Range::InvalidRange());
461  for (std::vector<base::string16>::const_iterator it = lines.begin();
462       it != lines.end(); ++it) {
463    gfx::RenderText* line = gfx::RenderText::CreateInstance();
464    line->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_UI);
465    line->SetText(*it);
466    const gfx::Size size(contents_area.width(), line->GetStringSize().height());
467    line->SetDisplayRect(gfx::Rect(position, size));
468    position.set_y(position.y() + size.height());
469
470    // Set the default text color for the line.
471    line->SetColor(kPublicAccountUserCardTextColor);
472
473    // If a range of the line contains the user's display name, apply a custom
474    // text color to it.
475    if (display_name.is_empty())
476      display_name.set_start(it->find(kDisplayNameMark));
477    if (!display_name.is_empty()) {
478      display_name.set_end(
479          it->find(kDisplayNameMark, display_name.start() + 1));
480      gfx::Range line_range(0, it->size());
481      line->ApplyColor(kPublicAccountUserCardNameColor,
482                       display_name.Intersect(line_range));
483      // Update the range for the next line.
484      if (display_name.end() >= line_range.end())
485        display_name.set_start(0);
486      else
487        display_name = gfx::Range::InvalidRange();
488    }
489
490    lines_.push_back(line);
491  }
492
493  // Position the link after the label text, separated by a space. If it does
494  // not fit onto the last line of the text, wrap the link onto its own line.
495  const gfx::Size last_line_size = lines_.back()->GetStringSize();
496  const int space_width = font.GetStringWidth(ASCIIToUTF16(" "));
497  const gfx::Size link_size = learn_more_->GetPreferredSize();
498  if (contents_area.width() - last_line_size.width() >=
499      space_width + link_size.width()) {
500    position.set_x(position.x() + last_line_size.width() + space_width);
501    position.set_y(position.y() - last_line_size.height());
502  }
503  position.set_y(position.y() - learn_more_->GetInsets().top());
504  gfx::Rect learn_more_bounds(position, link_size);
505  learn_more_bounds.Intersect(contents_area);
506  if (base::i18n::IsRTL()) {
507    const gfx::Insets insets = GetInsets();
508    learn_more_bounds.Offset(insets.right() - insets.left(), 0);
509  }
510  learn_more_->SetBoundsRect(learn_more_bounds);
511}
512
513gfx::Size PublicAccountUserDetails::GetPreferredSize() {
514  return preferred_size_;
515}
516
517void PublicAccountUserDetails::OnPaint(gfx::Canvas* canvas) {
518  for (ScopedVector<gfx::RenderText>::const_iterator it = lines_.begin();
519       it != lines_.end(); ++it) {
520    (*it)->Draw(canvas);
521  }
522  views::View::OnPaint(canvas);
523}
524
525void PublicAccountUserDetails::LinkClicked(views::Link* source,
526                                           int event_flags) {
527  DCHECK_EQ(source, learn_more_);
528  ash::Shell::GetInstance()->system_tray_delegate()->ShowPublicAccountInfo();
529}
530
531void PublicAccountUserDetails::CalculatePreferredSize(SystemTrayItem* owner,
532                                                      int used_width) {
533  const gfx::Font font;
534  const gfx::Size link_size = learn_more_->GetPreferredSize();
535  const int space_width = font.GetStringWidth(ASCIIToUTF16(" "));
536  const gfx::Insets insets = GetInsets();
537  views::TrayBubbleView* bubble_view =
538      owner->system_tray()->GetSystemBubble()->bubble_view();
539  int min_width = std::max(
540      link_size.width(),
541      bubble_view->GetPreferredSize().width() - (used_width + insets.width()));
542  int max_width = std::min(
543      font.GetStringWidth(text_) + space_width + link_size.width(),
544      bubble_view->GetMaximumSize().width() - (used_width + insets.width()));
545  // Do a binary search for the minimum width that ensures no more than three
546  // lines are needed. The lower bound is the minimum of the current bubble
547  // width and the width of the link (as no wrapping is permitted inside the
548  // link). The upper bound is the maximum of the largest allowed bubble width
549  // and the sum of the label text and link widths when put on a single line.
550  std::vector<base::string16> lines;
551  while (min_width < max_width) {
552    lines.clear();
553    const int width = (min_width + max_width) / 2;
554    const bool too_narrow = gfx::ElideRectangleText(
555        text_, font, width, INT_MAX, gfx::TRUNCATE_LONG_WORDS, &lines) != 0;
556    int line_count = lines.size();
557    if (!too_narrow && line_count == 3 &&
558            width - font.GetStringWidth(lines.back()) <=
559            space_width + link_size.width()) {
560      ++line_count;
561    }
562    if (too_narrow || line_count > 3)
563      min_width = width + 1;
564    else
565      max_width = width;
566  }
567
568  // Calculate the corresponding height and set the preferred size.
569  lines.clear();
570  gfx::ElideRectangleText(
571      text_, font, min_width, INT_MAX, gfx::TRUNCATE_LONG_WORDS, &lines);
572  int line_count = lines.size();
573  if (min_width - font.GetStringWidth(lines.back()) <=
574      space_width + link_size.width()) {
575    ++line_count;
576  }
577  const int line_height = font.GetHeight();
578  const int link_extra_height = std::max(
579      link_size.height() - learn_more_->GetInsets().top() - line_height, 0);
580  preferred_size_ = gfx::Size(
581      min_width + insets.width(),
582      line_count * line_height + link_extra_height + insets.height());
583
584  bubble_view->SetWidth(preferred_size_.width() + used_width);
585}
586
587UserCard::UserCard(views::ButtonListener* listener, bool active_user)
588    : CustomButton(listener),
589      is_active_user_(active_user),
590      button_hovered_(false),
591      show_border_(false) {
592  if (is_active_user_) {
593    set_background(
594        views::Background::CreateSolidBackground(kBackgroundColor));
595    ShowActive();
596  }
597}
598
599UserCard::~UserCard() {}
600
601void UserCard::ForceBorderVisible(bool show) {
602  show_border_ = show;
603  ShowActive();
604}
605
606void UserCard::OnMouseEntered(const ui::MouseEvent& event) {
607  if (is_active_user_) {
608    button_hovered_ = true;
609    background()->SetNativeControlColor(kHoverBackgroundColor);
610    ShowActive();
611  }
612}
613
614void UserCard::OnMouseExited(const ui::MouseEvent& event) {
615  if (is_active_user_) {
616    button_hovered_ = false;
617    background()->SetNativeControlColor(kBackgroundColor);
618    ShowActive();
619  }
620}
621
622void UserCard::ShowActive() {
623  int width = button_hovered_ || show_border_ ? 1 : 0;
624  set_border(views::Border::CreateSolidSidedBorder(width, width, width, 1,
625                                                   kBorderColor));
626  SchedulePaint();
627}
628
629UserView::UserView(SystemTrayItem* owner,
630                   ash::user::LoginStatus login,
631                   MultiProfileIndex index)
632    : multiprofile_index_(index),
633      user_card_view_(NULL),
634      owner_(owner),
635      is_user_card_(false),
636      logout_button_(NULL),
637      add_user_visible_but_disabled_(false) {
638  CHECK_NE(ash::user::LOGGED_IN_NONE, login);
639  if (!index) {
640    // Only the logged in user will have a background. All other users will have
641    // to allow the TrayPopupContainer highlighting the menu line.
642    set_background(views::Background::CreateSolidBackground(
643        login == ash::user::LOGGED_IN_PUBLIC ? kPublicAccountBackgroundColor :
644                                               kBackgroundColor));
645  }
646  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
647                                        kTrayPopupPaddingBetweenItems));
648  // The logout button must be added before the user card so that the user card
649  // can correctly calculate the remaining available width.
650  // Note that only the current multiprofile user gets a button.
651  if (!multiprofile_index_)
652    AddLogoutButton(login);
653  AddUserCard(owner, login);
654}
655
656UserView::~UserView() {}
657
658void UserView::MouseMovedOutOfHost() {
659  popup_message_.reset();
660  mouse_watcher_.reset();
661  add_menu_option_.reset();
662}
663
664TrayUser::TestState UserView::GetStateForTest() const {
665  if (add_menu_option_.get()) {
666    return add_user_visible_but_disabled_ ? TrayUser::ACTIVE_BUT_DISABLED :
667                                            TrayUser::ACTIVE;
668  }
669
670  if (!is_user_card_)
671    return TrayUser::SHOWN;
672
673  return static_cast<UserCard*>(user_card_view_)->is_hovered_for_test() ?
674      TrayUser::HOVERED : TrayUser::SHOWN;
675}
676
677gfx::Rect UserView::GetBoundsInScreenOfUserButtonForTest() {
678  DCHECK(user_card_view_);
679  return user_card_view_->GetBoundsInScreen();
680}
681
682gfx::Size UserView::GetPreferredSize() {
683  gfx::Size size = views::View::GetPreferredSize();
684  // Only the active user panel will be forced to a certain height.
685  if (!multiprofile_index_) {
686    size.set_height(std::max(size.height(),
687                             kTrayPopupItemHeight + GetInsets().height()));
688  }
689  return size;
690}
691
692int UserView::GetHeightForWidth(int width) {
693  return GetPreferredSize().height();
694}
695
696void UserView::Layout() {
697  gfx::Rect contents_area(GetContentsBounds());
698  if (user_card_view_ && logout_button_) {
699    // Give the logout button the space it requests.
700    gfx::Rect logout_area = contents_area;
701    logout_area.ClampToCenteredSize(logout_button_->GetPreferredSize());
702    logout_area.set_x(contents_area.right() - logout_area.width());
703
704    // Give the remaining space to the user card.
705    gfx::Rect user_card_area = contents_area;
706    int remaining_width = contents_area.width() - logout_area.width();
707    if (ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
708      // In multiprofile case |user_card_view_| and |logout_button_| have to
709      // have the same height.
710      int y = std::min(user_card_area.y(), logout_area.y());
711      int height = std::max(user_card_area.height(), logout_area.height());
712      logout_area.set_y(y);
713      logout_area.set_height(height);
714      user_card_area.set_y(y);
715      user_card_area.set_height(height);
716
717      // In multiprofile mode we have also to increase the size of the card by
718      // the size of the border to make it overlap with the logout button.
719      user_card_area.set_width(std::max(0, remaining_width + 1));
720
721      // To make the logout button symmetrical with the user card we also make
722      // the button longer by the same size the hover area in front of the icon
723      // got inset.
724      logout_area.set_width(logout_area.width() +
725                            kTrayUserTileHoverBorderInset);
726    } else {
727      // In all other modes we have to make sure that there is enough spacing
728      // between the two.
729      remaining_width -= kTrayPopupPaddingBetweenItems;
730    }
731    user_card_area.set_width(remaining_width);
732    user_card_view_->SetBoundsRect(user_card_area);
733    logout_button_->SetBoundsRect(logout_area);
734  } else if (user_card_view_) {
735    user_card_view_->SetBoundsRect(contents_area);
736  } else if (logout_button_) {
737    logout_button_->SetBoundsRect(contents_area);
738  }
739}
740
741void UserView::ButtonPressed(views::Button* sender, const ui::Event& event) {
742  if (sender == logout_button_) {
743    ash::Shell::GetInstance()->system_tray_delegate()->SignOut();
744  } else if (sender == user_card_view_ &&
745             ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
746    if (!multiprofile_index_) {
747      ToggleAddUserMenuOption();
748    } else {
749      ash::SessionStateDelegate* delegate =
750          ash::Shell::GetInstance()->session_state_delegate();
751      delegate->SwitchActiveUser(delegate->GetUserEmail(multiprofile_index_));
752      // Since the user list is about to change the system menu should get
753      // closed.
754      owner_->system_tray()->CloseSystemBubble();
755    }
756  } else if (add_menu_option_.get() &&
757             sender == add_menu_option_->GetContentsView()) {
758    // Let the user add another account to the session.
759    ash::Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
760  } else {
761    NOTREACHED();
762  }
763}
764
765void UserView::AddLogoutButton(ash::user::LoginStatus login) {
766  const base::string16 title = ash::user::GetLocalizedSignOutStringForStatus(
767      login, true);
768  TrayPopupLabelButton* logout_button = new TrayPopupLabelButton(this, title);
769  logout_button->SetAccessibleName(title);
770  logout_button_ = logout_button;
771  // In public account mode, the logout button border has a custom color.
772  if (login == ash::user::LOGGED_IN_PUBLIC) {
773    TrayPopupLabelButtonBorder* border =
774        static_cast<TrayPopupLabelButtonBorder*>(logout_button_->border());
775    border->SetPainter(false, views::Button::STATE_NORMAL,
776                       views::Painter::CreateImageGridPainter(
777                           kPublicAccountLogoutButtonBorderImagesNormal));
778    border->SetPainter(false, views::Button::STATE_HOVERED,
779                       views::Painter::CreateImageGridPainter(
780                           kPublicAccountLogoutButtonBorderImagesHovered));
781    border->SetPainter(false, views::Button::STATE_PRESSED,
782                       views::Painter::CreateImageGridPainter(
783                           kPublicAccountLogoutButtonBorderImagesHovered));
784  }
785  AddChildView(logout_button_);
786}
787
788void UserView::AddUserCard(SystemTrayItem* owner,
789                           ash::user::LoginStatus login) {
790  // Add padding around the panel.
791  set_border(views::Border::CreateEmptyBorder(
792      kUserCardVerticalPadding, kTrayPopupPaddingHorizontal,
793      kUserCardVerticalPadding, kTrayPopupPaddingHorizontal));
794
795  if (ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled() &&
796      login != ash::user::LOGGED_IN_RETAIL_MODE) {
797    user_card_view_ = new UserCard(this, multiprofile_index_ == 0);
798    is_user_card_ = true;
799  } else {
800    user_card_view_ = new views::View();
801    is_user_card_ = false;
802  }
803
804  user_card_view_->SetLayoutManager(new views::BoxLayout(
805      views::BoxLayout::kHorizontal, 0, 0 , kTrayPopupPaddingBetweenItems));
806  AddChildViewAt(user_card_view_, 0);
807
808  if (login == ash::user::LOGGED_IN_RETAIL_MODE) {
809    AddLoggedInRetailModeUserCardContent();
810    return;
811  }
812
813  // The entire user card should trigger hover (the inner items get disabled).
814  user_card_view_->SetEnabled(true);
815  user_card_view_->set_notify_enter_exit_on_child(true);
816
817  if (login == ash::user::LOGGED_IN_PUBLIC) {
818    AddLoggedInPublicModeUserCardContent(owner);
819    return;
820  }
821
822  views::View* icon = CreateIconForUserCard(login);
823  user_card_view_->AddChildView(icon);
824
825  // To allow the border to start before the icon, reduce the size before and
826  // add an inset to the icon to get the spacing.
827  if (multiprofile_index_ == 0 &&
828      ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
829    icon->set_border(views::Border::CreateEmptyBorder(
830        0, kTrayUserTileHoverBorderInset, 0, 0));
831    set_border(views::Border::CreateEmptyBorder(
832        kUserCardVerticalPadding,
833        kTrayPopupPaddingHorizontal - kTrayUserTileHoverBorderInset,
834        kUserCardVerticalPadding,
835        kTrayPopupPaddingHorizontal));
836  }
837  ash::SessionStateDelegate* delegate =
838      ash::Shell::GetInstance()->session_state_delegate();
839  views::Label* username = NULL;
840  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
841  if (!multiprofile_index_) {
842    base::string16 user_name_string =
843        login == ash::user::LOGGED_IN_GUEST ?
844            bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_GUEST_LABEL) :
845            delegate->GetUserDisplayName(multiprofile_index_);
846    if (!user_name_string.empty()) {
847      username = new views::Label(user_name_string);
848      username->SetHorizontalAlignment(gfx::ALIGN_LEFT);
849    }
850  }
851
852  views::Label* additional = NULL;
853  if (login != ash::user::LOGGED_IN_GUEST) {
854    base::string16 user_email_string =
855        login == ash::user::LOGGED_IN_LOCALLY_MANAGED ?
856            bundle.GetLocalizedString(
857                IDS_ASH_STATUS_TRAY_LOCALLY_MANAGED_LABEL) :
858            UTF8ToUTF16(delegate->GetUserEmail(multiprofile_index_));
859    if (!user_email_string.empty()) {
860      additional = new views::Label(user_email_string);
861      additional->SetFont(bundle.GetFont(ui::ResourceBundle::SmallFont));
862      additional->SetHorizontalAlignment(gfx::ALIGN_LEFT);
863    }
864  }
865
866  // Adjust text properties dependent on if it is an active or inactive user.
867  if (multiprofile_index_) {
868    // Fade the text of non active users to 50%.
869    SkColor text_color = additional->enabled_color();
870    text_color = SkColorSetA(text_color, SkColorGetA(text_color) / 2);
871    if (additional)
872      additional->SetDisabledColor(text_color);
873    if (username)
874      username->SetDisabledColor(text_color);
875  }
876
877  if (additional && username) {
878    views::View* details = new views::View;
879    details->SetLayoutManager(new views::BoxLayout(
880        views::BoxLayout::kVertical, 0, kUserDetailsVerticalPadding, 0));
881    details->AddChildView(username);
882    details->AddChildView(additional);
883    user_card_view_->AddChildView(details);
884  } else {
885    if (username)
886      user_card_view_->AddChildView(username);
887    if (additional)
888      user_card_view_->AddChildView(additional);
889  }
890}
891
892views::View* UserView::CreateIconForUserCard(ash::user::LoginStatus login) {
893  RoundedImageView* icon = new RoundedImageView(kProfileRoundedCornerRadius,
894                                                multiprofile_index_ == 0);
895  icon->SetEnabled(false);
896  if (login == ash::user::LOGGED_IN_GUEST) {
897    icon->SetImage(*ui::ResourceBundle::GetSharedInstance().
898        GetImageNamed(IDR_AURA_UBER_TRAY_GUEST_ICON).ToImageSkia(),
899        gfx::Size(kUserIconSize, kUserIconSize));
900  } else {
901    icon->SetImage(
902        ash::Shell::GetInstance()->session_state_delegate()->
903            GetUserImage(multiprofile_index_),
904        gfx::Size(kUserIconSize, kUserIconSize));
905  }
906  return icon;
907}
908
909void UserView::AddLoggedInRetailModeUserCardContent() {
910  views::Label* details = new views::Label;
911  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
912  details->SetText(
913      bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_KIOSK_LABEL));
914  details->set_border(views::Border::CreateEmptyBorder(0, 4, 0, 1));
915  details->SetHorizontalAlignment(gfx::ALIGN_LEFT);
916  user_card_view_->AddChildView(details);
917}
918
919void UserView::AddLoggedInPublicModeUserCardContent(SystemTrayItem* owner) {
920  user_card_view_->AddChildView(
921      CreateIconForUserCard(ash::user::LOGGED_IN_PUBLIC));
922  user_card_view_->AddChildView(new PublicAccountUserDetails(
923      owner, GetPreferredSize().width() + kTrayPopupPaddingBetweenItems));
924}
925
926void UserView::ToggleAddUserMenuOption() {
927  if (add_menu_option_.get()) {
928    popup_message_.reset();
929    mouse_watcher_.reset();
930    add_menu_option_.reset();
931    return;
932  }
933
934  // Note: We do not need to install a global event handler to delete this
935  // item since it will destroyed automatically before the menu / user menu item
936  // gets destroyed..
937  const SessionStateDelegate* session_state_delegate =
938      ash::Shell::GetInstance()->session_state_delegate();
939  add_user_visible_but_disabled_ =
940      session_state_delegate->NumberOfLoggedInUsers() >=
941          session_state_delegate->GetMaximumNumberOfLoggedInUsers();
942  add_menu_option_.reset(new views::Widget);
943  views::Widget::InitParams params;
944  params.type = views::Widget::InitParams::TYPE_TOOLTIP;
945  params.keep_on_top = true;
946  params.context = this->GetWidget()->GetNativeWindow();
947  params.accept_events = true;
948  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
949  params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
950  add_menu_option_->Init(params);
951  add_menu_option_->SetOpacity(0xFF);
952  add_menu_option_->GetNativeWindow()->set_owned_by_parent(false);
953  SetShadowType(add_menu_option_->GetNativeView(),
954                views::corewm::SHADOW_TYPE_NONE);
955
956  // Position it below our user card.
957  gfx::Rect bounds = user_card_view_->GetBoundsInScreen();
958  bounds.set_y(bounds.y() + bounds.height());
959  add_menu_option_->SetBounds(bounds);
960
961  // Show the content.
962  AddUserView* add_user_view = new AddUserView(
963      static_cast<UserCard*>(user_card_view_), this);
964  add_menu_option_->SetContentsView(add_user_view);
965  add_menu_option_->SetAlwaysOnTop(true);
966  add_menu_option_->Show();
967  if (add_user_visible_but_disabled_) {
968    ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
969    popup_message_.reset(new PopupMessage(
970        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_CAPTION_CANNOT_ADD_USER),
971        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_MESSAGE_CANNOT_ADD_USER),
972        PopupMessage::ICON_WARNING,
973        add_user_view->anchor(),
974        views::BubbleBorder::TOP_LEFT,
975        gfx::Size(parent()->bounds().width() - kPopupMessageOffset, 0),
976        2 * kPopupMessageOffset));
977  }
978  // Find the screen area which encloses both elements and sets then a mouse
979  // watcher which will close the "menu".
980  gfx::Rect area = user_card_view_->GetBoundsInScreen();
981  area.set_height(2 * area.height());
982  mouse_watcher_.reset(new views::MouseWatcher(
983      new UserViewMouseWatcherHost(area),
984      this));
985  mouse_watcher_->Start();
986}
987
988AddUserView::AddUserView(UserCard* owner, views::ButtonListener* listener)
989    : CustomButton(listener_),
990      add_user_(NULL),
991      listener_(listener),
992      owner_(owner),
993      anchor_(NULL) {
994  AddContent();
995  owner_->ForceBorderVisible(true);
996}
997
998AddUserView::~AddUserView() {
999  owner_->ForceBorderVisible(false);
1000}
1001
1002gfx::Size AddUserView::GetPreferredSize() {
1003  return owner_->bounds().size();
1004}
1005
1006int AddUserView::GetHeightForWidth(int width) {
1007  return owner_->bounds().size().height();
1008}
1009
1010void AddUserView::Layout() {
1011  gfx::Rect contents_area(GetContentsBounds());
1012  add_user_->SetBoundsRect(contents_area);
1013}
1014
1015void AddUserView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1016  if (add_user_ == sender)
1017    listener_->ButtonPressed(this, event);
1018  else
1019    NOTREACHED();
1020}
1021
1022void AddUserView::AddContent() {
1023  set_notify_enter_exit_on_child(true);
1024
1025  const SessionStateDelegate* delegate =
1026      ash::Shell::GetInstance()->session_state_delegate();
1027  bool enable = delegate->NumberOfLoggedInUsers() <
1028                    delegate->GetMaximumNumberOfLoggedInUsers();
1029
1030  SetLayoutManager(new views::FillLayout());
1031  set_background(views::Background::CreateSolidBackground(kBackgroundColor));
1032
1033  // Add padding around the panel.
1034  set_border(views::Border::CreateSolidBorder(1, kBorderColor));
1035
1036  add_user_ = new UserCard(this, enable);
1037  add_user_->set_border(views::Border::CreateEmptyBorder(
1038      kUserCardVerticalPadding,
1039      kTrayPopupPaddingHorizontal- kTrayUserTileHoverBorderInset,
1040      kUserCardVerticalPadding,
1041      kTrayPopupPaddingHorizontal- kTrayUserTileHoverBorderInset));
1042
1043  add_user_->SetLayoutManager(new views::BoxLayout(
1044      views::BoxLayout::kHorizontal, 0, 0 , kTrayPopupPaddingBetweenItems));
1045  AddChildViewAt(add_user_, 0);
1046
1047  // Add the [+] icon which is also the anchor for messages.
1048  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
1049  RoundedImageView* icon = new RoundedImageView(kProfileRoundedCornerRadius,
1050                                                true);
1051  anchor_ = icon;
1052  icon->SetImage(*ui::ResourceBundle::GetSharedInstance().
1053      GetImageNamed(IDR_AURA_UBER_TRAY_ADD_MULTIPROFILE_USER).ToImageSkia(),
1054      gfx::Size(kUserIconSize, kUserIconSize));
1055  add_user_->AddChildView(icon);
1056
1057  // Add the command text.
1058  views::Label* command_label = new views::Label(
1059      bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT));
1060  command_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1061  add_user_->AddChildView(command_label);
1062}
1063
1064}  // namespace tray
1065
1066TrayUser::TrayUser(SystemTray* system_tray, MultiProfileIndex index)
1067    : SystemTrayItem(system_tray),
1068      multiprofile_index_(index),
1069      user_(NULL),
1070      layout_view_(NULL),
1071      avatar_(NULL),
1072      label_(NULL),
1073      separator_shown_(false) {
1074  Shell::GetInstance()->system_tray_notifier()->AddUserObserver(this);
1075}
1076
1077TrayUser::~TrayUser() {
1078  Shell::GetInstance()->system_tray_notifier()->RemoveUserObserver(this);
1079}
1080
1081TrayUser::TestState TrayUser::GetStateForTest() const {
1082  if (separator_shown_)
1083    return SEPARATOR;
1084  if (!user_)
1085    return HIDDEN;
1086  return user_->GetStateForTest();
1087}
1088
1089gfx::Rect TrayUser::GetUserPanelBoundsInScreenForTest() const {
1090  DCHECK(user_);
1091  return user_->GetBoundsInScreenOfUserButtonForTest();
1092}
1093
1094views::View* TrayUser::CreateTrayView(user::LoginStatus status) {
1095  CHECK(layout_view_ == NULL);
1096  // Only the current user gets an icon. All other users will only be
1097  // represented in the tray menu.
1098  if (multiprofile_index_)
1099    return NULL;
1100
1101  layout_view_ = new views::View();
1102  layout_view_->SetLayoutManager(
1103      new views::BoxLayout(views::BoxLayout::kHorizontal,
1104                           0, 0, kUserLabelToIconPadding));
1105  UpdateAfterLoginStatusChange(status);
1106  return layout_view_;
1107}
1108
1109views::View* TrayUser::CreateDefaultView(user::LoginStatus status) {
1110  if (status == user::LOGGED_IN_NONE)
1111    return NULL;
1112
1113  CHECK(user_ == NULL);
1114
1115  const SessionStateDelegate* session_state_delegate =
1116      ash::Shell::GetInstance()->session_state_delegate();
1117  int logged_in_users = session_state_delegate->NumberOfLoggedInUsers();
1118
1119  // If there are multiple users logged in, the users will be separated from the
1120  // rest of the menu by a separator.
1121  if (multiprofile_index_ ==
1122          session_state_delegate->GetMaximumNumberOfLoggedInUsers() &&
1123      logged_in_users > 1) {
1124    separator_shown_ = true;
1125    return new views::View();
1126  }
1127
1128  // Do not show more UserView's then there are logged in users.
1129  if (multiprofile_index_ >= logged_in_users)
1130    return NULL;
1131
1132  user_ = new tray::UserView(this, status, multiprofile_index_);
1133  return user_;
1134}
1135
1136views::View* TrayUser::CreateDetailedView(user::LoginStatus status) {
1137  return NULL;
1138}
1139
1140void TrayUser::DestroyTrayView() {
1141  layout_view_ = NULL;
1142  avatar_ = NULL;
1143  label_ = NULL;
1144  separator_shown_ = false;
1145}
1146
1147void TrayUser::DestroyDefaultView() {
1148  user_ = NULL;
1149}
1150
1151void TrayUser::DestroyDetailedView() {
1152}
1153
1154void TrayUser::UpdateAfterLoginStatusChange(user::LoginStatus status) {
1155  // Only the active user is represented in the tray.
1156  if (!layout_view_)
1157    return;
1158  bool need_label = false;
1159  bool need_avatar = false;
1160  switch (status) {
1161    case user::LOGGED_IN_LOCKED:
1162    case user::LOGGED_IN_USER:
1163    case user::LOGGED_IN_OWNER:
1164    case user::LOGGED_IN_PUBLIC:
1165      need_avatar = true;
1166      break;
1167    case user::LOGGED_IN_LOCALLY_MANAGED:
1168      need_avatar = true;
1169      need_label = true;
1170      break;
1171    case user::LOGGED_IN_GUEST:
1172      need_label = true;
1173      break;
1174    case user::LOGGED_IN_RETAIL_MODE:
1175    case user::LOGGED_IN_KIOSK_APP:
1176    case user::LOGGED_IN_NONE:
1177      break;
1178  }
1179
1180  if ((need_avatar != (avatar_ != NULL)) ||
1181      (need_label != (label_ != NULL))) {
1182    layout_view_->RemoveAllChildViews(true);
1183    if (need_label) {
1184      label_ = new views::Label;
1185      SetupLabelForTray(label_);
1186      layout_view_->AddChildView(label_);
1187    } else {
1188      label_ = NULL;
1189    }
1190    if (need_avatar) {
1191      avatar_ = new tray::RoundedImageView(kProfileRoundedCornerRadius, true);
1192      layout_view_->AddChildView(avatar_);
1193    } else {
1194      avatar_ = NULL;
1195    }
1196  }
1197
1198  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
1199  if (status == user::LOGGED_IN_LOCALLY_MANAGED) {
1200    label_->SetText(
1201        bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOCALLY_MANAGED_LABEL));
1202  } else if (status == user::LOGGED_IN_GUEST) {
1203    label_->SetText(bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_GUEST_LABEL));
1204  }
1205
1206  if (avatar_ && ash::switches::UseAlternateShelfLayout()) {
1207    avatar_->SetCornerRadii(0,
1208                            kUserIconLargeCornerRadius,
1209                            kUserIconLargeCornerRadius,
1210                            0);
1211    avatar_->set_border(NULL);
1212  }
1213  UpdateAvatarImage(status);
1214
1215  // Update layout after setting label_ and avatar_ with new login status.
1216  if (Shell::GetPrimaryRootWindowController()->shelf())
1217    UpdateAfterShelfAlignmentChange(
1218        Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
1219            GetAlignment());
1220}
1221
1222void TrayUser::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
1223  // Inactive users won't have a layout.
1224  if (!layout_view_)
1225    return;
1226  if (alignment == SHELF_ALIGNMENT_BOTTOM ||
1227      alignment == SHELF_ALIGNMENT_TOP) {
1228    if (avatar_) {
1229      if (ash::switches::UseAlternateShelfLayout()) {
1230        avatar_->set_border(NULL);
1231        avatar_->SetCornerRadii(0,
1232                                kUserIconLargeCornerRadius,
1233                                kUserIconLargeCornerRadius,
1234                                0);
1235      } else {
1236        avatar_->set_border(views::Border::CreateEmptyBorder(
1237            0, kTrayImageItemHorizontalPaddingBottomAlignment + 2,
1238            0, kTrayImageItemHorizontalPaddingBottomAlignment));
1239      }
1240    }
1241    if (label_) {
1242      label_->set_border(views::Border::CreateEmptyBorder(
1243          0, kTrayLabelItemHorizontalPaddingBottomAlignment,
1244          0, kTrayLabelItemHorizontalPaddingBottomAlignment));
1245    }
1246    layout_view_->SetLayoutManager(
1247        new views::BoxLayout(views::BoxLayout::kHorizontal,
1248                             0, 0, kUserLabelToIconPadding));
1249  } else {
1250    if (avatar_) {
1251      if (ash::switches::UseAlternateShelfLayout()) {
1252        avatar_->set_border(NULL);
1253        avatar_->SetCornerRadii(0,
1254                                0,
1255                                kUserIconLargeCornerRadius,
1256                                kUserIconLargeCornerRadius);
1257      } else {
1258        SetTrayImageItemBorder(avatar_, alignment);
1259      }
1260    }
1261    if (label_) {
1262      label_->set_border(views::Border::CreateEmptyBorder(
1263          kTrayLabelItemVerticalPaddingVerticalAlignment,
1264          kTrayLabelItemHorizontalPaddingBottomAlignment,
1265          kTrayLabelItemVerticalPaddingVerticalAlignment,
1266          kTrayLabelItemHorizontalPaddingBottomAlignment));
1267    }
1268    layout_view_->SetLayoutManager(
1269        new views::BoxLayout(views::BoxLayout::kVertical,
1270                             0, 0, kUserLabelToIconPadding));
1271  }
1272}
1273
1274void TrayUser::OnUserUpdate() {
1275  UpdateAvatarImage(Shell::GetInstance()->system_tray_delegate()->
1276      GetUserLoginStatus());
1277}
1278
1279void TrayUser::UpdateAvatarImage(user::LoginStatus status) {
1280  if (!avatar_)
1281    return;
1282
1283  int icon_size = ash::switches::UseAlternateShelfLayout() ?
1284      kUserIconLargeSize : kUserIconSize;
1285
1286  avatar_->SetImage(
1287      ash::Shell::GetInstance()->session_state_delegate()->GetUserImage(
1288          multiprofile_index_),
1289      gfx::Size(icon_size, icon_size));
1290}
1291
1292}  // namespace internal
1293}  // namespace ash
1294