From c6c7b508b66bd8a047c5dd796d734c33c8769f6a Mon Sep 17 00:00:00 2001 From: Tiger Jove Date: Thu, 3 Jul 2025 23:10:05 +0200 Subject: [PATCH] TabBar current tab now stays -1 when adding first tab on deselect_enable --- scene/gui/tab_bar.cpp | 2 +- tests/scene/test_tab_bar.h | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp index 80cac446be6..ceb3ba43a45 100644 --- a/scene/gui/tab_bar.cpp +++ b/scene/gui/tab_bar.cpp @@ -1266,7 +1266,7 @@ void TabBar::add_tab(const String &p_str, const Ref &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 { diff --git a/tests/scene/test_tab_bar.h b/tests/scene/test_tab_bar.h index 459a26f78c2..ed4639e87a8 100644 --- a/tests/scene/test_tab_bar.h +++ b/tests/scene/test_tab_bar.h @@ -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");