Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix wrong tab when TabContainer was not in tree

kobewi60e9bdb8ba22
diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml
index 57e1491f166..cf13c48d047 100644
--- a/doc/classes/TabContainer.xml
+++ b/doc/classes/TabContainer.xml
@@ -215,6 +215,7 @@
<member name="current_tab" type="int" setter="set_current_tab" getter="get_current_tab" default="-1">
The current tab index. When set, this index's [Control] node's [code]visible[/code] property is set to [code]true[/code] and all others are set to [code]false[/code].
A value of [code]-1[/code] means that no tab is selected.
+ [b]Note:[/b] Setting this property before the [TabContainer] is in the scene tree will not switch the current tab, even if the index is within bounds (but the property will still be assigned). The actual switch will happen when the container enters the tree.
</member>
<member name="deselect_enabled" type="bool" setter="set_deselect_enabled" getter="get_deselect_enabled" default="false">
If [code]true[/code], all tabs can be deselected so that no tab is selected. Click on the [member current_tab] to deselect it.
diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp
index 6d748b3aca4..0ab94bdb1c6 100644
--- a/scene/gui/tab_container.cpp
+++ b/scene/gui/tab_container.cpp
@@ -147,9 +147,9 @@ void TabContainer::_notification(int p_what) {
_refresh_tab_names();
}
- if (setup_current_tab >= -1) {
+ if (setup_current_tab != INVALID_CURRENT_TAB) {
set_current_tab(setup_current_tab);
- setup_current_tab = -2;
+ setup_current_tab = INVALID_CURRENT_TAB;
}
} break;
@@ -187,7 +187,7 @@ void TabContainer::_notification(int p_what) {
// beat it to the punch and make sure that the correct node is the only one visible first.
// Otherwise, it can prevent a tab change done right before this container was made visible.
Vector<Control *> controls = _get_tab_controls();
- int current = setup_current_tab > -2 ? setup_current_tab : get_current_tab();
+ int current = get_current_tab();
for (int i = 0; i < controls.size(); i++) {
controls[i]->set_visible(i == current);
}
@@ -732,7 +732,7 @@ void TabContainer::set_current_tab(int p_current) {
}
int TabContainer::get_current_tab() const {
- return tab_bar->get_current_tab();
+ return setup_current_tab != INVALID_CURRENT_TAB ? setup_current_tab : tab_bar->get_current_tab();
}
int TabContainer::get_previous_tab() const {
diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h
index 7af7b3158ee..abe971f1f15 100644
--- a/scene/gui/tab_container.h
+++ b/scene/gui/tab_container.h
@@ -41,6 +41,8 @@ class Popup;
class TabContainer : public Container {
GDCLASS(TabContainer, Container);
+ static constexpr int INVALID_CURRENT_TAB = -2;
+
public:
enum TabPosition {
POSITION_TOP,
@@ -62,8 +64,7 @@ private:
bool theme_changing = false;
Vector<Control *> children_removing;
bool drag_to_rearrange_enabled = false;
- // Set the default setup current tab to be an invalid index.
- int setup_current_tab = -2;
+ int setup_current_tab = INVALID_CURRENT_TAB;
bool updating_visibility = false;
struct ThemeCache {
diff --git a/tests/scene/test_tab_container.cpp b/tests/scene/test_tab_container.cpp
index e78e7f3c8ab..fbb4c769e12 100644
--- a/tests/scene/test_tab_container.cpp
+++ b/tests/scene/test_tab_container.cpp
@@ -370,24 +370,26 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
// Set the current tab before there are any tabs.
// This queues the current tab to update on entering the tree.
tab_container->set_current_tab(1);
- CHECK(tab_container->get_current_tab() == -1);
+ CHECK(tab_container->get_tab_bar()->get_current_tab() == -1);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
tab_container->add_child(tab0);
CHECK(tab_container->get_tab_count() == 1);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_tab_bar()->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
tab_container->add_child(tab1);
CHECK(tab_container->get_tab_count() == 2);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
tab_container->add_child(tab2);
CHECK(tab_container->get_tab_count() == 3);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
@@ -397,7 +399,7 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
MessageQueue::get_singleton()->flush();
CHECK(tab_container->get_tab_count() == 3);
CHECK(tab_container->get_current_tab() == 1);
- CHECK(tab_container->get_previous_tab() == 0);
+ CHECK(tab_container->get_previous_tab() == 0); // Previous tab of TabBar.
SIGNAL_CHECK("tab_selected", { { 1 } });
SIGNAL_CHECK("tab_changed", { { 1 } });
CHECK_FALSE(tab0->is_visible());
@@ -405,29 +407,28 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
CHECK_FALSE(tab2->is_visible());
}
- SUBCASE("[TabContainer] cannot set current tab to an invalid value before tabs are set") {
+ SUBCASE("[TabContainer] set current tab to an invalid value before tabs are set") {
tab_container->set_current_tab(100);
- CHECK(tab_container->get_current_tab() == -1);
+ CHECK(tab_container->get_current_tab() == 100);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
tab_container->add_child(tab0);
CHECK(tab_container->get_tab_count() == 1);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 100);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
tab_container->add_child(tab1);
CHECK(tab_container->get_tab_count() == 2);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 100);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
// This will print an error message as if `set_current_tab` was called after.
- ERR_PRINT_OFF;
SceneTree::get_singleton()->get_root()->add_child(tab_container);
MessageQueue::get_singleton()->flush();
CHECK(tab_container->get_tab_count() == 2);
@@ -435,7 +436,28 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
+ }
+
+ SUBCASE("[TabContainer] set current tab to an invalid value after tabs are set") {
+ SceneTree::get_singleton()->get_root()->add_child(tab_container);
+ MessageQueue::get_singleton()->flush();
+
+ ERR_PRINT_OFF;
+ tab_container->set_current_tab(100);
+ CHECK(tab_container->get_current_tab() == -1);
+ CHECK(tab_container->get_previous_tab() == -1);
+ SIGNAL_CHECK_FALSE("tab_selected");
+ SIGNAL_CHECK_FALSE("tab_changed");
ERR_PRINT_ON;
+
+ Array tab0_check = { { 0 } };
+
+ tab_container->add_child(tab0);
+ CHECK(tab_container->get_tab_count() == 1);
+ CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_previous_tab() == -1);
+ SIGNAL_CHECK("tab_selected", tab0_check);
+ SIGNAL_CHECK("tab_changed", tab0_check);
}
SUBCASE("[TabContainer] children visibility before entering tree") {
@@ -465,7 +487,8 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
tab1->show();
MessageQueue::get_singleton()->flush();
CHECK(tab_container->get_tab_count() == 2);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_tab_bar()->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
@@ -494,7 +517,8 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
tab2->show();
MessageQueue::get_singleton()->flush();
CHECK(tab_container->get_tab_count() == 3);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_tab_bar()->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 2);
CHECK(tab_container->get_previous_tab() == -1);
SIGNAL_CHECK_FALSE("tab_selected");
SIGNAL_CHECK_FALSE("tab_changed");
@@ -504,7 +528,8 @@ TEST_CASE("[SceneTree][TabContainer] initialization") {
// Whichever happens last will have priority.
tab_container->set_current_tab(1);
- CHECK(tab_container->get_current_tab() == 0);
+ CHECK(tab_container->get_tab_bar()->get_current_tab() == 0);
+ CHECK(tab_container->get_current_tab() == 1);
CHECK(tab_container->get_previous_tab() == -1);
// Current tab is set when entering the tree.