Skip to content

Commit 4d1f26e

Browse files
committed
Merge pull request #108255 from thygrrr/tab-container-deselect-enable-fix
Fix: TabBar/TabContainer can't start with all tabs deselected
2 parents 85b0061 + c6c7b50 commit 4d1f26e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

scene/gui/tab_bar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
12661266
queue_redraw();
12671267
update_minimum_size();
12681268

1269-
if (tabs.size() == 1) {
1269+
if (!deselect_enabled && tabs.size() == 1) {
12701270
if (is_inside_tree()) {
12711271
set_current_tab(0);
12721272
} else {

tests/scene/test_tab_bar.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
5151
CHECK(tab_bar->get_previous_tab() == -1);
5252
}
5353

54-
SUBCASE("[TabBar] add tabs") {
54+
SUBCASE("[TabBar] add tabs disallowing deselection") {
55+
tab_bar->set_deselect_enabled(false);
5556
tab_bar->add_tab("tab0");
5657
CHECK(tab_bar->get_tab_count() == 1);
5758
CHECK(tab_bar->get_current_tab() == 0);
@@ -92,6 +93,23 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
9293
CHECK_FALSE(tab_bar->is_tab_hidden(2));
9394
}
9495

96+
SUBCASE("[TabBar] add tabs allowing deselection") {
97+
tab_bar->set_deselect_enabled(true);
98+
tab_bar->add_tab("tab0");
99+
CHECK(tab_bar->get_tab_count() == 1);
100+
CHECK(tab_bar->get_current_tab() == -1);
101+
CHECK(tab_bar->get_previous_tab() == -1);
102+
SIGNAL_CHECK_FALSE("tab_selected");
103+
SIGNAL_CHECK_FALSE("tab_changed");
104+
105+
tab_bar->add_tab("tab1");
106+
CHECK(tab_bar->get_tab_count() == 2);
107+
CHECK(tab_bar->get_current_tab() == -1);
108+
CHECK(tab_bar->get_previous_tab() == -1);
109+
SIGNAL_CHECK_FALSE("tab_selected");
110+
SIGNAL_CHECK_FALSE("tab_changed");
111+
}
112+
95113
SUBCASE("[TabBar] set tab count") {
96114
// Adds multiple tabs at once.
97115
tab_bar->set_tab_count(3);
@@ -320,7 +338,7 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
320338
SIGNAL_CHECK("tab_selected", { { -1 } });
321339
SIGNAL_CHECK("tab_changed", { { -1 } });
322340

323-
// Adding a tab will still set the current tab to 0.
341+
// Adding first tab will NOT change the current tab. (stays deselected)
324342
tab_bar->clear_tabs();
325343
CHECK(tab_bar->get_current_tab() == -1);
326344
CHECK(tab_bar->get_previous_tab() == -1);
@@ -329,10 +347,10 @@ TEST_CASE("[SceneTree][TabBar] tab operations") {
329347
tab_bar->add_tab("tab1");
330348
tab_bar->add_tab("tab2");
331349
CHECK(tab_bar->get_tab_count() == 3);
332-
CHECK(tab_bar->get_current_tab() == 0);
350+
CHECK(tab_bar->get_current_tab() == -1);
333351
CHECK(tab_bar->get_previous_tab() == -1);
334-
SIGNAL_CHECK("tab_selected", { { 0 } });
335-
SIGNAL_CHECK("tab_changed", { { 0 } });
352+
SIGNAL_CHECK_FALSE("tab_selected");
353+
SIGNAL_CHECK_FALSE("tab_changed");
336354

337355
tab_bar->set_current_tab(-1);
338356
SIGNAL_DISCARD("tab_selected");

0 commit comments

Comments
 (0)