electron/brightray/browser/mac/cocoa_notification.mm

65 lines
1.9 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 "browser/mac/cocoa_notification.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "browser/notification_delegate.h"
#include "browser/notification_presenter.h"
2015-12-24 13:55:18 +00:00
#include "skia/ext/skia_utils_mac.h"
namespace brightray {
// static
Notification* Notification::Create(NotificationDelegate* delegate,
NotificationPresenter* presenter) {
return new CocoaNotification(delegate, presenter);
2015-12-24 13:55:18 +00:00
}
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
}
void CocoaNotification::Show(const base::string16& title,
const base::string16& body,
const SkBitmap& icon) {
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()) {
NSImage* image = gfx::SkBitmapToNSImageWithColorSpace(
icon, base::mac::GetGenericRGBColorSpace());
[notification_ setContentImage:image];
}
[NSUserNotificationCenter.defaultUserNotificationCenter
deliverNotification:notification_];
}
void CocoaNotification::Dismiss() {
Destroy();
2015-12-24 13:55:18 +00:00
}
void CocoaNotification::NotifyDisplayed() {
delegate()->NotificationDisplayed();
2015-12-24 13:55:18 +00:00
}
void CocoaNotification::NotifyClick() {
delegate()->NotificationClick();
Destroy();
2015-12-24 13:55:18 +00:00
}
} // namespace brightray