🔧 Allow notifications debugging (macOS)

This commit is contained in:
Felix Rieseberg 2017-12-12 16:09:11 -08:00
parent 5720a8e7d5
commit ffd85bfaac
2 changed files with 26 additions and 1 deletions

View file

@ -32,6 +32,8 @@ class CocoaNotification : public Notification {
NSUserNotification* notification() const { return notification_; }
private:
void LogAction(const char* action);
base::scoped_nsobject<NSUserNotification> notification_;
int action_index_;

View file

@ -28,12 +28,19 @@ CocoaNotification::~CocoaNotification() {
void CocoaNotification::Show(const NotificationOptions& options) {
notification_.reset([[NSUserNotification alloc] init]);
NSString* identifier = [NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_];
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
[notification_ setIdentifier:[NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_]];
[notification_ setIdentifier:identifier];
g_identifier_++;
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
NSLog(@"%s (%@)", "Cocoa notification created", identifier);
}
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
!options.icon.drawsNothing()) {
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
@ -74,22 +81,38 @@ void CocoaNotification::Dismiss() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
NotificationDismissed();
this->LogAction("dismissed");
}
void CocoaNotification::NotificationDisplayed() {
if (delegate())
delegate()->NotificationDisplayed();
this->LogAction("displayed");
}
void CocoaNotification::NotificationReplied(const std::string& reply) {
if (delegate())
delegate()->NotificationReplied(reply);
this->LogAction("replied to");
}
void CocoaNotification::NotificationButtonClicked() {
if (delegate())
delegate()->NotificationAction(action_index_);
this->LogAction("clicked");
}
void CocoaNotification::LogAction(const char* action) {
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
NSString* identifier = [notification_ valueForKey:@"identifier"];
NSLog(@"%s %s (%@)", "Cocoa notification", action, identifier);
}
}
} // namespace brightray