OLD | NEW |
---|---|
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> |
jianli
2013/08/05 23:07:26
nit: this can be removed now.
| |
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 Loading... | |
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) { | |
152 const NotifierGroup& group = | |
153 notifier_settings_provider_->GetNotifierGroupAt(i); | |
154 | |
155 AddItem(i, group.login_info.empty() ? group.name : group.login_info); | |
156 | |
157 gfx::ImageSkia resized_icon = gfx::ImageSkiaOperations::CreateResizedImage( | |
158 *group.icon.ToImageSkia(), | |
159 skia::ImageOperations::RESIZE_BETTER, | |
160 gfx::Size(kSettingsIconSize, kSettingsIconSize)); | |
161 | |
162 SetIcon(i, gfx::Image(resized_icon)); | |
163 } | |
164 } | |
165 | |
166 NotifierGroupMenuModel::~NotifierGroupMenuModel() {} | |
167 | |
168 bool NotifierGroupMenuModel::IsCommandIdChecked(int command_id) const { | |
169 return false; | |
170 } | |
171 | |
172 bool NotifierGroupMenuModel::IsCommandIdEnabled(int command_id) const { | |
173 return true; | |
174 } | |
175 | |
176 bool NotifierGroupMenuModel::GetAcceleratorForCommandId( | |
177 int command_id, | |
178 ui::Accelerator* accelerator) { | |
179 return false; | |
180 } | |
181 | |
182 void NotifierGroupMenuModel::ExecuteCommand(int command_id, int event_flags) { | |
183 if (!notifier_settings_provider_) | |
184 return; | |
185 | |
186 size_t notifier_group_index = static_cast<size_t>(command_id); | |
187 size_t num_notifier_groups = | |
188 notifier_settings_provider_->GetNotifierGroupCount(); | |
189 if (notifier_group_index >= num_notifier_groups) | |
190 return; | |
191 | |
192 notifier_settings_provider_->SwitchToNotifierGroup(notifier_group_index); | |
193 } | |
194 | |
117 // We do not use views::Checkbox class directly because it doesn't support | 195 // We do not use views::Checkbox class directly because it doesn't support |
118 // showing 'icon'. | 196 // showing 'icon'. |
119 class NotifierSettingsView::NotifierButton : public views::CustomButton, | 197 class NotifierSettingsView::NotifierButton : public views::CustomButton, |
120 public views::ButtonListener { | 198 public views::ButtonListener { |
121 public: | 199 public: |
122 NotifierButton(Notifier* notifier, views::ButtonListener* listener) | 200 NotifierButton(Notifier* notifier, views::ButtonListener* listener) |
123 : views::CustomButton(listener), | 201 : views::CustomButton(listener), |
124 notifier_(notifier), | 202 notifier_(notifier), |
125 icon_view_(NULL), | 203 icon_view_(NULL), |
126 checkbox_(new views::Checkbox(string16())) { | 204 checkbox_(new views::Checkbox(string16())) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 title_layout->StartRow(0, 0); | 311 title_layout->StartRow(0, 0); |
234 title_layout->AddView(title_arrow_); | 312 title_layout->AddView(title_arrow_); |
235 title_layout->AddView(title_label); | 313 title_layout->AddView(title_label); |
236 title_entry_ = new EntryView(title_container); | 314 title_entry_ = new EntryView(title_container); |
237 AddChildView(title_entry_); | 315 AddChildView(title_entry_); |
238 | 316 |
239 scroller_ = new views::ScrollView(); | 317 scroller_ = new views::ScrollView(); |
240 scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); | 318 scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); |
241 AddChildView(scroller_); | 319 AddChildView(scroller_); |
242 | 320 |
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; | 321 std::vector<Notifier*> notifiers; |
255 if (provider_) | 322 if (provider_) |
256 provider_->GetNotifierList(¬ifiers); | 323 provider_->GetNotifierList(¬ifiers); |
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 | 324 |
266 contents_view->SetBoundsRect(gfx::Rect(contents_view->GetPreferredSize())); | 325 UpdateContentsView(notifiers); |
267 } | 326 } |
268 | 327 |
269 NotifierSettingsView::~NotifierSettingsView() { | 328 NotifierSettingsView::~NotifierSettingsView() { |
270 // |provider_| may be NULL in tests. | 329 // |provider_| may be NULL in tests. |
271 if (provider_) | 330 if (provider_) |
272 provider_->RemoveObserver(this); | 331 provider_->RemoveObserver(this); |
273 } | 332 } |
274 | 333 |
275 bool NotifierSettingsView::IsScrollable() { | 334 bool NotifierSettingsView::IsScrollable() { |
276 return scroller_->height() < scroller_->contents()->height(); | 335 return scroller_->height() < scroller_->contents()->height(); |
277 } | 336 } |
278 | 337 |
279 void NotifierSettingsView::UpdateIconImage(const NotifierId& notifier_id, | 338 void NotifierSettingsView::UpdateIconImage(const NotifierId& notifier_id, |
280 const gfx::Image& icon) { | 339 const gfx::Image& icon) { |
281 for (std::set<NotifierButton*>::iterator iter = buttons_.begin(); | 340 for (std::set<NotifierButton*>::iterator iter = buttons_.begin(); |
282 iter != buttons_.end(); ++iter) { | 341 iter != buttons_.end(); ++iter) { |
283 if ((*iter)->notifier().notifier_id == notifier_id) { | 342 if ((*iter)->notifier().notifier_id == notifier_id) { |
284 (*iter)->UpdateIconImage(icon); | 343 (*iter)->UpdateIconImage(icon); |
285 return; | 344 return; |
286 } | 345 } |
287 } | 346 } |
288 } | 347 } |
289 | 348 |
349 void NotifierSettingsView::NotifierGroupChanged() { | |
350 std::vector<Notifier*> notifiers; | |
351 if (provider_) | |
352 provider_->GetNotifierList(¬ifiers); | |
353 | |
354 UpdateContentsView(notifiers); | |
355 } | |
356 | |
357 void NotifierSettingsView::UpdateContentsView( | |
358 const std::vector<Notifier*>& notifiers) { | |
359 buttons_.clear(); | |
360 | |
361 views::View* contents_view = new views::View(); | |
362 contents_view->SetLayoutManager( | |
363 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | |
364 | |
365 views::View* contents_title_view = new views::View(); | |
366 contents_title_view->SetLayoutManager( | |
367 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 5)); | |
368 views::Label* top_label = new views::Label(l10n_util::GetStringUTF16( | |
369 IDS_MESSAGE_CENTER_SETTINGS_DIALOG_DESCRIPTION)); | |
370 top_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
371 top_label->SetMultiLine(true); | |
372 contents_title_view->AddChildView(top_label); | |
373 | |
374 string16 notifier_group_text; | |
375 if (provider_) { | |
376 const NotifierGroup& active_group = provider_->GetActiveNotifierGroup(); | |
377 notifier_group_text = active_group.login_info.empty() | |
378 ? active_group.name | |
379 : active_group.login_info; | |
380 } | |
381 | |
382 views::View* notifier_group_selector = | |
383 new views::MenuButton(NULL, notifier_group_text, this, true); | |
384 contents_title_view->AddChildView(notifier_group_selector); | |
385 contents_view->AddChildView(new EntryView(contents_title_view)); | |
386 | |
387 for (size_t i = 0; i < notifiers.size(); ++i) { | |
388 NotifierButton* button = new NotifierButton(notifiers[i], this); | |
389 EntryView* entry = new EntryView(button); | |
390 entry->set_focusable(true); | |
391 contents_view->AddChildView(entry); | |
392 buttons_.insert(button); | |
393 } | |
394 scroller_->SetContents(contents_view); | |
395 | |
396 contents_view->SetBoundsRect(gfx::Rect(contents_view->GetPreferredSize())); | |
397 InvalidateLayout(); | |
398 } | |
399 | |
290 void NotifierSettingsView::Layout() { | 400 void NotifierSettingsView::Layout() { |
291 int title_height = title_entry_->GetHeightForWidth(width()); | 401 int title_height = title_entry_->GetHeightForWidth(width()); |
292 title_entry_->SetBounds(0, 0, width(), title_height); | 402 title_entry_->SetBounds(0, 0, width(), title_height); |
293 | 403 |
294 views::View* contents_view = scroller_->contents(); | 404 views::View* contents_view = scroller_->contents(); |
295 int content_width = width(); | 405 int content_width = width(); |
296 int content_height = contents_view->GetHeightForWidth(content_width); | 406 int content_height = contents_view->GetHeightForWidth(content_width); |
297 if (title_height + content_height > height()) { | 407 if (title_height + content_height > height()) { |
298 content_width -= scroller_->GetScrollBarWidth(); | 408 content_width -= scroller_->GetScrollBarWidth(); |
299 content_height = contents_view->GetHeightForWidth(content_width); | 409 content_height = contents_view->GetHeightForWidth(content_width); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 void NotifierSettingsView::ButtonPressed(views::Button* sender, | 446 void NotifierSettingsView::ButtonPressed(views::Button* sender, |
337 const ui::Event& event) { | 447 const ui::Event& event) { |
338 if (sender == title_arrow_) { | 448 if (sender == title_arrow_) { |
339 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent()); | 449 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent()); |
340 center_view->SetSettingsVisible(!center_view->settings_visible()); | 450 center_view->SetSettingsVisible(!center_view->settings_visible()); |
341 return; | 451 return; |
342 } | 452 } |
343 | 453 |
344 std::set<NotifierButton*>::iterator iter = buttons_.find( | 454 std::set<NotifierButton*>::iterator iter = buttons_.find( |
345 static_cast<NotifierButton*>(sender)); | 455 static_cast<NotifierButton*>(sender)); |
346 DCHECK(iter != buttons_.end()); | 456 |
457 if (iter == buttons_.end()) | |
458 return; | |
347 | 459 |
348 (*iter)->SetChecked(!(*iter)->checked()); | 460 (*iter)->SetChecked(!(*iter)->checked()); |
349 if (provider_) | 461 if (provider_) |
350 provider_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); | 462 provider_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); |
351 } | 463 } |
352 | 464 |
465 void NotifierSettingsView::OnMenuButtonClicked(views::View* source, | |
466 const gfx::Point& point) { | |
467 notifier_group_menu_model_.reset(new NotifierGroupMenuModel(provider_)); | |
468 notifier_group_menu_runner_.reset( | |
469 new views::MenuRunner(notifier_group_menu_model_.get())); | |
470 if (views::MenuRunner::MENU_DELETED == | |
471 notifier_group_menu_runner_->RunMenuAt(GetWidget(), | |
472 NULL, | |
473 source->GetBoundsInScreen(), | |
474 views::MenuItemView::BUBBLE_ABOVE, | |
475 ui::MENU_SOURCE_MOUSE, | |
476 views::MenuRunner::CONTEXT_MENU)) | |
477 return; | |
478 MessageCenterView* center_view = static_cast<MessageCenterView*>(parent()); | |
479 center_view->OnSettingsChanged(); | |
480 } | |
481 | |
353 } // namespace message_center | 482 } // namespace message_center |
OLD | NEW |