cocoa_notification.mm 5.5 KB

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