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

Side by Side Diff: ui/message_center/views/notifier_settings_view.cc

Issue 20066003: Update notification settings to allow for multiprofile situations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix message_center_unittests Created 7 years, 4 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/message_center/views/notifier_settings_view.h" 5 #include "ui/message_center/views/notifier_settings_view.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
11 #include "grit/ui_resources.h" 12 #include "grit/ui_resources.h"
12 #include "grit/ui_strings.h" 13 #include "grit/ui_strings.h"
14 #include "skia/ext/image_operations.h"
13 #include "third_party/skia/include/core/SkColor.h" 15 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/base/keycodes/keyboard_codes.h" 16 #include "ui/base/keycodes/keyboard_codes.h"
15 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/models/simple_menu_model.h"
16 #include "ui/base/resource/resource_bundle.h" 19 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/image/image.h" 21 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/image/image_skia_operations.h"
19 #include "ui/gfx/size.h" 23 #include "ui/gfx/size.h"
20 #include "ui/message_center/message_center_style.h" 24 #include "ui/message_center/message_center_style.h"
21 #include "ui/message_center/views/message_center_view.h" 25 #include "ui/message_center/views/message_center_view.h"
22 #include "ui/views/background.h" 26 #include "ui/views/background.h"
23 #include "ui/views/border.h" 27 #include "ui/views/border.h"
24 #include "ui/views/controls/button/checkbox.h" 28 #include "ui/views/controls/button/checkbox.h"
25 #include "ui/views/controls/button/custom_button.h" 29 #include "ui/views/controls/button/custom_button.h"
30 #include "ui/views/controls/button/menu_button.h"
26 #include "ui/views/controls/image_view.h" 31 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h" 32 #include "ui/views/controls/label.h"
33 #include "ui/views/controls/menu/menu_runner.h"
28 #include "ui/views/controls/scroll_view.h" 34 #include "ui/views/controls/scroll_view.h"
29 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" 35 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
30 #include "ui/views/layout/box_layout.h" 36 #include "ui/views/layout/box_layout.h"
31 #include "ui/views/layout/grid_layout.h" 37 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/widget/widget.h" 38 #include "ui/views/widget/widget.h"
33 39
34 #if defined(USE_AURA) 40 #if defined(USE_AURA)
35 #include "ui/aura/window.h" 41 #include "ui/aura/window.h"
36 #endif 42 #endif
37 43
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 bool EntryView::OnKeyPressed(const ui::KeyEvent& event) { 113 bool EntryView::OnKeyPressed(const ui::KeyEvent& event) {
108 return child_at(0)->OnKeyPressed(event); 114 return child_at(0)->OnKeyPressed(event);
109 } 115 }
110 116
111 bool EntryView::OnKeyReleased(const ui::KeyEvent& event) { 117 bool EntryView::OnKeyReleased(const ui::KeyEvent& event) {
112 return child_at(0)->OnKeyReleased(event); 118 return child_at(0)->OnKeyReleased(event);
113 } 119 }
114 120
115 } // namespace 121 } // namespace
116 122
123 // NotifierGroupMenuModel //////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////////////
125 class NotifierGroupMenuModel : public ui::SimpleMenuModel,
126 public ui::SimpleMenuModel::Delegate {
127 public:
128 NotifierGroupMenuModel(NotifierSettingsProvider* notifier_settings_provider);
129 virtual ~NotifierGroupMenuModel();
130
131 // Overridden from ui::SimpleMenuModel::Delegate:
132 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
133 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
134 virtual bool GetAcceleratorForCommandId(
135 int command_id,
136 ui::Accelerator* accelerator) OVERRIDE;
137 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
138
139 private:
140 NotifierSettingsProvider* notifier_settings_provider_;
141 };
142
143 NotifierGroupMenuModel::NotifierGroupMenuModel(
144 NotifierSettingsProvider* notifier_settings_provider)
145 : ui::SimpleMenuModel(this),
146 notifier_settings_provider_(notifier_settings_provider) {
147 if (!notifier_settings_provider_)
148 return;
149
150 size_t num_menu_items = notifier_settings_provider_->GetNotifierGroupCount();
151 for (size_t i = 0; i < num_menu_items; i++) {
jianli 2013/08/05 20:48:03 nit: ++i
dewittj 2013/08/05 22:42:28 Done.
152 const NotifierGroup& group =
153 notifier_settings_provider_->GetNotifierGroupAt(i);
154
155 if (!group.login_info.empty())
156 AddItem(i, group.login_info);
157 else
158 AddItem(i, group.name);
jianli 2013/08/05 20:48:03 nit: AddItem(i, group.login_info.empty() ? group.n
dewittj 2013/08/05 22:42:28 Done.
159
160 gfx::ImageSkia resized_icon = gfx::ImageSkiaOperations::CreateResizedImage(
161 *group.icon.ToImageSkia(),
162 skia::ImageOperations::RESIZE_BETTER,
163 gfx::Size(kSettingsIconSize, kSettingsIconSize));
164
165 SetIcon(i, gfx::Image(resized_icon));
166 }
167 }
168
169 NotifierGroupMenuModel::~NotifierGroupMenuModel() {}
170
171 bool NotifierGroupMenuModel::IsCommandIdChecked(int command_id) const {
172 return false;
173 }
174
175 bool NotifierGroupMenuModel::IsCommandIdEnabled(int command_id) const {
176 return true;
177 }
178
179 bool NotifierGroupMenuModel::GetAcceleratorForCommandId(
180 int command_id,
181 ui::Accelerator* accelerator) {
182 return false;
183 }
184
185 void NotifierGroupMenuModel::ExecuteCommand(int command_id, int event_flags) {
186 if (!notifier_settings_provider_)
187 return;
188
189 size_t notifier_group_index = static_cast<size_t>(command_id);
190 size_t num_notifier_groups =
191 notifier_settings_provider_->GetNotifierGroupCount();
192 if (notifier_group_index > num_notifier_groups)
jianli 2013/08/05 20:48:03 Should ">" be ">="?
dewittj 2013/08/05 22:42:28 Done.
193 return;
194
195 notifier_settings_provider_->SwitchToNotifierGroup(notifier_group_index);
196 }
197
117 // We do not use views::Checkbox class directly because it doesn't support 198 // We do not use views::Checkbox class directly because it doesn't support
118 // showing 'icon'. 199 // showing 'icon'.
119 class NotifierSettingsView::NotifierButton : public views::CustomButton, 200 class NotifierSettingsView::NotifierButton : public views::CustomButton,
120 public views::ButtonListener { 201 public views::ButtonListener {
121 public: 202 public:
122 NotifierButton(Notifier* notifier, views::ButtonListener* listener) 203 NotifierButton(Notifier* notifier, views::ButtonListener* listener)
123 : views::CustomButton(listener), 204 : views::CustomButton(listener),
124 notifier_(notifier), 205 notifier_(notifier),
125 icon_view_(NULL), 206 icon_view_(NULL),
126 checkbox_(new views::Checkbox(string16())) { 207 checkbox_(new views::Checkbox(string16())) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 title_layout->StartRow(0, 0); 314 title_layout->StartRow(0, 0);
234 title_layout->AddView(title_arrow_); 315 title_layout->AddView(title_arrow_);
235 title_layout->AddView(title_label); 316 title_layout->AddView(title_label);
236 title_entry_ = new EntryView(title_container); 317 title_entry_ = new EntryView(title_container);
237 AddChildView(title_entry_); 318 AddChildView(title_entry_);
238 319
239 scroller_ = new views::ScrollView(); 320 scroller_ = new views::ScrollView();
240 scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); 321 scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false));
241 AddChildView(scroller_); 322 AddChildView(scroller_);
242 323
243 views::View* contents_view = new views::View();
244 contents_view->SetLayoutManager(new views::BoxLayout(
245 views::BoxLayout::kVertical, 0, 0, 0));
246
247 views::Label* top_label = new views::Label(l10n_util::GetStringUTF16(
248 IDS_MESSAGE_CENTER_SETTINGS_DIALOG_DESCRIPTION));
249 top_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
250 top_label->SetMultiLine(true);
251 top_label->SizeToFit(kMinimumWindowWidth - kMarginWidth * 2);
252 contents_view->AddChildView(new EntryView(top_label));
253
254 std::vector<Notifier*> notifiers; 324 std::vector<Notifier*> notifiers;
255 if (provider_) 325 if (provider_)
256 provider_->GetNotifierList(&notifiers); 326 provider_->GetNotifierList(&notifiers);
257 for (size_t i = 0; i < notifiers.size(); ++i) {
258 NotifierButton* button = new NotifierButton(notifiers[i], this);
259 EntryView* entry = new EntryView(button);
260 entry->set_focusable(true);
261 contents_view->AddChildView(entry);
262 buttons_.insert(button);
263 }
264 scroller_->SetContents(contents_view);
265 327
266 contents_view->SetBoundsRect(gfx::Rect(contents_view->GetPreferredSize())); 328 UpdateContentsView(notifiers);
267 } 329 }
268 330
269 NotifierSettingsView::~NotifierSettingsView() { 331 NotifierSettingsView::~NotifierSettingsView() {
270 // |provider_| may be NULL in tests. 332 // |provider_| may be NULL in tests.
271 if (provider_) 333 if (provider_)
272 provider_->RemoveObserver(this); 334 provider_->RemoveObserver(this);
273 } 335 }
274 336
275 bool NotifierSettingsView::IsScrollable() { 337 bool NotifierSettingsView::IsScrollable() {
276 return scroller_->height() < scroller_->contents()->height(); 338 return scroller_->height() < scroller_->contents()->height();
277 } 339 }
278 340
279 void NotifierSettingsView::UpdateIconImage(const NotifierId& notifier_id, 341 void NotifierSettingsView::UpdateIconImage(const NotifierId& notifier_id,
280 const gfx::Image& icon) { 342 const gfx::Image& icon) {
281 for (std::set<NotifierButton*>::iterator iter = buttons_.begin(); 343 for (std::set<NotifierButton*>::iterator iter = buttons_.begin();
282 iter != buttons_.end(); ++iter) { 344 iter != buttons_.end(); ++iter) {
283 if ((*iter)->notifier().notifier_id == notifier_id) { 345 if ((*iter)->notifier().notifier_id == notifier_id) {
284 (*iter)->UpdateIconImage(icon); 346 (*iter)->UpdateIconImage(icon);
285 return; 347 return;
286 } 348 }
287 } 349 }
288 } 350 }
289 351
352 void NotifierSettingsView::NotifierGroupChanged() {
353 std::vector<Notifier*> notifiers;
354 if (provider_)
355 provider_->GetNotifierList(&notifiers);
356
357 UpdateContentsView(notifiers);
358 }
359
360 void NotifierSettingsView::UpdateContentsView(
361 const std::vector<Notifier*>& notifiers) {
362 buttons_.clear();
363
364 views::View* contents_view = new views::View();
365 contents_view->SetLayoutManager(
366 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
367
368 views::View* contents_title_view = new views::View();
369 contents_title_view->SetLayoutManager(
370 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 5));
371 views::Label* top_label = new views::Label(l10n_util::GetStringUTF16(
372 IDS_MESSAGE_CENTER_SETTINGS_DIALOG_DESCRIPTION));
373 top_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
374 top_label->SetMultiLine(true);
375 contents_title_view->AddChildView(top_label);
376
377 string16 notifier_group_text;
378 if (provider_) {
379 const NotifierGroup& active_group = provider_->GetActiveNotifierGroup();
380 notifier_group_text = active_group.login_info.empty()
381 ? active_group.name
382 : active_group.login_info;
383 }
384
385 views::View* notifier_group_selector =
386 new views::MenuButton(NULL, notifier_group_text, this, true);
387 contents_title_view->AddChildView(notifier_group_selector);
388 contents_view->AddChildView(new EntryView(contents_title_view));
389
390 for (size_t i = 0; i < notifiers.size(); ++i) {
391 NotifierButton* button = new NotifierButton(notifiers[i], this);
392 EntryView* entry = new EntryView(button);
393 entry->set_focusable(true);
394 contents_view->AddChildView(entry);
395 buttons_.insert(button);
396 }
397 scroller_->SetContents(contents_view);
398
399 contents_view->SetBoundsRect(gfx::Rect(contents_view->GetPreferredSize()));
400 InvalidateLayout();
401 }
402
290 void NotifierSettingsView::Layout() { 403 void NotifierSettingsView::Layout() {
291 int title_height = title_entry_->GetHeightForWidth(width()); 404 int title_height = title_entry_->GetHeightForWidth(width());
292 title_entry_->SetBounds(0, 0, width(), title_height); 405 title_entry_->SetBounds(0, 0, width(), title_height);
293 406
294 views::View* contents_view = scroller_->contents(); 407 views::View* contents_view = scroller_->contents();
295 int content_width = width(); 408 int content_width = width();
296 int content_height = contents_view->GetHeightForWidth(content_width); 409 int content_height = contents_view->GetHeightForWidth(content_width);
297 if (title_height + content_height > height()) { 410 if (title_height + content_height > height()) {
298 content_width -= scroller_->GetScrollBarWidth(); 411 content_width -= scroller_->GetScrollBarWidth();
299 content_height = contents_view->GetHeightForWidth(content_width); 412 content_height = contents_view->GetHeightForWidth(content_width);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 void NotifierSettingsView::ButtonPressed(views::Button* sender, 449 void NotifierSettingsView::ButtonPressed(views::Button* sender,
337 const ui::Event& event) { 450 const ui::Event& event) {
338 if (sender == title_arrow_) { 451 if (sender == title_arrow_) {
339 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent()); 452 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent());
340 center_view->SetSettingsVisible(!center_view->settings_visible()); 453 center_view->SetSettingsVisible(!center_view->settings_visible());
341 return; 454 return;
342 } 455 }
343 456
344 std::set<NotifierButton*>::iterator iter = buttons_.find( 457 std::set<NotifierButton*>::iterator iter = buttons_.find(
345 static_cast<NotifierButton*>(sender)); 458 static_cast<NotifierButton*>(sender));
346 DCHECK(iter != buttons_.end()); 459
460 if (iter == buttons_.end())
461 return;
347 462
348 (*iter)->SetChecked(!(*iter)->checked()); 463 (*iter)->SetChecked(!(*iter)->checked());
349 if (provider_) 464 if (provider_)
350 provider_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); 465 provider_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked());
351 } 466 }
352 467
468 void NotifierSettingsView::OnMenuButtonClicked(views::View* source,
469 const gfx::Point& point) {
470 notifier_group_menu_model_.reset(new NotifierGroupMenuModel(provider_));
471 notifier_group_menu_runner_.reset(
472 new views::MenuRunner(notifier_group_menu_model_.get()));
473 if (views::MenuRunner::MENU_DELETED ==
474 notifier_group_menu_runner_->RunMenuAt(GetWidget(),
475 NULL,
476 source->GetBoundsInScreen(),
477 views::MenuItemView::BUBBLE_ABOVE,
478 ui::MENU_SOURCE_MOUSE,
479 views::MenuRunner::CONTEXT_MENU))
480 return;
481 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent());
482 center_view->OnSettingsChanged();
483 }
484
353 } // namespace message_center 485 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698