electron/shell/browser/ui/message_box_gtk.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

266 lines
8.7 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/message_box.h"
2018-02-10 01:29:32 +00:00
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/no_destructor.h"
2015-07-07 07:45:13 +00:00
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "electron/electron_gtk_stubs.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window_observer.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/gtk_util.h"
#include "ui/base/glib/scoped_gsignal.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gtk/gtk_ui.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
#if defined(USE_OZONE)
2020-10-20 18:24:52 +00:00
#include "ui/base/ui_base_features.h"
#endif
2018-02-15 21:20:55 +00:00
#define ANSI_FOREGROUND_RED "\x1b[31m"
#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"
namespace electron {
MessageBoxSettings::MessageBoxSettings() = default;
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
MessageBoxSettings::~MessageBoxSettings() = default;
2015-07-07 07:45:13 +00:00
namespace {
// <ID, messageBox> map
base::flat_map<int, GtkWidget*>& GetDialogsMap() {
static base::NoDestructor<base::flat_map<int, GtkWidget*>> dialogs;
return *dialogs;
}
refactor: inherit Observer classes privately, pt. 2 (#42237) * refactor: use private inheritance in PushNotifications * refactor: use private inheritance in electron::api::App * refactor: use private inheritance in electron::api::BrowserWindow * refactor: use private inheritance in electron::api::NativeTheme * refactor: use private inheritance in electron::api::Tray * refactor: use private inheritance in electron::api::Session * refactor: use private inheritance in electron::api::WebContents * refactor: use private inheritance in electron::api::DownloadItem * refactor: use private inheritance in electron::api::MenuBar * refactor: use private inheritance in ClearDataOperation * refactor: use private inheritance in electron::api::Screen * refactor: use private inheritance in electron::ElectronDesktopWindowTreeHostLinux * refactor: use private inheritance in SpellCheckerHolder * refactor: use private inheritance in electron::api::PowerMonitor * refactor: use private inheritance in electron::api::BaseWindow * refactor: use private inheritance in electron::api::AutoUpdater * refactor: use private inheritance in electron::api::Menu * refactor: use private inheritance in electron::api::NativeWindowViews * refactor: use private inheritance in electron::ElectronBrowserClient * refactor: use private inheritance in electron::AutofillPopupView * refactor: use private inheritance in GtkMessageBox * refactor: use private inheritance in electron::OffScreenRenderWidgetHostView * refactor: use private inheritance in electron::InspectableWebContents * refactor: use private inheritance in electron::ElectronUsbDelegate * refactor: use private inheritance in electron::LoginHandler * refactor: use private inheritance in WebFrameRenderer * refactor: use private inheritance in electron::ElectronSerialDelegate * refactor: use private inheritance in electron::ClientFrameViewLinux * refactor: use private inheritance in electron::ElectronHidDelegate * refactor: use private inheritance in IPCRenderer * refactor: use private inheritance in electron::WinCaptionButtonContainer * refactor: use private inheritance in electron::ElectronApiIPCHandlerImpl * refactor: use private inheritance in electron::api::ServiceWorkerContext * refactor: use private inheritance in ui::FileSelectHelper * refactor: use private inheritance in electron::api::WebContentsView * refactor: use private inheritance in electron::api::SimpleURLLoaderWrapper * refactor: use private inheritance in electron::api::InAppPurchase * refactor: use private inheritance in electron::api::Debugger * refactor: use private inheritance in electron::ElectronWebContentsUtilityHandlerImpl * refactor: use private inheritance in electron::OffScreenWebContentsView
2024-05-29 18:07:02 +00:00
class GtkMessageBox : private NativeWindowObserver {
2015-07-07 07:45:13 +00:00
public:
explicit GtkMessageBox(const MessageBoxSettings& settings)
: id_(settings.id),
cancel_id_(settings.cancel_id),
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
GetMessageType(settings.type), // type
2018-02-15 21:20:55 +00:00
GTK_BUTTONS_NONE, // no buttons
"%s", settings.message.c_str());
if (id_)
GetDialogsMap()[*id_] = dialog_;
if (!settings.detail.empty())
2018-02-15 21:20:55 +00:00
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_),
"%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
if (!settings.icon.isNull()) {
// 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;
GdkPixbuf* pixbuf =
gtk_util::GdkPixbufFromSkBitmap(*settings.icon.bitmap());
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);
g_clear_pointer(&scaled_pixbuf, g_object_unref);
g_clear_pointer(&pixbuf, g_object_unref);
}
if (!settings.checkbox_label.empty()) {
GtkWidget* message_area =
gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
GtkWidget* check_button =
gtk_check_button_new_with_label(settings.checkbox_label.c_str());
signals_.emplace_back(
check_button, "toggled",
base::BindRepeating(&GtkMessageBox::OnCheckboxToggled,
base::Unretained(this)));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
settings.checkbox_checked);
gtk_container_add(GTK_CONTAINER(message_area), check_button);
gtk_widget_show(check_button);
}
2015-07-07 07:45:13 +00:00
// Add buttons.
GtkDialog* dialog = GTK_DIALOG(dialog_);
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
}
gtk_dialog_set_default_response(dialog, settings.default_id);
2015-07-07 07:45:13 +00:00
// Parent window.
if (parent_) {
parent_->AddObserver(this);
2017-04-17 08:17:02 +00:00
static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
2015-07-07 07:45:13 +00:00
}
}
~GtkMessageBox() override {
2015-07-07 07:45:13 +00:00
gtk_widget_destroy(dialog_);
if (parent_) {
parent_->RemoveObserver(this);
2017-04-17 08:17:02 +00:00
static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
}
2015-07-07 07:45:13 +00:00
}
// disable copy
GtkMessageBox(const GtkMessageBox&) = delete;
GtkMessageBox& operator=(const GtkMessageBox&) = delete;
GtkMessageType GetMessageType(MessageBoxType type) {
switch (type) {
case MessageBoxType::kInformation:
return GTK_MESSAGE_INFO;
case MessageBoxType::kWarning:
return GTK_MESSAGE_WARNING;
case MessageBoxType::kQuestion:
return GTK_MESSAGE_QUESTION;
case MessageBoxType::kError:
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")
return gtk_util::GetCancelLabel();
2018-02-09 23:45:34 +00:00
if (lower == "no")
return gtk_util::GetNoLabel();
2018-02-09 23:45:34 +00:00
if (lower == "ok")
return gtk_util::GetOkLabel();
2018-02-09 23:45:34 +00:00
if (lower == "yes")
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() {
gtk_widget_show(dialog_);
gtk::GtkUi::GetPlatform()->ShowGtkWindow(GTK_WINDOW(dialog_));
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_));
return (response < 0) ? cancel_id_ : response;
2015-07-07 09:03:47 +00:00
}
void RunAsynchronous(MessageBoxCallback callback) {
callback_ = std::move(callback);
signals_.emplace_back(dialog_, "delete-event",
base::BindRepeating(gtk_widget_hide_on_delete));
signals_.emplace_back(dialog_, "response",
base::BindRepeating(&GtkMessageBox::OnResponseDialog,
base::Unretained(this)));
2015-07-07 08:42:03 +00:00
Show();
2015-07-07 07:45:13 +00:00
}
// NativeWindowObserver
2016-12-21 00:21:48 +00:00
void OnWindowClosed() override {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
void OnResponseDialog(GtkWidget* widget, int response);
void OnCheckboxToggled(GtkWidget* widget);
2015-07-07 07:45:13 +00:00
private:
// The id of the dialog.
std::optional<int> id_;
2015-07-07 07:45:13 +00:00
// The id to return when the dialog is closed without pressing buttons.
int cancel_id_ = 0;
2015-07-07 07:45:13 +00:00
bool checkbox_checked_ = false;
raw_ptr<NativeWindow> parent_;
RAW_PTR_EXCLUSION GtkWidget* dialog_;
2015-07-07 07:45:13 +00:00
MessageBoxCallback callback_;
std::vector<ScopedGSignal> signals_;
2015-07-07 07:45:13 +00:00
};
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
if (id_)
GetDialogsMap().erase(*id_);
gtk_widget_hide(dialog_);
2015-07-07 07:45:13 +00:00
if (response < 0)
std::move(callback_).Run(cancel_id_, checkbox_checked_);
2015-07-07 07:45:13 +00:00
else
std::move(callback_).Run(response, checkbox_checked_);
2015-07-07 07:45:13 +00:00
delete this;
}
void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
2017-09-27 14:20:44 +00:00
checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
}
2015-07-07 07:45:13 +00:00
} // namespace
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
return GtkMessageBox(settings).RunSynchronous();
}
void ShowMessageBox(const MessageBoxSettings& settings,
MessageBoxCallback callback) {
if (settings.id && base::Contains(GetDialogsMap(), *settings.id))
CloseMessageBox(*settings.id);
(new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
}
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));
}
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
if (Browser::Get()->is_ready()) {
electron::MessageBoxSettings settings;
settings.type = electron::MessageBoxType::kError;
settings.buttons = {};
settings.title = "Error";
settings.message = base::UTF16ToUTF8(title);
settings.detail = base::UTF16ToUTF8(content);
GtkMessageBox(settings).RunSynchronous();
} 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",
base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(content).c_str());
}
}
} // namespace electron