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 {
|
|
|
|
|
2017-10-07 03:45:46 +00:00
|
|
|
int g_identifier_ = 1;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-24 11:03:27 +00:00
|
|
|
void CocoaNotification::Show(const NotificationOptions& options) {
|
2015-12-24 13:55:18 +00:00
|
|
|
notification_.reset([[NSUserNotification alloc] init]);
|
2017-12-13 00:09:11 +00:00
|
|
|
|
|
|
|
NSString* identifier = [NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_];
|
|
|
|
|
2017-06-24 11:03:27 +00:00
|
|
|
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
|
2017-06-30 13:59:46 +00:00
|
|
|
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
|
2017-06-24 11:03:27 +00:00
|
|
|
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
|
2017-12-13 00:09:11 +00:00
|
|
|
[notification_ setIdentifier:identifier];
|
2017-10-07 03:45:46 +00:00
|
|
|
g_identifier_++;
|
2015-12-24 13:55:18 +00:00
|
|
|
|
2017-12-13 00:09:11 +00:00
|
|
|
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
2017-12-13 22:47:23 +00:00
|
|
|
LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")";
|
2017-12-13 00:09:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 02:59:30 +00:00
|
|
|
if (!options.icon.drawsNothing()) {
|
2016-03-08 11:59:29 +00:00
|
|
|
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
2017-06-24 11:03:27 +00:00
|
|
|
options.icon, base::mac::GetGenericRGBColorSpace());
|
2015-12-24 13:55:18 +00:00
|
|
|
[notification_ setContentImage:image];
|
|
|
|
}
|
2016-03-08 11:59:29 +00:00
|
|
|
|
2017-06-24 11:03:27 +00:00
|
|
|
if (options.silent) {
|
2016-01-21 00:36:41 +00:00
|
|
|
[notification_ setSoundName:nil];
|
2017-08-18 00:28:14 +00:00
|
|
|
} else if (options.sound != nil) {
|
|
|
|
[notification_ setSoundName:base::SysUTF16ToNSString(options.sound)];
|
2016-01-21 00:36:41 +00:00
|
|
|
} else {
|
|
|
|
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
|
|
|
}
|
2015-12-24 13:55:18 +00:00
|
|
|
|
2017-06-23 10:39:42 +00:00
|
|
|
[notification_ setHasActionButton:false];
|
|
|
|
|
2017-06-28 07:00:19 +00:00
|
|
|
int i = 0;
|
2018-01-16 04:50:03 +00:00
|
|
|
action_index_ = UINT_MAX;
|
|
|
|
NSMutableArray* additionalActions = [[[NSMutableArray alloc] init] autorelease];
|
2017-06-28 07:00:19 +00:00
|
|
|
for (const auto& action : options.actions) {
|
|
|
|
if (action.type == base::ASCIIToUTF16("button")) {
|
2018-01-16 04:50:03 +00:00
|
|
|
if (action_index_ == UINT_MAX) {
|
|
|
|
// First button observed is the displayed action
|
|
|
|
[notification_ setHasActionButton:true];
|
|
|
|
[notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.text)];
|
|
|
|
action_index_ = i;
|
|
|
|
} else {
|
|
|
|
// All of the rest are appended to the list of additional actions
|
|
|
|
NSString* actionIdentifier = [NSString stringWithFormat:@"%@Action%d", identifier, i];
|
|
|
|
NSUserNotificationAction* notificationAction =
|
|
|
|
[NSUserNotificationAction actionWithIdentifier:actionIdentifier
|
|
|
|
title:base::SysUTF16ToNSString(action.text)];
|
|
|
|
[additionalActions addObject:notificationAction];
|
|
|
|
additional_action_indices_.insert(std::make_pair(base::SysNSStringToUTF8(actionIdentifier), i));
|
|
|
|
}
|
2017-06-23 10:39:42 +00:00
|
|
|
}
|
2017-06-28 07:00:19 +00:00
|
|
|
i++;
|
2017-06-23 10:39:42 +00:00
|
|
|
}
|
2018-01-16 04:50:03 +00:00
|
|
|
if ([additionalActions count] > 0 &&
|
|
|
|
[notification_ respondsToSelector:@selector(setAdditionalActions:)]) {
|
|
|
|
[notification_ setAdditionalActions:additionalActions]; // Requires macOS 10.10
|
|
|
|
}
|
2017-06-23 10:39:42 +00:00
|
|
|
|
2017-06-24 11:03:27 +00:00
|
|
|
if (options.has_reply) {
|
|
|
|
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)];
|
2017-05-29 10:02:33 +00:00
|
|
|
[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_];
|
2017-12-13 00:09:11 +00:00
|
|
|
|
2016-04-15 07:14:13 +00:00
|
|
|
NotificationDismissed();
|
2017-12-13 00:09:11 +00:00
|
|
|
|
|
|
|
this->LogAction("dismissed");
|
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();
|
2017-12-13 00:09:11 +00:00
|
|
|
|
|
|
|
this->LogAction("displayed");
|
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-12-13 00:09:11 +00:00
|
|
|
|
|
|
|
this->LogAction("replied to");
|
2017-05-29 10:02:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 10:39:42 +00:00
|
|
|
void CocoaNotification::NotificationButtonClicked() {
|
|
|
|
if (delegate())
|
2017-06-28 07:00:19 +00:00
|
|
|
delegate()->NotificationAction(action_index_);
|
2017-12-13 00:09:11 +00:00
|
|
|
|
2017-12-13 18:31:59 +00:00
|
|
|
this->LogAction("button clicked");
|
2017-12-13 00:09:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 04:50:03 +00:00
|
|
|
void CocoaNotification::NotificationAdditionalActionClicked(NSUserNotificationAction* action) {
|
|
|
|
if (delegate()) {
|
|
|
|
unsigned index = action_index_;
|
|
|
|
std::string identifier = base::SysNSStringToUTF8(action.identifier);
|
|
|
|
for (const auto& it : additional_action_indices_) {
|
|
|
|
if (it.first == identifier) {
|
|
|
|
index = it.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delegate()->NotificationAction(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->LogAction("button clicked");
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:09:11 +00:00
|
|
|
void CocoaNotification::LogAction(const char* action) {
|
|
|
|
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
|
|
|
NSString* identifier = [notification_ valueForKey:@"identifier"];
|
2017-12-13 22:47:23 +00:00
|
|
|
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] << ")";
|
2017-12-13 00:09:11 +00:00
|
|
|
}
|
2017-06-23 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 13:55:18 +00:00
|
|
|
} // namespace brightray
|