editor_toaster.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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_settings.h"
  32. #include "editor/editor_string_names.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(TTR("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(TTR("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_text(p_tooltip);
  304. switch (p_severity) {
  305. case SEVERITY_INFO:
  306. panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  307. break;
  308. case SEVERITY_WARNING:
  309. panel->add_theme_style_override(SceneStringName(panel), warning_panel_style_background);
  310. break;
  311. case SEVERITY_ERROR:
  312. panel->add_theme_style_override(SceneStringName(panel), error_panel_style_background);
  313. break;
  314. default:
  315. break;
  316. }
  317. panel->set_modulate(Color(1, 1, 1, 0));
  318. panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
  319. panel->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_toast_theme_changed).bind(panel));
  320. Toast &toast = toasts[panel];
  321. // Horizontal container.
  322. HBoxContainer *hbox_container = memnew(HBoxContainer);
  323. hbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  324. panel->add_child(hbox_container);
  325. // Content control.
  326. p_control->set_h_size_flags(SIZE_EXPAND_FILL);
  327. hbox_container->add_child(p_control);
  328. // Add buttons.
  329. if (p_time > 0.0) {
  330. Button *copy_button = memnew(Button);
  331. copy_button->set_flat(true);
  332. copy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::copy).bind(panel));
  333. hbox_container->add_child(copy_button);
  334. Button *close_button = memnew(Button);
  335. close_button->set_flat(true);
  336. close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::instant_close).bind(panel));
  337. hbox_container->add_child(close_button);
  338. toast.copy_button = copy_button;
  339. toast.close_button = close_button;
  340. }
  341. toast.severity = p_severity;
  342. if (p_time > 0.0) {
  343. toast.duration = p_time;
  344. toast.remaining_time = p_time;
  345. } else {
  346. toast.duration = -1.0;
  347. }
  348. toast.popped = true;
  349. vbox_container->add_child(panel);
  350. _auto_hide_or_free_toasts();
  351. _update_vbox_position();
  352. _update_disable_notifications_button();
  353. main_button->queue_redraw();
  354. return panel;
  355. }
  356. void EditorToaster::popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  357. if (is_processing_error) {
  358. return;
  359. }
  360. // Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
  361. // thread-safe, it's better to defer the call to the next cycle to be thread-safe.
  362. is_processing_error = true;
  363. callable_mp(this, &EditorToaster::_popup_str).call_deferred(p_message, p_severity, p_tooltip);
  364. is_processing_error = false;
  365. }
  366. void EditorToaster::_popup_str(const String &p_message, Severity p_severity, const String &p_tooltip) {
  367. is_processing_error = true;
  368. // Check if we already have a popup with the given message.
  369. Control *control = nullptr;
  370. for (KeyValue<Control *, Toast> element : toasts) {
  371. if (element.value.message == p_message && element.value.severity == p_severity && element.value.tooltip == p_tooltip) {
  372. control = element.key;
  373. break;
  374. }
  375. }
  376. // Create a new message if needed.
  377. if (control == nullptr) {
  378. HBoxContainer *hb = memnew(HBoxContainer);
  379. hb->add_theme_constant_override("separation", 0);
  380. Label *label = memnew(Label);
  381. hb->add_child(label);
  382. Label *count_label = memnew(Label);
  383. hb->add_child(count_label);
  384. control = popup(hb, p_severity, default_message_duration, p_tooltip);
  385. Toast &toast = toasts[control];
  386. toast.message = p_message;
  387. toast.tooltip = p_tooltip;
  388. toast.count = 1;
  389. toast.message_label = label;
  390. toast.message_count_label = count_label;
  391. } else {
  392. Toast &toast = toasts[control];
  393. if (toast.popped) {
  394. toast.count += 1;
  395. } else {
  396. toast.count = 1;
  397. }
  398. toast.remaining_time = toast.duration;
  399. toast.popped = true;
  400. control->show();
  401. vbox_container->move_child(control, vbox_container->get_child_count());
  402. _auto_hide_or_free_toasts();
  403. _update_vbox_position();
  404. _update_disable_notifications_button();
  405. main_button->queue_redraw();
  406. }
  407. // Retrieve the label back, then update the text.
  408. Label *message_label = toasts[control].message_label;
  409. ERR_FAIL_NULL(message_label);
  410. message_label->set_text(p_message);
  411. message_label->set_text_overrun_behavior(TextServer::OVERRUN_NO_TRIMMING);
  412. message_label->set_custom_minimum_size(Size2());
  413. Size2i size = message_label->get_combined_minimum_size();
  414. int limit_width = get_viewport_rect().size.x / 2; // Limit label size to half the viewport size.
  415. if (size.x > limit_width) {
  416. message_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  417. message_label->set_custom_minimum_size(Size2(limit_width, 0));
  418. }
  419. // Retrieve the count label back, then update the text.
  420. Label *message_count_label = toasts[control].message_count_label;
  421. if (toasts[control].count == 1) {
  422. message_count_label->hide();
  423. } else {
  424. message_count_label->set_text(vformat("(%d)", toasts[control].count));
  425. message_count_label->show();
  426. }
  427. vbox_container->reset_size();
  428. is_processing_error = false;
  429. set_process_internal(true);
  430. }
  431. void EditorToaster::_toast_theme_changed(Control *p_control) {
  432. ERR_FAIL_COND(!toasts.has(p_control));
  433. Toast &toast = toasts[p_control];
  434. if (toast.close_button) {
  435. toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  436. }
  437. if (toast.copy_button) {
  438. toast.copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  439. }
  440. }
  441. void EditorToaster::close(Control *p_control) {
  442. ERR_FAIL_COND(!toasts.has(p_control));
  443. toasts[p_control].remaining_time = -1.0;
  444. toasts[p_control].popped = false;
  445. }
  446. void EditorToaster::instant_close(Control *p_control) {
  447. close(p_control);
  448. p_control->set_modulate(Color(1, 1, 1, 0));
  449. }
  450. void EditorToaster::copy(Control *p_control) {
  451. ERR_FAIL_COND(!toasts.has(p_control));
  452. DisplayServer::get_singleton()->clipboard_set(toasts[p_control].message);
  453. }
  454. void EditorToaster::_bind_methods() {
  455. ClassDB::bind_method(D_METHOD("push_toast", "message", "severity", "tooltip"), &EditorToaster::_popup_str, DEFVAL(EditorToaster::SEVERITY_INFO), DEFVAL(String()));
  456. BIND_ENUM_CONSTANT(SEVERITY_INFO);
  457. BIND_ENUM_CONSTANT(SEVERITY_WARNING);
  458. BIND_ENUM_CONSTANT(SEVERITY_ERROR);
  459. }
  460. EditorToaster *EditorToaster::get_singleton() {
  461. return singleton;
  462. }
  463. EditorToaster::EditorToaster() {
  464. set_notify_transform(true);
  465. // VBox.
  466. vbox_container = memnew(VBoxContainer);
  467. vbox_container->set_as_top_level(true);
  468. vbox_container->connect(SceneStringName(resized), callable_mp(this, &EditorToaster::_update_vbox_position));
  469. add_child(vbox_container);
  470. // Theming (background).
  471. info_panel_style_background.instantiate();
  472. info_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  473. warning_panel_style_background.instantiate();
  474. warning_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  475. warning_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  476. error_panel_style_background.instantiate();
  477. error_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  478. error_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  479. Ref<StyleBoxFlat> boxes[] = { info_panel_style_background, warning_panel_style_background, error_panel_style_background };
  480. for (int i = 0; i < 3; i++) {
  481. boxes[i]->set_content_margin_individual(int(stylebox_radius * 2.5), 3, int(stylebox_radius * 2.5), 3);
  482. }
  483. // Theming (progress).
  484. info_panel_style_progress.instantiate();
  485. info_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  486. warning_panel_style_progress.instantiate();
  487. warning_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  488. warning_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  489. error_panel_style_progress.instantiate();
  490. error_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  491. error_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  492. // Main button.
  493. main_button = memnew(Button);
  494. main_button->set_tooltip_text(TTR("No notifications."));
  495. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  496. main_button->set_disabled(true);
  497. main_button->set_theme_type_variation("FlatMenuButton");
  498. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(true));
  499. main_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_repop_old));
  500. main_button->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_button));
  501. add_child(main_button);
  502. // Disable notification button.
  503. disable_notifications_panel = memnew(PanelContainer);
  504. disable_notifications_panel->set_as_top_level(true);
  505. disable_notifications_panel->add_theme_style_override(SceneStringName(panel), info_panel_style_background);
  506. add_child(disable_notifications_panel);
  507. disable_notifications_button = memnew(Button);
  508. disable_notifications_button->set_tooltip_text(TTR("Silence the notifications."));
  509. disable_notifications_button->set_flat(true);
  510. disable_notifications_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(false));
  511. disable_notifications_panel->add_child(disable_notifications_button);
  512. // Other
  513. singleton = this;
  514. eh.errfunc = _error_handler;
  515. add_error_handler(&eh);
  516. }
  517. EditorToaster::~EditorToaster() {
  518. singleton = nullptr;
  519. remove_error_handler(&eh);
  520. }