Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: chrome/browser/ui/views/profiles/avatar_menu_button.cc

Issue 1972033002: Simplify some old avatar menu button code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reinstate ash browser test fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
6
7 #include <stddef.h>
8
9 #include "base/command_line.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/avatar_menu.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_attributes_entry.h"
16 #include "chrome/browser/profiles/profile_attributes_storage.h"
17 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/profiles/profile_metrics.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_commands.h"
22 #include "chrome/browser/ui/views/frame/browser_view.h"
23 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h"
24 #include "components/prefs/pref_service.h"
25 #include "components/signin/core/common/profile_management_switches.h"
26 #include "content/public/browser/notification_service.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/canvas.h"
30 #include "ui/views/view_targeter.h"
31 #include "ui/views/widget/widget.h"
32
33 static inline int Round(double x) {
34 return static_cast<int>(x + 0.5);
35 }
36
37 // static
38 const char AvatarMenuButton::kViewClassName[] = "AvatarMenuButton";
39
40 AvatarMenuButton::AvatarMenuButton(BrowserView* browser_view)
41 : MenuButton(base::string16(), this, false),
42 browser_view_(browser_view),
43 enabled_(browser_view_->IsRegularOrGuestSession()),
44 is_rectangle_(false),
45 old_height_(0) {
46 // In RTL mode, the avatar icon should be looking the opposite direction.
47 EnableCanvasFlippingForRTLUI(true);
48
49 SetEventTargeter(
50 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
51 }
52
53 AvatarMenuButton::~AvatarMenuButton() {
54 }
55
56 const char* AvatarMenuButton::GetClassName() const {
57 return kViewClassName;
58 }
59
60 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) {
61 if (!icon_.get())
62 return;
63
64 if (old_height_ != height() || button_icon_.isNull()) {
65 old_height_ = height();
66 button_icon_ = *profiles::GetAvatarIconForTitleBar(
67 *icon_, is_rectangle_, width(), height()).ToImageSkia();
68 }
69
70 // Scale the image to fit the width of the button.
71 int dst_width = std::min(button_icon_.width(), width());
72 // Truncate rather than rounding, so that for odd widths we put the extra
73 // pixel on the left.
74 int dst_x = (width() - dst_width) / 2;
75
76 // Scale the height and maintain aspect ratio. This means that the
77 // icon may not fit in the view. That's ok, we just vertically center it.
78 float scale =
79 static_cast<float>(dst_width) / static_cast<float>(button_icon_.width());
80 // Round here so that we minimize the aspect ratio drift.
81 int dst_height = Round(button_icon_.height() * scale);
82 // Round rather than truncating, so that for odd heights we select an extra
83 // pixel below the image center rather than above. This is because the
84 // incognito image has shadows at the top that make the apparent center below
85 // the real center.
86 int dst_y = Round((height() - dst_height) / 2.0);
87 canvas->DrawImageInt(button_icon_, 0, 0, button_icon_.width(),
88 button_icon_.height(), dst_x, dst_y, dst_width, dst_height, false);
89 }
90
91 void AvatarMenuButton::SetAvatarIcon(const gfx::Image& icon,
92 bool is_rectangle) {
93 icon_.reset(new gfx::Image(icon));
94 button_icon_ = gfx::ImageSkia();
95 is_rectangle_ = is_rectangle;
96 SchedulePaint();
97 }
98
99 // static
100 bool AvatarMenuButton::GetAvatarImages(
101 const BrowserNonClientFrameView* frame_view,
102 bool should_show_avatar_menu,
103 gfx::Image* avatar,
104 gfx::Image* taskbar_badge_avatar,
105 bool* is_rectangle) {
106 const Profile* profile = frame_view->browser_view()->browser()->profile();
107 if (profile->GetProfileType() == Profile::GUEST_PROFILE) {
108 *avatar = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
109 profiles::GetPlaceholderAvatarIconResourceID());
110 } else if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) {
111 *avatar = gfx::Image(frame_view->GetOTRAvatarIcon());
112 // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test
113 // environment handles profile directories correctly.
114 #if !defined(OS_CHROMEOS)
115 bool is_badge_rectangle = false;
116 // The taskbar badge should be the profile avatar, not the OTR avatar.
117 AvatarMenu::GetImageForMenuButton(profile->GetPath(),
118 taskbar_badge_avatar,
119 &is_badge_rectangle);
120 #endif
121 } else if (should_show_avatar_menu) {
122 ProfileAttributesEntry* entry;
123 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
124 GetProfileAttributesWithPath(profile->GetPath(), &entry))
125 return false;
126
127 #if defined(OS_CHROMEOS)
128 AvatarMenu::GetImageForMenuButton(profile->GetPath(), avatar, is_rectangle);
129 #else
130 *avatar = entry->GetAvatarIcon();
131 // TODO(noms): Once the code for the old avatar menu button is removed,
132 // this function will only be called for badging the taskbar icon. The
133 // function can be renamed to something like GetAvatarImageForBadging()
134 // and only needs to return the avatar from
135 // AvatarMenu::GetImageForMenuButton().
136 bool is_badge_rectangle = false;
137 AvatarMenu::GetImageForMenuButton(profile->GetPath(),
138 taskbar_badge_avatar,
139 &is_badge_rectangle);
140 #endif
141 }
142 return true;
143 }
144
145 // views::ViewTargeterDelegate:
146 bool AvatarMenuButton::DoesIntersectRect(const views::View* target,
147 const gfx::Rect& rect) const {
148 CHECK_EQ(target, this);
149 return enabled_ &&
150 views::ViewTargeterDelegate::DoesIntersectRect(target, rect);
151 }
152
153 // views::MenuButtonListener implementation
154 void AvatarMenuButton::OnMenuButtonClicked(views::MenuButton* source,
155 const gfx::Point& point,
156 const ui::Event* event) {
157 if (enabled_)
158 chrome::ShowAvatarMenu(browser_view_->browser());
159 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/profiles/avatar_menu_button.h ('k') | chrome/browser/ui/views/profiles/profile_chooser_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698