1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/profiles/avatar_menu_bubble_view.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/avatar_menu.h"
14 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/profiles/profile_window.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_commands.h"
21 #include "chrome/browser/ui/browser_list.h"
22 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/chrome_pages.h"
24 #include "chrome/common/url_constants.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "components/signin/core/common/profile_management_switches.h"
27 #include "content/public/browser/page_navigator.h"
28 #include "content/public/browser/web_contents.h"
29 #include "grit/generated_resources.h"
30 #include "grit/theme_resources.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/image/canvas_image_source.h"
35 #include "ui/gfx/image/image.h"
36 #include "ui/views/controls/button/custom_button.h"
37 #include "ui/views/controls/button/image_button.h"
38 #include "ui/views/controls/button/label_button.h"
39 #include "ui/views/controls/image_view.h"
40 #include "ui/views/controls/label.h"
41 #include "ui/views/controls/link.h"
42 #include "ui/views/controls/separator.h"
43 #include "ui/views/layout/grid_layout.h"
44 #include "ui/views/layout/layout_constants.h"
45 #include "ui/views/widget/widget.h"
49 const int kItemHeight
= 44;
50 const int kItemMarginY
= 4;
51 const int kIconMarginX
= 6;
52 const int kSeparatorPaddingY
= 5;
53 const int kMaxItemTextWidth
= 200;
54 const SkColor kHighlightColor
= 0xFFE3EDF6;
56 inline int Round(double x
) {
57 return static_cast<int>(x
+ 0.5);
60 gfx::Rect
GetCenteredAndScaledRect(int src_width
, int src_height
,
62 int dst_width
, int dst_height
) {
65 if (src_width
> src_height
) {
66 scaled_width
= std::min(src_width
, dst_width
);
67 float scale
= static_cast<float>(scaled_width
) /
68 static_cast<float>(src_width
);
69 scaled_height
= Round(src_height
* scale
);
71 scaled_height
= std::min(src_height
, dst_height
);
72 float scale
= static_cast<float>(scaled_height
) /
73 static_cast<float>(src_height
);
74 scaled_width
= Round(src_width
* scale
);
76 int x
= dst_x
+ (dst_width
- scaled_width
) / 2;
77 int y
= dst_y
+ (dst_height
- scaled_height
) / 2;
78 return gfx::Rect(x
, y
, scaled_width
, scaled_height
);
81 // BadgeImageSource -----------------------------------------------------------
82 class BadgeImageSource
: public gfx::CanvasImageSource
{
84 BadgeImageSource(const gfx::ImageSkia
& icon
,
85 const gfx::Size
& icon_size
,
86 const gfx::ImageSkia
& badge
);
88 virtual ~BadgeImageSource();
90 // Overridden from CanvasImageSource:
91 virtual void Draw(gfx::Canvas
* canvas
) OVERRIDE
;
94 gfx::Size
ComputeSize(const gfx::ImageSkia
& icon
,
95 const gfx::Size
& size
,
96 const gfx::ImageSkia
& badge
);
98 const gfx::ImageSkia icon_
;
100 const gfx::ImageSkia badge_
;
102 DISALLOW_COPY_AND_ASSIGN(BadgeImageSource
);
105 BadgeImageSource::BadgeImageSource(const gfx::ImageSkia
& icon
,
106 const gfx::Size
& icon_size
,
107 const gfx::ImageSkia
& badge
)
108 : gfx::CanvasImageSource(ComputeSize(icon
, icon_size
, badge
), false),
110 icon_size_(icon_size
),
114 BadgeImageSource::~BadgeImageSource() {
117 void BadgeImageSource::Draw(gfx::Canvas
* canvas
) {
118 canvas
->DrawImageInt(icon_
, 0, 0, icon_
.width(), icon_
.height(), 0, 0,
119 icon_size_
.width(), icon_size_
.height(), true);
120 canvas
->DrawImageInt(badge_
, size().width() - badge_
.width(),
121 size().height() - badge_
.height());
124 gfx::Size
BadgeImageSource::ComputeSize(const gfx::ImageSkia
& icon
,
125 const gfx::Size
& icon_size
,
126 const gfx::ImageSkia
& badge
) {
127 const float kBadgeOverlapRatioX
= 1.0f
/ 5.0f
;
128 int width
= icon_size
.width() + badge
.width() * kBadgeOverlapRatioX
;
129 const float kBadgeOverlapRatioY
= 1.0f
/ 3.0f
;
130 int height
= icon_size
.height() + badge
.height() * kBadgeOverlapRatioY
;
131 return gfx::Size(width
, height
);
134 // HighlightDelegate ----------------------------------------------------------
136 // Delegate to callback when the highlight state of a control changes.
137 class HighlightDelegate
{
139 virtual ~HighlightDelegate() {}
140 virtual void OnHighlightStateChanged() = 0;
141 virtual void OnFocusStateChanged(bool has_focus
) = 0;
145 // EditProfileLink ------------------------------------------------------------
147 // A custom Link control that forwards highlight state changes. We need to do
148 // this to make sure that the ProfileItemView looks highlighted even when
149 // the mouse is over this link.
150 class EditProfileLink
: public views::Link
{
152 explicit EditProfileLink(const base::string16
& title
,
153 HighlightDelegate
* delegate
);
155 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
156 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
157 virtual void OnFocus() OVERRIDE
;
158 virtual void OnBlur() OVERRIDE
;
160 views::CustomButton::ButtonState
state() { return state_
; }
163 HighlightDelegate
* delegate_
;
164 views::CustomButton::ButtonState state_
;
167 EditProfileLink::EditProfileLink(const base::string16
& title
,
168 HighlightDelegate
* delegate
)
169 : views::Link(title
),
171 state_(views::CustomButton::STATE_NORMAL
) {
174 void EditProfileLink::OnMouseEntered(const ui::MouseEvent
& event
) {
175 views::Link::OnMouseEntered(event
);
176 state_
= views::CustomButton::STATE_HOVERED
;
177 delegate_
->OnHighlightStateChanged();
180 void EditProfileLink::OnMouseExited(const ui::MouseEvent
& event
) {
181 views::Link::OnMouseExited(event
);
182 state_
= views::CustomButton::STATE_NORMAL
;
183 delegate_
->OnHighlightStateChanged();
186 void EditProfileLink::OnFocus() {
187 views::Link::OnFocus();
188 delegate_
->OnFocusStateChanged(true);
191 void EditProfileLink::OnBlur() {
192 views::Link::OnBlur();
193 state_
= views::CustomButton::STATE_NORMAL
;
194 delegate_
->OnFocusStateChanged(false);
198 // ProfileImageView -----------------------------------------------------------
200 // A custom image view that ignores mouse events so that the parent can receive
202 class ProfileImageView
: public views::ImageView
{
204 virtual bool HitTestRect(const gfx::Rect
& rect
) const OVERRIDE
;
207 bool ProfileImageView::HitTestRect(const gfx::Rect
& rect
) const {
213 // ProfileItemView ------------------------------------------------------------
215 // Control that shows information about a single profile.
216 class ProfileItemView
: public views::CustomButton
,
217 public HighlightDelegate
{
219 ProfileItemView(const AvatarMenu::Item
& item
,
220 AvatarMenuBubbleView
* parent
,
223 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
224 virtual void Layout() OVERRIDE
;
225 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
226 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
227 virtual void OnFocus() OVERRIDE
;
228 virtual void OnBlur() OVERRIDE
;
230 virtual void OnHighlightStateChanged() OVERRIDE
;
231 virtual void OnFocusStateChanged(bool has_focus
) OVERRIDE
;
233 const AvatarMenu::Item
& item() const { return item_
; }
234 EditProfileLink
* edit_link() { return edit_link_
; }
237 gfx::ImageSkia
GetBadgedIcon(const gfx::ImageSkia
& icon
);
239 bool IsHighlighted();
241 AvatarMenu::Item item_
;
242 AvatarMenuBubbleView
* parent_
;
244 views::ImageView
* image_view_
;
245 views::Label
* name_label_
;
246 views::Label
* sync_state_label_
;
247 EditProfileLink
* edit_link_
;
249 DISALLOW_COPY_AND_ASSIGN(ProfileItemView
);
252 ProfileItemView::ProfileItemView(const AvatarMenu::Item
& item
,
253 AvatarMenuBubbleView
* parent
,
255 : views::CustomButton(parent
),
259 set_notify_enter_exit_on_child(true);
261 image_view_
= new ProfileImageView();
262 // GetSizedAvatarIcon will resize the icon in case it's too large.
263 const gfx::ImageSkia profile_icon
= *profiles::GetSizedAvatarIcon(item_
.icon
,
264 false, profiles::kAvatarIconWidth
, kItemHeight
).ToImageSkia();
265 if (item_
.active
|| item_
.signin_required
)
266 image_view_
->SetImage(GetBadgedIcon(profile_icon
));
268 image_view_
->SetImage(profile_icon
);
269 AddChildView(image_view_
);
271 // Add a label to show the profile name.
272 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
273 name_label_
= new views::Label(item_
.name
,
274 rb
->GetFontList(item_
.active
?
275 ui::ResourceBundle::BoldFont
:
276 ui::ResourceBundle::BaseFont
));
277 name_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
278 AddChildView(name_label_
);
280 // Add a label to show the sync state.
281 sync_state_label_
= new views::Label(item_
.sync_state
);
283 sync_state_label_
->SetElideBehavior(views::Label::ELIDE_AS_EMAIL
);
284 sync_state_label_
->SetFontList(
285 rb
->GetFontList(ui::ResourceBundle::SmallFont
));
286 sync_state_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
287 sync_state_label_
->SetEnabled(false);
288 AddChildView(sync_state_label_
);
290 // Add an edit profile link.
291 edit_link_
= new EditProfileLink(
292 l10n_util::GetStringUTF16(IDS_PROFILES_EDIT_PROFILE_LINK
), this);
293 edit_link_
->set_listener(parent
);
294 edit_link_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
295 AddChildView(edit_link_
);
297 OnHighlightStateChanged();
300 gfx::Size
ProfileItemView::GetPreferredSize() const {
301 int text_width
= std::max(name_label_
->GetPreferredSize().width(),
302 sync_state_label_
->GetPreferredSize().width());
303 text_width
= std::max(edit_link_
->GetPreferredSize().width(), text_width
);
304 text_width
= std::min(kMaxItemTextWidth
, text_width
);
305 return gfx::Size(profiles::kAvatarIconWidth
+ kIconMarginX
+ text_width
,
309 void ProfileItemView::Layout() {
313 // If this is the active item then the icon is already scaled and so
314 // just use the preferred size.
315 icon_rect
.set_size(image_view_
->GetPreferredSize());
316 icon_rect
.set_y((height() - icon_rect
.height()) / 2);
318 const gfx::ImageSkia
& icon
= image_view_
->GetImage();
319 icon_rect
= GetCenteredAndScaledRect(icon
.width(), icon
.height(), 0, 0,
320 profiles::kAvatarIconWidth
, height());
322 image_view_
->SetBoundsRect(icon_rect
);
324 int label_x
= profiles::kAvatarIconWidth
+ kIconMarginX
;
325 int max_label_width
= std::max(width() - label_x
, 0);
326 gfx::Size name_size
= name_label_
->GetPreferredSize();
327 name_size
.set_width(std::min(name_size
.width(), max_label_width
));
328 gfx::Size state_size
= sync_state_label_
->GetPreferredSize();
329 state_size
.set_width(std::min(state_size
.width(), max_label_width
));
330 gfx::Size edit_size
= edit_link_
->GetPreferredSize();
331 edit_size
.set_width(std::min(edit_size
.width(), max_label_width
));
333 const int kNameStatePaddingY
= 2;
334 int labels_height
= name_size
.height() + kNameStatePaddingY
+
335 std::max(state_size
.height(), edit_size
.height());
336 int y
= (height() - labels_height
) / 2;
337 name_label_
->SetBounds(label_x
, y
, name_size
.width(), name_size
.height());
339 int bottom
= y
+ labels_height
;
340 sync_state_label_
->SetBounds(label_x
, bottom
- state_size
.height(),
341 state_size
.width(), state_size
.height());
342 // The edit link overlaps the sync state label.
343 edit_link_
->SetBounds(label_x
, bottom
- edit_size
.height(),
344 edit_size
.width(), edit_size
.height());
347 void ProfileItemView::OnMouseEntered(const ui::MouseEvent
& event
) {
348 views::CustomButton::OnMouseEntered(event
);
349 OnHighlightStateChanged();
352 void ProfileItemView::OnMouseExited(const ui::MouseEvent
& event
) {
353 views::CustomButton::OnMouseExited(event
);
354 OnHighlightStateChanged();
357 void ProfileItemView::OnFocus() {
358 views::CustomButton::OnFocus();
359 OnFocusStateChanged(true);
362 void ProfileItemView::OnBlur() {
363 views::CustomButton::OnBlur();
364 OnFocusStateChanged(false);
367 void ProfileItemView::OnHighlightStateChanged() {
368 const SkColor color
= IsHighlighted() ? kHighlightColor
: parent_
->color();
369 set_background(views::Background::CreateSolidBackground(color
));
370 name_label_
->SetBackgroundColor(color
);
371 sync_state_label_
->SetBackgroundColor(color
);
372 edit_link_
->SetBackgroundColor(color
);
374 bool show_edit
= IsHighlighted() && item_
.active
&&
375 menu_
->ShouldShowEditProfileLink();
376 sync_state_label_
->SetVisible(!show_edit
);
377 edit_link_
->SetVisible(show_edit
);
381 void ProfileItemView::OnFocusStateChanged(bool has_focus
) {
382 if (!has_focus
&& state() != views::CustomButton::STATE_DISABLED
)
383 SetState(views::CustomButton::STATE_NORMAL
);
384 OnHighlightStateChanged();
388 gfx::ImageSkia
ProfileItemView::GetBadgedIcon(const gfx::ImageSkia
& icon
) {
389 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
390 const gfx::ImageSkia
* badge
= NULL
;
393 badge
= rb
->GetImageSkiaNamed(IDR_PROFILE_SELECTED
);
394 else if (item_
.signin_required
) // TODO(bcwhite): create new icon
395 badge
= rb
->GetImageSkiaNamed(IDR_OMNIBOX_HTTPS_VALID
);
397 NOTREACHED(); // function should only be called if one of above is true
399 gfx::Size icon_size
= GetCenteredAndScaledRect(icon
.width(), icon
.height(),
400 0, 0, profiles::kAvatarIconWidth
, kItemHeight
).size();
401 gfx::CanvasImageSource
* source
=
402 new BadgeImageSource(icon
, icon_size
, *badge
);
403 // ImageSkia takes ownership of |source|.
404 return gfx::ImageSkia(source
, source
->size());
407 bool ProfileItemView::IsHighlighted() {
408 return state() == views::CustomButton::STATE_PRESSED
||
409 state() == views::CustomButton::STATE_HOVERED
||
410 edit_link_
->state() == views::CustomButton::STATE_PRESSED
||
411 edit_link_
->state() == views::CustomButton::STATE_HOVERED
||
413 edit_link_
->HasFocus();
417 // ActionButtonView -----------------------------------------------------------
419 // A custom view that manages the "action" buttons at the bottom of the list
421 class ActionButtonView
: public views::View
{
423 ActionButtonView(views::ButtonListener
* listener
, Profile
* profile
);
426 views::LabelButton
* manage_button_
;
427 views::LabelButton
* signout_button_
;
429 DISALLOW_COPY_AND_ASSIGN(ActionButtonView
);
433 ActionButtonView::ActionButtonView(views::ButtonListener
* listener
,
435 : manage_button_(NULL
),
436 signout_button_(NULL
) {
437 std::string username
;
438 SigninManagerBase
* signin
=
439 SigninManagerFactory::GetForProfile(profile
);
441 username
= signin
->GetAuthenticatedUsername();
443 manage_button_
= new views::LabelButton(
444 listener
, l10n_util::GetStringUTF16(IDS_PROFILES_MANAGE_PROFILES_BUTTON
));
445 manage_button_
->SetStyle(views::Button::STYLE_BUTTON
);
446 manage_button_
->SetTooltipText(
447 l10n_util::GetStringUTF16(IDS_PROFILES_MANAGE_PROFILES_BUTTON_TIP
));
448 manage_button_
->set_tag(IDS_PROFILES_MANAGE_PROFILES_BUTTON
);
450 signout_button_
= new views::LabelButton(
451 listener
, l10n_util::GetStringUTF16(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
));
452 signout_button_
->SetStyle(views::Button::STYLE_BUTTON
);
453 if (username
.empty()) {
454 signout_button_
->SetTooltipText(
455 l10n_util::GetStringUTF16(
456 IDS_PROFILES_PROFILE_SIGNOUT_BUTTON_TIP_UNAVAILABLE
));
457 signout_button_
->SetEnabled(false);
459 signout_button_
->SetTooltipText(
460 l10n_util::GetStringFUTF16(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON_TIP
,
461 base::UTF8ToUTF16(username
)));
463 signout_button_
->set_tag(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
);
465 views::GridLayout
* layout
= new views::GridLayout(this);
466 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
467 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 1,
468 views::GridLayout::USE_PREF
, 0, 0);
469 columns
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::FILL
, 1,
470 views::GridLayout::USE_PREF
, 0, 0);
471 layout
->StartRow(0, 0);
472 layout
->AddView(signout_button_
);
473 layout
->AddView(manage_button_
);
474 SetLayoutManager(layout
);
478 // AvatarMenuBubbleView -------------------------------------------------------
481 AvatarMenuBubbleView
* AvatarMenuBubbleView::avatar_bubble_
= NULL
;
482 bool AvatarMenuBubbleView::close_on_deactivate_for_testing_
= true;
485 void AvatarMenuBubbleView::ShowBubble(
486 views::View
* anchor_view
,
487 views::BubbleBorder::Arrow arrow
,
488 views::BubbleBorder::ArrowPaintType arrow_paint_type
,
489 views::BubbleBorder::BubbleAlignment border_alignment
,
490 const gfx::Rect
& anchor_rect
,
495 DCHECK(chrome::IsCommandEnabled(browser
, IDC_SHOW_AVATAR_MENU
));
496 avatar_bubble_
= new AvatarMenuBubbleView(
497 anchor_view
, arrow
, anchor_rect
, browser
);
498 views::BubbleDelegateView::CreateBubble(avatar_bubble_
);
499 avatar_bubble_
->set_close_on_deactivate(close_on_deactivate_for_testing_
);
500 avatar_bubble_
->SetBackgroundColors();
501 avatar_bubble_
->SetAlignment(border_alignment
);
502 avatar_bubble_
->SetArrowPaintType(arrow_paint_type
);
503 avatar_bubble_
->GetWidget()->Show();
507 bool AvatarMenuBubbleView::IsShowing() {
508 return avatar_bubble_
!= NULL
;
512 void AvatarMenuBubbleView::Hide() {
514 avatar_bubble_
->GetWidget()->Close();
517 AvatarMenuBubbleView::AvatarMenuBubbleView(
518 views::View
* anchor_view
,
519 views::BubbleBorder::Arrow arrow
,
520 const gfx::Rect
& anchor_rect
,
522 : BubbleDelegateView(anchor_view
, arrow
),
523 anchor_rect_(anchor_rect
),
527 managed_user_info_(NULL
),
528 separator_switch_users_(NULL
),
530 avatar_menu_
.reset(new AvatarMenu(
531 &g_browser_process
->profile_manager()->GetProfileInfoCache(),
534 avatar_menu_
->RebuildMenu();
537 AvatarMenuBubbleView::~AvatarMenuBubbleView() {
540 gfx::Size
AvatarMenuBubbleView::GetPreferredSize() const {
541 const int kBubbleViewMinWidth
= 175;
542 gfx::Size
preferred_size(kBubbleViewMinWidth
, 0);
543 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
544 gfx::Size size
= item_views_
[i
]->GetPreferredSize();
545 preferred_size
.Enlarge(0, size
.height() + kItemMarginY
);
546 preferred_size
.SetToMax(size
);
550 preferred_size
.Enlarge(
551 0, kSeparatorPaddingY
* 2 + separator_
->GetPreferredSize().height());
553 gfx::Size buttons_size
= buttons_view_
->GetPreferredSize();
554 preferred_size
.Enlarge(0, buttons_size
.height());
555 preferred_size
.SetToMax(buttons_size
);
559 if (managed_user_info_
) {
560 // First handle the switch profile link because it can still affect the
562 gfx::Size size
= switch_profile_link_
->GetPreferredSize();
563 preferred_size
.Enlarge(0, size
.height());
564 preferred_size
.SetToMax(size
);
566 // Add the height of the two separators.
567 preferred_size
.Enlarge(
569 kSeparatorPaddingY
* 4 + separator_
->GetPreferredSize().height() * 2);
572 const int kBubbleViewMaxWidth
= 800;
573 preferred_size
.SetToMin(
574 gfx::Size(kBubbleViewMaxWidth
, preferred_size
.height()));
576 // We have to do this after the final width is calculated, since the label
577 // will wrap based on the width.
578 if (managed_user_info_
) {
579 int remaining_width
=
580 preferred_size
.width() - icon_view_
->GetPreferredSize().width() -
581 views::kRelatedControlSmallHorizontalSpacing
;
582 preferred_size
.Enlarge(
584 managed_user_info_
->GetHeightForWidth(remaining_width
) + kItemMarginY
);
587 return preferred_size
;
590 void AvatarMenuBubbleView::Layout() {
592 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
593 views::CustomButton
* item_view
= item_views_
[i
];
594 int item_height
= item_view
->GetPreferredSize().height();
595 int item_width
= width();
596 item_view
->SetBounds(0, y
, item_width
, item_height
);
597 y
+= item_height
+ kItemMarginY
;
600 int separator_height
;
601 if (buttons_view_
|| managed_user_info_
) {
602 separator_height
= separator_
->GetPreferredSize().height();
603 y
+= kSeparatorPaddingY
;
604 separator_
->SetBounds(0, y
, width(), separator_height
);
605 y
+= kSeparatorPaddingY
+ separator_height
;
609 buttons_view_
->SetBounds(0, y
,
610 width(), buttons_view_
->GetPreferredSize().height());
611 } else if (managed_user_info_
) {
612 gfx::Size icon_size
= icon_view_
->GetPreferredSize();
613 gfx::Rect
icon_bounds(0, y
, icon_size
.width(), icon_size
.height());
614 icon_view_
->SetBoundsRect(icon_bounds
);
615 int info_width
= width() - icon_bounds
.right() -
616 views::kRelatedControlSmallHorizontalSpacing
;
617 int height
= managed_user_info_
->GetHeightForWidth(info_width
);
618 managed_user_info_
->SetBounds(
619 icon_bounds
.right() + views::kRelatedControlSmallHorizontalSpacing
,
620 y
, info_width
, height
);
621 y
+= height
+ kItemMarginY
+ kSeparatorPaddingY
;
622 separator_switch_users_
->SetBounds(0, y
, width(), separator_height
);
623 y
+= separator_height
+ kSeparatorPaddingY
;
624 int link_height
= switch_profile_link_
->GetPreferredSize().height();
625 switch_profile_link_
->SetBounds(0, y
, width(), link_height
);
629 bool AvatarMenuBubbleView::AcceleratorPressed(
630 const ui::Accelerator
& accelerator
) {
631 if (accelerator
.key_code() != ui::VKEY_DOWN
&&
632 accelerator
.key_code() != ui::VKEY_UP
)
633 return BubbleDelegateView::AcceleratorPressed(accelerator
);
635 if (item_views_
.empty())
638 // Find the currently focused item. Note that if there is no focused item, the
639 // code below correctly handles a |focus_index| of -1.
640 int focus_index
= -1;
641 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
642 if (item_views_
[i
]->HasFocus()) {
648 // Moved the focus up or down by 1.
649 if (accelerator
.key_code() == ui::VKEY_DOWN
)
650 focus_index
= (focus_index
+ 1) % item_views_
.size();
652 focus_index
= ((focus_index
> 0) ? focus_index
: item_views_
.size()) - 1;
653 item_views_
[focus_index
]->RequestFocus();
658 void AvatarMenuBubbleView::ButtonPressed(views::Button
* sender
,
659 const ui::Event
& event
) {
660 if (sender
->tag() == IDS_PROFILES_MANAGE_PROFILES_BUTTON
) {
661 std::string subpage
= chrome::kSearchUsersSubPage
;
662 chrome::ShowSettingsSubPage(browser_
, subpage
);
664 } else if (sender
->tag() == IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
) {
665 profiles::LockProfile(browser_
->profile());
669 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
670 ProfileItemView
* item_view
= item_views_
[i
];
671 if (sender
== item_view
) {
672 // Clicking on the active profile shouldn't do anything.
673 if (!item_view
->item().active
) {
674 avatar_menu_
->SwitchToProfile(
675 i
, ui::DispositionFromEventFlags(event
.flags()) == NEW_WINDOW
,
676 ProfileMetrics::SWITCH_PROFILE_ICON
);
683 void AvatarMenuBubbleView::LinkClicked(views::Link
* source
, int event_flags
) {
684 if (source
== buttons_view_
) {
685 avatar_menu_
->AddNewProfile(ProfileMetrics::ADD_NEW_USER_ICON
);
688 if (source
== switch_profile_link_
) {
690 OnAvatarMenuChanged(avatar_menu_
.get());
694 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
695 ProfileItemView
* item_view
= item_views_
[i
];
696 if (source
== item_view
->edit_link()) {
697 avatar_menu_
->EditProfile(i
);
703 gfx::Rect
AvatarMenuBubbleView::GetAnchorRect() const {
707 void AvatarMenuBubbleView::Init() {
708 // Build the menu for the first time.
709 OnAvatarMenuChanged(avatar_menu_
.get());
710 AddAccelerator(ui::Accelerator(ui::VKEY_DOWN
, ui::EF_NONE
));
711 AddAccelerator(ui::Accelerator(ui::VKEY_UP
, ui::EF_NONE
));
714 void AvatarMenuBubbleView::WindowClosing() {
715 DCHECK_EQ(avatar_bubble_
, this);
716 avatar_bubble_
= NULL
;
719 void AvatarMenuBubbleView::InitMenuContents(
720 AvatarMenu
* avatar_menu
) {
721 for (size_t i
= 0; i
< avatar_menu
->GetNumberOfItems(); ++i
) {
722 const AvatarMenu::Item
& item
= avatar_menu
->GetItemAt(i
);
723 ProfileItemView
* item_view
= new ProfileItemView(item
,
726 item_view
->SetAccessibleName(l10n_util::GetStringFUTF16(
727 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME
, item
.name
));
728 item_view
->SetFocusable(true);
729 AddChildView(item_view
);
730 item_views_
.push_back(item_view
);
733 if (avatar_menu_
->ShouldShowAddNewProfileLink()) {
734 views::Link
* add_profile_link
= new views::Link(
735 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK
));
736 add_profile_link
->set_listener(this);
737 add_profile_link
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
738 add_profile_link
->SetBackgroundColor(color());
739 separator_
= new views::Separator(views::Separator::HORIZONTAL
);
740 AddChildView(separator_
);
741 buttons_view_
= add_profile_link
;
742 AddChildView(buttons_view_
);
746 void AvatarMenuBubbleView::InitManagedUserContents(
747 AvatarMenu
* avatar_menu
) {
748 // Show the profile of the managed user.
749 size_t active_index
= avatar_menu
->GetActiveProfileIndex();
750 const AvatarMenu::Item
& item
=
751 avatar_menu
->GetItemAt(active_index
);
752 ProfileItemView
* item_view
= new ProfileItemView(item
,
755 item_view
->SetAccessibleName(l10n_util::GetStringFUTF16(
756 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME
, item
.name
));
757 item_views_
.push_back(item_view
);
758 AddChildView(item_view
);
759 separator_
= new views::Separator(views::Separator::HORIZONTAL
);
760 AddChildView(separator_
);
762 // Add information about managed users.
764 new views::Label(avatar_menu_
->GetManagedUserInformation(),
765 ui::ResourceBundle::GetSharedInstance().GetFontList(
766 ui::ResourceBundle::SmallFont
));
767 managed_user_info_
->SetMultiLine(true);
768 managed_user_info_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
769 managed_user_info_
->SetBackgroundColor(color());
770 AddChildView(managed_user_info_
);
772 // Add the managed user icon.
773 icon_view_
= new views::ImageView();
774 icon_view_
->SetImage(avatar_menu_
->GetManagedUserIcon().ToImageSkia());
775 AddChildView(icon_view_
);
777 // Add a link for switching profiles.
778 separator_switch_users_
= new views::Separator(views::Separator::HORIZONTAL
);
779 AddChildView(separator_switch_users_
);
780 switch_profile_link_
= new views::Link(
781 l10n_util::GetStringUTF16(IDS_PROFILES_SWITCH_PROFILE_LINK
));
782 switch_profile_link_
->set_listener(this);
783 switch_profile_link_
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
784 switch_profile_link_
->SetBackgroundColor(color());
785 AddChildView(switch_profile_link_
);
788 void AvatarMenuBubbleView::OnAvatarMenuChanged(
789 AvatarMenu
* avatar_menu
) {
790 // Unset all our child view references and call RemoveAllChildViews() which
791 // will actually delete them.
792 buttons_view_
= NULL
;
793 managed_user_info_
= NULL
;
795 RemoveAllChildViews(true);
797 if (avatar_menu_
->GetManagedUserInformation().empty() || expanded_
)
798 InitMenuContents(avatar_menu
);
800 InitManagedUserContents(avatar_menu
);
802 // If the bubble has already been shown then resize and reposition the bubble.
804 if (GetBubbleFrameView())
808 void AvatarMenuBubbleView::SetBackgroundColors() {
809 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
810 item_views_
[i
]->OnHighlightStateChanged();