Use NotificationPresenter - macOS
This commit is contained in:
parent
193c561815
commit
058bdfbced
17 changed files with 93 additions and 80 deletions
|
@ -7,89 +7,48 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include "atom/browser/browser.h"
|
||||
#include "atom/browser/ui/notification_delegate_adapter.h"
|
||||
#include "base/mac/mac_util.h"
|
||||
#include "base/mac/scoped_nsobject.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
|
||||
std::map<int, base::scoped_nsobject<NSUserNotification>> native_notifications_;
|
||||
|
||||
@interface AtomNotificationCenter : NSObject<NSUserNotificationCenterDelegate> {
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation AtomNotificationCenter
|
||||
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
|
||||
shouldPresentNotification:(NSUserNotification *)notification {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
|
||||
didActivateNotification:(NSUserNotification *)notification {
|
||||
int n_id = [[notification.userInfo objectForKey:@"id"] intValue];
|
||||
if (atom::api::Notification::HasID(n_id)) {
|
||||
auto atomNotification = atom::api::Notification::FromID(n_id);
|
||||
|
||||
if (notification.activationType == NSUserNotificationActivationTypeReplied){
|
||||
atomNotification->OnReplied([notification.response.string UTF8String]);
|
||||
} else {
|
||||
atomNotification->OnClicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
@end
|
||||
#include "brightray/browser/notification.h"
|
||||
#include "brightray/browser/notification_presenter.h"
|
||||
#include "brightray/browser/mac/notification_presenter_mac.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
AtomNotificationCenter* del = [[AtomNotificationCenter alloc] init];
|
||||
bool set_del_ = false;
|
||||
brightray::NotificationPresenterMac* presenter;
|
||||
|
||||
void Notification::Show() {
|
||||
base::scoped_nsobject<NSUserNotification> notification_ = native_notifications_[id_];
|
||||
[NSUserNotificationCenter.defaultUserNotificationCenter
|
||||
deliverNotification:notification_];
|
||||
OnShown();
|
||||
}
|
||||
|
||||
void Notification::OnInitialProps() {
|
||||
if (!set_del_) {
|
||||
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:del];
|
||||
set_del_ = true;
|
||||
SkBitmap image = *(new SkBitmap);
|
||||
if (has_icon_) {
|
||||
image = *(icon_.ToSkBitmap());
|
||||
}
|
||||
|
||||
base::scoped_nsobject<NSUserNotification> notification_;
|
||||
notification_.reset([[NSUserNotification alloc] init]);
|
||||
std::unique_ptr<AtomNotificationDelegateAdapter> adapter(
|
||||
new AtomNotificationDelegateAdapter(this));
|
||||
auto notif = presenter->CreateNotification(adapter.get());
|
||||
if (notif) {
|
||||
ignore_result(adapter.release()); // it will release itself automatically.
|
||||
GURL nullUrl = *(new GURL);
|
||||
notif->Show(title_, body_, "", nullUrl, image, silent_, has_reply_, reply_placeholder_);
|
||||
}
|
||||
}
|
||||
|
||||
native_notifications_[id_] = notification_;
|
||||
|
||||
NotifyPropsUpdated();
|
||||
bool initialized_ = false;
|
||||
void Notification::OnInitialProps() {
|
||||
if (!initialized_) {
|
||||
presenter = new brightray::NotificationPresenterMac;
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Notification::NotifyPropsUpdated() {
|
||||
base::scoped_nsobject<NSUserNotification> notification_ = native_notifications_[id_];
|
||||
|
||||
[notification_ setTitle:base::SysUTF16ToNSString(title_)];
|
||||
[notification_ setInformativeText:base::SysUTF16ToNSString(body_)];
|
||||
|
||||
NSDictionary * userInfo = [NSMutableDictionary dictionary];
|
||||
[userInfo setValue:[NSNumber numberWithInt:id_] forKey:@"id"];
|
||||
[notification_ setUserInfo:userInfo];
|
||||
|
||||
if ([notification_ respondsToSelector:@selector(setContentImage:)] && has_icon_) {
|
||||
[notification_ setContentImage:icon_.AsNSImage()];
|
||||
}
|
||||
|
||||
if (has_reply_) {
|
||||
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder_)];
|
||||
[notification_ setHasReplyButton:true];
|
||||
}
|
||||
|
||||
if (silent_) {
|
||||
[notification_ setSoundName:nil];
|
||||
} else {
|
||||
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue