editor_toaster.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*************************************************************************/
  2. /* editor_toaster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  32. #include "editor/editor_settings.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/panel_container.h"
  36. EditorToaster *EditorToaster::singleton = nullptr;
  37. void EditorToaster::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_INTERNAL_PROCESS: {
  40. double delta = get_process_delta_time();
  41. // Check if one element is hovered, if so, don't elapse time.
  42. bool hovered = false;
  43. for (const KeyValue<Control *, Toast> &element : toasts) {
  44. if (Rect2(Vector2(), element.key->get_size()).has_point(element.key->get_local_mouse_position())) {
  45. hovered = true;
  46. break;
  47. }
  48. }
  49. // Elapses the time and remove toasts if needed.
  50. if (!hovered) {
  51. for (const KeyValue<Control *, Toast> &element : toasts) {
  52. if (!element.value.popped || element.value.duration <= 0) {
  53. continue;
  54. }
  55. toasts[element.key].remaining_time -= delta;
  56. if (toasts[element.key].remaining_time < 0) {
  57. close(element.key);
  58. }
  59. element.key->queue_redraw();
  60. }
  61. } else {
  62. // Reset the timers when hovered.
  63. for (const KeyValue<Control *, Toast> &element : toasts) {
  64. if (!element.value.popped || element.value.duration <= 0) {
  65. continue;
  66. }
  67. toasts[element.key].remaining_time = element.value.duration;
  68. element.key->queue_redraw();
  69. }
  70. }
  71. // Change alpha over time.
  72. bool needs_update = false;
  73. for (const KeyValue<Control *, Toast> &element : toasts) {
  74. Color modulate = element.key->get_modulate();
  75. // Change alpha over time.
  76. if (element.value.popped && modulate.a < 1.0) {
  77. modulate.a += delta * 3;
  78. element.key->set_modulate(modulate);
  79. } else if (!element.value.popped && modulate.a > 0.0) {
  80. modulate.a -= delta * 2;
  81. element.key->set_modulate(modulate);
  82. }
  83. // Hide element if it is not visible anymore.
  84. if (modulate.a <= 0) {
  85. if (element.key->is_visible()) {
  86. element.key->hide();
  87. needs_update = true;
  88. }
  89. }
  90. }
  91. if (needs_update) {
  92. _update_vbox_position();
  93. _update_disable_notifications_button();
  94. main_button->queue_redraw();
  95. }
  96. } break;
  97. case NOTIFICATION_ENTER_TREE:
  98. case NOTIFICATION_THEME_CHANGED: {
  99. if (vbox_container->is_visible()) {
  100. main_button->set_icon(get_theme_icon(SNAME("Notification"), SNAME("EditorIcons")));
  101. } else {
  102. main_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  103. }
  104. disable_notifications_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  105. // Styleboxes background.
  106. info_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")));
  107. warning_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")));
  108. warning_panel_style_background->set_border_color(get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  109. error_panel_style_background->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")));
  110. error_panel_style_background->set_border_color(get_theme_color(SNAME("error_color"), SNAME("Editor")));
  111. // Styleboxes progress.
  112. info_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")).lightened(0.03));
  113. warning_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")).lightened(0.03));
  114. warning_panel_style_progress->set_border_color(get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  115. error_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), SNAME("Editor")).lightened(0.03));
  116. error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), SNAME("Editor")));
  117. main_button->queue_redraw();
  118. disable_notifications_button->queue_redraw();
  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. if (!EditorToaster::get_singleton() || !EditorToaster::get_singleton()->is_inside_tree()) {
  128. return;
  129. }
  130. #ifdef DEV_ENABLED
  131. bool in_dev = true;
  132. #else
  133. bool in_dev = false;
  134. #endif
  135. int show_all_setting = EDITOR_GET("interface/editor/show_internal_errors_in_toast_notifications");
  136. if (p_editor_notify || (show_all_setting == 0 && in_dev) || show_all_setting == 1) {
  137. String err_str;
  138. if (p_errorexp && p_errorexp[0]) {
  139. err_str = String::utf8(p_errorexp);
  140. } else {
  141. err_str = String::utf8(p_error);
  142. }
  143. String tooltip_str = String::utf8(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 = (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 (unsigned int i = 0; i < to_delete.size(); i++) {
  200. vbox_container->remove_child(to_delete[i]);
  201. to_delete[i]->queue_delete();
  202. toasts.erase(to_delete[i]);
  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. } else {
  209. main_button->set_tooltip_text(TTR("Show notifications."));
  210. main_button->set_modulate(Color(1, 1, 1));
  211. main_button->set_disabled(false);
  212. }
  213. }
  214. void EditorToaster::_draw_button() {
  215. bool has_one = false;
  216. Severity highest_severity = SEVERITY_INFO;
  217. for (const KeyValue<Control *, Toast> &element : toasts) {
  218. if (!element.key->is_visible()) {
  219. continue;
  220. }
  221. has_one = true;
  222. if (element.value.severity > highest_severity) {
  223. highest_severity = element.value.severity;
  224. }
  225. }
  226. if (!has_one) {
  227. return;
  228. }
  229. Color color;
  230. real_t button_radius = main_button->get_size().x / 8;
  231. switch (highest_severity) {
  232. case SEVERITY_INFO:
  233. color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  234. break;
  235. case SEVERITY_WARNING:
  236. color = get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  237. break;
  238. case SEVERITY_ERROR:
  239. color = get_theme_color(SNAME("error_color"), SNAME("Editor"));
  240. break;
  241. default:
  242. break;
  243. }
  244. main_button->draw_circle(Vector2(button_radius * 2, button_radius * 2), button_radius, color);
  245. }
  246. void EditorToaster::_draw_progress(Control *panel) {
  247. if (toasts.has(panel) && toasts[panel].remaining_time > 0 && toasts[panel].duration > 0) {
  248. Size2 size = panel->get_size();
  249. size.x *= MIN(1, Math::range_lerp(toasts[panel].remaining_time, 0, toasts[panel].duration, 0, 2));
  250. Ref<StyleBoxFlat> stylebox;
  251. switch (toasts[panel].severity) {
  252. case SEVERITY_INFO:
  253. stylebox = info_panel_style_progress;
  254. break;
  255. case SEVERITY_WARNING:
  256. stylebox = warning_panel_style_progress;
  257. break;
  258. case SEVERITY_ERROR:
  259. stylebox = error_panel_style_progress;
  260. break;
  261. default:
  262. break;
  263. }
  264. panel->draw_style_box(stylebox, Rect2(Vector2(), size));
  265. }
  266. }
  267. void EditorToaster::_set_notifications_enabled(bool p_enabled) {
  268. vbox_container->set_visible(p_enabled);
  269. if (p_enabled) {
  270. main_button->set_icon(get_theme_icon(SNAME("Notification"), SNAME("EditorIcons")));
  271. } else {
  272. main_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  273. }
  274. _update_disable_notifications_button();
  275. }
  276. void EditorToaster::_repop_old() {
  277. // Repop olds, up to max_temporary_count
  278. bool needs_update = false;
  279. int visible = 0;
  280. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  281. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  282. if (!control->is_visible()) {
  283. control->show();
  284. toasts[control].remaining_time = toasts[control].duration;
  285. toasts[control].popped = true;
  286. needs_update = true;
  287. }
  288. visible++;
  289. if (visible >= max_temporary_count) {
  290. break;
  291. }
  292. }
  293. if (needs_update) {
  294. _update_vbox_position();
  295. _update_disable_notifications_button();
  296. main_button->queue_redraw();
  297. }
  298. }
  299. Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_time, String p_tooltip) {
  300. // Create the panel according to the severity.
  301. PanelContainer *panel = memnew(PanelContainer);
  302. panel->set_tooltip_text(p_tooltip);
  303. switch (p_severity) {
  304. case SEVERITY_INFO:
  305. panel->add_theme_style_override("panel", info_panel_style_background);
  306. break;
  307. case SEVERITY_WARNING:
  308. panel->add_theme_style_override("panel", warning_panel_style_background);
  309. break;
  310. case SEVERITY_ERROR:
  311. panel->add_theme_style_override("panel", error_panel_style_background);
  312. break;
  313. default:
  314. break;
  315. }
  316. panel->set_modulate(Color(1, 1, 1, 0));
  317. panel->connect("draw", callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
  318. // Horizontal container.
  319. HBoxContainer *hbox_container = memnew(HBoxContainer);
  320. hbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  321. panel->add_child(hbox_container);
  322. // Content control.
  323. p_control->set_h_size_flags(SIZE_EXPAND_FILL);
  324. hbox_container->add_child(p_control);
  325. // Close button.
  326. if (p_time > 0.0) {
  327. Button *close_button = memnew(Button);
  328. close_button->set_flat(true);
  329. close_button->set_icon(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  330. close_button->connect("pressed", callable_mp(this, &EditorToaster::close).bind(panel));
  331. close_button->connect("theme_changed", callable_mp(this, &EditorToaster::_close_button_theme_changed).bind(close_button));
  332. hbox_container->add_child(close_button);
  333. }
  334. toasts[panel].severity = p_severity;
  335. if (p_time > 0.0) {
  336. toasts[panel].duration = p_time;
  337. toasts[panel].remaining_time = p_time;
  338. } else {
  339. toasts[panel].duration = -1.0;
  340. }
  341. toasts[panel].popped = true;
  342. vbox_container->add_child(panel);
  343. _auto_hide_or_free_toasts();
  344. _update_vbox_position();
  345. _update_disable_notifications_button();
  346. main_button->queue_redraw();
  347. return panel;
  348. }
  349. void EditorToaster::popup_str(String p_message, Severity p_severity, String p_tooltip) {
  350. if (is_processing_error) {
  351. return;
  352. }
  353. // Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
  354. // thread-safe, it's better to defer the call to the next cycle to be thread-safe.
  355. is_processing_error = true;
  356. call_deferred(SNAME("_popup_str"), p_message, p_severity, p_tooltip);
  357. is_processing_error = false;
  358. }
  359. void EditorToaster::_popup_str(String p_message, Severity p_severity, String p_tooltip) {
  360. is_processing_error = true;
  361. // Check if we already have a popup with the given message.
  362. Control *control = nullptr;
  363. for (KeyValue<Control *, Toast> element : toasts) {
  364. if (element.value.message == p_message && element.value.severity == p_severity && element.value.tooltip == p_tooltip) {
  365. control = element.key;
  366. break;
  367. }
  368. }
  369. // Create a new message if needed.
  370. if (control == nullptr) {
  371. Label *label = memnew(Label);
  372. control = popup(label, p_severity, default_message_duration, p_tooltip);
  373. toasts[control].message = p_message;
  374. toasts[control].tooltip = p_tooltip;
  375. toasts[control].count = 1;
  376. } else {
  377. if (toasts[control].popped) {
  378. toasts[control].count += 1;
  379. } else {
  380. toasts[control].count = 1;
  381. }
  382. toasts[control].remaining_time = toasts[control].duration;
  383. toasts[control].popped = true;
  384. control->show();
  385. vbox_container->move_child(control, vbox_container->get_child_count());
  386. _auto_hide_or_free_toasts();
  387. _update_vbox_position();
  388. _update_disable_notifications_button();
  389. main_button->queue_redraw();
  390. }
  391. // Retrieve the label back then update the text.
  392. Label *label = Object::cast_to<Label>(control->get_child(0)->get_child(0));
  393. ERR_FAIL_COND(!label);
  394. if (toasts[control].count == 1) {
  395. label->set_text(p_message);
  396. } else {
  397. label->set_text(vformat("%s (%d)", p_message, toasts[control].count));
  398. }
  399. is_processing_error = false;
  400. }
  401. void EditorToaster::close(Control *p_control) {
  402. ERR_FAIL_COND(!toasts.has(p_control));
  403. toasts[p_control].remaining_time = -1.0;
  404. toasts[p_control].popped = false;
  405. }
  406. void EditorToaster::_close_button_theme_changed(Control *p_close_button) {
  407. Button *close_button = Object::cast_to<Button>(p_close_button);
  408. if (close_button) {
  409. close_button->set_icon(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  410. }
  411. }
  412. EditorToaster *EditorToaster::get_singleton() {
  413. return singleton;
  414. }
  415. void EditorToaster::_bind_methods() {
  416. // Binding method to make it defer-able.
  417. ClassDB::bind_method(D_METHOD("_popup_str", "message", "severity", "tooltip"), &EditorToaster::_popup_str);
  418. }
  419. EditorToaster::EditorToaster() {
  420. set_notify_transform(true);
  421. set_process_internal(true);
  422. // VBox.
  423. vbox_container = memnew(VBoxContainer);
  424. vbox_container->set_as_top_level(true);
  425. vbox_container->connect("resized", callable_mp(this, &EditorToaster::_update_vbox_position));
  426. add_child(vbox_container);
  427. // Theming (background).
  428. info_panel_style_background.instantiate();
  429. info_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  430. warning_panel_style_background.instantiate();
  431. warning_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  432. warning_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  433. error_panel_style_background.instantiate();
  434. error_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  435. error_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  436. Ref<StyleBoxFlat> boxes[] = { info_panel_style_background, warning_panel_style_background, error_panel_style_background };
  437. for (int i = 0; i < 3; i++) {
  438. boxes[i]->set_default_margin(SIDE_LEFT, int(stylebox_radius * 2.5));
  439. boxes[i]->set_default_margin(SIDE_RIGHT, int(stylebox_radius * 2.5));
  440. boxes[i]->set_default_margin(SIDE_TOP, 3);
  441. boxes[i]->set_default_margin(SIDE_BOTTOM, 3);
  442. }
  443. // Theming (progress).
  444. info_panel_style_progress.instantiate();
  445. info_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  446. warning_panel_style_progress.instantiate();
  447. warning_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  448. warning_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  449. error_panel_style_progress.instantiate();
  450. error_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  451. error_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  452. // Main button.
  453. main_button = memnew(Button);
  454. main_button->set_tooltip_text(TTR("No notifications."));
  455. main_button->set_modulate(Color(0.5, 0.5, 0.5));
  456. main_button->set_disabled(true);
  457. main_button->set_flat(true);
  458. main_button->connect("pressed", callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(true));
  459. main_button->connect("pressed", callable_mp(this, &EditorToaster::_repop_old));
  460. main_button->connect("draw", callable_mp(this, &EditorToaster::_draw_button));
  461. add_child(main_button);
  462. // Disable notification button.
  463. disable_notifications_panel = memnew(PanelContainer);
  464. disable_notifications_panel->set_as_top_level(true);
  465. disable_notifications_panel->add_theme_style_override("panel", info_panel_style_background);
  466. add_child(disable_notifications_panel);
  467. disable_notifications_button = memnew(Button);
  468. disable_notifications_button->set_tooltip_text(TTR("Silence the notifications."));
  469. disable_notifications_button->set_flat(true);
  470. disable_notifications_button->connect("pressed", callable_mp(this, &EditorToaster::_set_notifications_enabled).bind(false));
  471. disable_notifications_panel->add_child(disable_notifications_button);
  472. // Other
  473. singleton = this;
  474. eh.errfunc = _error_handler;
  475. add_error_handler(&eh);
  476. };
  477. EditorToaster::~EditorToaster() {
  478. singleton = nullptr;
  479. remove_error_handler(&eh);
  480. }