2015-12-24 13:55:18 +00: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 22:58:12 +00:00
|
|
|
#include "brightray/browser/mac/cocoa_notification.h"
|
2015-12-24 13:55:18 +00:00
|
|
|
|
|
|
|
#include "base/mac/mac_util.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2017-06-23 10:39:42 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/notification_delegate.h"
|
|
|
|
#include "brightray/browser/notification_presenter.h"
|
2015-12-24 13:55:18 +00:00
|
|
|
#include "skia/ext/skia_utils_mac.h"
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
CocoaNotification::CocoaNotification(NotificationDelegate* delegate,
|
|
|
|
NotificationPresenter* presenter)
|
|
|
|
: Notification(delegate, presenter) {
|
2015-12-24 13:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CocoaNotification::~CocoaNotification() {
|
2016-10-25 13:32:06 +00:00
|
|
|
if (notification_)
|
|
|
|
[NSUserNotificationCenter.defaultUserNotificationCenter
|
|
|
|
removeDeliveredNotification:notification_];
|
2015-12-24 13:55:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
void CocoaNotification::Show(const base::string16& title,
|
|
|
|
const base::string16& body,
|
2016-04-13 03:31:56 +00:00
|
|
|
const std::string& tag,
|
2015-12-25 03:05:48 +00:00
|
|
|
const GURL& icon_url,
|
2016-01-21 00:36:41 +00:00
|
|
|
const SkBitmap& icon,
|
2017-05-30 09:06:51 +00:00
|
|
|
bool silent,
|
|
|
|
bool has_reply,
|
2017-06-23 10:39:42 +00:00
|
|
|
const base::string16& reply_placeholder,
|
|
|
|
const std::vector<NotificationAction> actions) {
|
2015-12-24 13:55:18 +00:00
|
|
|
notification_.reset([[NSUserNotification alloc] init]);
|
|
|
|
[notification_ setTitle:base::SysUTF16ToNSString(title)];
|
|
|
|
[notification_ setInformativeText:base::SysUTF16ToNSString(body)];
|
|
|
|
|
|
|
|
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
|
|
|
|
!icon.drawsNothing()) {
|
2016-03-08 11:59:29 +00:00
|
|
|
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
2015-12-24 13:55:18 +00:00
|
|
|
icon, base::mac::GetGenericRGBColorSpace());
|
|
|
|
[notification_ setContentImage:image];
|
|
|
|
}
|
2016-03-08 11:59:29 +00:00
|
|
|
|
2016-01-21 00:36:41 +00:00
|
|
|
if (silent) {
|
|
|
|
[notification_ setSoundName:nil];
|
|
|
|
} else {
|
|
|
|
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
|
|
|
}
|
2015-12-24 13:55:18 +00:00
|
|
|
|
2017-06-23 10:39:42 +00:00
|
|
|
[notification_ setHasActionButton:false];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < actions.size(); i++) {
|
|
|
|
NotificationAction action = actions[i];
|
|
|
|
|
|
|
|
if (action.type == base::UTF8ToUTF16("button")) {
|
|
|
|
[notification_ setHasActionButton:true];
|
|
|
|
[notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.label)];
|
|
|
|
actionIndex_ = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-29 10:02:33 +00:00
|
|
|
if (has_reply) {
|
|
|
|
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)];
|
|
|
|
[notification_ setHasReplyButton:true];
|
|
|
|
}
|
|
|
|
|
2015-12-24 13:55:18 +00:00
|
|
|
[NSUserNotificationCenter.defaultUserNotificationCenter
|
|
|
|
deliverNotification:notification_];
|
|
|
|
}
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
void CocoaNotification::Dismiss() {
|
2016-10-28 17:54:55 +00:00
|
|
|
if (notification_)
|
|
|
|
[NSUserNotificationCenter.defaultUserNotificationCenter
|
|
|
|
removeDeliveredNotification:notification_];
|
2016-04-15 07:14:13 +00:00
|
|
|
NotificationDismissed();
|
2015-12-24 13:55:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 07:14:13 +00:00
|
|
|
void CocoaNotification::NotificationDisplayed() {
|
2017-05-30 09:06:51 +00:00
|
|
|
if (delegate())
|
|
|
|
delegate()->NotificationDisplayed();
|
2015-12-24 13:55:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:17:29 +00:00
|
|
|
void CocoaNotification::NotificationReplied(const std::string& reply) {
|
2017-05-30 09:06:51 +00:00
|
|
|
if (delegate())
|
|
|
|
delegate()->NotificationReplied(reply);
|
2017-05-29 10:02:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 10:39:42 +00:00
|
|
|
void CocoaNotification::NotificationButtonClicked() {
|
|
|
|
if (delegate())
|
|
|
|
delegate()->NotificationAction(actionIndex_);
|
|
|
|
}
|
|
|
|
|
2015-12-24 13:55:18 +00:00
|
|
|
} // namespace brightray
|