Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/message_center/views/notifier_settings_view.h" |
| 6 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/strings/string16.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 11 | #include "base/strings/utf_string_conversions.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 12 | #include "grit/ui_resources.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 13 | #include "grit/ui_strings.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 14 | #include "skia/ext/image_operations.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkColor.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 16 | #include "ui/base/keycodes/keyboard_codes.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "ui/base/l10n/l10n_util.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 18 | #include "ui/base/models/simple_menu_model.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | #include "ui/base/resource/resource_bundle.h" |
| 20 | #include "ui/gfx/canvas.h" |
| 21 | #include "ui/gfx/image/image.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 22 | #include "ui/gfx/image/image_skia_operations.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 23 | #include "ui/gfx/size.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 24 | #include "ui/message_center/message_center_style.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 25 | #include "ui/message_center/views/message_center_view.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 26 | #include "ui/views/background.h" |
| 27 | #include "ui/views/border.h" |
| 28 | #include "ui/views/controls/button/checkbox.h" |
| 29 | #include "ui/views/controls/button/custom_button.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 30 | #include "ui/views/controls/button/menu_button.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 31 | #include "ui/views/controls/image_view.h" |
| 32 | #include "ui/views/controls/label.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 33 | #include "ui/views/controls/menu/menu_runner.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 34 | #include "ui/views/controls/scroll_view.h" |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 35 | #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 36 | #include "ui/views/layout/box_layout.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 37 | #include "ui/views/layout/grid_layout.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 38 | #include "ui/views/widget/widget.h" |
| 39 | |
| 40 | #if defined(USE_AURA) |
| 41 | #include "ui/aura/window.h" |
| 42 | #endif |
| 43 | |
| 44 | namespace message_center { |
| 45 | namespace { |
| 46 | |
| 47 | const int kSpaceInButtonComponents = 16; |
| 48 | const int kMarginWidth = 16; |
| 49 | const int kMinimumWindowWidth = 320; |
| 50 | const int kMinimumWindowHeight = 480; |
| 51 | const int kEntryHeight = kMinimumWindowHeight / 10; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 52 | |
| 53 | // The view to guarantee the 48px height and place the contents at the |
| 54 | // middle. It also guarantee the left margin. |
| 55 | class EntryView : public views::View { |
| 56 | public: |
| 57 | EntryView(views::View* contents); |
| 58 | virtual ~EntryView(); |
| 59 | |
| 60 | // Overridden from views::View: |
| 61 | virtual void Layout() OVERRIDE; |
| 62 | virtual gfx::Size GetPreferredSize() OVERRIDE; |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 63 | virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 64 | virtual void OnFocus() OVERRIDE; |
| 65 | virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE; |
| 66 | virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; |
| 67 | virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | DISALLOW_COPY_AND_ASSIGN(EntryView); |
| 71 | }; |
| 72 | |
| 73 | EntryView::EntryView(views::View* contents) { |
| 74 | AddChildView(contents); |
| 75 | } |
| 76 | |
| 77 | EntryView::~EntryView() { |
| 78 | } |
| 79 | |
| 80 | void EntryView::Layout() { |
| 81 | DCHECK_EQ(1, child_count()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 82 | views::View* content = child_at(0); |
| 83 | int content_width = width() - kMarginWidth * 2; |
| 84 | int content_height = content->GetHeightForWidth(content_width); |
| 85 | int y = std::max((height() - content_height) / 2, 0); |
| 86 | content->SetBounds(kMarginWidth, y, content_width, content_height); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | gfx::Size EntryView::GetPreferredSize() { |
| 90 | DCHECK_EQ(1, child_count()); |
| 91 | gfx::Size size = child_at(0)->GetPreferredSize(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 92 | size.SetToMax(gfx::Size(kMinimumWindowWidth, kEntryHeight)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | return size; |
| 94 | } |
| 95 | |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 96 | void EntryView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 97 | DCHECK_EQ(1, child_count()); |
| 98 | child_at(0)->GetAccessibleState(state); |
| 99 | } |
| 100 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 101 | void EntryView::OnFocus() { |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 102 | views::View::OnFocus(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 103 | ScrollRectToVisible(GetLocalBounds()); |
| 104 | } |
| 105 | |
| 106 | void EntryView::OnPaintFocusBorder(gfx::Canvas* canvas) { |
| 107 | if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { |
| 108 | canvas->DrawRect(gfx::Rect(2, 1, width() - 4, height() - 3), |
| 109 | kFocusBorderColor); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool EntryView::OnKeyPressed(const ui::KeyEvent& event) { |
| 114 | return child_at(0)->OnKeyPressed(event); |
| 115 | } |
| 116 | |
| 117 | bool EntryView::OnKeyReleased(const ui::KeyEvent& event) { |
| 118 | return child_at(0)->OnKeyReleased(event); |
| 119 | } |
| 120 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 121 | } // namespace |
| 122 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 195 | // We do not use views::Checkbox class directly because it doesn't support |
| 196 | // showing 'icon'. |
| 197 | class NotifierSettingsView::NotifierButton : public views::CustomButton, |
| 198 | public views::ButtonListener { |
| 199 | public: |
| 200 | NotifierButton(Notifier* notifier, views::ButtonListener* listener) |
| 201 | : views::CustomButton(listener), |
| 202 | notifier_(notifier), |
| 203 | icon_view_(NULL), |
| 204 | checkbox_(new views::Checkbox(string16())) { |
| 205 | DCHECK(notifier); |
| 206 | SetLayoutManager(new views::BoxLayout( |
| 207 | views::BoxLayout::kHorizontal, 0, 0, kSpaceInButtonComponents)); |
| 208 | checkbox_->SetChecked(notifier_->enabled); |
| 209 | checkbox_->set_listener(this); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 210 | checkbox_->set_focusable(false); |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 211 | checkbox_->SetAccessibleName(notifier_->name); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 212 | AddChildView(checkbox_); |
| 213 | UpdateIconImage(notifier_->icon); |
| 214 | AddChildView(new views::Label(notifier_->name)); |
| 215 | } |
| 216 | |
| 217 | void UpdateIconImage(const gfx::Image& icon) { |
| 218 | notifier_->icon = icon; |
| 219 | if (icon.IsEmpty()) { |
| 220 | delete icon_view_; |
| 221 | icon_view_ = NULL; |
| 222 | } else { |
| 223 | if (!icon_view_) { |
| 224 | icon_view_ = new views::ImageView(); |
| 225 | AddChildViewAt(icon_view_, 1); |
| 226 | } |
| 227 | icon_view_->SetImage(icon.ToImageSkia()); |
| 228 | icon_view_->SetImageSize(gfx::Size(kSettingsIconSize, kSettingsIconSize)); |
| 229 | } |
| 230 | Layout(); |
| 231 | SchedulePaint(); |
| 232 | } |
| 233 | |
| 234 | void SetChecked(bool checked) { |
| 235 | checkbox_->SetChecked(checked); |
| 236 | notifier_->enabled = checked; |
| 237 | } |
| 238 | |
| 239 | bool checked() const { |
| 240 | return checkbox_->checked(); |
| 241 | } |
| 242 | |
| 243 | const Notifier& notifier() const { |
| 244 | return *notifier_.get(); |
| 245 | } |
| 246 | |
| 247 | private: |
| 248 | // Overridden from views::ButtonListener: |
| 249 | virtual void ButtonPressed(views::Button* button, |
| 250 | const ui::Event& event) OVERRIDE { |
| 251 | DCHECK(button == checkbox_); |
| 252 | // The checkbox state has already changed at this point, but we'll update |
| 253 | // the state on NotifierSettingsView::ButtonPressed() too, so here change |
| 254 | // back to the previous state. |
| 255 | checkbox_->SetChecked(!checkbox_->checked()); |
| 256 | CustomButton::NotifyClick(event); |
| 257 | } |
| 258 | |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 259 | virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE { |
| 260 | static_cast<views::View*>(checkbox_)->GetAccessibleState(state); |
| 261 | } |
| 262 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 263 | scoped_ptr<Notifier> notifier_; |
| 264 | views::ImageView* icon_view_; |
| 265 | views::Checkbox* checkbox_; |
| 266 | |
| 267 | DISALLOW_COPY_AND_ASSIGN(NotifierButton); |
| 268 | }; |
| 269 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 270 | NotifierSettingsView::NotifierSettingsView(NotifierSettingsProvider* provider) |
| 271 | : provider_(provider) { |
| 272 | // |provider_| may be NULL in tests. |
| 273 | if (provider_) |
| 274 | provider_->AddObserver(this); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 275 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 276 | set_focusable(true); |
| 277 | set_focus_border(NULL); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 278 | set_background(views::Background::CreateSolidBackground( |
| 279 | kMessageCenterBackgroundColor)); |
| 280 | if (get_use_acceleration_when_possible()) |
| 281 | SetPaintToLayer(true); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 282 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 283 | ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| 284 | |
| 285 | views::View* title_container = new views::View; |
| 286 | // The title_arrow and title_label aren't aligned well in Windows for the |
| 287 | // horizontal BoxLayout. That's why GridLayout with vertical alignment is |
| 288 | // used here. |
| 289 | views::GridLayout* title_layout = new views::GridLayout(title_container); |
| 290 | title_container->SetLayoutManager(title_layout); |
| 291 | views::ColumnSet* columns = title_layout->AddColumnSet(0); |
| 292 | columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 293 | 0, views::GridLayout::USE_PREF, 0, 0); |
| 294 | columns->AddPaddingColumn(0, kMarginWidth); |
| 295 | columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 296 | 1, views::GridLayout::USE_PREF, 0, 0); |
| 297 | title_arrow_ = new views::ImageButton(this); |
| 298 | title_arrow_->SetImage(views::Button::STATE_NORMAL, bundle.GetImageSkiaNamed( |
| 299 | IDR_NOTIFICATION_ARROW)); |
| 300 | title_arrow_->SetImage(views::Button::STATE_HOVERED, bundle.GetImageSkiaNamed( |
| 301 | IDR_NOTIFICATION_ARROW_HOVER)); |
| 302 | title_arrow_->SetImage(views::Button::STATE_PRESSED, bundle.GetImageSkiaNamed( |
| 303 | IDR_NOTIFICATION_ARROW_PRESSED)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 304 | gfx::Font title_font = |
| 305 | ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont); |
| 306 | views::Label* title_label = new views::Label( |
| 307 | l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS_BUTTON_LABEL), |
| 308 | title_font); |
| 309 | title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 310 | title_label->SetMultiLine(true); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 311 | title_layout->StartRow(0, 0); |
| 312 | title_layout->AddView(title_arrow_); |
| 313 | title_layout->AddView(title_label); |
| 314 | title_entry_ = new EntryView(title_container); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 315 | AddChildView(title_entry_); |
| 316 | |
| 317 | scroller_ = new views::ScrollView(); |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 318 | scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 319 | AddChildView(scroller_); |
| 320 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 321 | std::vector<Notifier*> notifiers; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 322 | if (provider_) |
| 323 | provider_->GetNotifierList(¬ifiers); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 324 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 325 | UpdateContentsView(notifiers); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | NotifierSettingsView::~NotifierSettingsView() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 329 | // |provider_| may be NULL in tests. |
| 330 | if (provider_) |
| 331 | provider_->RemoveObserver(this); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 334 | bool NotifierSettingsView::IsScrollable() { |
| 335 | return scroller_->height() < scroller_->contents()->height(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 338 | void NotifierSettingsView::UpdateIconImage(const NotifierId& notifier_id, |
| 339 | const gfx::Image& icon) { |
| 340 | for (std::set<NotifierButton*>::iterator iter = buttons_.begin(); |
| 341 | iter != buttons_.end(); ++iter) { |
| 342 | if ((*iter)->notifier().notifier_id == notifier_id) { |
| 343 | (*iter)->UpdateIconImage(icon); |
| 344 | return; |
| 345 | } |
| 346 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 400 | void NotifierSettingsView::Layout() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 401 | int title_height = title_entry_->GetHeightForWidth(width()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 402 | title_entry_->SetBounds(0, 0, width(), title_height); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 403 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 404 | views::View* contents_view = scroller_->contents(); |
| 405 | int content_width = width(); |
| 406 | int content_height = contents_view->GetHeightForWidth(content_width); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 407 | if (title_height + content_height > height()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 408 | content_width -= scroller_->GetScrollBarWidth(); |
| 409 | content_height = contents_view->GetHeightForWidth(content_width); |
| 410 | } |
| 411 | contents_view->SetBounds(0, 0, content_width, content_height); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 412 | scroller_->SetBounds(0, title_height, width(), height() - title_height); |
| 413 | } |
| 414 | |
| 415 | gfx::Size NotifierSettingsView::GetMinimumSize() { |
| 416 | gfx::Size size(kMinimumWindowWidth, kMinimumWindowHeight); |
| 417 | int total_height = title_entry_->GetPreferredSize().height() + |
| 418 | scroller_->contents()->GetPreferredSize().height(); |
| 419 | if (total_height > kMinimumWindowHeight) |
| 420 | size.Enlarge(scroller_->GetScrollBarWidth(), 0); |
| 421 | return size; |
| 422 | } |
| 423 | |
| 424 | gfx::Size NotifierSettingsView::GetPreferredSize() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 425 | gfx::Size preferred_size; |
| 426 | std::vector<gfx::Size> child_sizes; |
| 427 | gfx::Size title_size = title_entry_->GetPreferredSize(); |
| 428 | gfx::Size content_size = scroller_->contents()->GetPreferredSize(); |
| 429 | return gfx::Size(std::max(title_size.width(), content_size.width()), |
| 430 | title_size.height() + content_size.height()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 433 | bool NotifierSettingsView::OnKeyPressed(const ui::KeyEvent& event) { |
| 434 | if (event.key_code() == ui::VKEY_ESCAPE) { |
| 435 | GetWidget()->Close(); |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | return scroller_->OnKeyPressed(event); |
| 440 | } |
| 441 | |
| 442 | bool NotifierSettingsView::OnMouseWheel(const ui::MouseWheelEvent& event) { |
| 443 | return scroller_->OnMouseWheel(event); |
| 444 | } |
| 445 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 446 | void NotifierSettingsView::ButtonPressed(views::Button* sender, |
| 447 | const ui::Event& event) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 448 | if (sender == title_arrow_) { |
| 449 | MessageCenterView* center_view = static_cast<MessageCenterView*>(parent()); |
| 450 | center_view->SetSettingsVisible(!center_view->settings_visible()); |
| 451 | return; |
| 452 | } |
| 453 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 454 | std::set<NotifierButton*>::iterator iter = buttons_.find( |
| 455 | static_cast<NotifierButton*>(sender)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 456 | |
| 457 | if (iter == buttons_.end()) |
| 458 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 459 | |
| 460 | (*iter)->SetChecked(!(*iter)->checked()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 461 | if (provider_) |
| 462 | provider_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 482 | } // namespace message_center |