Skip to content

Commit

Permalink
chore: replace usage of deprecated beginSheetModalForWindow API (#17162)
Browse files Browse the repository at this point in the history
  • Loading branch information
trop[bot] authored and codebytere committed Feb 28, 2019
1 parent 47f005f commit 7e3ff0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 65 deletions.
77 changes: 13 additions & 64 deletions atom/browser/ui/message_box_mac.mm
Expand Up @@ -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 {
Expand Down Expand Up @@ -125,10 +88,6 @@ - (void)alertDidEnd:(NSAlert*)alert
return alert;
}

void SetReturnCode(int* ret_code, int result, bool checkbox_checked) {
*ret_code = result;
}

} // namespace

int ShowMessageBox(NativeWindow* parent_window,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}];
}
}

Expand Down
2 changes: 1 addition & 1 deletion docs/api/dialog.md
Expand Up @@ -168,7 +168,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)`
Expand Down

0 comments on commit 7e3ff0f

Please sign in to comment.