Skip to content

Commit 025976d

Browse files
committed
Improve EditorToaster code
1 parent 1bffd6c commit 025976d

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

editor/gui/editor_toaster.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ void EditorToaster::_notification(int p_what) {
108108
}
109109
} break;
110110

111-
case NOTIFICATION_ENTER_TREE:
112111
case NOTIFICATION_THEME_CHANGED: {
113112
if (vbox_container->is_visible()) {
114113
main_button->set_button_icon(get_editor_theme_icon(SNAME("Notification")));
@@ -134,9 +133,6 @@ void EditorToaster::_notification(int p_what) {
134133

135134
error_panel_style_progress->set_bg_color(get_theme_color(SNAME("base_color"), EditorStringName(Editor)).lightened(0.03));
136135
error_panel_style_progress->set_border_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
137-
138-
main_button->queue_redraw();
139-
disable_notifications_button->queue_redraw();
140136
} break;
141137

142138
case NOTIFICATION_TRANSFORM_CHANGED: {
@@ -243,6 +239,7 @@ void EditorToaster::_auto_hide_or_free_toasts() {
243239
main_button->set_tooltip_text(TTR("No notifications."));
244240
main_button->set_modulate(Color(0.5, 0.5, 0.5));
245241
main_button->set_disabled(true);
242+
set_process_internal(false);
246243
} else {
247244
main_button->set_tooltip_text(TTR("Show notifications."));
248245
main_button->set_modulate(Color(1, 1, 1));
@@ -361,6 +358,9 @@ Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_
361358
}
362359
panel->set_modulate(Color(1, 1, 1, 0));
363360
panel->connect(SceneStringName(draw), callable_mp(this, &EditorToaster::_draw_progress).bind(panel));
361+
panel->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_toast_theme_changed).bind(panel));
362+
363+
Toast &toast = toasts[panel];
364364

365365
// Horizontal container.
366366
HBoxContainer *hbox_container = memnew(HBoxContainer);
@@ -375,20 +375,20 @@ Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_
375375
if (p_time > 0.0) {
376376
Button *close_button = memnew(Button);
377377
close_button->set_flat(true);
378-
close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
379378
close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::close).bind(panel));
380-
close_button->connect(SceneStringName(theme_changed), callable_mp(this, &EditorToaster::_close_button_theme_changed).bind(close_button));
381379
hbox_container->add_child(close_button);
380+
381+
toast.close_button = close_button;
382382
}
383383

384-
toasts[panel].severity = p_severity;
384+
toast.severity = p_severity;
385385
if (p_time > 0.0) {
386-
toasts[panel].duration = p_time;
387-
toasts[panel].remaining_time = p_time;
386+
toast.duration = p_time;
387+
toast.remaining_time = p_time;
388388
} else {
389-
toasts[panel].duration = -1.0;
389+
toast.duration = -1.0;
390390
}
391-
toasts[panel].popped = true;
391+
toast.popped = true;
392392
vbox_container->add_child(panel);
393393
_auto_hide_or_free_toasts();
394394
_update_vbox_position();
@@ -406,7 +406,7 @@ void EditorToaster::popup_str(const String &p_message, Severity p_severity, cons
406406
// Since "_popup_str" adds nodes to the tree, and since the "add_child" method is not
407407
// thread-safe, it's better to defer the call to the next cycle to be thread-safe.
408408
is_processing_error = true;
409-
MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &EditorToaster::_popup_str).bind(p_message, p_severity, p_tooltip));
409+
callable_mp(this, &EditorToaster::_popup_str).call_deferred(p_message, p_severity, p_tooltip);
410410
is_processing_error = false;
411411
}
412412

@@ -433,19 +433,22 @@ void EditorToaster::_popup_str(const String &p_message, Severity p_severity, con
433433
hb->add_child(count_label);
434434

435435
control = popup(hb, p_severity, default_message_duration, p_tooltip);
436-
toasts[control].message = p_message;
437-
toasts[control].tooltip = p_tooltip;
438-
toasts[control].count = 1;
439-
toasts[control].message_label = label;
440-
toasts[control].message_count_label = count_label;
436+
437+
Toast &toast = toasts[control];
438+
toast.message = p_message;
439+
toast.tooltip = p_tooltip;
440+
toast.count = 1;
441+
toast.message_label = label;
442+
toast.message_count_label = count_label;
441443
} else {
442-
if (toasts[control].popped) {
443-
toasts[control].count += 1;
444+
Toast &toast = toasts[control];
445+
if (toast.popped) {
446+
toast.count += 1;
444447
} else {
445-
toasts[control].count = 1;
448+
toast.count = 1;
446449
}
447-
toasts[control].remaining_time = toasts[control].duration;
448-
toasts[control].popped = true;
450+
toast.remaining_time = toast.duration;
451+
toast.popped = true;
449452
control->show();
450453
vbox_container->move_child(control, vbox_container->get_child_count());
451454
_auto_hide_or_free_toasts();
@@ -480,6 +483,16 @@ void EditorToaster::_popup_str(const String &p_message, Severity p_severity, con
480483
vbox_container->reset_size();
481484

482485
is_processing_error = false;
486+
set_process_internal(true);
487+
}
488+
489+
void EditorToaster::_toast_theme_changed(Control *p_control) {
490+
ERR_FAIL_COND(!toasts.has(p_control));
491+
492+
Toast &toast = toasts[p_control];
493+
if (toast.close_button) {
494+
toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
495+
}
483496
}
484497

485498
void EditorToaster::close(Control *p_control) {
@@ -488,20 +501,12 @@ void EditorToaster::close(Control *p_control) {
488501
toasts[p_control].popped = false;
489502
}
490503

491-
void EditorToaster::_close_button_theme_changed(Control *p_close_button) {
492-
Button *close_button = Object::cast_to<Button>(p_close_button);
493-
if (close_button) {
494-
close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
495-
}
496-
}
497-
498504
EditorToaster *EditorToaster::get_singleton() {
499505
return singleton;
500506
}
501507

502508
EditorToaster::EditorToaster() {
503509
set_notify_transform(true);
504-
set_process_internal(true);
505510

506511
// VBox.
507512
vbox_container = memnew(VBoxContainer);

editor/gui/editor_toaster.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
#ifndef EDITOR_TOASTER_H
3232
#define EDITOR_TOASTER_H
3333

34-
#include "core/string/ustring.h"
35-
#include "core/templates/local_vector.h"
3634
#include "scene/gui/box_container.h"
3735

3836
class Button;
@@ -76,6 +74,9 @@ class EditorToaster : public HBoxContainer {
7674
real_t remaining_time = 0.0;
7775
bool popped = false;
7876

77+
// Buttons
78+
Button *close_button = nullptr;
79+
7980
// Messages
8081
String message;
8182
String tooltip;
@@ -101,7 +102,7 @@ class EditorToaster : public HBoxContainer {
101102
void _set_notifications_enabled(bool p_enabled);
102103
void _repop_old();
103104
void _popup_str(const String &p_message, Severity p_severity, const String &p_tooltip);
104-
void _close_button_theme_changed(Control *p_close_button);
105+
void _toast_theme_changed(Control *p_control);
105106

106107
protected:
107108
static EditorToaster *singleton;

0 commit comments

Comments
 (0)