Merge pull request #108255 from thygrrr/tab-container-deselect-enable-fix

Fix: TabBar/TabContainer can't start with all tabs deselected
This commit is contained in:
Thaddeus Crews 2025-07-07 10:08:04 -05:00
commit 4d1f26e1fd
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
2 changed files with 24 additions and 6 deletions

View file

@ -1266,7 +1266,7 @@ void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
queue_redraw();
update_minimum_size();
if (tabs.size() == 1) {
if (!deselect_enabled && tabs.size() == 1) {
if (is_inside_tree()) {
set_current_tab(0);
} else {

View file

@ -51,7 +51,8 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
CHECK(tab_bar->get_previous_tab() == -1);
}
SUBCASE("[TabBar] add tabs") {
SUBCASE("[TabBar] add tabs disallowing deselection") {
tab_bar->set_deselect_enabled(false);
tab_bar->add_tab("tab0");
CHECK(tab_bar->get_tab_count() == 1);
CHECK(tab_bar->get_current_tab() == 0);
@ -92,6 +93,23 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
CHECK_FALSE(tab_bar->is_tab_hidden(2));
}
SUBCASE("[TabBar] add tabs allowing deselection") {
tab_bar->set_deselect_enabled(true);
tab_bar->add_tab("tab0");
CHECK(tab_bar->get_tab_count() == 1);
CHECK(tab_bar->get_current_tab() == -1);
CHECK(tab_bar->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
tab_bar->add_tab("tab1");
CHECK(tab_bar->get_tab_count() == 2);
CHECK(tab_bar->get_current_tab() == -1);
CHECK(tab_bar->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
}
SUBCASE("[TabBar] set tab count") {
// Adds multiple tabs at once.
tab_bar->set_tab_count(3);
@ -320,7 +338,7 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
SIGNAL_CHECK("tab_selected", { { -1 } });
SIGNAL_CHECK("tab_changed", { { -1 } });
// Adding a tab will still set the current tab to 0.
// Adding first tab will NOT change the current tab. (stays deselected)
tab_bar->clear_tabs();
CHECK(tab_bar->get_current_tab() == -1);
CHECK(tab_bar->get_previous_tab() == -1);
@ -329,10 +347,10 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
tab_bar->add_tab("tab1");
tab_bar->add_tab("tab2");
CHECK(tab_bar->get_tab_count() == 3);
CHECK(tab_bar->get_current_tab() == 0);
CHECK(tab_bar->get_current_tab() == -1);
CHECK(tab_bar->get_previous_tab() == -1);
SIGNAL_CHECK("tab_selected", { { 0 } });
SIGNAL_CHECK("tab_changed", { { 0 } });
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
tab_bar->set_current_tab(-1);
SIGNAL_DISCARD("tab_selected");