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

@ -22,33 +22,58 @@
namespace {
void ShowMessageBox(int 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 std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
atom::NativeWindow* window,
mate::Arguments* args) {
v8::Local<v8::Value> peek = args->PeekNext();
atom::MessageBoxCallback callback;
if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(), peek,
&callback)) {
atom::ShowMessageBox(window, static_cast<atom::MessageBoxType>(type),
buttons, default_id, cancel_id, options, title,
message, detail, checkbox_label, checkbox_checked,
icon, callback);
} else {
int chosen = atom::ShowMessageBox(
window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
cancel_id, options, title, message, detail, icon);
args->Return(chosen);
}
int ShowMessageBoxSync(int 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 std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
atom::NativeWindow* window) {
return atom::ShowMessageBoxSync(
window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
cancel_id, options, title, message, detail, icon);
}
void ResolvePromiseObject(atom::util::Promise promise,
int result,
bool checkbox_checked) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
dict.Set("response", result);
dict.Set("checkboxChecked", checkbox_checked);
promise.Resolve(dict.GetHandle());
}
v8::Local<v8::Promise> ShowMessageBox(int 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 std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
atom::NativeWindow* window,
mate::Arguments* args) {
v8::Isolate* isolate = args->isolate();
atom::util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
atom::ShowMessageBox(
window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
cancel_id, options, title, message, detail, checkbox_label,
checkbox_checked, icon,
base::BindOnce(&ResolvePromiseObject, std::move(promise)));
return handle;
}
void ShowOpenDialogSync(const file_dialog::DialogSettings& settings,
@ -89,6 +114,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("showMessageBoxSync", &ShowMessageBoxSync);
dict.SetMethod("showMessageBox", &ShowMessageBox);
dict.SetMethod("showErrorBox", &atom::ShowErrorBox);
dict.SetMethod("showOpenDialogSync", &ShowOpenDialogSync);

View file

@ -32,19 +32,19 @@ enum MessageBoxOptions {
MESSAGE_BOX_NO_LINK = 1 << 0,
};
typedef base::Callback<void(int code, bool checkbox_checked)>
MessageBoxCallback;
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);
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);
typedef base::OnceCallback<void(int code, bool checkbox_checked)>
MessageBoxCallback;
void ShowMessageBox(NativeWindow* parent_window,
MessageBoxType type,
@ -58,7 +58,7 @@ void ShowMessageBox(NativeWindow* parent_window,
const std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback);
MessageBoxCallback callback);
// Like ShowMessageBox with simplest settings, but safe to call at very early
// stage of application.

View file

@ -148,14 +148,12 @@ class GtkMessageBox : public NativeWindowObserver {
int RunSynchronous() {
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
if (response < 0)
return cancel_id_;
else
return response;
return (response < 0) ? cancel_id_ : response;
}
void RunAsynchronous(const MessageBoxCallback& callback) {
callback_ = callback;
void RunAsynchronous(MessageBoxCallback callback) {
callback_ = std::move(callback);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseDialogThunk),
@ -190,9 +188,9 @@ void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
gtk_widget_hide(dialog_);
if (response < 0)
callback_.Run(cancel_id_, checkbox_checked_);
std::move(callback_).Run(cancel_id_, checkbox_checked_);
else
callback_.Run(response, checkbox_checked_);
std::move(callback_).Run(response, checkbox_checked_);
delete this;
}
@ -202,16 +200,16 @@ void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
} // namespace
int ShowMessageBox(NativeWindow* parent,
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,
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) {
return GtkMessageBox(parent, type, buttons, default_id, cancel_id, title,
message, detail, "", false, icon)
.RunSynchronous();
@ -229,10 +227,10 @@ void ShowMessageBox(NativeWindow* parent,
const std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
MessageBoxCallback callback) {
(new GtkMessageBox(parent, type, buttons, default_id, cancel_id, title,
message, detail, checkbox_label, checkbox_checked, icon))
->RunAsynchronous(callback);
->RunAsynchronous(std::move(callback));
}
void ShowErrorBox(const base::string16& title, const base::string16& content) {

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);
}];
}
}

View file

@ -14,7 +14,6 @@
#include "atom/browser/browser.h"
#include "atom/browser/native_window_views.h"
#include "atom/browser/unresponsive_suppressor.h"
#include "base/callback.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
@ -220,28 +219,29 @@ void RunMessageBoxInNewThread(base::Thread* thread,
const std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
MessageBoxCallback callback) {
int result = ShowTaskDialogUTF8(parent, type, buttons, default_id, cancel_id,
options, title, message, detail,
checkbox_label, &checkbox_checked, icon);
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
base::Bind(callback, result, checkbox_checked));
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(std::move(callback), result, checkbox_checked));
content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
thread);
}
} // namespace
int ShowMessageBox(NativeWindow* parent,
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,
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) {
atom::UnresponsiveSuppressor suppressor;
return ShowTaskDialogUTF8(parent, type, buttons, default_id, cancel_id,
options, title, message, detail, "", nullptr, icon);
@ -259,22 +259,22 @@ void ShowMessageBox(NativeWindow* parent,
const std::string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
MessageBoxCallback callback) {
auto thread =
std::make_unique<base::Thread>(ATOM_PRODUCT_NAME "MessageBoxThread");
thread->init_com_with_mta(false);
if (!thread->Start()) {
callback.Run(cancel_id, checkbox_checked);
std::move(callback).Run(cancel_id, checkbox_checked);
return;
}
base::Thread* unretained = thread.release();
unretained->task_runner()->PostTask(
FROM_HERE,
base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
parent, type, buttons, default_id, cancel_id, options, title,
message, detail, checkbox_label, checkbox_checked, icon,
callback));
base::BindOnce(&RunMessageBoxInNewThread, base::Unretained(unretained),
parent, type, buttons, default_id, cancel_id, options,
title, message, detail, checkbox_label, checkbox_checked,
icon, std::move(callback)));
}
void ShowErrorBox(const base::string16& title, const base::string16& content) {