Browse Source

chore: replace usage of deprecated beginSheetModalForWindow API (#16994)

Samuel Attard 6 years ago
parent
commit
e01c3615c4
2 changed files with 14 additions and 65 deletions
  1. 13 64
      atom/browser/ui/message_box_mac.mm
  2. 1 1
      docs/api/dialog.md

+ 13 - 64
atom/browser/ui/message_box_mac.mm

@@ -13,43 +13,6 @@
 #include "skia/ext/skia_utils_mac.h"
 #include "ui/gfx/image/image_skia.h"
 
-@interface ModalDelegate : NSObject {
- @private
-  atom::MessageBoxCallback callback_;
-  NSAlert* alert_;
-  bool callEndModal_;
-}
-- (id)initWithCallback:(const atom::MessageBoxCallback&)callback
-              andAlert:(NSAlert*)alert
-          callEndModal:(bool)flag;
-@end
-
-@implementation ModalDelegate
-
-- (id)initWithCallback:(const atom::MessageBoxCallback&)callback
-              andAlert:(NSAlert*)alert
-          callEndModal:(bool)flag {
-  if ((self = [super init])) {
-    callback_ = callback;
-    alert_ = alert;
-    callEndModal_ = flag;
-  }
-  return self;
-}
-
-- (void)alertDidEnd:(NSAlert*)alert
-         returnCode:(NSInteger)returnCode
-        contextInfo:(void*)contextInfo {
-  callback_.Run(returnCode, alert.suppressionButton.state == NSOnState);
-  [alert_ release];
-  [self release];
-
-  if (callEndModal_)
-    [NSApp stopModal];
-}
-
-@end
-
 namespace atom {
 
 namespace {
@@ -125,10 +88,6 @@ NSAlert* CreateNSAlert(NativeWindow* parent_window,
   return alert;
 }
 
-void SetReturnCode(int* ret_code, int result, bool checkbox_checked) {
-  *ret_code = result;
-}
-
 }  // namespace
 
 int ShowMessageBox(NativeWindow* parent_window,
@@ -150,20 +109,14 @@ int ShowMessageBox(NativeWindow* parent_window,
   if (!parent_window)
     return [[alert autorelease] runModal];
 
-  int ret_code = -1;
-  ModalDelegate* delegate = [[ModalDelegate alloc]
-      initWithCallback:base::Bind(&SetReturnCode, &ret_code)
-              andAlert:alert
-          callEndModal:true];
+  __block int ret_code = -1;
 
   NSWindow* window = parent_window->GetNativeWindow().GetNativeNSWindow();
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
   [alert beginSheetModalForWindow:window
-                    modalDelegate:delegate
-                   didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
-                      contextInfo:nil];
-#pragma clang diagnostic pop
+                completionHandler:^(NSModalResponse response) {
+                  ret_code = response;
+                  [NSApp stopModal];
+                }];
 
   [NSApp runModalForWindow:window];
   return ret_code;
@@ -192,21 +145,17 @@ void ShowMessageBox(NativeWindow* parent_window,
     int ret = [[alert autorelease] runModal];
     callback.Run(ret, alert.suppressionButton.state == NSOnState);
   } else {
-    ModalDelegate* delegate = [[ModalDelegate alloc] initWithCallback:callback
-                                                             andAlert:alert
-                                                         callEndModal:false];
-
     NSWindow* window =
         parent_window ? parent_window->GetNativeWindow().GetNativeNSWindow()
                       : nil;
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-    [alert
-        beginSheetModalForWindow:window
-                   modalDelegate:delegate
-                  didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
-                     contextInfo:nil];
-#pragma clang diagnostic pop
+    // Duplicate the callback object here since c is a reference and gcd would
+    // only store the pointer, by duplication we can force gcd to store a copy.
+    __block MessageBoxCallback callback_ = callback;
+    [alert beginSheetModalForWindow:window
+                  completionHandler:^(NSModalResponse response) {
+                    callback_.Run(response,
+                                  alert.suppressionButton.state == NSOnState);
+                  }];
   }
 }
 

+ 1 - 1
docs/api/dialog.md

@@ -171,7 +171,7 @@ It returns the index of the clicked button.
 
 The `browserWindow` argument allows the dialog to attach itself to a parent window, making it modal.
 
-If a `callback` is passed, the dialog will not block the process. The API call
+If the `callback` and `browserWindow` arguments are passed, the dialog will not block the process. The API call
 will be asynchronous and the result will be passed via `callback(response)`.
 
 ### `dialog.showErrorBox(title, content)`