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 "chrome/grit/generated_resources.h"
26 #include "components/signin/core/browser/signin_manager.h"
27 #include "components/signin/core/common/profile_management_switches.h"
28 #include "content/public/browser/page_navigator.h"
29 #include "content/public/browser/web_contents.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 ~BadgeImageSource() override
;
90 // Overridden from CanvasImageSource:
91 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 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
156 void OnMouseExited(const ui::MouseEvent
& event
) override
;
157 void OnFocus() override
;
158 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);
200 // ProfileItemView ------------------------------------------------------------
202 // Control that shows information about a single profile.
203 class ProfileItemView
: public views::CustomButton
,
204 public HighlightDelegate
{
206 ProfileItemView(const AvatarMenu::Item
& item
,
207 AvatarMenuBubbleView
* parent
,
210 gfx::Size
GetPreferredSize() const override
;
211 void Layout() override
;
212 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
213 void OnMouseExited(const ui::MouseEvent
& event
) override
;
214 void OnFocus() override
;
215 void OnBlur() override
;
217 void OnHighlightStateChanged() override
;
218 void OnFocusStateChanged(bool has_focus
) override
;
220 const AvatarMenu::Item
& item() const { return item_
; }
221 EditProfileLink
* edit_link() { return edit_link_
; }
224 gfx::ImageSkia
GetBadgedIcon(const gfx::ImageSkia
& icon
);
226 bool IsHighlighted();
228 AvatarMenu::Item item_
;
229 AvatarMenuBubbleView
* parent_
;
231 views::ImageView
* image_view_
;
232 views::Label
* name_label_
;
233 views::Label
* sync_state_label_
;
234 EditProfileLink
* edit_link_
;
236 DISALLOW_COPY_AND_ASSIGN(ProfileItemView
);
239 ProfileItemView::ProfileItemView(const AvatarMenu::Item
& item
,
240 AvatarMenuBubbleView
* parent
,
242 : views::CustomButton(parent
),
246 set_notify_enter_exit_on_child(true);
248 // Create an image-view for the profile. Make sure it ignores events so that
249 // the parent can receive the events instead.
250 image_view_
= new views::ImageView();
251 image_view_
->set_interactive(false);
253 // GetSizedAvatarIcon will resize the icon in case it's too large.
254 const gfx::ImageSkia profile_icon
= *profiles::GetSizedAvatarIcon(item_
.icon
,
255 false, profiles::kAvatarIconWidth
, kItemHeight
).ToImageSkia();
256 if (item_
.active
|| item_
.signin_required
)
257 image_view_
->SetImage(GetBadgedIcon(profile_icon
));
259 image_view_
->SetImage(profile_icon
);
260 AddChildView(image_view_
);
262 // Add a label to show the profile name.
263 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
264 name_label_
= new views::Label(item_
.name
,
265 rb
->GetFontList(item_
.active
?
266 ui::ResourceBundle::BoldFont
:
267 ui::ResourceBundle::BaseFont
));
268 name_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
269 AddChildView(name_label_
);
271 // Add a label to show the sync state.
272 sync_state_label_
= new views::Label(item_
.sync_state
);
274 sync_state_label_
->SetElideBehavior(gfx::ELIDE_EMAIL
);
275 sync_state_label_
->SetFontList(
276 rb
->GetFontList(ui::ResourceBundle::SmallFont
));
277 sync_state_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
278 sync_state_label_
->SetEnabled(false);
279 AddChildView(sync_state_label_
);
281 // Add an edit profile link.
282 edit_link_
= new EditProfileLink(
283 l10n_util::GetStringUTF16(IDS_PROFILES_EDIT_PROFILE_LINK
), this);
284 edit_link_
->set_listener(parent
);
285 edit_link_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
286 AddChildView(edit_link_
);
288 OnHighlightStateChanged();
291 gfx::Size
ProfileItemView::GetPreferredSize() const {
292 int text_width
= std::max(name_label_
->GetPreferredSize().width(),
293 sync_state_label_
->GetPreferredSize().width());
294 text_width
= std::max(edit_link_
->GetPreferredSize().width(), text_width
);
295 text_width
= std::min(kMaxItemTextWidth
, text_width
);
296 return gfx::Size(profiles::kAvatarIconWidth
+ kIconMarginX
+ text_width
,
300 void ProfileItemView::Layout() {
304 // If this is the active item then the icon is already scaled and so
305 // just use the preferred size.
306 icon_rect
.set_size(image_view_
->GetPreferredSize());
307 icon_rect
.set_y((height() - icon_rect
.height()) / 2);
309 const gfx::ImageSkia
& icon
= image_view_
->GetImage();
310 icon_rect
= GetCenteredAndScaledRect(icon
.width(), icon
.height(), 0, 0,
311 profiles::kAvatarIconWidth
, height());
313 image_view_
->SetBoundsRect(icon_rect
);
315 int label_x
= profiles::kAvatarIconWidth
+ kIconMarginX
;
316 int max_label_width
= std::max(width() - label_x
, 0);
317 gfx::Size name_size
= name_label_
->GetPreferredSize();
318 name_size
.set_width(std::min(name_size
.width(), max_label_width
));
319 gfx::Size state_size
= sync_state_label_
->GetPreferredSize();
320 state_size
.set_width(std::min(state_size
.width(), max_label_width
));
321 gfx::Size edit_size
= edit_link_
->GetPreferredSize();
322 edit_size
.set_width(std::min(edit_size
.width(), max_label_width
));
324 const int kNameStatePaddingY
= 2;
325 int labels_height
= name_size
.height() + kNameStatePaddingY
+
326 std::max(state_size
.height(), edit_size
.height());
327 int y
= (height() - labels_height
) / 2;
328 name_label_
->SetBounds(label_x
, y
, name_size
.width(), name_size
.height());
330 int bottom
= y
+ labels_height
;
331 sync_state_label_
->SetBounds(label_x
, bottom
- state_size
.height(),
332 state_size
.width(), state_size
.height());
333 // The edit link overlaps the sync state label.
334 edit_link_
->SetBounds(label_x
, bottom
- edit_size
.height(),
335 edit_size
.width(), edit_size
.height());
338 void ProfileItemView::OnMouseEntered(const ui::MouseEvent
& event
) {
339 views::CustomButton::OnMouseEntered(event
);
340 OnHighlightStateChanged();
343 void ProfileItemView::OnMouseExited(const ui::MouseEvent
& event
) {
344 views::CustomButton::OnMouseExited(event
);
345 OnHighlightStateChanged();
348 void ProfileItemView::OnFocus() {
349 views::CustomButton::OnFocus();
350 OnFocusStateChanged(true);
353 void ProfileItemView::OnBlur() {
354 views::CustomButton::OnBlur();
355 OnFocusStateChanged(false);
358 void ProfileItemView::OnHighlightStateChanged() {
359 const SkColor color
= IsHighlighted() ? kHighlightColor
: parent_
->color();
360 set_background(views::Background::CreateSolidBackground(color
));
361 name_label_
->SetBackgroundColor(color
);
362 sync_state_label_
->SetBackgroundColor(color
);
363 edit_link_
->SetBackgroundColor(color
);
365 bool show_edit
= IsHighlighted() && item_
.active
&&
366 menu_
->ShouldShowEditProfileLink();
367 sync_state_label_
->SetVisible(!show_edit
);
368 edit_link_
->SetVisible(show_edit
);
372 void ProfileItemView::OnFocusStateChanged(bool has_focus
) {
373 if (!has_focus
&& state() != views::CustomButton::STATE_DISABLED
)
374 SetState(views::CustomButton::STATE_NORMAL
);
375 OnHighlightStateChanged();
379 gfx::ImageSkia
ProfileItemView::GetBadgedIcon(const gfx::ImageSkia
& icon
) {
380 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
381 const gfx::ImageSkia
* badge
= NULL
;
384 badge
= rb
->GetImageSkiaNamed(IDR_PROFILE_SELECTED
);
385 else if (item_
.signin_required
) // TODO(bcwhite): create new icon
386 badge
= rb
->GetImageSkiaNamed(IDR_OMNIBOX_HTTPS_VALID
);
388 NOTREACHED(); // function should only be called if one of above is true
390 gfx::Size icon_size
= GetCenteredAndScaledRect(icon
.width(), icon
.height(),
391 0, 0, profiles::kAvatarIconWidth
, kItemHeight
).size();
392 gfx::CanvasImageSource
* source
=
393 new BadgeImageSource(icon
, icon_size
, *badge
);
394 // ImageSkia takes ownership of |source|.
395 return gfx::ImageSkia(source
, source
->size());
398 bool ProfileItemView::IsHighlighted() {
399 return state() == views::CustomButton::STATE_PRESSED
||
400 state() == views::CustomButton::STATE_HOVERED
||
401 edit_link_
->state() == views::CustomButton::STATE_PRESSED
||
402 edit_link_
->state() == views::CustomButton::STATE_HOVERED
||
404 edit_link_
->HasFocus();
408 // ActionButtonView -----------------------------------------------------------
410 // A custom view that manages the "action" buttons at the bottom of the list
412 class ActionButtonView
: public views::View
{
414 ActionButtonView(views::ButtonListener
* listener
, Profile
* profile
);
417 views::LabelButton
* manage_button_
;
418 views::LabelButton
* signout_button_
;
420 DISALLOW_COPY_AND_ASSIGN(ActionButtonView
);
424 ActionButtonView::ActionButtonView(views::ButtonListener
* listener
,
426 : manage_button_(NULL
),
427 signout_button_(NULL
) {
428 std::string username
;
429 SigninManagerBase
* signin
=
430 SigninManagerFactory::GetForProfile(profile
);
432 username
= signin
->GetAuthenticatedUsername();
434 manage_button_
= new views::LabelButton(
435 listener
, l10n_util::GetStringUTF16(IDS_PROFILES_MANAGE_PROFILES_BUTTON
));
436 manage_button_
->SetStyle(views::Button::STYLE_BUTTON
);
437 manage_button_
->SetTooltipText(
438 l10n_util::GetStringUTF16(IDS_PROFILES_MANAGE_PROFILES_BUTTON_TIP
));
439 manage_button_
->set_tag(IDS_PROFILES_MANAGE_PROFILES_BUTTON
);
441 signout_button_
= new views::LabelButton(
442 listener
, l10n_util::GetStringUTF16(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
));
443 signout_button_
->SetStyle(views::Button::STYLE_BUTTON
);
444 if (username
.empty()) {
445 signout_button_
->SetTooltipText(
446 l10n_util::GetStringUTF16(
447 IDS_PROFILES_PROFILE_SIGNOUT_BUTTON_TIP_UNAVAILABLE
));
448 signout_button_
->SetEnabled(false);
450 signout_button_
->SetTooltipText(
451 l10n_util::GetStringFUTF16(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON_TIP
,
452 base::UTF8ToUTF16(username
)));
454 signout_button_
->set_tag(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
);
456 views::GridLayout
* layout
= new views::GridLayout(this);
457 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
458 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 1,
459 views::GridLayout::USE_PREF
, 0, 0);
460 columns
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::FILL
, 1,
461 views::GridLayout::USE_PREF
, 0, 0);
462 layout
->StartRow(0, 0);
463 layout
->AddView(signout_button_
);
464 layout
->AddView(manage_button_
);
465 SetLayoutManager(layout
);
469 // AvatarMenuBubbleView -------------------------------------------------------
472 AvatarMenuBubbleView
* AvatarMenuBubbleView::avatar_bubble_
= NULL
;
473 bool AvatarMenuBubbleView::close_on_deactivate_for_testing_
= true;
476 void AvatarMenuBubbleView::ShowBubble(
477 views::View
* anchor_view
,
478 views::BubbleBorder::Arrow arrow
,
479 views::BubbleBorder::ArrowPaintType arrow_paint_type
,
480 views::BubbleBorder::BubbleAlignment border_alignment
,
481 const gfx::Rect
& anchor_rect
,
486 DCHECK(chrome::IsCommandEnabled(browser
, IDC_SHOW_AVATAR_MENU
));
487 avatar_bubble_
= new AvatarMenuBubbleView(
488 anchor_view
, arrow
, anchor_rect
, browser
);
489 views::BubbleDelegateView::CreateBubble(avatar_bubble_
);
490 avatar_bubble_
->set_close_on_deactivate(close_on_deactivate_for_testing_
);
491 avatar_bubble_
->SetBackgroundColors();
492 avatar_bubble_
->SetAlignment(border_alignment
);
493 avatar_bubble_
->SetArrowPaintType(arrow_paint_type
);
494 avatar_bubble_
->GetWidget()->Show();
498 bool AvatarMenuBubbleView::IsShowing() {
499 return avatar_bubble_
!= NULL
;
503 void AvatarMenuBubbleView::Hide() {
505 avatar_bubble_
->GetWidget()->Close();
508 AvatarMenuBubbleView::AvatarMenuBubbleView(
509 views::View
* anchor_view
,
510 views::BubbleBorder::Arrow arrow
,
511 const gfx::Rect
& anchor_rect
,
513 : BubbleDelegateView(anchor_view
, arrow
),
514 anchor_rect_(anchor_rect
),
518 supervised_user_info_(NULL
),
519 separator_switch_users_(NULL
),
520 switch_profile_link_(nullptr),
522 avatar_menu_
.reset(new AvatarMenu(
523 &g_browser_process
->profile_manager()->GetProfileInfoCache(),
526 avatar_menu_
->RebuildMenu();
529 AvatarMenuBubbleView::~AvatarMenuBubbleView() {
532 gfx::Size
AvatarMenuBubbleView::GetPreferredSize() const {
533 const int kBubbleViewMinWidth
= 175;
534 gfx::Size
preferred_size(kBubbleViewMinWidth
, 0);
535 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
536 gfx::Size size
= item_views_
[i
]->GetPreferredSize();
537 preferred_size
.Enlarge(0, size
.height() + kItemMarginY
);
538 preferred_size
.SetToMax(size
);
542 preferred_size
.Enlarge(
543 0, kSeparatorPaddingY
* 2 + separator_
->GetPreferredSize().height());
545 gfx::Size buttons_size
= buttons_view_
->GetPreferredSize();
546 preferred_size
.Enlarge(0, buttons_size
.height());
547 preferred_size
.SetToMax(buttons_size
);
551 if (supervised_user_info_
) {
552 // First handle the switch profile link because it can still affect the
554 gfx::Size size
= switch_profile_link_
->GetPreferredSize();
555 preferred_size
.Enlarge(0, size
.height());
556 preferred_size
.SetToMax(size
);
558 // Add the height of the two separators.
559 preferred_size
.Enlarge(
561 kSeparatorPaddingY
* 4 + separator_
->GetPreferredSize().height() * 2);
564 const int kBubbleViewMaxWidth
= 800;
565 preferred_size
.SetToMin(
566 gfx::Size(kBubbleViewMaxWidth
, preferred_size
.height()));
568 // We have to do this after the final width is calculated, since the label
569 // will wrap based on the width.
570 if (supervised_user_info_
) {
571 int remaining_width
=
572 preferred_size
.width() - icon_view_
->GetPreferredSize().width() -
573 views::kRelatedControlSmallHorizontalSpacing
;
574 preferred_size
.Enlarge(
576 supervised_user_info_
->GetHeightForWidth(remaining_width
) +
580 return preferred_size
;
583 void AvatarMenuBubbleView::Layout() {
585 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
586 views::CustomButton
* item_view
= item_views_
[i
];
587 int item_height
= item_view
->GetPreferredSize().height();
588 int item_width
= width();
589 item_view
->SetBounds(0, y
, item_width
, item_height
);
590 y
+= item_height
+ kItemMarginY
;
593 int separator_height
= 0;
594 if (buttons_view_
|| supervised_user_info_
) {
595 separator_height
= separator_
->GetPreferredSize().height();
596 y
+= kSeparatorPaddingY
;
597 separator_
->SetBounds(0, y
, width(), separator_height
);
598 y
+= kSeparatorPaddingY
+ separator_height
;
602 buttons_view_
->SetBounds(0, y
,
603 width(), buttons_view_
->GetPreferredSize().height());
604 } else if (supervised_user_info_
) {
605 gfx::Size icon_size
= icon_view_
->GetPreferredSize();
606 gfx::Rect
icon_bounds(0, y
, icon_size
.width(), icon_size
.height());
607 icon_view_
->SetBoundsRect(icon_bounds
);
608 int info_width
= width() - icon_bounds
.right() -
609 views::kRelatedControlSmallHorizontalSpacing
;
610 int height
= supervised_user_info_
->GetHeightForWidth(info_width
);
611 supervised_user_info_
->SetBounds(
612 icon_bounds
.right() + views::kRelatedControlSmallHorizontalSpacing
,
613 y
, info_width
, height
);
614 y
+= height
+ kItemMarginY
+ kSeparatorPaddingY
;
615 separator_switch_users_
->SetBounds(0, y
, width(), separator_height
);
616 y
+= separator_height
+ kSeparatorPaddingY
;
617 int link_height
= switch_profile_link_
->GetPreferredSize().height();
618 switch_profile_link_
->SetBounds(0, y
, width(), link_height
);
622 bool AvatarMenuBubbleView::AcceleratorPressed(
623 const ui::Accelerator
& accelerator
) {
624 if (accelerator
.key_code() != ui::VKEY_DOWN
&&
625 accelerator
.key_code() != ui::VKEY_UP
)
626 return BubbleDelegateView::AcceleratorPressed(accelerator
);
628 if (item_views_
.empty())
631 // Find the currently focused item. Note that if there is no focused item, the
632 // code below correctly handles a |focus_index| of -1.
633 int focus_index
= -1;
634 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
635 if (item_views_
[i
]->HasFocus()) {
641 // Moved the focus up or down by 1.
642 if (accelerator
.key_code() == ui::VKEY_DOWN
)
643 focus_index
= (focus_index
+ 1) % item_views_
.size();
645 focus_index
= ((focus_index
> 0) ? focus_index
: item_views_
.size()) - 1;
646 item_views_
[focus_index
]->RequestFocus();
651 void AvatarMenuBubbleView::ButtonPressed(views::Button
* sender
,
652 const ui::Event
& event
) {
653 if (sender
->tag() == IDS_PROFILES_MANAGE_PROFILES_BUTTON
) {
654 std::string subpage
= chrome::kSearchUsersSubPage
;
655 chrome::ShowSettingsSubPage(browser_
, subpage
);
657 } else if (sender
->tag() == IDS_PROFILES_PROFILE_SIGNOUT_BUTTON
) {
658 profiles::LockProfile(browser_
->profile());
662 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
663 ProfileItemView
* item_view
= item_views_
[i
];
664 if (sender
== item_view
) {
665 // Clicking on the active profile shouldn't do anything.
666 if (!item_view
->item().active
) {
667 avatar_menu_
->SwitchToProfile(
668 i
, ui::DispositionFromEventFlags(event
.flags()) == NEW_WINDOW
,
669 ProfileMetrics::SWITCH_PROFILE_ICON
);
676 void AvatarMenuBubbleView::LinkClicked(views::Link
* source
, int event_flags
) {
677 if (source
== buttons_view_
) {
678 avatar_menu_
->AddNewProfile(ProfileMetrics::ADD_NEW_USER_ICON
);
681 if (source
== switch_profile_link_
) {
683 OnAvatarMenuChanged(avatar_menu_
.get());
687 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
688 ProfileItemView
* item_view
= item_views_
[i
];
689 if (source
== item_view
->edit_link()) {
690 avatar_menu_
->EditProfile(i
);
696 gfx::Rect
AvatarMenuBubbleView::GetAnchorRect() const {
700 void AvatarMenuBubbleView::Init() {
701 // Build the menu for the first time.
702 OnAvatarMenuChanged(avatar_menu_
.get());
703 AddAccelerator(ui::Accelerator(ui::VKEY_DOWN
, ui::EF_NONE
));
704 AddAccelerator(ui::Accelerator(ui::VKEY_UP
, ui::EF_NONE
));
707 void AvatarMenuBubbleView::WindowClosing() {
708 DCHECK_EQ(avatar_bubble_
, this);
709 avatar_bubble_
= NULL
;
712 void AvatarMenuBubbleView::InitMenuContents(
713 AvatarMenu
* avatar_menu
) {
714 for (size_t i
= 0; i
< avatar_menu
->GetNumberOfItems(); ++i
) {
715 const AvatarMenu::Item
& item
= avatar_menu
->GetItemAt(i
);
716 ProfileItemView
* item_view
= new ProfileItemView(item
,
719 item_view
->SetAccessibleName(l10n_util::GetStringFUTF16(
720 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME
, item
.name
));
721 item_view
->SetFocusable(true);
722 AddChildView(item_view
);
723 item_views_
.push_back(item_view
);
726 if (avatar_menu_
->ShouldShowAddNewProfileLink()) {
727 views::Link
* add_profile_link
= new views::Link(
728 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK
));
729 add_profile_link
->set_listener(this);
730 add_profile_link
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
731 add_profile_link
->SetBackgroundColor(color());
732 separator_
= new views::Separator(views::Separator::HORIZONTAL
);
733 AddChildView(separator_
);
734 buttons_view_
= add_profile_link
;
735 AddChildView(buttons_view_
);
739 void AvatarMenuBubbleView::InitSupervisedUserContents(
740 AvatarMenu
* avatar_menu
) {
741 // Show the profile of the supervised user.
742 size_t active_index
= avatar_menu
->GetActiveProfileIndex();
743 const AvatarMenu::Item
& item
=
744 avatar_menu
->GetItemAt(active_index
);
745 ProfileItemView
* item_view
= new ProfileItemView(item
,
748 item_view
->SetAccessibleName(l10n_util::GetStringFUTF16(
749 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME
, item
.name
));
750 item_views_
.push_back(item_view
);
751 AddChildView(item_view
);
752 separator_
= new views::Separator(views::Separator::HORIZONTAL
);
753 AddChildView(separator_
);
755 // Add information about supervised users.
756 supervised_user_info_
=
757 new views::Label(avatar_menu_
->GetSupervisedUserInformation(),
758 ui::ResourceBundle::GetSharedInstance().GetFontList(
759 ui::ResourceBundle::SmallFont
));
760 supervised_user_info_
->SetMultiLine(true);
761 supervised_user_info_
->SetAllowCharacterBreak(true);
762 supervised_user_info_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
763 supervised_user_info_
->SetBackgroundColor(color());
764 AddChildView(supervised_user_info_
);
766 // Add the supervised user icon.
767 icon_view_
= new views::ImageView();
768 icon_view_
->SetImage(avatar_menu_
->GetSupervisedUserIcon().ToImageSkia());
769 AddChildView(icon_view_
);
771 // Add a link for switching profiles.
772 separator_switch_users_
= new views::Separator(views::Separator::HORIZONTAL
);
773 AddChildView(separator_switch_users_
);
774 switch_profile_link_
= new views::Link(
775 l10n_util::GetStringUTF16(IDS_PROFILES_SWITCH_PROFILE_LINK
));
776 switch_profile_link_
->set_listener(this);
777 switch_profile_link_
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
778 switch_profile_link_
->SetBackgroundColor(color());
779 AddChildView(switch_profile_link_
);
782 void AvatarMenuBubbleView::OnAvatarMenuChanged(
783 AvatarMenu
* avatar_menu
) {
784 // Unset all our child view references and call RemoveAllChildViews() which
785 // will actually delete them.
786 buttons_view_
= NULL
;
787 supervised_user_info_
= NULL
;
789 RemoveAllChildViews(true);
791 if (avatar_menu_
->GetSupervisedUserInformation().empty() || expanded_
)
792 InitMenuContents(avatar_menu
);
794 InitSupervisedUserContents(avatar_menu
);
796 // If the bubble has already been shown then resize and reposition the bubble.
798 if (GetBubbleFrameView())
802 void AvatarMenuBubbleView::SetBackgroundColors() {
803 for (size_t i
= 0; i
< item_views_
.size(); ++i
) {
804 item_views_
[i
]->OnHighlightStateChanged();