editor_toaster.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**************************************************************************/
  2. /* editor_toaster.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_toaster.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/settings/editor_settings.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/panel_container.h"
  37. #include "scene/resources/style_box_flat.h"
  38. EditorToaster *EditorToaster::singleton = nullptr;
  39. void EditorToaster::_notification(int p_what) {
  40. switch (p_what) {
  41. case NOTIFICATION_INTERNAL_PROCESS: {
  42. double delta = get_process_delta_time();
  43. // Check if one element is hovered, if so, don't elapse time.
  44. bool hovered = false;
  45. for (const KeyValue<Control *, Toast> &element : toasts) {
  46. if (Rect2(Vector2(), element.key->get_size()).has_point(element.key->get_local_mouse_position())) {
  47. hovered = true;
  48. break;
  49. }
  50. }
  51. // Elapses the time and remove toasts if needed.
  52. if (!hovered) {
  53. for (const KeyValue<Control *, Toast> &element : toasts) {
  54. if (!element.value.popped || element.value.duration <= 0) {
  55. continue;
  56. }
  57. toasts[element.key].remaining_time -= delta;
  58. if (toasts[element.key].remaining_time < 0) {
  59. close(element.key);
  60. }
  61. element.key->queue_redraw();
  62. }
  63. } else {
  64. // Reset the timers when hovered.
  65. for (const KeyValue<Control *, Toast> &element : toasts) {
  66. if (!element.value.popped || element.value.duration <= 0) {
  67. continue;
  68. }
  69. toasts[element.key].remaining_time = element.value.duration;
  70. element.key->queue_redraw();
  71. }
  72. }
  73. // Change alpha over time.
  74. bool needs_update = false;
  75. for (const KeyValue<Control *, Toast> &element : toasts) {
  76. Color modulate_fade = element.key->get_modulate();
  77. // Change alpha over time.
  78. if (element.value.popped && modulate_fade.a < 1.0) {
  79. modulate_fade.a += delta * 3;
  80. element.key->set_modulate(modulate_fade);
  81. } else if (!element.value.popped && modulate_fade.a > 0.0) {
  82. modulate_fade.a -= delta * 2;
  83. element.key->set_modulate(modulate_fade);
  84. }
  85. // Hide element if it is not visible anymore.
  86. if (modulate_fade.a <= 0.0 && element.key->is_visible()) {
  87. element.key->hide();
  88. needs_update = true;
  89. } else if (modulate_fade.a > 0.0 && !element.key->is_visible()) {
  90. element.key->show();
  91. needs_update = true;
  92. }
  93. }
  94. if (needs_update) {
  95. _update_vbox_position();
  96. _update_disable_notifications_button();
  97. main_button->queue_redraw();
  98. }
  99. } break;
  100. case NOTIFICATION_THEME_CHANGED: {
  101. if (vbox_container->is_visible()) {
  102. main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification")));
  103. } else {
  104. main_button->set_button_icon(get_editor_theme_icon(SNAME("NotificationDisabled")));
  105. }
  106. disable_notifications_button->set_button_icon(get_editor_theme_icon(SNAME("NotificationDisabled")));
  107. // Styleboxes background.
  108. info_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)));
  109. warning_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)));
  110. warning_panel_style_background->set_border_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  111. error_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)));
  112. error_panel_style_background->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  113. // Styleboxes progress.
  114. info_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03));
  115. warning_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03));
  116. warning_panel_style_progress->set_border_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  117. error_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03));
  118. error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  119. } break;
  120. case NOTIFICATION_TRANSFORM_CHANGED: {
  121. _update_vbox_position();
  122. _update_disable_notifications_button();
  123. } break;
  124. }
  125. }
  126. void EditorToaster::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, bool p_editor_notify, ErrorHandlerType p_type) {
  127. // This may be called from a thread. Since we will deal with non-thread-safe elements,
  128. // we have to put it in the queue for safety.
  129. callable_mp_static(&EditorToaster::_error_handler_impl).call_deferred(String::utf8(p_file), p_line, String::utf8(p_error), String::utf8(p_errorexp), p_editor_notify, p_type);
  130. }
  131. void EditorToaster::_error_handler_impl(const String &p_file, int p_line, const String &p_error, const String &p_errorexp, bool p_editor_notify, int p_type) {
  132. if (!EditorToaster::get_singleton() || !EditorToaster::get_singleton()->is_inside_tree()) {
  133. return;
  134. }
  135. #ifdef DEV_ENABLED
  136. bool in_dev = true;
  137. #else
  138. bool in_dev = false;
  139. #endif
  140. int show_all_setting = EDITOR_GET("interface/editor/show_internal_errors_in_toast_notifications");
  141. if (p_editor_notify || (show_all_setting == 0 && in_dev) || show_all_setting == 1) {
  142. String err_str = !p_errorexp.is_empty() ? p_errorexp : p_error;
  143. String tooltip_str = p_file + ":" + itos(p_line);
  144. if (!p_editor_notify) {
  145. if (p_type == ERR_HANDLER_WARNING) {
  146. err_str = "INTERNAL WARNING: " + err_str;
  147. } else {
  148. err_str = "INTERNAL ERROR: " + err_str;
  149. }
  150. }
  151. Severity severity = ((ErrorHandlerType)p_type == ERR_HANDLER_WARNING) ? SEVERITY_WARNING : SEVERITY_ERROR;
  152. EditorToaster::get_singleton()->popup_str(err_str, severity, tooltip_str);
  153. }
  154. }
  155. void EditorToaster::_update_vbox_position() {
  156. // This is kind of a workaround because it's hard to keep the VBox anchroed to the bottom.
  157. vbox_container->set_size(Vector2());
  158. vbox_container->set_position(get_global_position() - vbox_container->get_size() + Vector2(get_size().x, -5 * EDSCALE));
  159. }
  160. void EditorToaster::_update_disable_notifications_button() {
  161. bool any_visible = false;
  162. for (KeyValue<Control *, Toast> element : toasts) {
  163. if (element.key->is_visible()) {
  164. any_visible = true;
  165. break;
  166. }
  167. }
  168. if (!any_visible || !vbox_container->is_visible()) {
  169. disable_notifications_panel->hide();
  170. } else {
  171. disable_notifications_panel->show();
  172. disable_notifications_panel->set_position(get_global_position() + Vector2(5 * EDSCALE, -disable_notifications_panel->get_minimum_size().y) + Vector2(get_size().x, -5 * EDSCALE));
  173. }
  174. }
  175. void EditorToaster::_auto_hide_or_free_toasts() {
  176. // Hide or free old temporary items.
  177. int visible_temporary = 0;
  178. int temporary = 0;
  179. LocalVector<Control *> to_delete;
  180. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  181. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  182. if (toasts[control].duration <= 0) {
  183. continue; // Ignore non-temporary toasts.
  184. }
  185. temporary++;
  186. if (control->is_visible()) {
  187. visible_temporary++;
  188. }
  189. // Hide
  190. if (visible_temporary > max_temporary_count) {
  191. close(control);
  192. }
  193. // Free
  194. if (temporary > max_temporary_count * 2) {
  195. to_delete.push_back(control);
  196. }
  197. }
  198. // Delete the control right away (removed as child) as it might cause issues otherwise when iterative over the vbox_container children.
  199. for (Control *c : to_delete) {
  200. vbox_container->remove_child(c);
  201. c->queue_free();
  202. toasts.erase(c);
  203. }
  204. if (toasts.is_empty()) {
  205. main_button->set_tooltip_text(TTRC("No notifications."));
  206. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  207. main_button->set_disabled(true);
  208. set_process_internal(false);
  209. } else {
  210. main_button->set_tooltip_text(TTRC("Show notifications."));
  211. main_button->set_modulate(Color(1, 1, 1));
  212. main_button->set_disabled(false);
  213. }
  214. }
  215. void EditorToaster::_draw_button() {
  216. bool has_one = false;
  217. Severity highest_severity = SEVERITY_INFO;
  218. for (const KeyValue<Control *, Toast> &element : toasts) {
  219. if (!element.key->is_visible()) {
  220. continue;
  221. }
  222. has_one = true;
  223. if (element.value.severity > highest_severity) {
  224. highest_severity = element.value.severity;
  225. }
  226. }
  227. if (!has_one) {
  228. return;
  229. }
  230. Color color;
  231. real_t button_radius = main_button->get_size().x / 8;
  232. switch (highest_severity) {
  233. case SEVERITY_INFO:
  234. color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  235. break;
  236. case SEVERITY_WARNING:
  237. color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  238. break;
  239. case SEVERITY_ERROR:
  240. color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  241. break;
  242. default:
  243. break;
  244. }
  245. main_button->draw_circle(Vector2(button_radius * 2, button_radius * 2), button_radius, color);
  246. }
  247. void EditorToaster::_draw_progress(Control *panel) {
  248. if (toasts.has(panel) && toasts[panel].remaining_time > 0 && toasts[panel].duration > 0) {
  249. Size2 size = panel->get_size();
  250. size.x *= MIN(1, Math::remap(toasts[panel].remaining_time, 0, toasts[panel].duration, 0, 2));
  251. Ref<StyleBoxFlat> stylebox;
  252. switch (toasts[panel].severity) {
  253. case SEVERITY_INFO:
  254. stylebox = info_panel_style_progress;
  255. break;
  256. case SEVERITY_WARNING:
  257. stylebox = warning_panel_style_progress;
  258. break;
  259. case SEVERITY_ERROR:
  260. stylebox = error_panel_style_progress;
  261. break;
  262. default:
  263. break;
  264. }
  265. panel->draw_style_box(stylebox, Rect2(Vector2(), size));
  266. }
  267. }
  268. void EditorToaster::_set_notifications_enabled(bool p_enabled) {
  269. vbox_container->set_visible(p_enabled);
  270. if (p_enabled) {
  271. main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification")));
  272. } else {
  273. main_button->set_button_icon(get_editor_theme_icon(SNAME("NotificationDisabled")));
  274. }
  275. _update_disable_notifications_button();
  276. }
  277. void EditorToaster::_repop_old() {
  278. // Repop olds, up to max_temporary_count
  279. bool needs_update = false;
  280. int visible_count = 0;
  281. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  282. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  283. if (!control->is_visible()) {
  284. control->show();
  285. toasts[control].remaining_time = toasts[control].duration;
  286. toasts[control].popped = true;
  287. needs_update = true;
  288. }
  289. visible_count++;
  290. if (visible_count >= max_temporary_count) {
  291. break;
  292. }
  293. }
  294. if (needs_update) {
  295. _update_vbox_position();
  296. _update_disable_notifications_button();
  297. main_button->queue_redraw();
  298. }
  299. }
  300. Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_time, const String &p_tooltip) {
  301. // Create the panel according to the severity.
  302. PanelContainer *panel = memnew(PanelContainer);
  303. panel->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  304. panel->set_tooltip_text(p_tooltip);
  305. switch (p_severity) {
  306. case SEVERITY_INFO:
  307. panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  308. break;
  309. case SEVERITY_WARNING:
  310. panel->add_theme_style_override(SceneStringName(panel), warning_panel_style_background);
  311. break;
  312. case SEVERITY_ERROR:
  313. panel->add_theme_style_override(SceneStringName(panel), error_panel_style_background);
  314. break;
  315. default:
  316. break;
  317. }
  318. panel->set_modulate(Color(1, 1, 1, 0));
  319. panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
  320. panel->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_toast_theme_changed).bind(panel));
  321. Toast &toast = toasts[panel];
  322. // Horizontal container.
  323. HBoxContainer *hbox_container = memnew(HBoxContainer);
  324. hbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  325. panel->add_child(hbox_container);
  326. // Content control.
  327. p_control->set_h_size_flags(SIZE_EXPAND_FILL);
  328. hbox_container->add_child(p_control);
  329. // Add buttons.
  330. if (p_time > 0.0) {
  331. Button *copy_button = memnew(Button);
  332. copy_button->set_accessibility_name(TTRC("Copy"));
  333. copy_button->set_flat(true);
  334. copy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::copy).bind(panel));
  335. hbox_container->add_child(copy_button);
  336. Button *close_button = memnew(Button);
  337. close_button->set_accessibility_name(TTRC("Close"));
  338. close_button->set_flat(true);
  339. close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::instant_close).bind(panel));
  340. hbox_container->add_child(close_button);
  341. toast.copy_button = copy_button;
  342. toast.close_button = close_button;
  343. }
  344. toast.severity = p_severity;
  345. if (p_time > 0.0) {
  346. toast.duration = p_time;
  347. toast.remaining_time = p_time;
  348. } else {
  349. toast.duration = -1.0;
  350. }
  351. toast.popped = true;
  352. vbox_container->add_child(panel);
  353. _auto_hide_or_free_toasts();
  354. _update_vbox_position();
  355. _update_disable_notifications_button();
  356. main_button->queue_redraw();
  357. return panel;
  358. }
  359. void EditorToaster::popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  360. if (is_processing_error) {
  361. return;
  362. }
  363. // Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
  364. // thread-safe, it's better to defer the call to the next cycle to be thread-safe.
  365. is_processing_error = true;
  366. MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &EditorToaster::_popup_str), p_message, p_severity, p_tooltip);
  367. is_processing_error = false;
  368. }
  369. void EditorToaster::_popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  370. is_processing_error = true;
  371. // Check if we already have a popup with the given message.
  372. Control *control = nullptr;
  373. for (KeyValue<Control *, Toast> element : toasts) {
  374. if (element.value.message == p_message && element.value.severity == p_severity && element.value.tooltip == p_tooltip) {
  375. control = element.key;
  376. break;
  377. }
  378. }
  379. // Create a new message if needed.
  380. if (control == nullptr) {
  381. HBoxContainer *hb = memnew(HBoxContainer);
  382. hb->add_theme_constant_override("separation", 0);
  383. Label *label = memnew(Label);
  384. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  385. label->set_focus_mode(FOCUS_ACCESSIBILITY);
  386. hb->add_child(label);
  387. Label *count_label = memnew(Label);
  388. count_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  389. hb->add_child(count_label);
  390. control = popup(hb, p_severity, default_message_duration, p_tooltip);
  391. Toast &toast = toasts[control];
  392. toast.message = p_message;
  393. toast.tooltip = p_tooltip;
  394. toast.count = 1;
  395. toast.message_label = label;
  396. toast.message_count_label = count_label;
  397. } else {
  398. Toast &toast = toasts[control];
  399. if (toast.popped) {
  400. toast.count += 1;
  401. } else {
  402. toast.count = 1;
  403. }
  404. toast.remaining_time = toast.duration;
  405. toast.popped = true;
  406. control->show();
  407. vbox_container->move_child(control, vbox_container->get_child_count());
  408. _auto_hide_or_free_toasts();
  409. _update_vbox_position();
  410. _update_disable_notifications_button();
  411. main_button->queue_redraw();
  412. }
  413. // Retrieve the label back, then update the text.
  414. Label *message_label = toasts[control].message_label;
  415. ERR_FAIL_NULL(message_label);
  416. message_label->set_text(p_message);
  417. message_label->set_text_overrun_behavior(TextServer::OVERRUN_NO_TRIMMING);
  418. message_label->set_custom_minimum_size(Size2());
  419. Size2i size = message_label->get_combined_minimum_size();
  420. int limit_width = get_viewport_rect().size.x / 2; // Limit label size to half the viewport size.
  421. if (size.x > limit_width) {
  422. message_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  423. message_label->set_custom_minimum_size(Size2(limit_width, 0));
  424. }
  425. // Retrieve the count label back, then update the text.
  426. Label *message_count_label = toasts[control].message_count_label;
  427. if (toasts[control].count == 1) {
  428. message_count_label->hide();
  429. } else {
  430. message_count_label->set_text(vformat("(%d)", toasts[control].count));
  431. message_count_label->show();
  432. }
  433. vbox_container->reset_size();
  434. is_processing_error = false;
  435. set_process_internal(true);
  436. }
  437. void EditorToaster::_toast_theme_changed(Control *p_control) {
  438. ERR_FAIL_COND(!toasts.has(p_control));
  439. Toast &toast = toasts[p_control];
  440. if (toast.close_button) {
  441. toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  442. }
  443. if (toast.copy_button) {
  444. toast.copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  445. }
  446. }
  447. void EditorToaster::close(Control *p_control) {
  448. ERR_FAIL_COND(!toasts.has(p_control));
  449. toasts[p_control].remaining_time = -1.0;
  450. toasts[p_control].popped = false;
  451. }
  452. void EditorToaster::instant_close(Control *p_control) {
  453. close(p_control);
  454. p_control->set_modulate(Color(1, 1, 1, 0));
  455. }
  456. void EditorToaster::copy(Control *p_control) {
  457. ERR_FAIL_COND(!toasts.has(p_control));
  458. DisplayServer::get_singleton()->clipboard_set(toasts[p_control].message);
  459. }
  460. void EditorToaster::_bind_methods() {
  461. ClassDB::bind_method(D_METHOD("push_toast", "message", "severity", "tooltip"), &EditorToaster::_popup_str, DEFVAL(EditorToaster::SEVERITY_INFO), DEFVAL(String()));
  462. BIND_ENUM_CONSTANT(SEVERITY_INFO);
  463. BIND_ENUM_CONSTANT(SEVERITY_WARNING);
  464. BIND_ENUM_CONSTANT(SEVERITY_ERROR);
  465. }
  466. EditorToaster *EditorToaster::get_singleton() {
  467. return singleton;
  468. }
  469. EditorToaster::EditorToaster() {
  470. set_notify_transform(true);
  471. // VBox.
  472. vbox_container = memnew(VBoxContainer);
  473. vbox_container->set_as_top_level(true);
  474. vbox_container->connect(SceneStringName(resized), callable_mp(this, &EditorToaster::_update_vbox_position));
  475. add_child(vbox_container);
  476. // Theming (background).
  477. info_panel_style_background.instantiate();
  478. info_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  479. warning_panel_style_background.instantiate();
  480. warning_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  481. warning_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  482. error_panel_style_background.instantiate();
  483. error_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  484. error_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  485. Ref<StyleBoxFlat> boxes[] = { info_panel_style_background, warning_panel_style_background, error_panel_style_background };
  486. for (int i = 0; i < 3; i++) {
  487. boxes[i]->set_content_margin_individual(int(stylebox_radius * 2.5), 3, int(stylebox_radius * 2.5), 3);
  488. }
  489. // Theming (progress).
  490. info_panel_style_progress.instantiate();
  491. info_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  492. warning_panel_style_progress.instantiate();
  493. warning_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  494. warning_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  495. error_panel_style_progress.instantiate();
  496. error_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  497. error_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  498. // Main button.
  499. main_button = memnew(Button);
  500. main_button->set_accessibility_name(TTRC("Notifications:"));
  501. main_button->set_tooltip_text(TTRC("No notifications."));
  502. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  503. main_button->set_disabled(true);
  504. main_button->set_theme_type_variation("FlatMenuButton");
  505. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(true));
  506. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_repop_old));
  507. main_button->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_button));
  508. add_child(main_button);
  509. // Disable notification button.
  510. disable_notifications_panel = memnew(PanelContainer);
  511. disable_notifications_panel->set_as_top_level(true);
  512. disable_notifications_panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  513. add_child(disable_notifications_panel);
  514. disable_notifications_button = memnew(Button);
  515. disable_notifications_button->set_tooltip_text(TTRC("Silence the notifications."));
  516. disable_notifications_button->set_flat(true);
  517. disable_notifications_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(false));
  518. disable_notifications_panel->add_child(disable_notifications_button);
  519. // Other
  520. singleton = this;
  521. eh.errfunc = _error_handler;
  522. add_error_handler(&eh);
  523. }
  524. EditorToaster::~EditorToaster() {
  525. singleton = nullptr;
  526. remove_error_handler(&eh);
  527. }