| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | // Copyright (c) 2015 GitHub, Inc. | 
					
						
							|  |  |  | // Use of this source code is governed by the MIT license that can be | 
					
						
							|  |  |  | // found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 15:58:12 -07:00
										 |  |  | #include "brightray/browser/mac/cocoa_notification.h" | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "base/mac/mac_util.h" | 
					
						
							|  |  |  | #include "base/strings/sys_string_conversions.h" | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  | #include "base/strings/utf_string_conversions.h" | 
					
						
							| 
									
										
										
										
											2017-05-18 15:58:12 -07:00
										 |  |  | #include "brightray/browser/notification_delegate.h" | 
					
						
							|  |  |  | #include "brightray/browser/notification_presenter.h" | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | #include "skia/ext/skia_utils_mac.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace brightray { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 10:16:07 +08:00
										 |  |  | CocoaNotification::CocoaNotification(NotificationDelegate* delegate, | 
					
						
							|  |  |  |                                      NotificationPresenter* presenter) | 
					
						
							|  |  |  |     : Notification(delegate, presenter) { | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CocoaNotification::~CocoaNotification() { | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  |   if (notification_) | 
					
						
							|  |  |  |     [NSUserNotificationCenter.defaultUserNotificationCenter | 
					
						
							|  |  |  |         removeDeliveredNotification:notification_]; | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  | void CocoaNotification::Show(const NotificationOptions& options) { | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  |   notification_.reset([[NSUserNotification alloc] init]); | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   [notification_ setTitle:base::SysUTF16ToNSString(options.title)]; | 
					
						
							| 
									
										
										
										
											2017-06-30 15:59:46 +02:00
										 |  |  |   [notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)]; | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)]; | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ([notification_ respondsToSelector:@selector(setContentImage:)] && | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |       !options.icon.drawsNothing()) { | 
					
						
							| 
									
										
										
										
											2016-03-08 20:59:29 +09:00
										 |  |  |     NSImage* image = skia::SkBitmapToNSImageWithColorSpace( | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |         options.icon, base::mac::GetGenericRGBColorSpace()); | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  |     [notification_ setContentImage:image]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-08 20:59:29 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   if (options.silent) { | 
					
						
							| 
									
										
										
										
											2016-01-20 16:36:41 -08:00
										 |  |  |     [notification_ setSoundName:nil]; | 
					
						
							| 
									
										
										
										
											2017-08-17 17:28:14 -07:00
										 |  |  |   } else if (options.sound != nil) { | 
					
						
							|  |  |  |     [notification_ setSoundName:base::SysUTF16ToNSString(options.sound)]; | 
					
						
							| 
									
										
										
										
											2016-01-20 16:36:41 -08:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     [notification_ setSoundName:NSUserNotificationDefaultSoundName]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  |   [notification_ setHasActionButton:false]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:19 +10:00
										 |  |  |   int i = 0; | 
					
						
							|  |  |  |   for (const auto& action : options.actions) { | 
					
						
							|  |  |  |     if (action.type == base::ASCIIToUTF16("button")) { | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  |       [notification_ setHasActionButton:true]; | 
					
						
							| 
									
										
										
										
											2017-06-23 21:04:39 +10:00
										 |  |  |       [notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.text)]; | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:19 +10:00
										 |  |  |       action_index_ = i; | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:19 +10:00
										 |  |  |     i++; | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   if (options.has_reply) { | 
					
						
							|  |  |  |     [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)]; | 
					
						
							| 
									
										
										
										
											2017-05-29 20:02:33 +10:00
										 |  |  |     [notification_ setHasReplyButton:true]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  |   [NSUserNotificationCenter.defaultUserNotificationCenter | 
					
						
							|  |  |  |       deliverNotification:notification_]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 10:16:07 +08:00
										 |  |  | void CocoaNotification::Dismiss() { | 
					
						
							| 
									
										
										
										
											2016-10-28 10:54:55 -07:00
										 |  |  |   if (notification_) | 
					
						
							|  |  |  |     [NSUserNotificationCenter.defaultUserNotificationCenter | 
					
						
							|  |  |  |         removeDeliveredNotification:notification_]; | 
					
						
							| 
									
										
										
										
											2016-04-15 16:14:13 +09:00
										 |  |  |   NotificationDismissed(); | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 16:14:13 +09:00
										 |  |  | void CocoaNotification::NotificationDisplayed() { | 
					
						
							| 
									
										
										
										
											2017-05-30 19:06:51 +10:00
										 |  |  |   if (delegate()) | 
					
						
							|  |  |  |     delegate()->NotificationDisplayed(); | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 16:17:29 +09:00
										 |  |  | void CocoaNotification::NotificationReplied(const std::string& reply) { | 
					
						
							| 
									
										
										
										
											2017-05-30 19:06:51 +10:00
										 |  |  |   if (delegate()) | 
					
						
							|  |  |  |     delegate()->NotificationReplied(reply); | 
					
						
							| 
									
										
										
										
											2017-05-29 20:02:33 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  | void CocoaNotification::NotificationButtonClicked() { | 
					
						
							|  |  |  |   if (delegate()) | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:19 +10:00
										 |  |  |     delegate()->NotificationAction(action_index_); | 
					
						
							| 
									
										
										
										
											2017-06-23 20:39:42 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 21:55:18 +08:00
										 |  |  | }  // namespace brightray |