diff options
author | Michael Alexsander <michaelalexsander@protonmail.com> | 2024-04-09 02:02:05 -0300 |
---|---|---|
committer | Michael Alexsander <michaelalexsander@protonmail.com> | 2024-04-09 02:02:05 -0300 |
commit | adea1a4b9bf72460f344f55ece1356b7d04a22fd (patch) | |
tree | 10e5a1b6bf0d34d532eb6ebb9e0d63b473fec514 | |
parent | a5565c86ffd3e47292ddd5eb4efc84602419fad0 (diff) | |
download | redot-engine-adea1a4b9bf72460f344f55ece1356b7d04a22fd.tar.gz |
Fix `TabContainer` desync when tabs share names
-rw-r--r-- | scene/gui/tab_container.cpp | 27 | ||||
-rw-r--r-- | scene/gui/tab_container.h | 1 |
2 files changed, 16 insertions, 12 deletions
diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index df3c9631f9..105f4484b2 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -500,6 +500,13 @@ void TabContainer::_on_tab_visibility_changed(Control *p_child) { updating_visibility = false; } +void TabContainer::_refresh_tab_indices() { + Vector<Control *> controls = _get_tab_controls(); + for (int i = 0; i < controls.size(); i++) { + controls[i]->set_meta("_tab_index", i); + } +} + void TabContainer::_refresh_tab_names() { Vector<Control *> controls = _get_tab_controls(); for (int i = 0; i < controls.size(); i++) { @@ -523,6 +530,7 @@ void TabContainer::add_child_notify(Node *p_child) { c->hide(); tab_bar->add_tab(p_child->get_name()); + c->set_meta("_tab_index", tab_bar->get_tab_count() - 1); _update_margins(); if (get_tab_count() == 1) { @@ -547,19 +555,10 @@ void TabContainer::move_child_notify(Node *p_child) { Control *c = Object::cast_to<Control>(p_child); if (c && !c->is_set_as_top_level()) { - int old_idx = -1; - String tab_name = String(c->get_meta("_tab_name", c->get_name())); - - // Find the previous tab index of the control. - for (int i = 0; i < get_tab_count(); i++) { - if (get_tab_title(i) == tab_name) { - old_idx = i; - break; - } - } - - tab_bar->move_tab(old_idx, get_tab_idx_from_control(c)); + tab_bar->move_tab(c->get_meta("_tab_index"), get_tab_idx_from_control(c)); } + + _refresh_tab_indices(); } void TabContainer::remove_child_notify(Node *p_child) { @@ -578,7 +577,10 @@ void TabContainer::remove_child_notify(Node *p_child) { // As the child hasn't been removed yet, keep track of it so when the "tab_changed" signal is fired it can be ignored. children_removing.push_back(c); + tab_bar->remove_tab(idx); + _refresh_tab_indices(); + children_removing.erase(c); _update_margins(); @@ -586,6 +588,7 @@ void TabContainer::remove_child_notify(Node *p_child) { queue_redraw(); } + p_child->remove_meta("_tab_index"); p_child->remove_meta("_tab_name"); p_child->disconnect("renamed", callable_mp(this, &TabContainer::_refresh_tab_names)); p_child->disconnect(SNAME("visibility_changed"), callable_mp(this, &TabContainer::_on_tab_visibility_changed)); diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h index 03eff5d944..8645a6d14e 100644 --- a/scene/gui/tab_container.h +++ b/scene/gui/tab_container.h @@ -101,6 +101,7 @@ private: Vector<Control *> _get_tab_controls() const; void _on_theme_changed(); void _repaint(); + void _refresh_tab_indices(); void _refresh_tab_names(); void _update_margins(); void _on_mouse_exited(); |