Merge pull request #98778 from jaydensipe/add-copy-to-toast

Add copy button to toast notification
This commit is contained in:
Rémi Verschelde 2024-12-02 15:50:48 +01:00
commit d05323793f
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 17 additions and 1 deletions

View File

@ -371,13 +371,19 @@ Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_
p_control->set_h_size_flags(SIZE_EXPAND_FILL);
hbox_container->add_child(p_control);
// Close button.
// Add buttons.
if (p_time > 0.0) {
Button *copy_button = memnew(Button);
copy_button->set_flat(true);
copy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::copy).bind(panel));
hbox_container->add_child(copy_button);
Button *close_button = memnew(Button);
close_button->set_flat(true);
close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorToaster::instant_close).bind(panel));
hbox_container->add_child(close_button);
toast.copy_button = copy_button;
toast.close_button = close_button;
}
@ -493,6 +499,9 @@ void EditorToaster::_toast_theme_changed(Control *p_control) {
if (toast.close_button) {
toast.close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
}
if (toast.copy_button) {
toast.copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
}
}
void EditorToaster::close(Control *p_control) {
@ -506,6 +515,11 @@ void EditorToaster::instant_close(Control *p_control) {
p_control->set_modulate(Color(1, 1, 1, 0));
}
void EditorToaster::copy(Control *p_control) {
ERR_FAIL_COND(!toasts.has(p_control));
DisplayServer::get_singleton()->clipboard_set(toasts[p_control].message);
}
void EditorToaster::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_toast", "message", "severity", "tooltip"), &EditorToaster::_popup_str, DEFVAL(EditorToaster::SEVERITY_INFO), DEFVAL(String()));

View File

@ -75,6 +75,7 @@ private:
bool popped = false;
// Buttons
Button *copy_button = nullptr;
Button *close_button = nullptr;
// Messages
@ -117,6 +118,7 @@ public:
void popup_str(const String &p_message, Severity p_severity = SEVERITY_INFO, const String &p_tooltip = String());
void close(Control *p_control);
void instant_close(Control *p_control);
void copy(Control *p_control);
EditorToaster();
~EditorToaster();