editor_toaster.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* editor_toaster.h */
  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. #ifndef EDITOR_TOASTER_H
  31. #define EDITOR_TOASTER_H
  32. #include "core/string/ustring.h"
  33. #include "core/templates/local_vector.h"
  34. #include "scene/gui/box_container.h"
  35. class Button;
  36. class PanelContainer;
  37. class StyleBoxFlat;
  38. class EditorToaster : public HBoxContainer {
  39. GDCLASS(EditorToaster, HBoxContainer);
  40. public:
  41. enum Severity {
  42. SEVERITY_INFO = 0,
  43. SEVERITY_WARNING,
  44. SEVERITY_ERROR,
  45. };
  46. private:
  47. ErrorHandlerList eh;
  48. const int stylebox_radius = 3;
  49. Ref<StyleBoxFlat> info_panel_style_background;
  50. Ref<StyleBoxFlat> warning_panel_style_background;
  51. Ref<StyleBoxFlat> error_panel_style_background;
  52. Ref<StyleBoxFlat> info_panel_style_progress;
  53. Ref<StyleBoxFlat> warning_panel_style_progress;
  54. Ref<StyleBoxFlat> error_panel_style_progress;
  55. Button *main_button = nullptr;
  56. PanelContainer *disable_notifications_panel = nullptr;
  57. Button *disable_notifications_button = nullptr;
  58. VBoxContainer *vbox_container = nullptr;
  59. const int max_temporary_count = 5;
  60. struct Toast {
  61. Severity severity = SEVERITY_INFO;
  62. // Timing.
  63. real_t duration = -1.0;
  64. real_t remaining_time = 0.0;
  65. bool popped = false;
  66. // Messages
  67. String message;
  68. String tooltip;
  69. int count = 0;
  70. Label *message_label = nullptr;
  71. Label *message_count_label = nullptr;
  72. };
  73. HashMap<Control *, Toast> toasts;
  74. bool is_processing_error = false; // Makes sure that we don't handle errors that are triggered within the EditorToaster error processing.
  75. const double default_message_duration = 5.0;
  76. static void _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);
  77. static void _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);
  78. void _update_vbox_position();
  79. void _update_disable_notifications_button();
  80. void _auto_hide_or_free_toasts();
  81. void _draw_button();
  82. void _draw_progress(Control *panel);
  83. void _set_notifications_enabled(bool p_enabled);
  84. void _repop_old();
  85. void _popup_str(const String &p_message, Severity p_severity, const String &p_tooltip);
  86. void _close_button_theme_changed(Control *p_close_button);
  87. protected:
  88. static EditorToaster *singleton;
  89. void _notification(int p_what);
  90. public:
  91. static EditorToaster *get_singleton();
  92. Control *popup(Control *p_control, Severity p_severity = SEVERITY_INFO, double p_time = 0.0, const String &p_tooltip = String());
  93. void popup_str(const String &p_message, Severity p_severity = SEVERITY_INFO, const String &p_tooltip = String());
  94. void close(Control *p_control);
  95. EditorToaster();
  96. ~EditorToaster();
  97. };
  98. VARIANT_ENUM_CAST(EditorToaster::Severity);
  99. #endif // EDITOR_TOASTER_H