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 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);
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 virtual gfx::Size GetPreferredSize() const OVERRIDE;
211 virtual void Layout() OVERRIDE;
212 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
213 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
214 virtual void OnFocus() OVERRIDE;
215 virtual void OnBlur() OVERRIDE;
217 virtual void OnHighlightStateChanged() OVERRIDE;
218 virtual 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),
521 avatar_menu_.reset(new AvatarMenu(
522 &g_browser_process->profile_manager()->GetProfileInfoCache(),
525 avatar_menu_->RebuildMenu();
528 AvatarMenuBubbleView::~AvatarMenuBubbleView() {
531 gfx::Size AvatarMenuBubbleView::GetPreferredSize() const {
532 const int kBubbleViewMinWidth = 175;
533 gfx::Size preferred_size(kBubbleViewMinWidth, 0);
534 for (size_t i = 0; i < item_views_.size(); ++i) {
535 gfx::Size size = item_views_[i]->GetPreferredSize();
536 preferred_size.Enlarge(0, size.height() + kItemMarginY);
537 preferred_size.SetToMax(size);
541 preferred_size.Enlarge(
542 0, kSeparatorPaddingY * 2 + separator_->GetPreferredSize().height());
544 gfx::Size buttons_size = buttons_view_->GetPreferredSize();
545 preferred_size.Enlarge(0, buttons_size.height());
546 preferred_size.SetToMax(buttons_size);
550 if (supervised_user_info_) {
551 // First handle the switch profile link because it can still affect the
553 gfx::Size size = switch_profile_link_->GetPreferredSize();
554 preferred_size.Enlarge(0, size.height());
555 preferred_size.SetToMax(size);
557 // Add the height of the two separators.
558 preferred_size.Enlarge(
560 kSeparatorPaddingY * 4 + separator_->GetPreferredSize().height() * 2);
563 const int kBubbleViewMaxWidth = 800;
564 preferred_size.SetToMin(
565 gfx::Size(kBubbleViewMaxWidth, preferred_size.height()));
567 // We have to do this after the final width is calculated, since the label
568 // will wrap based on the width.
569 if (supervised_user_info_) {
570 int remaining_width =
571 preferred_size.width() - icon_view_->GetPreferredSize().width() -
572 views::kRelatedControlSmallHorizontalSpacing;
573 preferred_size.Enlarge(
575 supervised_user_info_->GetHeightForWidth(remaining_width) +
579 return preferred_size;
582 void AvatarMenuBubbleView::Layout() {
584 for (size_t i = 0; i < item_views_.size(); ++i) {
585 views::CustomButton* item_view = item_views_[i];
586 int item_height = item_view->GetPreferredSize().height();
587 int item_width = width();
588 item_view->SetBounds(0, y, item_width, item_height);
589 y += item_height + kItemMarginY;
592 int separator_height = 0;
593 if (buttons_view_ || supervised_user_info_) {
594 separator_height = separator_->GetPreferredSize().height();
595 y += kSeparatorPaddingY;
596 separator_->SetBounds(0, y, width(), separator_height);
597 y += kSeparatorPaddingY + separator_height;
601 buttons_view_->SetBounds(0, y,
602 width(), buttons_view_->GetPreferredSize().height());
603 } else if (supervised_user_info_) {
604 gfx::Size icon_size = icon_view_->GetPreferredSize();
605 gfx::Rect icon_bounds(0, y, icon_size.width(), icon_size.height());
606 icon_view_->SetBoundsRect(icon_bounds);
607 int info_width = width() - icon_bounds.right() -
608 views::kRelatedControlSmallHorizontalSpacing;
609 int height = supervised_user_info_->GetHeightForWidth(info_width);
610 supervised_user_info_->SetBounds(
611 icon_bounds.right() + views::kRelatedControlSmallHorizontalSpacing,
612 y, info_width, height);
613 y += height + kItemMarginY + kSeparatorPaddingY;
614 separator_switch_users_->SetBounds(0, y, width(), separator_height);
615 y += separator_height + kSeparatorPaddingY;
616 int link_height = switch_profile_link_->GetPreferredSize().height();
617 switch_profile_link_->SetBounds(0, y, width(), link_height);
621 bool AvatarMenuBubbleView::AcceleratorPressed(
622 const ui::Accelerator& accelerator) {
623 if (accelerator.key_code() != ui::VKEY_DOWN &&
624 accelerator.key_code() != ui::VKEY_UP)
625 return BubbleDelegateView::AcceleratorPressed(accelerator);
627 if (item_views_.empty())
630 // Find the currently focused item. Note that if there is no focused item, the
631 // code below correctly handles a |focus_index| of -1.
632 int focus_index = -1;
633 for (size_t i = 0; i < item_views_.size(); ++i) {
634 if (item_views_[i]->HasFocus()) {
640 // Moved the focus up or down by 1.
641 if (accelerator.key_code() == ui::VKEY_DOWN)
642 focus_index = (focus_index + 1) % item_views_.size();
644 focus_index = ((focus_index > 0) ? focus_index : item_views_.size()) - 1;
645 item_views_[focus_index]->RequestFocus();
650 void AvatarMenuBubbleView::ButtonPressed(views::Button* sender,
651 const ui::Event& event) {
652 if (sender->tag() == IDS_PROFILES_MANAGE_PROFILES_BUTTON) {
653 std::string subpage = chrome::kSearchUsersSubPage;
654 chrome::ShowSettingsSubPage(browser_, subpage);
656 } else if (sender->tag() == IDS_PROFILES_PROFILE_SIGNOUT_BUTTON) {
657 profiles::LockProfile(browser_->profile());
661 for (size_t i = 0; i < item_views_.size(); ++i) {
662 ProfileItemView* item_view = item_views_[i];
663 if (sender == item_view) {
664 // Clicking on the active profile shouldn't do anything.
665 if (!item_view->item().active) {
666 avatar_menu_->SwitchToProfile(
667 i, ui::DispositionFromEventFlags(event.flags()) == NEW_WINDOW,
668 ProfileMetrics::SWITCH_PROFILE_ICON);
675 void AvatarMenuBubbleView::LinkClicked(views::Link* source, int event_flags) {
676 if (source == buttons_view_) {
677 avatar_menu_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_ICON);
680 if (source == switch_profile_link_) {
682 OnAvatarMenuChanged(avatar_menu_.get());
686 for (size_t i = 0; i < item_views_.size(); ++i) {
687 ProfileItemView* item_view = item_views_[i];
688 if (source == item_view->edit_link()) {
689 avatar_menu_->EditProfile(i);
695 gfx::Rect AvatarMenuBubbleView::GetAnchorRect() const {
699 void AvatarMenuBubbleView::Init() {
700 // Build the menu for the first time.
701 OnAvatarMenuChanged(avatar_menu_.get());
702 AddAccelerator(ui::Accelerator(ui::VKEY_DOWN, ui::EF_NONE));
703 AddAccelerator(ui::Accelerator(ui::VKEY_UP, ui::EF_NONE));
706 void AvatarMenuBubbleView::WindowClosing() {
707 DCHECK_EQ(avatar_bubble_, this);
708 avatar_bubble_ = NULL;
711 void AvatarMenuBubbleView::InitMenuContents(
712 AvatarMenu* avatar_menu) {
713 for (size_t i = 0; i < avatar_menu->GetNumberOfItems(); ++i) {
714 const AvatarMenu::Item& item = avatar_menu->GetItemAt(i);
715 ProfileItemView* item_view = new ProfileItemView(item,
718 item_view->SetAccessibleName(l10n_util::GetStringFUTF16(
719 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME, item.name));
720 item_view->SetFocusable(true);
721 AddChildView(item_view);
722 item_views_.push_back(item_view);
725 if (avatar_menu_->ShouldShowAddNewProfileLink()) {
726 views::Link* add_profile_link = new views::Link(
727 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK));
728 add_profile_link->set_listener(this);
729 add_profile_link->SetHorizontalAlignment(gfx::ALIGN_CENTER);
730 add_profile_link->SetBackgroundColor(color());
731 separator_ = new views::Separator(views::Separator::HORIZONTAL);
732 AddChildView(separator_);
733 buttons_view_ = add_profile_link;
734 AddChildView(buttons_view_);
738 void AvatarMenuBubbleView::InitSupervisedUserContents(
739 AvatarMenu* avatar_menu) {
740 // Show the profile of the supervised user.
741 size_t active_index = avatar_menu->GetActiveProfileIndex();
742 const AvatarMenu::Item& item =
743 avatar_menu->GetItemAt(active_index);
744 ProfileItemView* item_view = new ProfileItemView(item,
747 item_view->SetAccessibleName(l10n_util::GetStringFUTF16(
748 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME, item.name));
749 item_views_.push_back(item_view);
750 AddChildView(item_view);
751 separator_ = new views::Separator(views::Separator::HORIZONTAL);
752 AddChildView(separator_);
754 // Add information about supervised users.
755 supervised_user_info_ =
756 new views::Label(avatar_menu_->GetSupervisedUserInformation(),
757 ui::ResourceBundle::GetSharedInstance().GetFontList(
758 ui::ResourceBundle::SmallFont));
759 supervised_user_info_->SetMultiLine(true);
760 supervised_user_info_->SetAllowCharacterBreak(true);
761 supervised_user_info_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
762 supervised_user_info_->SetBackgroundColor(color());
763 AddChildView(supervised_user_info_);
765 // Add the supervised user icon.
766 icon_view_ = new views::ImageView();
767 icon_view_->SetImage(avatar_menu_->GetSupervisedUserIcon().ToImageSkia());
768 AddChildView(icon_view_);
770 // Add a link for switching profiles.
771 separator_switch_users_ = new views::Separator(views::Separator::HORIZONTAL);
772 AddChildView(separator_switch_users_);
773 switch_profile_link_ = new views::Link(
774 l10n_util::GetStringUTF16(IDS_PROFILES_SWITCH_PROFILE_LINK));
775 switch_profile_link_->set_listener(this);
776 switch_profile_link_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
777 switch_profile_link_->SetBackgroundColor(color());
778 AddChildView(switch_profile_link_);
781 void AvatarMenuBubbleView::OnAvatarMenuChanged(
782 AvatarMenu* avatar_menu) {
783 // Unset all our child view references and call RemoveAllChildViews() which
784 // will actually delete them.
785 buttons_view_ = NULL;
786 supervised_user_info_ = NULL;
788 RemoveAllChildViews(true);
790 if (avatar_menu_->GetSupervisedUserInformation().empty() || expanded_)
791 InitMenuContents(avatar_menu);
793 InitSupervisedUserContents(avatar_menu);
795 // If the bubble has already been shown then resize and reposition the bubble.
797 if (GetBubbleFrameView())
801 void AvatarMenuBubbleView::SetBackgroundColors() {
802 for (size_t i = 0; i < item_views_.size(); ++i) {
803 item_views_[i]->OnHighlightStateChanged();