message_box_mac.mm 6.2 KB

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