message_box_gtk.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/ui/message_box.h"
  5. #include "base/containers/contains.h"
  6. #include "base/containers/flat_map.h"
  7. #include "base/functional/bind.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/raw_ptr_exclusion.h"
  10. #include "base/no_destructor.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "electron/electron_gtk_stubs.h"
  14. #include "shell/browser/browser.h"
  15. #include "shell/browser/native_window_observer.h"
  16. #include "shell/browser/native_window_views.h"
  17. #include "shell/browser/ui/gtk_util.h"
  18. #include "ui/base/glib/scoped_gsignal.h"
  19. #include "ui/gfx/image/image_skia.h"
  20. #include "ui/gtk/gtk_ui.h" // nogncheck
  21. #include "ui/gtk/gtk_util.h" // nogncheck
  22. #if defined(USE_OZONE)
  23. #include "ui/base/ui_base_features.h"
  24. #endif
  25. #define ANSI_FOREGROUND_RED "\x1b[31m"
  26. #define ANSI_FOREGROUND_BLACK "\x1b[30m"
  27. #define ANSI_TEXT_BOLD "\x1b[1m"
  28. #define ANSI_BACKGROUND_GRAY "\x1b[47m"
  29. #define ANSI_RESET "\x1b[0m"
  30. namespace electron {
  31. MessageBoxSettings::MessageBoxSettings() = default;
  32. MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
  33. MessageBoxSettings::~MessageBoxSettings() = default;
  34. namespace {
  35. // <ID, messageBox> map
  36. base::flat_map<int, GtkWidget*>& GetDialogsMap() {
  37. static base::NoDestructor<base::flat_map<int, GtkWidget*>> dialogs;
  38. return *dialogs;
  39. }
  40. class GtkMessageBox : private NativeWindowObserver {
  41. public:
  42. explicit GtkMessageBox(const MessageBoxSettings& settings)
  43. : id_(settings.id),
  44. cancel_id_(settings.cancel_id),
  45. parent_(static_cast<NativeWindow*>(settings.parent_window)) {
  46. // Create dialog.
  47. dialog_ =
  48. gtk_message_dialog_new(nullptr, // parent
  49. static_cast<GtkDialogFlags>(0), // no flags
  50. GetMessageType(settings.type), // type
  51. GTK_BUTTONS_NONE, // no buttons
  52. "%s", settings.message.c_str());
  53. if (id_)
  54. GetDialogsMap()[*id_] = dialog_;
  55. if (!settings.detail.empty())
  56. gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_),
  57. "%s", settings.detail.c_str());
  58. if (!settings.title.empty())
  59. gtk_window_set_title(GTK_WINDOW(dialog_), settings.title.c_str());
  60. if (!settings.icon.isNull()) {
  61. // No easy way to obtain this programmatically, but GTK+'s docs
  62. // define GTK_ICON_SIZE_DIALOG to be 48 pixels
  63. static constexpr int pixel_width = 48;
  64. static constexpr int pixel_height = 48;
  65. GdkPixbuf* pixbuf =
  66. gtk_util::GdkPixbufFromSkBitmap(*settings.icon.bitmap());
  67. GdkPixbuf* scaled_pixbuf = gdk_pixbuf_scale_simple(
  68. pixbuf, pixel_width, pixel_height, GDK_INTERP_BILINEAR);
  69. GtkWidget* w = gtk_image_new_from_pixbuf(scaled_pixbuf);
  70. gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog_), w);
  71. gtk_widget_show(w);
  72. g_clear_pointer(&scaled_pixbuf, g_object_unref);
  73. g_clear_pointer(&pixbuf, g_object_unref);
  74. }
  75. if (!settings.checkbox_label.empty()) {
  76. GtkWidget* message_area =
  77. gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
  78. GtkWidget* check_button =
  79. gtk_check_button_new_with_label(settings.checkbox_label.c_str());
  80. signals_.emplace_back(
  81. check_button, "toggled",
  82. base::BindRepeating(&GtkMessageBox::OnCheckboxToggled,
  83. base::Unretained(this)));
  84. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
  85. settings.checkbox_checked);
  86. gtk_container_add(GTK_CONTAINER(message_area), check_button);
  87. gtk_widget_show(check_button);
  88. }
  89. // Add buttons.
  90. GtkDialog* dialog = GTK_DIALOG(dialog_);
  91. if (settings.buttons.size() == 0) {
  92. gtk_dialog_add_button(dialog, TranslateToStock("OK"), 0);
  93. } else {
  94. for (size_t i = 0; i < settings.buttons.size(); ++i) {
  95. gtk_dialog_add_button(dialog, TranslateToStock(settings.buttons[i]), i);
  96. }
  97. }
  98. gtk_dialog_set_default_response(dialog, settings.default_id);
  99. // Parent window.
  100. if (parent_) {
  101. parent_->AddObserver(this);
  102. static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
  103. gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
  104. gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
  105. }
  106. }
  107. ~GtkMessageBox() override {
  108. gtk_widget_destroy(dialog_);
  109. if (parent_) {
  110. parent_->RemoveObserver(this);
  111. static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
  112. }
  113. }
  114. // disable copy
  115. GtkMessageBox(const GtkMessageBox&) = delete;
  116. GtkMessageBox& operator=(const GtkMessageBox&) = delete;
  117. GtkMessageType GetMessageType(MessageBoxType type) {
  118. switch (type) {
  119. case MessageBoxType::kInformation:
  120. return GTK_MESSAGE_INFO;
  121. case MessageBoxType::kWarning:
  122. return GTK_MESSAGE_WARNING;
  123. case MessageBoxType::kQuestion:
  124. return GTK_MESSAGE_QUESTION;
  125. case MessageBoxType::kError:
  126. return GTK_MESSAGE_ERROR;
  127. default:
  128. return GTK_MESSAGE_OTHER;
  129. }
  130. }
  131. static const char* TranslateToStock(const std::string& text) {
  132. const std::string lower = base::ToLowerASCII(text);
  133. if (lower == "cancel") {
  134. return gtk_util::GetCancelLabel();
  135. }
  136. if (lower == "no") {
  137. return gtk_util::GetNoLabel();
  138. }
  139. if (lower == "ok") {
  140. return gtk_util::GetOkLabel();
  141. }
  142. if (lower == "yes") {
  143. return gtk_util::GetYesLabel();
  144. }
  145. return text.c_str();
  146. }
  147. void Show() {
  148. gtk_widget_show(dialog_);
  149. gtk::GtkUi::GetPlatform()->ShowGtkWindow(GTK_WINDOW(dialog_));
  150. }
  151. int RunSynchronous() {
  152. Show();
  153. int response = gtk_dialog_run(GTK_DIALOG(dialog_));
  154. return (response < 0) ? cancel_id_ : response;
  155. }
  156. void RunAsynchronous(MessageBoxCallback callback) {
  157. callback_ = std::move(callback);
  158. signals_.emplace_back(dialog_, "delete-event",
  159. base::BindRepeating(gtk_widget_hide_on_delete));
  160. signals_.emplace_back(dialog_, "response",
  161. base::BindRepeating(&GtkMessageBox::OnResponseDialog,
  162. base::Unretained(this)));
  163. Show();
  164. }
  165. // NativeWindowObserver
  166. void OnWindowClosed() override {
  167. parent_->RemoveObserver(this);
  168. parent_ = nullptr;
  169. }
  170. void OnResponseDialog(GtkWidget* widget, int response);
  171. void OnCheckboxToggled(GtkWidget* widget);
  172. private:
  173. // The id of the dialog.
  174. std::optional<int> id_;
  175. // The id to return when the dialog is closed without pressing buttons.
  176. int cancel_id_ = 0;
  177. bool checkbox_checked_ = false;
  178. raw_ptr<NativeWindow> parent_;
  179. RAW_PTR_EXCLUSION GtkWidget* dialog_;
  180. MessageBoxCallback callback_;
  181. std::vector<ScopedGSignal> signals_;
  182. };
  183. void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
  184. if (id_)
  185. GetDialogsMap().erase(*id_);
  186. gtk_widget_hide(dialog_);
  187. if (response < 0)
  188. std::move(callback_).Run(cancel_id_, checkbox_checked_);
  189. else
  190. std::move(callback_).Run(response, checkbox_checked_);
  191. delete this;
  192. }
  193. void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
  194. checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
  195. }
  196. } // namespace
  197. int ShowMessageBoxSync(const MessageBoxSettings& settings) {
  198. return GtkMessageBox(settings).RunSynchronous();
  199. }
  200. void ShowMessageBox(const MessageBoxSettings& settings,
  201. MessageBoxCallback callback) {
  202. if (settings.id && base::Contains(GetDialogsMap(), *settings.id))
  203. CloseMessageBox(*settings.id);
  204. (new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
  205. }
  206. void CloseMessageBox(int id) {
  207. auto it = GetDialogsMap().find(id);
  208. if (it == GetDialogsMap().end()) {
  209. LOG(ERROR) << "CloseMessageBox called with nonexistent ID";
  210. return;
  211. }
  212. gtk_window_close(GTK_WINDOW(it->second));
  213. }
  214. void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
  215. if (Browser::Get()->is_ready()) {
  216. electron::MessageBoxSettings settings;
  217. settings.type = electron::MessageBoxType::kError;
  218. settings.buttons = {};
  219. settings.title = "Error";
  220. settings.message = base::UTF16ToUTF8(title);
  221. settings.detail = base::UTF16ToUTF8(content);
  222. GtkMessageBox(settings).RunSynchronous();
  223. } else {
  224. fprintf(stderr,
  225. ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY ANSI_FOREGROUND_RED
  226. "%s\n" ANSI_FOREGROUND_BLACK "%s" ANSI_RESET "\n",
  227. base::UTF16ToUTF8(title).c_str(),
  228. base::UTF16ToUTF8(content).c_str());
  229. }
  230. }
  231. } // namespace electron