Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TabContainer desync when tabs share names #90415

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions scene/gui/tab_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -578,14 +577,18 @@ 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();
if (get_tab_count() == 0) {
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));
Expand Down
1 change: 1 addition & 0 deletions scene/gui/tab_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class TabContainer : public Container {
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();
Expand Down
Loading