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

Side by Side Diff: chrome/browser/ui/views/avatar_menu.cc

Issue 7331017: Multi-Profiles: Add icon chooser to profiles menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Multi-Profiles: Add icon chooser to profiles menu Created 9 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/views/avatar_menu.h ('k') | chrome/browser/ui/views/avatar_menu_button.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/avatar_menu.h"
6
7 #include "base/string_number_conversions.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/profile_menu_model.h"
16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/image/image.h"
23 #include "views/controls/button/image_button.h"
24 #include "views/controls/button/menu_button.h"
25 #include "views/controls/menu/menu_item_view.h"
26 #include "views/controls/menu/submenu_view.h"
27 #include "views/widget/widget.h"
28
29 namespace {
30
31 const int kCellWidth = 32;
32 const int kCellHeight = 32;
33 const int kCellPaddingX = 5;
34 const int kCellPaddingY = 5;
35 const int kGridMaxCol = 3;
36
37 static inline int Round(double x) {
38 return static_cast<int>(x + 0.5);
39 }
40
41 // This is an button that scales its image to fit its bounds.
42 class ScaledImageButton : public views::ImageButton {
43 public:
44 explicit ScaledImageButton(views::ButtonListener* listener);
45 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(ScaledImageButton);
49 };
50
51 // A view that displays avatar icons in a grid.
52 class AvatarIconGridView : public views::View, public views::ButtonListener {
53 public:
54 AvatarIconGridView(Profile* profile, views::MenuItemView* menu);
55
56 virtual void Layout() OVERRIDE;
57 virtual gfx::Size GetPreferredSize() OVERRIDE;
58 virtual void ButtonPressed(views::Button* sender,
59 const views::Event& event) OVERRIDE;
60
61 private:
62 // Sets the profile's avatar icon to the icon at the given index.
63 void SetAvatarIconToIndex(int icon_index);
64
65 typedef std::map<int, views::View*> IconIndexToButtonMap;
66 IconIndexToButtonMap icon_index_to_button_map_;
67 Profile* profile_;
68 views::MenuItemView* menu_;
69
70 DISALLOW_COPY_AND_ASSIGN(AvatarIconGridView);
71 };
72
73 ScaledImageButton::ScaledImageButton(views::ButtonListener* listener)
74 : views::ImageButton(listener) {
75 }
76
77 void ScaledImageButton::OnPaint(gfx::Canvas* canvas) {
78 if (state() == views::CustomButton::BS_HOT)
79 canvas->FillRectInt(SkColorSetARGB(20, 0, 0, 0), 0, 0, width(), height());
80 else if (state() == views::CustomButton::BS_PUSHED)
81 canvas->FillRectInt(SkColorSetARGB(40, 0, 0, 0), 0, 0, width(), height());
82
83 const SkBitmap& icon = GetImageToPaint();
84 if (icon.isNull())
85 return;
86
87 int dst_width;
88 int dst_height;
89 if (icon.width() > icon.height()) {
90 dst_width = std::min(width(), icon.width());
91 float scale = static_cast<float>(dst_width) /
92 static_cast<float>(icon.width());
93 dst_height = Round(icon.height() * scale);
94 } else {
95 dst_height = std::min(height(), icon.height());
96 float scale = static_cast<float>(dst_height) /
97 static_cast<float>(icon.height());
98 dst_width = Round(icon.width() * scale);
99 }
100 int dst_x = (width() - dst_width) / 2;
101 int dst_y = (height() - dst_height) / 2;
102 canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(),
103 dst_x, dst_y, dst_width, dst_height, false);
104 }
105
106 AvatarIconGridView::AvatarIconGridView(Profile* profile,
107 views::MenuItemView* menu)
108 : profile_(profile),
109 menu_(menu) {
110 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
111 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) {
112 int resource_id =
113 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i);
114 views::ImageButton* button = new ScaledImageButton(this);
115 button->SetImage(views::CustomButton::BS_NORMAL,
116 rb.GetImageNamed(resource_id));
117 button->SetAccessibleName(
118 l10n_util::GetStringFUTF16Int(IDS_NUMBERED_AVATAR_NAME,
119 static_cast<int>(i + 1)));
120 AddChildView(button);
121 icon_index_to_button_map_[i] = button;
122 }
123 }
124
125 void AvatarIconGridView::Layout() {
126 for (IconIndexToButtonMap::const_iterator it =
127 icon_index_to_button_map_.begin();
128 it != icon_index_to_button_map_.end(); ++it) {
129 views::View* view = it->second;
130 int icon_index = it->first;
131 int col = icon_index % kGridMaxCol;
132 int row = icon_index / kGridMaxCol;
133 int x = col * kCellWidth + col * kCellPaddingX;
134 int y = row * kCellHeight + row * kCellPaddingY;
135 view->SetBounds(x, y, kCellWidth, kCellHeight);
136 }
137 }
138
139 gfx::Size AvatarIconGridView::GetPreferredSize() {
140 int cols = kGridMaxCol;
141 int rows = ProfileInfoCache::GetDefaultAvatarIconCount() / cols;
142 if (ProfileInfoCache::GetDefaultAvatarIconCount() % cols != 0)
143 rows++;
144 return gfx::Size(cols * kCellWidth + (cols - 1) * kCellPaddingX,
145 rows * kCellHeight + (rows - 1) * kCellPaddingY);
146 }
147
148 void AvatarIconGridView::ButtonPressed(views::Button* sender,
149 const views::Event& event) {
150 for (IconIndexToButtonMap::const_iterator it =
151 icon_index_to_button_map_.begin();
152 it != icon_index_to_button_map_.end(); ++it) {
153 if (it->second == sender) {
154 SetAvatarIconToIndex(it->first);
155 menu_->Cancel();
156 return;
157 }
158 }
159 NOTREACHED();
160 }
161
162 void AvatarIconGridView::SetAvatarIconToIndex(int icon_index) {
163 ProfileInfoCache& cache =
164 g_browser_process->profile_manager()->GetProfileInfoCache();
165 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
166 cache.SetAvatarIconOfProfileAtIndex(profile_index, icon_index);
167 }
168
169 } // namespace
170
171 AvatarMenu::AvatarMenu(ui::MenuModel* model, Profile* profile)
172 : MenuModelAdapter(model),
173 profile_(profile) {
174 root_.reset(new views::MenuItemView(this));
175 BuildMenu(root_.get());
176 views::MenuItemView* item =
177 root_->GetMenuItemByID(ProfileMenuModel::COMMAND_CHOOSE_AVATAR_ICON);
178 item->AddChildView(new AvatarIconGridView(profile_, root_.get()));
179 }
180
181 void AvatarMenu::RunMenu(views::MenuButton* host) {
182 // Up the ref count while the menu is displaying. This way if the window is
183 // deleted while we're running we won't prematurely delete the menu.
184 // TODO(sky): fix this, the menu should really take ownership of the menu
185 // (57890).
186 scoped_refptr<AvatarMenu> dont_delete_while_running(this);
187 gfx::Point screen_loc;
188 views::View::ConvertPointToScreen(host, &screen_loc);
189 gfx::Rect bounds(screen_loc, host->size());
190 root_->RunMenuAt(host->GetWidget()->GetNativeWindow(), host, bounds,
191 views::MenuItemView::TOPLEFT, true);
192 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/avatar_menu.h ('k') | chrome/browser/ui/views/avatar_menu_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698