2015-07-07 07:02:27 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/message_box.h"
|
2018-02-10 01:29:32 +00:00
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
#include <map>
|
|
|
|
|
2015-07-07 07:02:27 +00:00
|
|
|
#include "base/callback.h"
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "base/containers/contains.h"
|
|
|
|
#include "base/no_destructor.h"
|
2015-07-07 07:45:13 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2015-07-07 07:02:27 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/native_window_observer.h"
|
|
|
|
#include "shell/browser/native_window_views.h"
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "shell/browser/ui/gtk_util.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/unresponsive_suppressor.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "ui/base/glib/glib_signal.h"
|
2018-04-10 06:23:16 +00:00
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2020-04-13 23:39:26 +00:00
|
|
|
#include "ui/gtk/gtk_util.h"
|
2015-07-07 07:02:27 +00:00
|
|
|
|
2020-10-20 18:24:52 +00:00
|
|
|
#if defined(USE_X11)
|
|
|
|
#include "ui/events/platform/x11/x11_event_source.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_OZONE) || defined(USE_X11)
|
|
|
|
#include "ui/base/ui_base_features.h"
|
|
|
|
#endif
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
#define ANSI_FOREGROUND_RED "\x1b[31m"
|
2015-07-07 07:02:27 +00:00
|
|
|
#define ANSI_FOREGROUND_BLACK "\x1b[30m"
|
2018-02-15 21:20:55 +00:00
|
|
|
#define ANSI_TEXT_BOLD "\x1b[1m"
|
|
|
|
#define ANSI_BACKGROUND_GRAY "\x1b[47m"
|
|
|
|
#define ANSI_RESET "\x1b[0m"
|
2015-07-07 07:02:27 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-07-07 07:02:27 +00:00
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
MessageBoxSettings::MessageBoxSettings() = default;
|
|
|
|
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
|
|
|
|
MessageBoxSettings::~MessageBoxSettings() = default;
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
// <ID, messageBox> map
|
|
|
|
std::map<int, GtkWidget*>& GetDialogsMap() {
|
|
|
|
static base::NoDestructor<std::map<int, GtkWidget*>> dialogs;
|
|
|
|
return *dialogs;
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:52:58 +00:00
|
|
|
class GtkMessageBox : public NativeWindowObserver {
|
2015-07-07 07:45:13 +00:00
|
|
|
public:
|
2019-06-14 15:26:25 +00:00
|
|
|
explicit GtkMessageBox(const MessageBoxSettings& settings)
|
2021-07-14 22:59:27 +00:00
|
|
|
: id_(settings.id),
|
|
|
|
cancel_id_(settings.cancel_id),
|
2019-06-14 15:26:25 +00:00
|
|
|
parent_(static_cast<NativeWindow*>(settings.parent_window)) {
|
2015-07-07 07:45:13 +00:00
|
|
|
// Create dialog.
|
2018-02-15 21:20:55 +00:00
|
|
|
dialog_ =
|
|
|
|
gtk_message_dialog_new(nullptr, // parent
|
|
|
|
static_cast<GtkDialogFlags>(0), // no flags
|
2019-06-14 15:26:25 +00:00
|
|
|
GetMessageType(settings.type), // type
|
2018-02-15 21:20:55 +00:00
|
|
|
GTK_BUTTONS_NONE, // no buttons
|
2019-06-14 15:26:25 +00:00
|
|
|
"%s", settings.message.c_str());
|
2021-07-14 22:59:27 +00:00
|
|
|
if (id_)
|
|
|
|
GetDialogsMap()[*id_] = dialog_;
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.detail.empty())
|
2018-02-15 21:20:55 +00:00
|
|
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_),
|
2019-06-14 15:26:25 +00:00
|
|
|
"%s", settings.detail.c_str());
|
|
|
|
if (!settings.title.empty())
|
|
|
|
gtk_window_set_title(GTK_WINDOW(dialog_), settings.title.c_str());
|
2015-07-07 07:45:13 +00:00
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.icon.isNull()) {
|
2018-10-23 17:44:47 +00:00
|
|
|
// No easy way to obtain this programmatically, but GTK+'s docs
|
|
|
|
// define GTK_ICON_SIZE_DIALOG to be 48 pixels
|
|
|
|
static constexpr int pixel_width = 48;
|
|
|
|
static constexpr int pixel_height = 48;
|
2019-06-14 15:26:25 +00:00
|
|
|
GdkPixbuf* pixbuf =
|
2019-10-28 22:12:35 +00:00
|
|
|
gtk_util::GdkPixbufFromSkBitmap(*settings.icon.bitmap());
|
2018-10-23 17:44:47 +00:00
|
|
|
GdkPixbuf* scaled_pixbuf = gdk_pixbuf_scale_simple(
|
|
|
|
pixbuf, pixel_width, pixel_height, GDK_INTERP_BILINEAR);
|
|
|
|
GtkWidget* w = gtk_image_new_from_pixbuf(scaled_pixbuf);
|
|
|
|
gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog_), w);
|
|
|
|
gtk_widget_show(w);
|
2018-11-27 21:42:02 +00:00
|
|
|
g_clear_pointer(&scaled_pixbuf, g_object_unref);
|
|
|
|
g_clear_pointer(&pixbuf, g_object_unref);
|
2018-10-23 17:44:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.checkbox_label.empty()) {
|
2017-02-06 15:35:36 +00:00
|
|
|
GtkWidget* message_area =
|
|
|
|
gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
|
|
|
|
GtkWidget* check_button =
|
2019-06-14 15:26:25 +00:00
|
|
|
gtk_check_button_new_with_label(settings.checkbox_label.c_str());
|
2017-02-06 15:35:36 +00:00
|
|
|
g_signal_connect(check_button, "toggled",
|
|
|
|
G_CALLBACK(OnCheckboxToggledThunk), this);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
|
2019-06-14 15:26:25 +00:00
|
|
|
settings.checkbox_checked);
|
2017-02-06 15:35:36 +00:00
|
|
|
gtk_container_add(GTK_CONTAINER(message_area), check_button);
|
2018-10-23 17:44:47 +00:00
|
|
|
gtk_widget_show(check_button);
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
// Add buttons.
|
2018-10-23 17:44:47 +00:00
|
|
|
GtkDialog* dialog = GTK_DIALOG(dialog_);
|
2020-12-09 23:28:56 +00:00
|
|
|
if (settings.buttons.size() == 0) {
|
|
|
|
gtk_dialog_add_button(dialog, TranslateToStock(0, "OK"), 0);
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < settings.buttons.size(); ++i) {
|
|
|
|
gtk_dialog_add_button(dialog, TranslateToStock(i, settings.buttons[i]),
|
|
|
|
i);
|
|
|
|
}
|
2015-07-07 07:45:13 +00:00
|
|
|
}
|
2019-06-14 15:26:25 +00:00
|
|
|
gtk_dialog_set_default_response(dialog, settings.default_id);
|
2015-07-07 07:45:13 +00:00
|
|
|
|
|
|
|
// Parent window.
|
2016-06-21 08:54:55 +00:00
|
|
|
if (parent_) {
|
2016-12-20 23:52:58 +00:00
|
|
|
parent_->AddObserver(this);
|
2017-04-17 08:17:02 +00:00
|
|
|
static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
|
2020-01-29 12:01:37 +00:00
|
|
|
gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
|
2016-06-21 08:54:55 +00:00
|
|
|
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
|
2015-07-07 07:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 19:12:45 +00:00
|
|
|
~GtkMessageBox() override {
|
2015-07-07 07:45:13 +00:00
|
|
|
gtk_widget_destroy(dialog_);
|
2016-12-20 23:52:58 +00:00
|
|
|
if (parent_) {
|
|
|
|
parent_->RemoveObserver(this);
|
2017-04-17 08:17:02 +00:00
|
|
|
static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
|
2016-12-20 23:52:58 +00:00
|
|
|
}
|
2015-07-07 07:45:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 08:52:53 +00:00
|
|
|
GtkMessageType GetMessageType(MessageBoxType type) {
|
|
|
|
switch (type) {
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kInformation:
|
2015-07-07 08:52:53 +00:00
|
|
|
return GTK_MESSAGE_INFO;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kWarning:
|
2015-07-07 08:52:53 +00:00
|
|
|
return GTK_MESSAGE_WARNING;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kQuestion:
|
2015-07-07 08:52:53 +00:00
|
|
|
return GTK_MESSAGE_QUESTION;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kError:
|
2015-07-07 08:52:53 +00:00
|
|
|
return GTK_MESSAGE_ERROR;
|
|
|
|
default:
|
|
|
|
return GTK_MESSAGE_OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
const char* TranslateToStock(int id, const std::string& text) {
|
2018-02-09 23:45:34 +00:00
|
|
|
const std::string lower = base::ToLowerASCII(text);
|
2015-07-07 10:33:11 +00:00
|
|
|
if (lower == "cancel")
|
2021-09-22 18:12:50 +00:00
|
|
|
return gtk_util::GetCancelLabel();
|
2018-02-09 23:45:34 +00:00
|
|
|
if (lower == "no")
|
2021-09-22 18:12:50 +00:00
|
|
|
return gtk_util::GetNoLabel();
|
2018-02-09 23:45:34 +00:00
|
|
|
if (lower == "ok")
|
2021-09-22 18:12:50 +00:00
|
|
|
return gtk_util::GetOkLabel();
|
2018-02-09 23:45:34 +00:00
|
|
|
if (lower == "yes")
|
2021-09-22 18:12:50 +00:00
|
|
|
return gtk_util::GetYesLabel();
|
2018-02-09 23:45:34 +00:00
|
|
|
return text.c_str();
|
2015-07-07 07:45:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 08:42:03 +00:00
|
|
|
void Show() {
|
2018-10-23 17:44:47 +00:00
|
|
|
gtk_widget_show(dialog_);
|
2020-10-20 18:24:52 +00:00
|
|
|
|
|
|
|
#if defined(USE_X11)
|
|
|
|
if (!features::IsUsingOzonePlatform()) {
|
|
|
|
// We need to call gtk_window_present after making the widgets visible to
|
|
|
|
// make sure window gets correctly raised and gets focus.
|
|
|
|
x11::Time time = ui::X11EventSource::GetInstance()->GetTimestamp();
|
|
|
|
gtk_window_present_with_time(GTK_WINDOW(dialog_),
|
|
|
|
static_cast<uint32_t>(time));
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-07 08:42:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 09:03:47 +00:00
|
|
|
int RunSynchronous() {
|
|
|
|
Show();
|
|
|
|
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
|
2019-03-12 18:06:59 +00:00
|
|
|
return (response < 0) ? cancel_id_ : response;
|
2015-07-07 09:03:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 18:06:59 +00:00
|
|
|
void RunAsynchronous(MessageBoxCallback callback) {
|
|
|
|
callback_ = std::move(callback);
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
g_signal_connect(dialog_, "delete-event",
|
|
|
|
G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
|
2018-02-15 21:20:55 +00:00
|
|
|
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseDialogThunk),
|
|
|
|
this);
|
2015-07-07 08:42:03 +00:00
|
|
|
Show();
|
2015-07-07 07:45:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 00:21:48 +00:00
|
|
|
void OnWindowClosed() override {
|
2016-12-20 23:52:58 +00:00
|
|
|
parent_->RemoveObserver(this);
|
|
|
|
parent_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
CHROMEG_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, GtkWidget*, int);
|
|
|
|
CHROMEG_CALLBACK_0(GtkMessageBox, void, OnCheckboxToggled, GtkWidget*);
|
2015-07-07 07:45:13 +00:00
|
|
|
|
|
|
|
private:
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::UnresponsiveSuppressor unresponsive_suppressor_;
|
2015-07-07 07:45:13 +00:00
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
// The id of the dialog.
|
|
|
|
absl::optional<int> id_;
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
// The id to return when the dialog is closed without pressing buttons.
|
2018-05-21 22:18:38 +00:00
|
|
|
int cancel_id_ = 0;
|
2015-07-07 07:45:13 +00:00
|
|
|
|
2018-05-21 22:18:38 +00:00
|
|
|
bool checkbox_checked_ = false;
|
2017-02-06 15:35:36 +00:00
|
|
|
|
2017-04-17 08:17:02 +00:00
|
|
|
NativeWindow* parent_;
|
2015-07-07 07:45:13 +00:00
|
|
|
GtkWidget* dialog_;
|
|
|
|
MessageBoxCallback callback_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(GtkMessageBox);
|
|
|
|
};
|
|
|
|
|
|
|
|
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
|
2021-07-14 22:59:27 +00:00
|
|
|
if (id_)
|
|
|
|
GetDialogsMap().erase(*id_);
|
2016-05-11 19:33:43 +00:00
|
|
|
gtk_widget_hide(dialog_);
|
2015-07-07 07:45:13 +00:00
|
|
|
|
|
|
|
if (response < 0)
|
2019-03-12 18:06:59 +00:00
|
|
|
std::move(callback_).Run(cancel_id_, checkbox_checked_);
|
2015-07-07 07:45:13 +00:00
|
|
|
else
|
2019-03-12 18:06:59 +00:00
|
|
|
std::move(callback_).Run(response, checkbox_checked_);
|
2015-07-07 07:45:13 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2017-02-09 17:49:09 +00:00
|
|
|
void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
|
2017-09-27 14:20:44 +00:00
|
|
|
checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 07:45:13 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
|
|
|
|
return GtkMessageBox(settings).RunSynchronous();
|
2015-07-07 07:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
void ShowMessageBox(const MessageBoxSettings& settings,
|
2019-03-12 18:06:59 +00:00
|
|
|
MessageBoxCallback callback) {
|
2021-07-14 22:59:27 +00:00
|
|
|
if (settings.id && base::Contains(GetDialogsMap(), *settings.id))
|
|
|
|
CloseMessageBox(*settings.id);
|
2019-06-14 15:26:25 +00:00
|
|
|
(new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
|
2015-07-07 07:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
void CloseMessageBox(int id) {
|
|
|
|
auto it = GetDialogsMap().find(id);
|
|
|
|
if (it == GetDialogsMap().end()) {
|
|
|
|
LOG(ERROR) << "CloseMessageBox called with nonexistent ID";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gtk_window_close(GTK_WINDOW(it->second));
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
|
2015-07-07 07:02:27 +00:00
|
|
|
if (Browser::Get()->is_ready()) {
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::MessageBoxSettings settings;
|
|
|
|
settings.type = electron::MessageBoxType::kError;
|
2020-12-09 23:28:56 +00:00
|
|
|
settings.buttons = {};
|
2019-06-14 15:26:25 +00:00
|
|
|
settings.title = "Error";
|
|
|
|
settings.message = base::UTF16ToUTF8(title);
|
|
|
|
settings.detail = base::UTF16ToUTF8(content);
|
|
|
|
|
|
|
|
GtkMessageBox(settings).RunSynchronous();
|
2015-07-07 07:02:27 +00:00
|
|
|
} else {
|
2018-04-18 01:55:30 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY ANSI_FOREGROUND_RED
|
2018-02-15 21:20:55 +00:00
|
|
|
"%s\n" ANSI_FOREGROUND_BLACK "%s" ANSI_RESET "\n",
|
2015-07-07 07:02:27 +00:00
|
|
|
base::UTF16ToUTF8(title).c_str(),
|
|
|
|
base::UTF16ToUTF8(content).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|