Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / profiles / avatar_menu_button.cc
blob1c0aa1828d731b9a7e8827a990adb61c876a7e65
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/profile_chooser_view.h"
21 #include "components/signin/core/common/profile_management_switches.h"
22 #include "content/public/browser/notification_service.h"
23 #include "grit/theme_resources.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/views/view_targeter.h"
27 #include "ui/views/widget/widget.h"
29 static inline int Round(double x) {
30 return static_cast<int>(x + 0.5);
33 // static
34 const char AvatarMenuButton::kViewClassName[] = "AvatarMenuButton";
36 AvatarMenuButton::AvatarMenuButton(Browser* browser, bool disabled)
37 : MenuButton(NULL, base::string16(), this, false),
38 browser_(browser),
39 disabled_(disabled),
40 is_rectangle_(false),
41 old_height_(0),
42 button_on_right_(false) {
43 // In RTL mode, the avatar icon should be looking the opposite direction.
44 EnableCanvasFlippingForRTLUI(true);
46 SetEventTargeter(
47 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
50 AvatarMenuButton::~AvatarMenuButton() {
53 const char* AvatarMenuButton::GetClassName() const {
54 return kViewClassName;
57 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) {
58 if (!icon_.get())
59 return;
61 if (old_height_ != height() || button_icon_.isNull()) {
62 old_height_ = height();
63 button_icon_ = *profiles::GetAvatarIconForTitleBar(
64 *icon_, is_rectangle_, width(), height()).ToImageSkia();
67 // Scale the image to fit the width of the button.
68 int dst_width = std::min(button_icon_.width(), width());
69 // Truncate rather than rounding, so that for odd widths we put the extra
70 // pixel on the left.
71 int dst_x = (width() - dst_width) / 2;
73 // Scale the height and maintain aspect ratio. This means that the
74 // icon may not fit in the view. That's ok, we just vertically center it.
75 float scale =
76 static_cast<float>(dst_width) / static_cast<float>(button_icon_.width());
77 // Round here so that we minimize the aspect ratio drift.
78 int dst_height = Round(button_icon_.height() * scale);
79 // Round rather than truncating, so that for odd heights we select an extra
80 // pixel below the image center rather than above. This is because the
81 // incognito image has shadows at the top that make the apparent center below
82 // the real center.
83 int dst_y = Round((height() - dst_height) / 2.0);
84 canvas->DrawImageInt(button_icon_, 0, 0, button_icon_.width(),
85 button_icon_.height(), dst_x, dst_y, dst_width, dst_height, false);
88 void AvatarMenuButton::SetAvatarIcon(const gfx::Image& icon,
89 bool is_rectangle) {
90 icon_.reset(new gfx::Image(icon));
91 button_icon_ = gfx::ImageSkia();
92 is_rectangle_ = is_rectangle;
93 SchedulePaint();
96 // static
97 bool AvatarMenuButton::GetAvatarImages(Profile* profile,
98 bool should_show_avatar_menu,
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 AvatarMenu::GetImageForMenuButton(profile->GetPath(),
114 taskbar_badge_avatar,
115 &is_badge_rectangle);
116 #endif
117 } else if (should_show_avatar_menu) {
118 ProfileInfoCache& cache =
119 g_browser_process->profile_manager()->GetProfileInfoCache();
120 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
121 if (index == std::string::npos)
122 return false;
124 if (switches::IsNewAvatarMenu()) {
125 *avatar = cache.GetAvatarIconOfProfileAtIndex(index);
126 // TODO(noms): Once the code for the old avatar menu button is removed,
127 // this function will only be called for badging the taskbar icon. The
128 // function can be renamed to something like GetAvatarImageForBadging()
129 // and only needs to return the avatar from
130 // AvatarMenu::GetImageForMenuButton().
131 #if !defined(OS_CHROMEOS)
132 bool is_badge_rectangle = false;
133 AvatarMenu::GetImageForMenuButton(profile->GetPath(),
134 taskbar_badge_avatar,
135 &is_badge_rectangle);
136 #endif
137 } else {
138 AvatarMenu::GetImageForMenuButton(profile->GetPath(),
139 avatar,
140 is_rectangle);
143 return true;
146 // views::ViewTargeterDelegate:
147 bool AvatarMenuButton::DoesIntersectRect(const views::View* target,
148 const gfx::Rect& rect) const {
149 CHECK_EQ(target, this);
150 return !disabled_ &&
151 views::ViewTargeterDelegate::DoesIntersectRect(target, rect);
154 // views::MenuButtonListener implementation
155 void AvatarMenuButton::OnMenuButtonClicked(views::View* source,
156 const gfx::Point& point) {
157 if (!disabled_)
158 chrome::ShowAvatarMenu(browser_);