feat: promisify dialog.showMessageBox() (#17298)

* feat: promisify dialog.showMessageBox()

* address feedback from review
This commit is contained in:
Shelley Vohr 2019-03-12 11:06:59 -07:00 committed by GitHub
parent ea6a926494
commit 8991c0056e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 234 additions and 191 deletions

View file

@ -90,16 +90,16 @@ NSAlert* CreateNSAlert(NativeWindow* parent_window,
} // namespace
int ShowMessageBox(NativeWindow* parent_window,
MessageBoxType type,
const std::vector<std::string>& buttons,
int default_id,
int cancel_id,
int options,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon) {
int ShowMessageBoxSync(NativeWindow* parent_window,
MessageBoxType type,
const std::vector<std::string>& buttons,
int default_id,
int cancel_id,
int options,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon) {
NSAlert* alert =
CreateNSAlert(parent_window, type, buttons, default_id, cancel_id, title,
message, detail, "", false, icon);
@ -134,7 +134,7 @@ void ShowMessageBox(NativeWindow* parent_window,
const std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
MessageBoxCallback callback) {
NSAlert* alert =
CreateNSAlert(parent_window, type, buttons, default_id, cancel_id, title,
message, detail, checkbox_label, checkbox_checked, icon);
@ -143,18 +143,20 @@ void ShowMessageBox(NativeWindow* parent_window,
// window to wait for.
if (!parent_window) {
int ret = [[alert autorelease] runModal];
callback.Run(ret, alert.suppressionButton.state == NSOnState);
std::move(callback).Run(ret, alert.suppressionButton.state == NSOnState);
} else {
NSWindow* window =
parent_window ? parent_window->GetNativeWindow().GetNativeNSWindow()
: nil;
// 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;
__block MessageBoxCallback callback_ = std::move(callback);
[alert beginSheetModalForWindow:window
completionHandler:^(NSModalResponse response) {
callback_.Run(response,
alert.suppressionButton.state == NSOnState);
std::move(callback_).Run(
response, alert.suppressionButton.state == NSOnState);
}];
}
}