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

This commit is contained in:
Samuel Attard 2019-02-27 10:23:17 -08:00 committed by GitHub
parent 1bbb47be5b
commit e01c3615c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 65 deletions

View file

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

View file

@ -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. 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)`. will be asynchronous and the result will be passed via `callback(response)`.
### `dialog.showErrorBox(title, content)` ### `dialog.showErrorBox(title, content)`