electron/shell/browser/ui/message_box_gtk.cc
Electron Bot 8798571a77
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687)
* chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a

* viz: Do not apply white level scaling for RGBA fp16 HDR video

https://chromium-review.googlesource.com/c/chromium/src/+/2296006

* Move WebPreferences to WebContents

https://chromium-review.googlesource.com/c/chromium/src/+/2263635

* Fix missing WeakPtr check in PreconnectManager

https://chromium-review.googlesource.com/c/chromium/src/+/2309029

* Fixup swiftshader roll revision

* Update patch indices

* Move WebDeviceEmulationParams into common.

https://chromium-review.googlesource.com/c/chromium/src/+/2303356

* Move EnableDisableDeviceEmulation to blink mojom messages

https://chromium-review.googlesource.com/c/chromium/src/+/2303367

* PDF Viewer: Remove flag for two-up view

https://chromium-review.googlesource.com/c/chromium/src/+/2311130

* Add mojom definition for DeviceEmulationParams.

https://chromium-review.googlesource.com/c/chromium/src/+/2303491

* Remove ServiceWorkerContextWatcher from PaymentAppInstaller

https://chromium-review.googlesource.com/c/chromium/src/+/2291186

* Loader: Move transferrable_url_loader.mojom into blink's mojom directory

https://chromium-review.googlesource.com/c/chromium/src/+/2306123

* chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46

* mac: Work around Xcode 12b3 SDK bug

https://chromium-review.googlesource.com/c/chromium/src/+/2315078

* Reland Update core items for macOS Big Sur.

https://chromium-review.googlesource.com/c/chromium/src/+/2315162

* Update Swiftshader revision

* mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host.

https://chromium-review.googlesource.com/c/v8/v8/+/2310575

* Update patch indices

* Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested()

https://chromium-review.googlesource.com/c/chromium/src/+/2247838

* chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71

* chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5

* chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce

* chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798

* chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5

* chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c

* chore: update patches

* chore: base::DeleteFile with two params is removed

Should use base::DeleteFile and base::DeletePathRecursively when
appropriate

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376

* chore: add patch for NodePlatform::PostJob impl

* chore: update patches

* chore: extension file access is now instrumented

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995

* chore: implement SetWindowFrameInScreen in OSR RWHV

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409

* chore: NotifyUserActivation requires a type now

This is just for a histogram thing and therefore it does not matter what
we pass in

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303

* chore: update patches

* chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c

* chore: update patches

* chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0

* chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9

* chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50

* chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702

* chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e

* update patches

* change OS_MACOSX -> OS_MAC

Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907

* patch: add header for ToExecutionContext in WebMessagePortConverter

* chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b

* update patches

* NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711

* chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23

* update patches

* add dragdrop header for autofill popup

* int -> x11::Time

* patch out accessibility private API use

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812

* remove usage of XEvent

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767

* trigger recalculation of WebPreferences before renderer initialization

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635

* chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a

* chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5

* chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6

* chore: update patches

* fixup! trigger recalculation of WebPreferences before renderer initialization

* views: Make MenuButton and RadioButton default constructible

https://chromium-review.googlesource.com/c/chromium/src/+/2339586

* chore: fix code style

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Samuel Attard <sattard@slack-corp.com>
Co-authored-by: Andy Locascio <andy@slack-corp.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 11:33:58 -07:00

226 lines
7.6 KiB
C++

// 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/gtk_util.h"
#include "shell/browser/ui/message_box.h"
#include "base/callback.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window_observer.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/unresponsive_suppressor.h"
#include "ui/base/glib/glib_signal.h"
#include "ui/events/platform/x11/x11_event_source.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gtk/gtk_util.h"
#define ANSI_FOREGROUND_RED "\x1b[31m"
#define ANSI_FOREGROUND_BLACK "\x1b[30m"
#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;
namespace {
class GtkMessageBox : public NativeWindowObserver {
public:
explicit GtkMessageBox(const MessageBoxSettings& settings)
: cancel_id_(settings.cancel_id),
parent_(static_cast<NativeWindow*>(settings.parent_window)) {
// Create dialog.
dialog_ =
gtk_message_dialog_new(nullptr, // parent
static_cast<GtkDialogFlags>(0), // no flags
GetMessageType(settings.type), // type
GTK_BUTTONS_NONE, // no buttons
"%s", settings.message.c_str());
if (!settings.detail.empty())
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());
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());
g_signal_connect(check_button, "toggled",
G_CALLBACK(OnCheckboxToggledThunk), 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);
}
// Add buttons.
GtkDialog* dialog = GTK_DIALOG(dialog_);
for (size_t i = 0; i < settings.buttons.size(); ++i) {
gtk_dialog_add_button(dialog, TranslateToStock(i, settings.buttons[i]),
i);
}
gtk_dialog_set_default_response(dialog, settings.default_id);
// Parent window.
if (parent_) {
parent_->AddObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
}
}
~GtkMessageBox() override {
gtk_widget_destroy(dialog_);
if (parent_) {
parent_->RemoveObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
}
}
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;
}
}
const char* TranslateToStock(int id, const std::string& text) {
const std::string lower = base::ToLowerASCII(text);
if (lower == "cancel")
return gtk_util::kCancelLabel;
if (lower == "no")
return gtk_util::kNoLabel;
if (lower == "ok")
return gtk_util::kOkLabel;
if (lower == "yes")
return gtk_util::kYesLabel;
return text.c_str();
}
void Show() {
gtk_widget_show(dialog_);
// 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));
}
int RunSynchronous() {
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
return (response < 0) ? cancel_id_ : response;
}
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),
this);
Show();
}
void OnWindowClosed() override {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
CHROMEG_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, GtkWidget*, int);
CHROMEG_CALLBACK_0(GtkMessageBox, void, OnCheckboxToggled, GtkWidget*);
private:
electron::UnresponsiveSuppressor unresponsive_suppressor_;
// The id to return when the dialog is closed without pressing buttons.
int cancel_id_ = 0;
bool checkbox_checked_ = false;
NativeWindow* parent_;
GtkWidget* dialog_;
MessageBoxCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GtkMessageBox);
};
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
gtk_widget_hide(dialog_);
if (response < 0)
std::move(callback_).Run(cancel_id_, checkbox_checked_);
else
std::move(callback_).Run(response, checkbox_checked_);
delete this;
}
void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
}
} // namespace
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
return GtkMessageBox(settings).RunSynchronous();
}
void ShowMessageBox(const MessageBoxSettings& settings,
MessageBoxCallback callback) {
(new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
}
void ShowErrorBox(const base::string16& title, const base::string16& content) {
if (Browser::Get()->is_ready()) {
electron::MessageBoxSettings settings;
settings.type = electron::MessageBoxType::kError;
settings.buttons = {"OK"};
settings.title = "Error";
settings.message = base::UTF16ToUTF8(title);
settings.detail = base::UTF16ToUTF8(content);
GtkMessageBox(settings).RunSynchronous();
} else {
fprintf(stderr,
ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY ANSI_FOREGROUND_RED
"%s\n" ANSI_FOREGROUND_BLACK "%s" ANSI_RESET "\n",
base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(content).c_str());
}
}
} // namespace electron