editor_toaster.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. const Color base_color = get_theme_color(SNAME("base_color"), EditorStringName(Editor));
  109. const Color bg_color = base_color.lerp(get_theme_color(SNAME("mono_color"), EditorStringName(Editor)), 0.08);
  110. info_panel_style_background->set_bg_color(bg_color);
  111. warning_panel_style_background->set_bg_color(bg_color);
  112. warning_panel_style_background->set_border_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  113. error_panel_style_background->set_bg_color(bg_color);
  114. error_panel_style_background->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  115. // Styleboxes progress.
  116. const Color bg_progress_color = base_color.lerp(get_theme_color(SNAME("mono_color"), EditorStringName(Editor)), 0.135);
  117. info_panel_style_progress->set_bg_color(bg_progress_color);
  118. warning_panel_style_progress->set_bg_color(bg_progress_color);
  119. warning_panel_style_progress->set_border_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  120. error_panel_style_progress->set_bg_color(bg_progress_color);
  121. error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  122. } break;
  123. case NOTIFICATION_TRANSFORM_CHANGED: {
  124. _update_vbox_position();
  125. _update_disable_notifications_button();
  126. } break;
  127. }
  128. }
  129. 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) {
  130. // This may be called from a thread. Since we will deal with non-thread-safe elements,
  131. // we have to put it in the queue for safety.
  132. 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);
  133. }
  134. 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) {
  135. if (!EditorToaster::get_singleton() || !EditorToaster::get_singleton()->is_inside_tree()) {
  136. return;
  137. }
  138. #ifdef DEV_ENABLED
  139. bool in_dev = true;
  140. #else
  141. bool in_dev = false;
  142. #endif
  143. int show_all_setting = EDITOR_GET("interface/editor/show_internal_errors_in_toast_notifications");
  144. if (p_editor_notify || (show_all_setting == 0 && in_dev) || show_all_setting == 1) {
  145. String err_str = !p_errorexp.is_empty() ? p_errorexp : p_error;
  146. String tooltip_str = p_file + ":" + itos(p_line);
  147. if (!p_editor_notify) {
  148. if (p_type == ERR_HANDLER_WARNING) {
  149. err_str = "INTERNAL WARNING: " + err_str;
  150. } else {
  151. err_str = "INTERNAL ERROR: " + err_str;
  152. }
  153. }
  154. Severity severity = ((ErrorHandlerType)p_type == ERR_HANDLER_WARNING) ? SEVERITY_WARNING : SEVERITY_ERROR;
  155. EditorToaster::get_singleton()->popup_str(err_str, severity, tooltip_str);
  156. }
  157. }
  158. // This is kind of a workaround because it's hard to keep the VBox anchored to the bottom.
  159. void EditorToaster::_update_vbox_position() {
  160. vbox_container->set_size(Vector2());
  161. Point2 pos = get_global_position();
  162. Size2 vbox_size = vbox_container->get_size();
  163. pos.y -= vbox_size.y + 5 * EDSCALE;
  164. if (!is_layout_rtl()) {
  165. pos.x = pos.x - vbox_size.x + get_size().x;
  166. }
  167. vbox_container->set_position(pos);
  168. }
  169. void EditorToaster::_update_disable_notifications_button() {
  170. bool any_visible = false;
  171. for (KeyValue<Control *, Toast> element : toasts) {
  172. if (element.key->is_visible()) {
  173. any_visible = true;
  174. break;
  175. }
  176. }
  177. if (!any_visible || !vbox_container->is_visible()) {
  178. disable_notifications_panel->hide();
  179. } else {
  180. disable_notifications_panel->show();
  181. Point2 pos = get_global_position();
  182. int sep = 5 * EDSCALE;
  183. Size2 disable_panel_size = disable_notifications_panel->get_minimum_size();
  184. pos.y -= disable_panel_size.y + sep;
  185. if (is_layout_rtl()) {
  186. pos.x = pos.x - disable_panel_size.x - sep;
  187. } else {
  188. pos.x += get_size().x + sep;
  189. }
  190. disable_notifications_panel->set_position(pos);
  191. }
  192. }
  193. void EditorToaster::_auto_hide_or_free_toasts() {
  194. // Hide or free old temporary items.
  195. int visible_temporary = 0;
  196. int temporary = 0;
  197. LocalVector<Control *> to_delete;
  198. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  199. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  200. if (toasts[control].duration <= 0) {
  201. continue; // Ignore non-temporary toasts.
  202. }
  203. temporary++;
  204. if (control->is_visible()) {
  205. visible_temporary++;
  206. }
  207. // Hide
  208. if (visible_temporary > max_temporary_count) {
  209. close(control);
  210. }
  211. // Free
  212. if (temporary > max_temporary_count * 2) {
  213. to_delete.push_back(control);
  214. }
  215. }
  216. // Delete the control right away (removed as child) as it might cause issues otherwise when iterative over the vbox_container children.
  217. for (Control *c : to_delete) {
  218. vbox_container->remove_child(c);
  219. c->queue_free();
  220. toasts.erase(c);
  221. }
  222. if (toasts.is_empty()) {
  223. main_button->set_tooltip_text(TTRC("No notifications."));
  224. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  225. main_button->set_disabled(true);
  226. set_process_internal(false);
  227. } else {
  228. main_button->set_tooltip_text(TTRC("Show notifications."));
  229. main_button->set_modulate(Color(1, 1, 1));
  230. main_button->set_disabled(false);
  231. }
  232. }
  233. void EditorToaster::_draw_button() {
  234. bool has_one = false;
  235. Severity highest_severity = SEVERITY_INFO;
  236. for (const KeyValue<Control *, Toast> &element : toasts) {
  237. if (!element.key->is_visible()) {
  238. continue;
  239. }
  240. has_one = true;
  241. if (element.value.severity > highest_severity) {
  242. highest_severity = element.value.severity;
  243. }
  244. }
  245. if (!has_one) {
  246. return;
  247. }
  248. Color color;
  249. real_t button_radius = main_button->get_size().x / 8;
  250. switch (highest_severity) {
  251. case SEVERITY_INFO:
  252. color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  253. break;
  254. case SEVERITY_WARNING:
  255. color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  256. break;
  257. case SEVERITY_ERROR:
  258. color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  259. break;
  260. default:
  261. break;
  262. }
  263. main_button->draw_circle(Vector2(button_radius * 2, button_radius * 2), button_radius, color);
  264. }
  265. void EditorToaster::_draw_progress(Control *panel) {
  266. if (toasts.has(panel) && toasts[panel].remaining_time > 0 && toasts[panel].duration > 0) {
  267. Ref<StyleBoxFlat> stylebox;
  268. switch (toasts[panel].severity) {
  269. case SEVERITY_INFO:
  270. stylebox = info_panel_style_progress;
  271. break;
  272. case SEVERITY_WARNING:
  273. stylebox = warning_panel_style_progress;
  274. break;
  275. case SEVERITY_ERROR:
  276. stylebox = error_panel_style_progress;
  277. break;
  278. default:
  279. break;
  280. }
  281. Size2 size = panel->get_size();
  282. Size2 progress = size;
  283. progress.width *= MIN(1, Math::remap(toasts[panel].remaining_time, 0, toasts[panel].duration, 0, 2));
  284. if (is_layout_rtl()) {
  285. panel->draw_style_box(stylebox, Rect2(size - progress, progress));
  286. } else {
  287. panel->draw_style_box(stylebox, Rect2(Vector2(), progress));
  288. }
  289. }
  290. }
  291. void EditorToaster::_set_notifications_enabled(bool p_enabled) {
  292. vbox_container->set_visible(p_enabled);
  293. if (p_enabled) {
  294. main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification")));
  295. } else {
  296. main_button->set_button_icon(get_editor_theme_icon(SNAME("NotificationDisabled")));
  297. }
  298. _update_disable_notifications_button();
  299. }
  300. void EditorToaster::_repop_old() {
  301. // Repop olds, up to max_temporary_count
  302. bool needs_update = false;
  303. int visible_count = 0;
  304. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  305. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  306. if (!control->is_visible()) {
  307. control->show();
  308. toasts[control].remaining_time = toasts[control].duration;
  309. toasts[control].popped = true;
  310. needs_update = true;
  311. }
  312. visible_count++;
  313. if (visible_count >= max_temporary_count) {
  314. break;
  315. }
  316. }
  317. if (needs_update) {
  318. _update_vbox_position();
  319. _update_disable_notifications_button();
  320. main_button->queue_redraw();
  321. }
  322. }
  323. Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_time, const String &p_tooltip) {
  324. // Create the panel according to the severity.
  325. PanelContainer *panel = memnew(PanelContainer);
  326. panel->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  327. panel->set_tooltip_text(p_tooltip);
  328. switch (p_severity) {
  329. case SEVERITY_INFO:
  330. panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  331. break;
  332. case SEVERITY_WARNING:
  333. panel->add_theme_style_override(SceneStringName(panel), warning_panel_style_background);
  334. break;
  335. case SEVERITY_ERROR:
  336. panel->add_theme_style_override(SceneStringName(panel), error_panel_style_background);
  337. break;
  338. default:
  339. break;
  340. }
  341. panel->set_modulate(Color(1, 1, 1, 0));
  342. panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
  343. panel->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_toast_theme_changed).bind(panel));
  344. Toast &toast = toasts[panel];
  345. // Horizontal container.
  346. HBoxContainer *hbox_container = memnew(HBoxContainer);
  347. hbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  348. panel->add_child(hbox_container);
  349. // Content control.
  350. p_control->set_h_size_flags(SIZE_EXPAND_FILL);
  351. hbox_container->add_child(p_control);
  352. // Add buttons.
  353. if (p_time > 0.0) {
  354. Button *copy_button = memnew(Button);
  355. copy_button->set_accessibility_name(TTRC("Copy"));
  356. copy_button->set_flat(true);
  357. copy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::copy).bind(panel));
  358. hbox_container->add_child(copy_button);
  359. Button *close_button = memnew(Button);
  360. close_button->set_accessibility_name(TTRC("Close"));
  361. close_button->set_flat(true);
  362. close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::instant_close).bind(panel));
  363. hbox_container->add_child(close_button);
  364. toast.copy_button = copy_button;
  365. toast.close_button = close_button;
  366. }
  367. toast.severity = p_severity;
  368. if (p_time > 0.0) {
  369. toast.duration = p_time;
  370. toast.remaining_time = p_time;
  371. } else {
  372. toast.duration = -1.0;
  373. }
  374. toast.popped = true;
  375. vbox_container->add_child(panel);
  376. _auto_hide_or_free_toasts();
  377. _update_vbox_position();
  378. _update_disable_notifications_button();
  379. main_button->queue_redraw();
  380. return panel;
  381. }
  382. void EditorToaster::popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  383. if (is_processing_error) {
  384. return;
  385. }
  386. // Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
  387. // thread-safe, it's better to defer the call to the next cycle to be thread-safe.
  388. is_processing_error = true;
  389. MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &EditorToaster::_popup_str), p_message, p_severity, p_tooltip);
  390. is_processing_error = false;
  391. }
  392. void EditorToaster::_popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  393. is_processing_error = true;
  394. // Check if we already have a popup with the given message.
  395. Control *control = nullptr;
  396. for (KeyValue<Control *, Toast> element : toasts) {
  397. if (element.value.message == p_message && element.value.severity == p_severity && element.value.tooltip == p_tooltip) {
  398. control = element.key;
  399. break;
  400. }
  401. }
  402. // Create a new message if needed.
  403. if (control == nullptr) {
  404. HBoxContainer *hb = memnew(HBoxContainer);
  405. hb->add_theme_constant_override("separation", 0);
  406. Label *label = memnew(Label);
  407. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  408. label->set_focus_mode(FOCUS_ACCESSIBILITY);
  409. hb->add_child(label);
  410. Label *count_label = memnew(Label);
  411. count_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  412. hb->add_child(count_label);
  413. control = popup(hb, p_severity, default_message_duration, p_tooltip);
  414. Toast &toast = toasts[control];
  415. toast.message = p_message;
  416. toast.tooltip = p_tooltip;
  417. toast.count = 1;
  418. toast.message_label = label;
  419. toast.message_count_label = count_label;
  420. } else {
  421. Toast &toast = toasts[control];
  422. if (toast.popped) {
  423. toast.count += 1;
  424. } else {
  425. toast.count = 1;
  426. }
  427. toast.remaining_time = toast.duration;
  428. toast.popped = true;
  429. control->show();
  430. vbox_container->move_child(control, vbox_container->get_child_count());
  431. _auto_hide_or_free_toasts();
  432. _update_vbox_position();
  433. _update_disable_notifications_button();
  434. main_button->queue_redraw();
  435. }
  436. // Retrieve the label back, then update the text.
  437. Label *message_label = toasts[control].message_label;
  438. ERR_FAIL_NULL(message_label);
  439. message_label->set_text(p_message);
  440. message_label->set_text_overrun_behavior(TextServer::OVERRUN_NO_TRIMMING);
  441. message_label->set_custom_minimum_size(Size2());
  442. Size2i size = message_label->get_combined_minimum_size();
  443. int limit_width = get_viewport_rect().size.x / 2; // Limit label size to half the viewport size.
  444. if (size.x > limit_width) {
  445. message_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  446. message_label->set_custom_minimum_size(Size2(limit_width, 0));
  447. }
  448. // Retrieve the count label back, then update the text.
  449. Label *message_count_label = toasts[control].message_count_label;
  450. if (toasts[control].count == 1) {
  451. message_count_label->hide();
  452. } else {
  453. message_count_label->set_text(vformat("(%d)", toasts[control].count));
  454. message_count_label->show();
  455. }
  456. vbox_container->reset_size();
  457. is_processing_error = false;
  458. set_process_internal(true);
  459. }
  460. void EditorToaster::_toast_theme_changed(Control *p_control) {
  461. ERR_FAIL_COND(!toasts.has(p_control));
  462. Toast &toast = toasts[p_control];
  463. if (toast.close_button) {
  464. toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  465. }
  466. if (toast.copy_button) {
  467. toast.copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  468. }
  469. }
  470. void EditorToaster::close(Control *p_control) {
  471. ERR_FAIL_COND(!toasts.has(p_control));
  472. toasts[p_control].remaining_time = -1.0;
  473. toasts[p_control].popped = false;
  474. }
  475. void EditorToaster::instant_close(Control *p_control) {
  476. close(p_control);
  477. p_control->set_modulate(Color(1, 1, 1, 0));
  478. }
  479. void EditorToaster::copy(Control *p_control) {
  480. ERR_FAIL_COND(!toasts.has(p_control));
  481. DisplayServer::get_singleton()->clipboard_set(toasts[p_control].message);
  482. }
  483. void EditorToaster::_bind_methods() {
  484. ClassDB::bind_method(D_METHOD("push_toast", "message", "severity", "tooltip"), &EditorToaster::_popup_str, DEFVAL(EditorToaster::SEVERITY_INFO), DEFVAL(String()));
  485. BIND_ENUM_CONSTANT(SEVERITY_INFO);
  486. BIND_ENUM_CONSTANT(SEVERITY_WARNING);
  487. BIND_ENUM_CONSTANT(SEVERITY_ERROR);
  488. }
  489. EditorToaster *EditorToaster::get_singleton() {
  490. return singleton;
  491. }
  492. EditorToaster::EditorToaster() {
  493. set_notify_transform(true);
  494. // VBox.
  495. vbox_container = memnew(VBoxContainer);
  496. vbox_container->set_as_top_level(true);
  497. vbox_container->connect(SceneStringName(resized), callable_mp(this, &EditorToaster::_update_vbox_position));
  498. add_child(vbox_container);
  499. Side border_side = is_layout_rtl() ? SIDE_RIGHT : SIDE_LEFT;
  500. // Theming (background).
  501. info_panel_style_background.instantiate();
  502. info_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  503. warning_panel_style_background.instantiate();
  504. warning_panel_style_background->set_border_width(border_side, stylebox_radius * EDSCALE);
  505. warning_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  506. error_panel_style_background.instantiate();
  507. error_panel_style_background->set_border_width(border_side, stylebox_radius * EDSCALE);
  508. error_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  509. Ref<StyleBoxFlat> boxes[] = { info_panel_style_background, warning_panel_style_background, error_panel_style_background };
  510. for (int i = 0; i < 3; i++) {
  511. boxes[i]->set_content_margin_individual(int(stylebox_radius * 2.5), 3, int(stylebox_radius * 2.5), 3);
  512. }
  513. // Theming (progress).
  514. info_panel_style_progress.instantiate();
  515. info_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  516. warning_panel_style_progress.instantiate();
  517. warning_panel_style_progress->set_border_width(border_side, stylebox_radius * EDSCALE);
  518. warning_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  519. error_panel_style_progress.instantiate();
  520. error_panel_style_progress->set_border_width(border_side, stylebox_radius * EDSCALE);
  521. error_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  522. // Main button.
  523. main_button = memnew(Button);
  524. main_button->set_accessibility_name(TTRC("Notifications:"));
  525. main_button->set_tooltip_text(TTRC("No notifications."));
  526. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  527. main_button->set_disabled(true);
  528. main_button->set_theme_type_variation("FlatMenuButton");
  529. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(true));
  530. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_repop_old));
  531. main_button->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_button));
  532. add_child(main_button);
  533. // Disable notification button.
  534. disable_notifications_panel = memnew(PanelContainer);
  535. disable_notifications_panel->set_as_top_level(true);
  536. disable_notifications_panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  537. add_child(disable_notifications_panel);
  538. disable_notifications_button = memnew(Button);
  539. disable_notifications_button->set_tooltip_text(TTRC("Silence the notifications."));
  540. disable_notifications_button->set_flat(true);
  541. disable_notifications_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(false));
  542. disable_notifications_panel->add_child(disable_notifications_button);
  543. // Other
  544. singleton = this;
  545. eh.errfunc = _error_handler;
  546. add_error_handler(&eh);
  547. }
  548. EditorToaster::~EditorToaster() {
  549. singleton = nullptr;
  550. remove_error_handler(&eh);
  551. }