|
29 | 29 | void CocoaNotification::Show(const NotificationOptions& options) { |
30 | 30 | notification_.reset([[NSUserNotification alloc] init]); |
31 | 31 |
|
32 | | - NSString* identifier = [NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_]; |
| 32 | + NSString* identifier = [NSString stringWithFormat:@"ElectronNotification%d", g_identifier_++]; |
33 | 33 |
|
34 | 34 | [notification_ setTitle:base::SysUTF16ToNSString(options.title)]; |
35 | 35 | [notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)]; |
36 | 36 | [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)]; |
37 | 37 | [notification_ setIdentifier:identifier]; |
38 | | - g_identifier_++; |
39 | 38 |
|
40 | 39 | if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { |
41 | 40 | LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")"; |
42 | 41 | } |
43 | 42 |
|
44 | | - if ([notification_ respondsToSelector:@selector(setContentImage:)] && |
45 | | - !options.icon.drawsNothing()) { |
| 43 | + if (!options.icon.drawsNothing()) { |
46 | 44 | NSImage* image = skia::SkBitmapToNSImageWithColorSpace( |
47 | 45 | options.icon, base::mac::GetGenericRGBColorSpace()); |
48 | 46 | [notification_ setContentImage:image]; |
49 | 47 | } |
50 | 48 |
|
51 | 49 | if (options.silent) { |
52 | 50 | [notification_ setSoundName:nil]; |
53 | | - } else if (options.sound != nil) { |
54 | | - [notification_ setSoundName:base::SysUTF16ToNSString(options.sound)]; |
55 | | - } else { |
| 51 | + } else if (options.sound.empty()) { |
56 | 52 | [notification_ setSoundName:NSUserNotificationDefaultSoundName]; |
| 53 | + } else { |
| 54 | + [notification_ setSoundName:base::SysUTF16ToNSString(options.sound)]; |
57 | 55 | } |
58 | 56 |
|
59 | 57 | [notification_ setHasActionButton:false]; |
60 | 58 |
|
61 | 59 | int i = 0; |
| 60 | + action_index_ = UINT_MAX; |
| 61 | + NSMutableArray* additionalActions = [[[NSMutableArray alloc] init] autorelease]; |
62 | 62 | for (const auto& action : options.actions) { |
63 | 63 | if (action.type == base::ASCIIToUTF16("button")) { |
64 | | - [notification_ setHasActionButton:true]; |
65 | | - [notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.text)]; |
66 | | - action_index_ = i; |
| 64 | + if (action_index_ == UINT_MAX) { |
| 65 | + // First button observed is the displayed action |
| 66 | + [notification_ setHasActionButton:true]; |
| 67 | + [notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.text)]; |
| 68 | + action_index_ = i; |
| 69 | + } else { |
| 70 | + // All of the rest are appended to the list of additional actions |
| 71 | + NSString* actionIdentifier = [NSString stringWithFormat:@"%@Action%d", identifier, i]; |
| 72 | + NSUserNotificationAction* notificationAction = |
| 73 | + [NSUserNotificationAction actionWithIdentifier:actionIdentifier |
| 74 | + title:base::SysUTF16ToNSString(action.text)]; |
| 75 | + [additionalActions addObject:notificationAction]; |
| 76 | + additional_action_indices_.insert(std::make_pair(base::SysNSStringToUTF8(actionIdentifier), i)); |
| 77 | + } |
67 | 78 | } |
68 | 79 | i++; |
69 | 80 | } |
| 81 | + if ([additionalActions count] > 0 && |
| 82 | + [notification_ respondsToSelector:@selector(setAdditionalActions:)]) { |
| 83 | + [notification_ setAdditionalActions:additionalActions]; // Requires macOS 10.10 |
| 84 | + } |
70 | 85 |
|
71 | 86 | if (options.has_reply) { |
72 | 87 | [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)]; |
|
101 | 116 | this->LogAction("replied to"); |
102 | 117 | } |
103 | 118 |
|
104 | | -void CocoaNotification::NotificationButtonClicked() { |
| 119 | +void CocoaNotification::NotificationActivated() { |
105 | 120 | if (delegate()) |
106 | 121 | delegate()->NotificationAction(action_index_); |
107 | 122 |
|
108 | 123 | this->LogAction("button clicked"); |
109 | 124 | } |
110 | 125 |
|
| 126 | +void CocoaNotification::NotificationActivated(NSUserNotificationAction* action) { |
| 127 | + if (delegate()) { |
| 128 | + unsigned index = action_index_; |
| 129 | + std::string identifier = base::SysNSStringToUTF8(action.identifier); |
| 130 | + for (const auto& it : additional_action_indices_) { |
| 131 | + if (it.first == identifier) { |
| 132 | + index = it.second; |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + delegate()->NotificationAction(index); |
| 138 | + } |
| 139 | + |
| 140 | + this->LogAction("button clicked"); |
| 141 | +} |
| 142 | + |
111 | 143 | void CocoaNotification::LogAction(const char* action) { |
112 | 144 | if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { |
113 | 145 | NSString* identifier = [notification_ valueForKey:@"identifier"]; |
|
0 commit comments