Browse Source

TabBar current tab now stays -1 when adding first tab on deselect_enable

Tiger Jove 1 month ago
parent
commit
c6c7b508b6
2 changed files with 24 additions and 6 deletions
  1. 1 1
      scene/gui/tab_bar.cpp
  2. 23 5
      tests/scene/test_tab_bar.h

+ 1 - 1
scene/gui/tab_bar.cpp

@@ -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 {

+ 23 - 5
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");