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

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: remove debug code 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
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 CELL_WIDTH = 32;
sky 2011/07/08 21:01:44 kCellWidth. Same for all these constants.
sail 2011/07/08 21:53:57 Done.
32 const int CELL_HEIGHT = 32;
33 const int CELL_PADDING_X = 5;
34 const int CELL_PADDING_Y = 5;
35 const int GRID_MAX_COL = 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 explicit AvatarIconGridView(Profile* profile);
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 void SetAvatarIconToIndex(int icon_index);
sky 2011/07/08 21:01:44 Description?
sail 2011/07/08 21:53:57 Done.
63
64 typedef std::map<int, views::View*> IconIndexToButtonMap;
65 IconIndexToButtonMap icon_index_to_button_map_;
66 Profile* profile_;
67
68 DISALLOW_COPY_AND_ASSIGN(AvatarIconGridView);
69 };
70
71 ScaledImageButton::ScaledImageButton(views::ButtonListener* listener)
72 : views::ImageButton(listener) {
73 }
74
75 void ScaledImageButton::OnPaint(gfx::Canvas* canvas) {
76 if (state() == views::CustomButton::BS_HOT)
77 canvas->FillRectInt(SkColorSetARGB(20, 0, 0, 0), 0, 0, width(), height());
78 else if (state() == views::CustomButton::BS_PUSHED)
79 canvas->FillRectInt(SkColorSetARGB(40, 0, 0, 0), 0, 0, width(), height());
80
81 const SkBitmap& icon = GetImageToPaint();
82 if (icon.isNull())
83 return;
84
85 int dst_width;
86 int dst_height;
87 if (icon.width() > icon.height()) {
88 dst_width = std::min(width(), icon.width());
89 float scale = static_cast<float>(dst_width) /
90 static_cast<float>(icon.width());
91 dst_height = Round(icon.height() * scale);
92 } else {
93 dst_height = std::min(height(), icon.height());
94 float scale = static_cast<float>(dst_height) /
95 static_cast<float>(icon.height());
96 dst_width = Round(icon.width() * scale);
97 }
98 int dst_x = (width() - dst_width) / 2;
99 int dst_y = (height() - dst_height) / 2;
100 canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(),
101 dst_x, dst_y, dst_width, dst_height, false);
102 }
103
104 AvatarIconGridView::AvatarIconGridView(Profile* profile) : profile_(profile) {
105 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
106 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) {
107 int resource_id =
108 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i);
sky 2011/07/08 21:01:44 indent by 4.
sail 2011/07/08 21:53:57 Done.
109 views::ImageButton* button = new ScaledImageButton(this);
110 button->SetImage(views::CustomButton::BS_NORMAL,
111 rb.GetImageNamed(resource_id));
112 button->SetAccessibleName(
113 l10n_util::GetStringFUTF16Int(IDS_NUMBERED_AVATAR_NAME,
114 static_cast<int>(i + 1)));
115 AddChildView(button);
116 icon_index_to_button_map_[i] = button;
117 }
118 }
119
120 void AvatarIconGridView::Layout() {
121 for (IconIndexToButtonMap::const_iterator it =
122 icon_index_to_button_map_.begin();
123 it != icon_index_to_button_map_.end(); ++it) {
124 views::View* view = it->second;
125 int icon_index = it->first;
126 int col = icon_index % GRID_MAX_COL;
127 int row = icon_index / GRID_MAX_COL;
128 int x = col * CELL_WIDTH + col * CELL_PADDING_X;
129 int y = row * CELL_HEIGHT + row * CELL_PADDING_Y;
130 view->SetBounds(x, y, CELL_WIDTH, CELL_HEIGHT);
131 }
132 }
133
134 gfx::Size AvatarIconGridView::GetPreferredSize() {
135 int cols = GRID_MAX_COL;
136 int rows = ProfileInfoCache::GetDefaultAvatarIconCount() / cols;
137 if (ProfileInfoCache::GetDefaultAvatarIconCount() % cols != 0)
138 rows++;
139 return gfx::Size(cols * CELL_WIDTH + (cols - 1) * CELL_PADDING_X,
140 rows * CELL_HEIGHT + (rows - 1) * CELL_PADDING_Y);
141 }
142
143 void AvatarIconGridView::ButtonPressed(views::Button* sender,
144 const views::Event& event) {
145 for (IconIndexToButtonMap::const_iterator it =
146 icon_index_to_button_map_.begin();
147 it != icon_index_to_button_map_.end(); ++it) {
148 if (it->second == sender) {
149 SetAvatarIconToIndex(it->first);
150 break;
sky 2011/07/08 21:01:44 Do you want to close the menu when the user clicks
sail 2011/07/08 21:53:57 Ahh, that would be a good idea. Off the top of you
sky 2011/07/08 22:31:25 CAncel will close the menu.
151 }
152 }
153 }
sky 2011/07/08 21:01:44 NOTREACHED
sail 2011/07/08 21:53:57 Done.
154
155 void AvatarIconGridView::SetAvatarIconToIndex(int icon_index) {
156 ProfileInfoCache& cache =
157 g_browser_process->profile_manager()->GetProfileInfoCache();
158 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
159 cache.SetAvatarIconOfProfileAtIndex(profile_index, icon_index);
160 }
161
162 } // namespace
163
164 AvatarMenu::AvatarMenu(Browser* browser) : browser_(browser) {
165 }
166
167 void AvatarMenu::Init(ui::MenuModel* model) {
168 DCHECK(!root_.get());
169 root_.reset(new views::MenuItemView(this));
170 int next_id = 1;
171 PopulateMenu(root_.get(), model, &next_id);
172 }
173
174 void AvatarMenu::RunMenu(views::MenuButton* host) {
175 // Up the ref count while the menu is displaying. This way if the window is
176 // deleted while we're running we won't prematurely delete the menu.
177 // TODO(sky): fix this, the menu should really take ownership of the menu
178 // (57890).
179 scoped_refptr<AvatarMenu> dont_delete_while_running(this);
180 gfx::Point screen_loc;
181 views::View::ConvertPointToScreen(host, &screen_loc);
182 gfx::Rect bounds(screen_loc, host->size());
183 root_->RunMenuAt(host->GetWidget()->GetNativeWindow(), host, bounds,
184 views::MenuItemView::TOPRIGHT, true);
185 }
186
187 void AvatarMenu::PopulateMenu(views::MenuItemView* parent,
188 ui::MenuModel* model,
189 int* next_id) {
190 int index_offset = model->GetFirstItemIndex(NULL);
191 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
192 int index = i + index_offset;
193
194 views::MenuItemView* item =
195 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
196
197 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU)
198 PopulateMenu(item, model->GetSubmenuModelAt(index), next_id);
199
200 if (model->GetCommandIdAt(index) ==
201 ProfileMenuModel::COMMAND_CHOOSE_AVATAR_ICON)
202 item->AddChildView(new AvatarIconGridView(browser_->profile()));
203 }
204 }
205
206 views::MenuItemView* AvatarMenu::AppendMenuItem(
207 views::MenuItemView* parent,
208 ui::MenuModel* model,
209 int index,
210 ui::MenuModel::ItemType menu_type,
211 int* next_id) {
212 int id = (*next_id)++;
213 id_to_entry_[id].first = model;
214 id_to_entry_[id].second = index;
215 views::MenuItemView* menu_item =
216 parent->AppendMenuItemFromModel(model, index, id);
217 if (menu_item)
218 menu_item->SetVisible(model->IsVisibleAt(index));
219 if (menu_type == ui::MenuModel::TYPE_COMMAND && model->HasIcons()) {
220 SkBitmap icon;
221 if (model->GetIconAt(index, &icon))
222 menu_item->SetIcon(icon);
223 }
224 return menu_item;
225 }
226
227 bool AvatarMenu::IsItemChecked(int id) const {
228 const Entry& entry = id_to_entry_.find(id)->second;
229 return entry.first->IsItemCheckedAt(entry.second);
230 }
231
232 bool AvatarMenu::IsCommandEnabled(int id) const {
233 if (id == 0)
234 return false; // The root item.
235 const Entry& entry = id_to_entry_.find(id)->second;
236 return entry.first->IsEnabledAt(entry.second);
237 }
238
239 void AvatarMenu::ExecuteCommand(int id, int mouse_event_flags) {
240 const Entry& entry = id_to_entry_.find(id)->second;
241 return entry.first->ActivatedAt(entry.second);
242 }
243
244 bool AvatarMenu::GetAccelerator(int id, views::Accelerator* accelerator) {
245 const Entry& entry = id_to_entry_.find(id)->second;
246 ui::Accelerator menu_accelerator;
247 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
248 return false;
249
250 *accelerator = views::Accelerator(menu_accelerator.key_code(),
251 menu_accelerator.modifiers());
252 return true;
253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698