Improve EditorToaster code

This commit is contained in:
kobewi 2024-11-03 12:49:06 +01:00
parent 1bffd6c73b
commit 025976dc45
2 changed files with 39 additions and 33 deletions

View file

@ -108,7 +108,6 @@ void EditorToaster::_notification(int p_what) {
} }
} break; } break;
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: { case NOTIFICATION_THEME_CHANGED: {
if (vbox_container->is_visible()) { if (vbox_container->is_visible()) {
main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification"))); main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification")));
@ -134,9 +133,6 @@ void EditorToaster::_notification(int p_what) {
error_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03)); error_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03));
error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor))); error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
main_button->queue_redraw();
disable_notifications_button->queue_redraw();
} break; } break;
case NOTIFICATION_TRANSFORM_CHANGED: { case NOTIFICATION_TRANSFORM_CHANGED: {
@ -243,6 +239,7 @@ void EditorToaster::_auto_hide_or_free_toasts() {
main_button->set_tooltip_text(TTR("No notifications.")); main_button->set_tooltip_text(TTR("No notifications."));
main_button->set_modulate(Color(0.5, 0.5, 0.5)); main_button->set_modulate(Color(0.5, 0.5, 0.5));
main_button->set_disabled(true); main_button->set_disabled(true);
set_process_internal(false);
} else { } else {
main_button->set_tooltip_text(TTR("Show notifications.")); main_button->set_tooltip_text(TTR("Show notifications."));
main_button->set_modulate(Color(1, 1, 1)); main_button->set_modulate(Color(1, 1, 1));
@ -361,6 +358,9 @@ Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_
} }
panel->set_modulate(Color(1, 1, 1, 0)); panel->set_modulate(Color(1, 1, 1, 0));
panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel)); panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
panel->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_toast_theme_changed).bind(panel));
Toast &toast = toasts[panel];
// Horizontal container. // Horizontal container.
HBoxContainer *hbox_container = memnew(HBoxContainer); HBoxContainer *hbox_container = memnew(HBoxContainer);
@ -375,20 +375,20 @@ Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_
if (p_time > 0.0) { if (p_time > 0.0) {
Button *close_button = memnew(Button); Button *close_button = memnew(Button);
close_button->set_flat(true); close_button->set_flat(true);
close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::close).bind(panel)); close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::close).bind(panel));
close_button->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_close_button_theme_changed).bind(close_button));
hbox_container->add_child(close_button); hbox_container->add_child(close_button);
toast.close_button = close_button;
} }
toasts[panel].severity = p_severity; toast.severity = p_severity;
if (p_time > 0.0) { if (p_time > 0.0) {
toasts[panel].duration = p_time; toast.duration = p_time;
toasts[panel].remaining_time = p_time; toast.remaining_time = p_time;
} else { } else {
toasts[panel].duration = -1.0; toast.duration = -1.0;
} }
toasts[panel].popped = true; toast.popped = true;
vbox_container->add_child(panel); vbox_container->add_child(panel);
_auto_hide_or_free_toasts(); _auto_hide_or_free_toasts();
_update_vbox_position(); _update_vbox_position();
@ -406,7 +406,7 @@ void EditorToaster::popup_str(const String &p_message, Severity p_severity, cons
// Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not // Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
// thread-safe, it's better to defer the call to the next cycle to be thread-safe. // thread-safe, it's better to defer the call to the next cycle to be thread-safe.
is_processing_error = true; is_processing_error = true;
MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &EditorToaster::_popup_str).bind(p_message, p_severity, p_tooltip)); callable_mp(this, &EditorToaster::_popup_str).call_deferred(p_message, p_severity, p_tooltip);
is_processing_error = false; is_processing_error = false;
} }
@ -433,19 +433,22 @@ void EditorToaster::_popup_str(const String &p_message, Severity p_severity, con
hb->add_child(count_label); hb->add_child(count_label);
control = popup(hb, p_severity, default_message_duration, p_tooltip); control = popup(hb, p_severity, default_message_duration, p_tooltip);
toasts[control].message = p_message;
toasts[control].tooltip = p_tooltip; Toast &toast = toasts[control];
toasts[control].count = 1; toast.message = p_message;
toasts[control].message_label = label; toast.tooltip = p_tooltip;
toasts[control].message_count_label = count_label; toast.count = 1;
toast.message_label = label;
toast.message_count_label = count_label;
} else { } else {
if (toasts[control].popped) { Toast &toast = toasts[control];
toasts[control].count += 1; if (toast.popped) {
toast.count += 1;
} else { } else {
toasts[control].count = 1; toast.count = 1;
} }
toasts[control].remaining_time = toasts[control].duration; toast.remaining_time = toast.duration;
toasts[control].popped = true; toast.popped = true;
control->show(); control->show();
vbox_container->move_child(control, vbox_container->get_child_count()); vbox_container->move_child(control, vbox_container->get_child_count());
_auto_hide_or_free_toasts(); _auto_hide_or_free_toasts();
@ -480,6 +483,16 @@ void EditorToaster::_popup_str(const String &p_message, Severity p_severity, con
vbox_container->reset_size(); vbox_container->reset_size();
is_processing_error = false; is_processing_error = false;
set_process_internal(true);
}
void EditorToaster::_toast_theme_changed(Control *p_control) {
ERR_FAIL_COND(!toasts.has(p_control));
Toast &toast = toasts[p_control];
if (toast.close_button) {
toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
}
} }
void EditorToaster::close(Control *p_control) { void EditorToaster::close(Control *p_control) {
@ -488,20 +501,12 @@ void EditorToaster::close(Control *p_control) {
toasts[p_control].popped = false; toasts[p_control].popped = false;
} }
void EditorToaster::_close_button_theme_changed(Control *p_close_button) {
Button *close_button = Object::cast_to<Button>(p_close_button);
if (close_button) {
close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
}
}
EditorToaster *EditorToaster::get_singleton() { EditorToaster *EditorToaster::get_singleton() {
return singleton; return singleton;
} }
EditorToaster::EditorToaster() { EditorToaster::EditorToaster() {
set_notify_transform(true); set_notify_transform(true);
set_process_internal(true);
// VBox. // VBox.
vbox_container = memnew(VBoxContainer); vbox_container = memnew(VBoxContainer);

View file

@ -31,8 +31,6 @@
#ifndef EDITOR_TOASTER_H #ifndef EDITOR_TOASTER_H
#define EDITOR_TOASTER_H #define EDITOR_TOASTER_H
#include "core/string/ustring.h"
#include "core/templates/local_vector.h"
#include "scene/gui/box_container.h" #include "scene/gui/box_container.h"
class Button; class Button;
@ -76,6 +74,9 @@ private:
real_t remaining_time = 0.0; real_t remaining_time = 0.0;
bool popped = false; bool popped = false;
// Buttons
Button *close_button = nullptr;
// Messages // Messages
String message; String message;
String tooltip; String tooltip;
@ -101,7 +102,7 @@ private:
void _set_notifications_enabled(bool p_enabled); void _set_notifications_enabled(bool p_enabled);
void _repop_old(); void _repop_old();
void _popup_str(const String &p_message, Severity p_severity, const String &p_tooltip); void _popup_str(const String &p_message, Severity p_severity, const String &p_tooltip);
void _close_button_theme_changed(Control *p_close_button); void _toast_theme_changed(Control *p_control);
protected: protected:
static EditorToaster *singleton; static EditorToaster *singleton;