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

@ -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) {