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