message_box_mac.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2013 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/ui/message_box.h"
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #import <Cocoa/Cocoa.h>
  9. #include "base/callback.h"
  10. #include "base/mac/mac_util.h"
  11. #include "base/mac/scoped_nsobject.h"
  12. #include "base/strings/sys_string_conversions.h"
  13. #include "shell/browser/native_window.h"
  14. #include "skia/ext/skia_utils_mac.h"
  15. #include "ui/gfx/image/image_skia.h"
  16. namespace electron {
  17. MessageBoxSettings::MessageBoxSettings() = default;
  18. MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
  19. MessageBoxSettings::~MessageBoxSettings() = default;
  20. namespace {
  21. NSAlert* CreateNSAlert(const MessageBoxSettings& settings) {
  22. // Ignore the title; it's the window title on other platforms and ignorable.
  23. NSAlert* alert = [[NSAlert alloc] init];
  24. [alert setMessageText:base::SysUTF8ToNSString(settings.message)];
  25. [alert setInformativeText:base::SysUTF8ToNSString(settings.detail)];
  26. switch (settings.type) {
  27. case MessageBoxType::kInformation:
  28. alert.alertStyle = NSInformationalAlertStyle;
  29. break;
  30. case MessageBoxType::kWarning:
  31. case MessageBoxType::kError:
  32. // NSWarningAlertStyle shows the app icon while NSCriticalAlertStyle
  33. // shows a warning icon with an app icon badge. Since there is no
  34. // error variant, lets just use NSCriticalAlertStyle.
  35. alert.alertStyle = NSCriticalAlertStyle;
  36. break;
  37. default:
  38. break;
  39. }
  40. for (size_t i = 0; i < settings.buttons.size(); ++i) {
  41. NSString* title = base::SysUTF8ToNSString(settings.buttons[i]);
  42. // An empty title causes crash on macOS.
  43. if (settings.buttons[i].empty())
  44. title = @"(empty)";
  45. NSButton* button = [alert addButtonWithTitle:title];
  46. [button setTag:i];
  47. }
  48. NSArray* ns_buttons = [alert buttons];
  49. int button_count = static_cast<int>([ns_buttons count]);
  50. if (settings.default_id >= 0 && settings.default_id < button_count) {
  51. // Highlight the button at default_id
  52. [[ns_buttons objectAtIndex:settings.default_id] highlight:YES];
  53. // The first button added gets set as the default selected, so remove
  54. // that and set the button @ default_id to be default.
  55. [[ns_buttons objectAtIndex:0] setKeyEquivalent:@""];
  56. [[ns_buttons objectAtIndex:settings.default_id] setKeyEquivalent:@"\r"];
  57. }
  58. // Bind cancel id button to escape key if there is more than one button
  59. if (button_count > 1 && settings.cancel_id >= 0 &&
  60. settings.cancel_id < button_count) {
  61. [[ns_buttons objectAtIndex:settings.cancel_id] setKeyEquivalent:@"\e"];
  62. }
  63. if (!settings.checkbox_label.empty()) {
  64. alert.showsSuppressionButton = YES;
  65. alert.suppressionButton.title =
  66. base::SysUTF8ToNSString(settings.checkbox_label);
  67. alert.suppressionButton.state =
  68. settings.checkbox_checked ? NSOnState : NSOffState;
  69. }
  70. if (!settings.icon.isNull()) {
  71. NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
  72. *settings.icon.bitmap(), base::mac::GetGenericRGBColorSpace());
  73. [alert setIcon:image];
  74. }
  75. return alert;
  76. }
  77. } // namespace
  78. int ShowMessageBoxSync(const MessageBoxSettings& settings) {
  79. base::scoped_nsobject<NSAlert> alert(CreateNSAlert(settings));
  80. // Use runModal for synchronous alert without parent, since we don't have a
  81. // window to wait for. Also use it when window is provided but it is not
  82. // shown as it would be impossible to dismiss the alert if it is connected
  83. // to invisible window (see #22671).
  84. if (!settings.parent_window || !settings.parent_window->IsVisible())
  85. return [alert runModal];
  86. __block int ret_code = -1;
  87. NSWindow* window =
  88. settings.parent_window->GetNativeWindow().GetNativeNSWindow();
  89. [alert beginSheetModalForWindow:window
  90. completionHandler:^(NSModalResponse response) {
  91. ret_code = response;
  92. [NSApp stopModal];
  93. }];
  94. [NSApp runModalForWindow:window];
  95. return ret_code;
  96. }
  97. void ShowMessageBox(const MessageBoxSettings& settings,
  98. MessageBoxCallback callback) {
  99. NSAlert* alert = CreateNSAlert(settings);
  100. // Use runModal for synchronous alert without parent, since we don't have a
  101. // window to wait for.
  102. if (!settings.parent_window) {
  103. int ret = [[alert autorelease] runModal];
  104. std::move(callback).Run(ret, alert.suppressionButton.state == NSOnState);
  105. } else {
  106. NSWindow* window =
  107. settings.parent_window
  108. ? settings.parent_window->GetNativeWindow().GetNativeNSWindow()
  109. : nil;
  110. // Duplicate the callback object here since c is a reference and gcd would
  111. // only store the pointer, by duplication we can force gcd to store a copy.
  112. __block MessageBoxCallback callback_ = std::move(callback);
  113. [alert beginSheetModalForWindow:window
  114. completionHandler:^(NSModalResponse response) {
  115. std::move(callback_).Run(
  116. response, alert.suppressionButton.state == NSOnState);
  117. [alert release];
  118. }];
  119. }
  120. }
  121. void ShowErrorBox(const base::string16& title, const base::string16& content) {
  122. NSAlert* alert = [[NSAlert alloc] init];
  123. [alert setMessageText:base::SysUTF16ToNSString(title)];
  124. [alert setInformativeText:base::SysUTF16ToNSString(content)];
  125. [alert setAlertStyle:NSCriticalAlertStyle];
  126. [alert runModal];
  127. [alert release];
  128. }
  129. } // namespace electron