message_box_mac.mm 7.1 KB

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