message_box_gtk.cc 8.7 KB

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