@@ -500,6 +500,13 @@ void TabContainer::_on_tab_visibility_changed(Control *p_child) {
500500 updating_visibility = false ;
501501}
502502
503+ void TabContainer::_refresh_tab_indices () {
504+ Vector<Control *> controls = _get_tab_controls ();
505+ for (int i = 0 ; i < controls.size (); i++) {
506+ controls[i]->set_meta (" _tab_index" , i);
507+ }
508+ }
509+
503510void TabContainer::_refresh_tab_names () {
504511 Vector<Control *> controls = _get_tab_controls ();
505512 for (int i = 0 ; i < controls.size (); i++) {
@@ -523,6 +530,7 @@ void TabContainer::add_child_notify(Node *p_child) {
523530 c->hide ();
524531
525532 tab_bar->add_tab (p_child->get_name ());
533+ c->set_meta (" _tab_index" , tab_bar->get_tab_count () - 1 );
526534
527535 _update_margins ();
528536 if (get_tab_count () == 1 ) {
@@ -547,19 +555,10 @@ void TabContainer::move_child_notify(Node *p_child) {
547555
548556 Control *c = Object::cast_to<Control>(p_child);
549557 if (c && !c->is_set_as_top_level ()) {
550- int old_idx = -1 ;
551- String tab_name = String (c->get_meta (" _tab_name" , c->get_name ()));
552-
553- // Find the previous tab index of the control.
554- for (int i = 0 ; i < get_tab_count (); i++) {
555- if (get_tab_title (i) == tab_name) {
556- old_idx = i;
557- break ;
558- }
559- }
560-
561- tab_bar->move_tab (old_idx, get_tab_idx_from_control (c));
558+ tab_bar->move_tab (c->get_meta (" _tab_index" ), get_tab_idx_from_control (c));
562559 }
560+
561+ _refresh_tab_indices ();
563562}
564563
565564void TabContainer::remove_child_notify (Node *p_child) {
@@ -578,14 +577,18 @@ void TabContainer::remove_child_notify(Node *p_child) {
578577
579578 // As the child hasn't been removed yet, keep track of it so when the "tab_changed" signal is fired it can be ignored.
580579 children_removing.push_back (c);
580+
581581 tab_bar->remove_tab (idx);
582+ _refresh_tab_indices ();
583+
582584 children_removing.erase (c);
583585
584586 _update_margins ();
585587 if (get_tab_count () == 0 ) {
586588 queue_redraw ();
587589 }
588590
591+ p_child->remove_meta (" _tab_index" );
589592 p_child->remove_meta (" _tab_name" );
590593 p_child->disconnect (" renamed" , callable_mp (this , &TabContainer::_refresh_tab_names));
591594 p_child->disconnect (SNAME (" visibility_changed" ), callable_mp (this , &TabContainer::_on_tab_visibility_changed));
0 commit comments