| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/message_view.h" | 5 #include "ui/message_center/message_view.h" |
| 6 | 6 |
| 7 #include "grit/ui_resources.h" | 7 #include "grit/ui_resources.h" |
| 8 #include "grit/ui_strings.h" | 8 #include "grit/ui_strings.h" |
| 9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
| 10 #include "ui/base/models/simple_menu_model.h" | 10 #include "ui/base/models/simple_menu_model.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // Menu constants | 137 // Menu constants |
| 138 const int kTogglePermissionCommand = 0; | 138 const int kTogglePermissionCommand = 0; |
| 139 const int kToggleExtensionCommand = 1; | 139 const int kToggleExtensionCommand = 1; |
| 140 const int kShowSettingsCommand = 2; | 140 const int kShowSettingsCommand = 2; |
| 141 | 141 |
| 142 // A dropdown menu for notifications. | 142 // A dropdown menu for notifications. |
| 143 class WebNotificationMenuModel : public ui::SimpleMenuModel, | 143 class WebNotificationMenuModel : public ui::SimpleMenuModel, |
| 144 public ui::SimpleMenuModel::Delegate { | 144 public ui::SimpleMenuModel::Delegate { |
| 145 public: | 145 public: |
| 146 WebNotificationMenuModel(NotificationList::Delegate* list_delegate, | 146 WebNotificationMenuModel(NotificationList::Delegate* list_delegate, |
| 147 const Notification& notification) | 147 Notification* notification) |
| 148 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 148 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
| 149 list_delegate_(list_delegate), | 149 list_delegate_(list_delegate), |
| 150 notification_(notification) { | 150 notification_(notification) { |
| 151 // Add 'disable notifications' menu item. | 151 // Add 'disable notifications' menu item. |
| 152 if (!notification.extension_id.empty()) { | 152 if (!notification->extension_id().empty()) { |
| 153 AddItem(kToggleExtensionCommand, | 153 AddItem(kToggleExtensionCommand, |
| 154 GetLabelForCommandId(kToggleExtensionCommand)); | 154 GetLabelForCommandId(kToggleExtensionCommand)); |
| 155 } else if (!notification.display_source.empty()) { | 155 } else if (!notification->display_source().empty()) { |
| 156 AddItem(kTogglePermissionCommand, | 156 AddItem(kTogglePermissionCommand, |
| 157 GetLabelForCommandId(kTogglePermissionCommand)); | 157 GetLabelForCommandId(kTogglePermissionCommand)); |
| 158 } | 158 } |
| 159 // Add settings menu item. | 159 // Add settings menu item. |
| 160 if (!notification.display_source.empty()) { | 160 if (!notification->display_source().empty()) { |
| 161 AddItem(kShowSettingsCommand, | 161 AddItem(kShowSettingsCommand, |
| 162 GetLabelForCommandId(kShowSettingsCommand)); | 162 GetLabelForCommandId(kShowSettingsCommand)); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 virtual ~WebNotificationMenuModel() { | 166 virtual ~WebNotificationMenuModel() { |
| 167 } | 167 } |
| 168 | 168 |
| 169 // Overridden from ui::SimpleMenuModel: | 169 // Overridden from ui::SimpleMenuModel: |
| 170 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE { | 170 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE { |
| 171 switch (command_id) { | 171 switch (command_id) { |
| 172 case kToggleExtensionCommand: | 172 case kToggleExtensionCommand: |
| 173 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_EXTENSIONS_DISABLE); | 173 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_EXTENSIONS_DISABLE); |
| 174 case kTogglePermissionCommand: | 174 case kTogglePermissionCommand: |
| 175 return l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_SITE_DISABLE, | 175 return l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_SITE_DISABLE, |
| 176 notification_.display_source); | 176 notification_->display_source()); |
| 177 case kShowSettingsCommand: | 177 case kShowSettingsCommand: |
| 178 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS); | 178 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS); |
| 179 default: | 179 default: |
| 180 NOTREACHED(); | 180 NOTREACHED(); |
| 181 } | 181 } |
| 182 return string16(); | 182 return string16(); |
| 183 } | 183 } |
| 184 | 184 |
| 185 // Overridden from ui::SimpleMenuModel::Delegate: | 185 // Overridden from ui::SimpleMenuModel::Delegate: |
| 186 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { | 186 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { |
| 187 return false; | 187 return false; |
| 188 } | 188 } |
| 189 | 189 |
| 190 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE { | 190 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE { |
| 191 return false; | 191 return false; |
| 192 } | 192 } |
| 193 | 193 |
| 194 virtual bool GetAcceleratorForCommandId( | 194 virtual bool GetAcceleratorForCommandId( |
| 195 int command_id, | 195 int command_id, |
| 196 ui::Accelerator* accelerator) OVERRIDE { | 196 ui::Accelerator* accelerator) OVERRIDE { |
| 197 return false; | 197 return false; |
| 198 } | 198 } |
| 199 | 199 |
| 200 virtual void ExecuteCommand(int command_id) OVERRIDE { | 200 virtual void ExecuteCommand(int command_id) OVERRIDE { |
| 201 switch (command_id) { | 201 switch (command_id) { |
| 202 case kToggleExtensionCommand: | 202 case kToggleExtensionCommand: |
| 203 list_delegate_->DisableNotificationByExtension(notification_.id); | 203 list_delegate_->DisableNotificationByExtension(notification_->id()); |
| 204 break; | 204 break; |
| 205 case kTogglePermissionCommand: | 205 case kTogglePermissionCommand: |
| 206 list_delegate_->DisableNotificationByUrl(notification_.id); | 206 list_delegate_->DisableNotificationByUrl(notification_->id()); |
| 207 break; | 207 break; |
| 208 case kShowSettingsCommand: | 208 case kShowSettingsCommand: |
| 209 list_delegate_->ShowNotificationSettings(notification_.id); | 209 list_delegate_->ShowNotificationSettings(notification_->id()); |
| 210 break; | 210 break; |
| 211 default: | 211 default: |
| 212 NOTREACHED(); | 212 NOTREACHED(); |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 | 215 |
| 216 private: | 216 private: |
| 217 NotificationList::Delegate* list_delegate_; | 217 NotificationList::Delegate* list_delegate_; // Weak, global MessageCenter |
| 218 Notification notification_; | 218 Notification* notification_; // Weak, owned by NotificationList |
| 219 | 219 |
| 220 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel); | 220 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel); |
| 221 }; | 221 }; |
| 222 | 222 |
| 223 MessageView::MessageView( | 223 MessageView::MessageView( |
| 224 NotificationList::Delegate* list_delegate, | 224 NotificationList::Delegate* list_delegate, |
| 225 const Notification& notification) | 225 Notification* notification) |
| 226 : list_delegate_(list_delegate), | 226 : list_delegate_(list_delegate), |
| 227 notification_(notification), | 227 notification_(notification), |
| 228 scroller_(NULL) { | 228 scroller_(NULL) { |
| 229 ControlButton *close = new ControlButton(this); | 229 ControlButton *close = new ControlButton(this); |
| 230 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding); | 230 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding); |
| 231 close->SetNormalImage(IDR_NOTIFICATION_CLOSE); | 231 close->SetNormalImage(IDR_NOTIFICATION_CLOSE); |
| 232 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER); | 232 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER); |
| 233 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED); | 233 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED); |
| 234 close_button_.reset(close); | 234 close_button_.reset(close); |
| 235 } | 235 } |
| 236 | 236 |
| 237 MessageView::MessageView() { | 237 MessageView::MessageView() { |
| 238 } | 238 } |
| 239 | 239 |
| 240 MessageView::~MessageView() { | 240 MessageView::~MessageView() { |
| 241 } | 241 } |
| 242 | 242 |
| 243 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { | 243 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { |
| 244 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) { | 244 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) { |
| 245 ShowMenu(event.location()); | 245 ShowMenu(event.location()); |
| 246 return true; | 246 return true; |
| 247 } | 247 } |
| 248 list_delegate_->OnNotificationClicked(notification_.id); | 248 list_delegate_->OnNotificationClicked(notification_->id()); |
| 249 return true; | 249 return true; |
| 250 } | 250 } |
| 251 | 251 |
| 252 void MessageView::OnGestureEvent(ui::GestureEvent* event) { | 252 void MessageView::OnGestureEvent(ui::GestureEvent* event) { |
| 253 if (event->type() == ui::ET_GESTURE_TAP) { | 253 if (event->type() == ui::ET_GESTURE_TAP) { |
| 254 list_delegate_->OnNotificationClicked(notification_.id); | 254 list_delegate_->OnNotificationClicked(notification_->id()); |
| 255 event->SetHandled(); | 255 event->SetHandled(); |
| 256 return; | 256 return; |
| 257 } | 257 } |
| 258 | 258 |
| 259 if (event->type() == ui::ET_GESTURE_LONG_PRESS) { | 259 if (event->type() == ui::ET_GESTURE_LONG_PRESS) { |
| 260 ShowMenu(event->location()); | 260 ShowMenu(event->location()); |
| 261 event->SetHandled(); | 261 event->SetHandled(); |
| 262 return; | 262 return; |
| 263 } | 263 } |
| 264 | 264 |
| 265 SlideOutView::OnGestureEvent(event); | 265 SlideOutView::OnGestureEvent(event); |
| 266 // Do not return here by checking handled(). SlideOutView calls SetHandled() | 266 // Do not return here by checking handled(). SlideOutView calls SetHandled() |
| 267 // even though the scroll gesture doesn't make no (or little) effects on the | 267 // even though the scroll gesture doesn't make no (or little) effects on the |
| 268 // slide-out behavior. See http://crbug.com/172991 | 268 // slide-out behavior. See http://crbug.com/172991 |
| 269 | 269 |
| 270 if (!event->IsScrollGestureEvent()) | 270 if (!event->IsScrollGestureEvent()) |
| 271 return; | 271 return; |
| 272 | 272 |
| 273 if (scroller_) | 273 if (scroller_) |
| 274 scroller_->OnGestureEvent(event); | 274 scroller_->OnGestureEvent(event); |
| 275 event->SetHandled(); | 275 event->SetHandled(); |
| 276 } | 276 } |
| 277 | 277 |
| 278 void MessageView::ButtonPressed(views::Button* sender, | 278 void MessageView::ButtonPressed(views::Button* sender, |
| 279 const ui::Event& event) { | 279 const ui::Event& event) { |
| 280 if (sender == close_button()) | 280 if (sender == close_button()) |
| 281 list_delegate_->SendRemoveNotification(notification_.id); | 281 list_delegate_->SendRemoveNotification(notification_->id()); |
| 282 } | 282 } |
| 283 | 283 |
| 284 void MessageView::ShowMenu(gfx::Point screen_location) { | 284 void MessageView::ShowMenu(gfx::Point screen_location) { |
| 285 WebNotificationMenuModel menu_model(list_delegate_, notification_); | 285 WebNotificationMenuModel menu_model(list_delegate_, notification_); |
| 286 if (menu_model.GetItemCount() == 0) | 286 if (menu_model.GetItemCount() == 0) |
| 287 return; | 287 return; |
| 288 | 288 |
| 289 views::MenuModelAdapter menu_model_adapter(&menu_model); | 289 views::MenuModelAdapter menu_model_adapter(&menu_model); |
| 290 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); | 290 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); |
| 291 | 291 |
| 292 views::View::ConvertPointToScreen(this, &screen_location); | 292 views::View::ConvertPointToScreen(this, &screen_location); |
| 293 ignore_result(menu_runner.RunMenuAt( | 293 ignore_result(menu_runner.RunMenuAt( |
| 294 GetWidget()->GetTopLevelWidget(), | 294 GetWidget()->GetTopLevelWidget(), |
| 295 NULL, | 295 NULL, |
| 296 gfx::Rect(screen_location, gfx::Size()), | 296 gfx::Rect(screen_location, gfx::Size()), |
| 297 views::MenuItemView::TOPRIGHT, | 297 views::MenuItemView::TOPRIGHT, |
| 298 views::MenuRunner::HAS_MNEMONICS)); | 298 views::MenuRunner::HAS_MNEMONICS)); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void MessageView::OnSlideOut() { | 301 void MessageView::OnSlideOut() { |
| 302 list_delegate_->SendRemoveNotification(notification_.id); | 302 list_delegate_->SendRemoveNotification(notification_->id()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 } // namespace message_center | 305 } // namespace message_center |
| OLD | NEW |