editor_toaster.h 5.0 KB

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