electron/brightray/browser/mac/cocoa_notification.mm

89 lines
2.7 KiB
Text
Raw Normal View History

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.
#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"
#include "base/strings/utf_string_conversions.h"
#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 {
CocoaNotification::CocoaNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {
2015-12-24 13:55:18 +00:00
}
CocoaNotification::~CocoaNotification() {
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-06-24 11:03:27 +00:00
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
2015-12-24 13:55:18 +00:00
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
2017-06-24 11:03:27 +00:00
!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) {
[notification_ setSoundName:nil];
} else {
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
}
2015-12-24 13:55:18 +00:00
[notification_ setHasActionButton:false];
2017-06-28 07:00:19 +00:00
int i = 0;
for (const auto& action : options.actions) {
if (action.type == base::ASCIIToUTF16("button")) {
[notification_ setHasActionButton:true];
2017-06-23 11:04:39 +00:00
[notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.text)];
2017-06-28 07:00:19 +00:00
action_index_ = i;
}
2017-06-28 07:00:19 +00:00
i++;
}
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_];
}
void CocoaNotification::Dismiss() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
NotificationDismissed();
2015-12-24 13:55:18 +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
}
void CocoaNotification::NotificationButtonClicked() {
if (delegate())
2017-06-28 07:00:19 +00:00
delegate()->NotificationAction(action_index_);
}
2015-12-24 13:55:18 +00:00
} // namespace brightray