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

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