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_button.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/avatar_menu.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profile_metrics.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/views/frame/browser_view.h"
20 #include "chrome/browser/ui/views/profiles/avatar_menu_bubble_view.h"
21 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h"
22 #include "components/signin/core/common/profile_management_switches.h"
23 #include "content/public/browser/notification_service.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/canvas.h"
27 #include "ui/views/view_targeter.h"
28 #include "ui/views/widget/widget.h"
30 static inline int Round(double x
) {
31 return static_cast<int>(x
+ 0.5);
35 const char AvatarMenuButton::kViewClassName
[] = "AvatarMenuButton";
37 AvatarMenuButton::AvatarMenuButton(Browser
* browser
, bool disabled
)
38 : MenuButton(NULL
, base::string16(), this, false),
43 button_on_right_(false) {
44 // In RTL mode, the avatar icon should be looking the opposite direction.
45 EnableCanvasFlippingForRTLUI(true);
48 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
51 AvatarMenuButton::~AvatarMenuButton() {
54 const char* AvatarMenuButton::GetClassName() const {
55 return kViewClassName
;
58 void AvatarMenuButton::OnPaint(gfx::Canvas
* canvas
) {
62 if (old_height_
!= height() || button_icon_
.isNull()) {
63 old_height_
= height();
64 button_icon_
= *profiles::GetAvatarIconForTitleBar(
65 *icon_
, is_rectangle_
, width(), height()).ToImageSkia();
68 // Scale the image to fit the width of the button.
69 int dst_width
= std::min(button_icon_
.width(), width());
70 // Truncate rather than rounding, so that for odd widths we put the extra
72 int dst_x
= (width() - dst_width
) / 2;
74 // Scale the height and maintain aspect ratio. This means that the
75 // icon may not fit in the view. That's ok, we just vertically center it.
77 static_cast<float>(dst_width
) / static_cast<float>(button_icon_
.width());
78 // Round here so that we minimize the aspect ratio drift.
79 int dst_height
= Round(button_icon_
.height() * scale
);
80 // Round rather than truncating, so that for odd heights we select an extra
81 // pixel below the image center rather than above. This is because the
82 // incognito image has shadows at the top that make the apparent center below
84 int dst_y
= Round((height() - dst_height
) / 2.0);
85 canvas
->DrawImageInt(button_icon_
, 0, 0, button_icon_
.width(),
86 button_icon_
.height(), dst_x
, dst_y
, dst_width
, dst_height
, false);
89 void AvatarMenuButton::SetAvatarIcon(const gfx::Image
& icon
,
91 icon_
.reset(new gfx::Image(icon
));
92 button_icon_
= gfx::ImageSkia();
93 is_rectangle_
= is_rectangle
;
98 bool AvatarMenuButton::GetAvatarImages(Profile
* profile
,
99 bool should_show_avatar_menu
,
101 gfx::Image
* taskbar_badge_avatar
,
102 bool* is_rectangle
) {
103 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
104 if (profile
->GetProfileType() == Profile::GUEST_PROFILE
) {
106 GetImageNamed(profiles::GetPlaceholderAvatarIconResourceID());
107 } else if (profile
->GetProfileType() == Profile::INCOGNITO_PROFILE
) {
108 *avatar
= rb
.GetImageNamed(IDR_OTR_ICON
);
109 // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test
110 // environment handles profile directories correctly.
111 #if !defined(OS_CHROMEOS)
112 bool is_badge_rectangle
= false;
113 // The taskbar badge should be the profile avatar, not the OTR avatar.
114 AvatarMenu::GetImageForMenuButton(profile
->GetPath(),
115 taskbar_badge_avatar
,
116 &is_badge_rectangle
);
118 } else if (should_show_avatar_menu
) {
119 ProfileInfoCache
& cache
=
120 g_browser_process
->profile_manager()->GetProfileInfoCache();
121 size_t index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
122 if (index
== std::string::npos
)
125 if (switches::IsNewAvatarMenu()) {
126 *avatar
= cache
.GetAvatarIconOfProfileAtIndex(index
);
127 // TODO(noms): Once the code for the old avatar menu button is removed,
128 // this function will only be called for badging the taskbar icon. The
129 // function can be renamed to something like GetAvatarImageForBadging()
130 // and only needs to return the avatar from
131 // AvatarMenu::GetImageForMenuButton().
132 #if !defined(OS_CHROMEOS)
133 bool is_badge_rectangle
= false;
134 AvatarMenu::GetImageForMenuButton(profile
->GetPath(),
135 taskbar_badge_avatar
,
136 &is_badge_rectangle
);
139 AvatarMenu::GetImageForMenuButton(profile
->GetPath(),
147 // views::ViewTargeterDelegate:
148 bool AvatarMenuButton::DoesIntersectRect(const views::View
* target
,
149 const gfx::Rect
& rect
) const {
150 CHECK_EQ(target
, this);
152 views::ViewTargeterDelegate::DoesIntersectRect(target
, rect
);
155 // views::MenuButtonListener implementation
156 void AvatarMenuButton::OnMenuButtonClicked(views::View
* source
,
157 const gfx::Point
& point
) {
159 chrome::ShowAvatarMenu(browser_
);