[Win] Add a fast profile switcher to the Windows taskbar item.
[chromium-blink-merge.git] / chrome / browser / ui / views / profiles / avatar_menu_button.cc
blob9ee925000458e64ca71ad28341122495bcfe6a6c
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);
34 // static
35 const char AvatarMenuButton::kViewClassName[] = "AvatarMenuButton";
37 AvatarMenuButton::AvatarMenuButton(Browser* browser, bool disabled)
38 : MenuButton(NULL, base::string16(), this, false),
39 browser_(browser),
40 disabled_(disabled),
41 is_rectangle_(false),
42 old_height_(0),
43 button_on_right_(false) {
44 // In RTL mode, the avatar icon should be looking the opposite direction.
45 EnableCanvasFlippingForRTLUI(true);
47 SetEventTargeter(
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) {
59 if (!icon_.get())
60 return;
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
71 // pixel on the left.
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.
76 float scale =
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
83 // the real center.
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,
90 bool is_rectangle) {
91 icon_.reset(new gfx::Image(icon));
92 button_icon_ = gfx::ImageSkia();
93 is_rectangle_ = is_rectangle;
94 SchedulePaint();
97 // static
98 void AvatarMenuButton::GetAvatarImages(Profile* profile,
99 gfx::Image* avatar,
100 gfx::Image* taskbar_badge_avatar,
101 bool *is_rectangle) {
102 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
103 if (profile->GetProfileType() == Profile::GUEST_PROFILE) {
104 *avatar = rb.
105 GetImageNamed(profiles::GetPlaceholderAvatarIconResourceID());
106 } else if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) {
107 *avatar = rb.GetImageNamed(IDR_OTR_ICON);
108 // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test
109 // environment handles profile directories correctly.
110 #if !defined(OS_CHROMEOS)
111 bool is_badge_rectangle = false;
112 // The taskbar badge should be the profile avatar, not the OTR avatar.
113 profiles::GetTransparentBackgroundProfileAvatar(
114 profile->GetPath(), taskbar_badge_avatar, &is_badge_rectangle);
115 #endif
116 } else if (AvatarMenu::ShouldShowAvatarMenu()) {
117 ProfileInfoCache& cache =
118 g_browser_process->profile_manager()->GetProfileInfoCache();
119 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
120 if (index == std::string::npos)
121 return;
123 if (switches::IsNewAvatarMenu()) {
124 *avatar = cache.GetAvatarIconOfProfileAtIndex(index);
125 // TODO(noms): Once the code for the old avatar menu button is removed,
126 // this function will only be called for badging the taskbar icon. The
127 // function can be renamed to something like GetAvatarImageForBadging()
128 // and only needs to return the avatar from
129 // profiles::GetTransparentBackgroundProfileAvatar.
130 #if !defined(OS_CHROMEOS)
131 bool is_badge_rectangle = false;
132 profiles::GetTransparentBackgroundProfileAvatar(
133 profile->GetPath(), taskbar_badge_avatar, &is_badge_rectangle);
134 #endif
135 } else {
136 profiles::GetTransparentBackgroundProfileAvatar(
137 profile->GetPath(), avatar, is_rectangle);
142 // views::ViewTargeterDelegate:
143 bool AvatarMenuButton::DoesIntersectRect(const views::View* target,
144 const gfx::Rect& rect) const {
145 CHECK_EQ(target, this);
146 return !disabled_ &&
147 views::ViewTargeterDelegate::DoesIntersectRect(target, rect);
150 // views::MenuButtonListener implementation
151 void AvatarMenuButton::OnMenuButtonClicked(views::View* source,
152 const gfx::Point& point) {
153 if (!disabled_)
154 chrome::ShowAvatarMenu(browser_);