cocoa_notification.mm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/notifications/mac/cocoa_notification.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "base/mac/mac_util.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "shell/browser/notifications/notification_delegate.h"
  12. #include "shell/browser/notifications/notification_presenter.h"
  13. #include "skia/ext/skia_utils_mac.h"
  14. namespace electron {
  15. CocoaNotification::CocoaNotification(NotificationDelegate* delegate,
  16. NotificationPresenter* presenter)
  17. : Notification(delegate, presenter) {}
  18. CocoaNotification::~CocoaNotification() {
  19. if (notification_)
  20. [NSUserNotificationCenter.defaultUserNotificationCenter
  21. removeDeliveredNotification:notification_];
  22. }
  23. void CocoaNotification::Show(const NotificationOptions& options) {
  24. notification_ = [[NSUserNotification alloc] init];
  25. NSString* identifier =
  26. [NSString stringWithFormat:@"%@:notification:%@",
  27. [[NSBundle mainBundle] bundleIdentifier],
  28. [[NSUUID UUID] UUIDString]];
  29. [notification_ setTitle:base::SysUTF16ToNSString(options.title)];
  30. [notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
  31. [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
  32. [notification_ setIdentifier:identifier];
  33. if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
  34. LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")";
  35. }
  36. if (!options.icon.drawsNothing()) {
  37. NSImage* image = skia::SkBitmapToNSImage(options.icon);
  38. [notification_ setContentImage:image];
  39. }
  40. if (options.silent) {
  41. [notification_ setSoundName:nil];
  42. } else if (options.sound.empty()) {
  43. [notification_ setSoundName:NSUserNotificationDefaultSoundName];
  44. } else {
  45. [notification_ setSoundName:base::SysUTF16ToNSString(options.sound)];
  46. }
  47. if (options.has_reply) {
  48. [notification_ setHasReplyButton:true];
  49. [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(
  50. options.reply_placeholder)];
  51. }
  52. // We need to explicitly set this to false if there are no
  53. // actions, otherwise a Show button will appear by default.
  54. if (options.actions.size() == 0)
  55. [notification_ setHasActionButton:false];
  56. int i = 0;
  57. action_index_ = UINT_MAX;
  58. NSMutableArray* additionalActions = [[NSMutableArray alloc] init];
  59. for (const auto& action : options.actions) {
  60. if (action.type == u"button") {
  61. // If the notification has both a reply and actions,
  62. // the reply takes precedence and the actions all
  63. // become additional actions.
  64. if (!options.has_reply && action_index_ == UINT_MAX) {
  65. // First button observed is the displayed action
  66. [notification_
  67. 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 =
  72. [NSString stringWithFormat:@"%@Action%d", identifier, i];
  73. NSUserNotificationAction* notificationAction = [NSUserNotificationAction
  74. actionWithIdentifier:actionIdentifier
  75. title:base::SysUTF16ToNSString(action.text)];
  76. [additionalActions addObject:notificationAction];
  77. additional_action_indices_.insert(
  78. std::make_pair(base::SysNSStringToUTF8(actionIdentifier), i));
  79. }
  80. }
  81. i++;
  82. }
  83. if ([additionalActions count] > 0) {
  84. [notification_ setAdditionalActions:additionalActions];
  85. }
  86. if (!options.close_button_text.empty()) {
  87. [notification_ setOtherButtonTitle:base::SysUTF16ToNSString(
  88. options.close_button_text)];
  89. }
  90. [NSUserNotificationCenter.defaultUserNotificationCenter
  91. deliverNotification:notification_];
  92. }
  93. void CocoaNotification::Dismiss() {
  94. if (notification_)
  95. [NSUserNotificationCenter.defaultUserNotificationCenter
  96. removeDeliveredNotification:notification_];
  97. NotificationDismissed();
  98. notification_ = nil;
  99. }
  100. void CocoaNotification::NotificationDisplayed() {
  101. if (delegate())
  102. delegate()->NotificationDisplayed();
  103. this->LogAction("displayed");
  104. }
  105. void CocoaNotification::NotificationReplied(const std::string& reply) {
  106. if (delegate())
  107. delegate()->NotificationReplied(reply);
  108. this->LogAction("replied to");
  109. }
  110. void CocoaNotification::NotificationActivated() {
  111. if (delegate())
  112. delegate()->NotificationAction(action_index_);
  113. this->LogAction("button clicked");
  114. }
  115. void CocoaNotification::NotificationActivated(
  116. NSUserNotificationAction* action) {
  117. if (delegate()) {
  118. unsigned index = action_index_;
  119. std::string identifier = base::SysNSStringToUTF8(action.identifier);
  120. for (const auto& it : additional_action_indices_) {
  121. if (it.first == identifier) {
  122. index = it.second;
  123. break;
  124. }
  125. }
  126. delegate()->NotificationAction(index);
  127. }
  128. this->LogAction("button clicked");
  129. }
  130. void CocoaNotification::NotificationDismissed() {
  131. if (delegate())
  132. delegate()->NotificationClosed();
  133. this->LogAction("dismissed");
  134. }
  135. void CocoaNotification::LogAction(const char* action) {
  136. if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) {
  137. NSString* identifier = [notification_ valueForKey:@"identifier"];
  138. DCHECK(identifier);
  139. LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String]
  140. << ")";
  141. }
  142. }
  143. } // namespace electron