message_box_mac.mm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. @interface ModalDelegate : NSObject {
  12. @private
  13. atom::MessageBoxCallback callback_;
  14. NSAlert* alert_;
  15. bool callEndModal_;
  16. }
  17. - (id)initWithCallback:(const atom::MessageBoxCallback&)callback
  18. andAlert:(NSAlert*)alert
  19. callEndModal:(bool)flag;
  20. @end
  21. @implementation ModalDelegate
  22. - (id)initWithCallback:(const atom::MessageBoxCallback&)callback
  23. andAlert:(NSAlert*)alert
  24. callEndModal:(bool)flag {
  25. if ((self = [super init])) {
  26. callback_ = callback;
  27. alert_ = alert;
  28. callEndModal_ = flag;
  29. }
  30. return self;
  31. }
  32. - (void)alertDidEnd:(NSAlert*)alert
  33. returnCode:(NSInteger)returnCode
  34. contextInfo:(void*)contextInfo {
  35. callback_.Run(returnCode, alert.suppressionButton.state == NSOnState);
  36. [alert_ release];
  37. [self release];
  38. if (callEndModal_)
  39. [NSApp stopModal];
  40. }
  41. @end
  42. namespace atom {
  43. namespace {
  44. NSAlert* CreateNSAlert(NativeWindow* parent_window,
  45. MessageBoxType type,
  46. const std::vector<std::string>& buttons,
  47. int default_id,
  48. int cancel_id,
  49. const std::string& title,
  50. const std::string& message,
  51. const std::string& detail,
  52. const std::string& checkbox_label,
  53. bool checkbox_checked,
  54. const gfx::ImageSkia& icon) {
  55. // Ignore the title; it's the window title on other platforms and ignorable.
  56. NSAlert* alert = [[NSAlert alloc] init];
  57. [alert setMessageText:base::SysUTF8ToNSString(message)];
  58. [alert setInformativeText:base::SysUTF8ToNSString(detail)];
  59. switch (type) {
  60. case MESSAGE_BOX_TYPE_INFORMATION:
  61. alert.alertStyle = NSInformationalAlertStyle;
  62. break;
  63. case MESSAGE_BOX_TYPE_WARNING:
  64. case MESSAGE_BOX_TYPE_ERROR:
  65. // NSWarningAlertStyle shows the app icon while NSCriticalAlertStyle
  66. // shows a warning icon with an app icon badge. Since there is no
  67. // error variant, lets just use NSCriticalAlertStyle.
  68. alert.alertStyle = NSCriticalAlertStyle;
  69. break;
  70. default:
  71. break;
  72. }
  73. for (size_t i = 0; i < buttons.size(); ++i) {
  74. NSString* title = base::SysUTF8ToNSString(buttons[i]);
  75. // An empty title causes crash on macOS.
  76. if (buttons[i].empty())
  77. title = @"(empty)";
  78. NSButton* button = [alert addButtonWithTitle:title];
  79. [button setTag:i];
  80. }
  81. NSArray* ns_buttons = [alert buttons];
  82. int button_count = static_cast<int>([ns_buttons count]);
  83. if (default_id >= 0 && default_id < button_count) {
  84. // Focus the button at default_id if the user opted to do so.
  85. // The first button added gets set as the default selected.
  86. // So remove that default, and make the requested button the default.
  87. [[ns_buttons objectAtIndex:0] setKeyEquivalent:@""];
  88. [[ns_buttons objectAtIndex:default_id] setKeyEquivalent:@"\r"];
  89. }
  90. // Bind cancel id button to escape key if there is more than one button
  91. if (button_count > 1 && cancel_id >= 0 && cancel_id < button_count) {
  92. [[ns_buttons objectAtIndex:cancel_id] setKeyEquivalent:@"\e"];
  93. }
  94. if (!checkbox_label.empty()) {
  95. alert.showsSuppressionButton = YES;
  96. alert.suppressionButton.title = base::SysUTF8ToNSString(checkbox_label);
  97. alert.suppressionButton.state = checkbox_checked ? NSOnState : NSOffState;
  98. }
  99. if (!icon.isNull()) {
  100. NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
  101. *icon.bitmap(), base::mac::GetGenericRGBColorSpace());
  102. [alert setIcon:image];
  103. }
  104. return alert;
  105. }
  106. void SetReturnCode(int* ret_code, int result, bool checkbox_checked) {
  107. *ret_code = result;
  108. }
  109. } // namespace
  110. int ShowMessageBox(NativeWindow* parent_window,
  111. MessageBoxType type,
  112. const std::vector<std::string>& buttons,
  113. int default_id,
  114. int cancel_id,
  115. int options,
  116. const std::string& title,
  117. const std::string& message,
  118. const std::string& detail,
  119. const gfx::ImageSkia& icon) {
  120. NSAlert* alert = CreateNSAlert(parent_window, type, buttons, default_id,
  121. cancel_id, title, message, detail, "", false,
  122. icon);
  123. // Use runModal for synchronous alert without parent, since we don't have a
  124. // window to wait for.
  125. if (!parent_window || !parent_window->GetNativeWindow() ||
  126. parent_window->is_offscreen_dummy())
  127. return [[alert autorelease] runModal];
  128. int ret_code = -1;
  129. ModalDelegate* delegate = [[ModalDelegate alloc]
  130. initWithCallback:base::Bind(&SetReturnCode, &ret_code)
  131. andAlert:alert
  132. callEndModal:true];
  133. NSWindow* window = parent_window->GetNativeWindow();
  134. [alert beginSheetModalForWindow:window
  135. modalDelegate:delegate
  136. didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
  137. contextInfo:nil];
  138. [NSApp runModalForWindow:window];
  139. return ret_code;
  140. }
  141. void ShowMessageBox(NativeWindow* parent_window,
  142. MessageBoxType type,
  143. const std::vector<std::string>& buttons,
  144. int default_id,
  145. int cancel_id,
  146. int options,
  147. const std::string& title,
  148. const std::string& message,
  149. const std::string& detail,
  150. const std::string& checkbox_label,
  151. bool checkbox_checked,
  152. const gfx::ImageSkia& icon,
  153. const MessageBoxCallback& callback) {
  154. NSAlert* alert =
  155. CreateNSAlert(parent_window, type, buttons, default_id, cancel_id, title,
  156. message, detail, checkbox_label, checkbox_checked, icon);
  157. // Use runModal for synchronous alert without parent, since we don't have a
  158. // window to wait for.
  159. if (!parent_window || !parent_window->GetNativeWindow() ||
  160. parent_window->is_offscreen_dummy()) {
  161. int ret = [[alert autorelease] runModal];
  162. callback.Run(ret, alert.suppressionButton.state == NSOnState);
  163. } else {
  164. ModalDelegate* delegate = [[ModalDelegate alloc] initWithCallback:callback
  165. andAlert:alert
  166. callEndModal:false];
  167. NSWindow* window = parent_window ? parent_window->GetNativeWindow() : nil;
  168. [alert beginSheetModalForWindow:window
  169. modalDelegate:delegate
  170. didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
  171. contextInfo:nil];
  172. }
  173. }
  174. void ShowErrorBox(const base::string16& title, const base::string16& content) {
  175. NSAlert* alert = [[NSAlert alloc] init];
  176. [alert setMessageText:base::SysUTF16ToNSString(title)];
  177. [alert setInformativeText:base::SysUTF16ToNSString(content)];
  178. [alert setAlertStyle:NSCriticalAlertStyle];
  179. [alert runModal];
  180. [alert release];
  181. }
  182. } // namespace atom