summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-10-28 12:31:38 +0200
committerRémi Verschelde <rverschelde@gmail.com>2023-10-28 12:31:38 +0200
commit17da2c5b53ffd34d15d90286e9f73dabbbe23ec4 (patch)
treec4739f2960382461f29db9ba693b75af6ca9bd87
parentb6c941fe9cdee91e2cb6ba2d8ceb95b21436bba8 (diff)
parentc3fc87964f534d5064a5d94dac7c7dc623867aa0 (diff)
downloadredot-engine-17da2c5b53ffd34d15d90286e9f73dabbbe23ec4.tar.gz
Merge pull request #83963 from YeldhamDev/try_active_tabs_first
Fix disabled tabs being selected when removing the current one
-rw-r--r--doc/classes/TabBar.xml2
-rw-r--r--doc/classes/TabContainer.xml2
-rw-r--r--scene/gui/tab_bar.cpp21
3 files changed, 21 insertions, 4 deletions
diff --git a/doc/classes/TabBar.xml b/doc/classes/TabBar.xml
index 9b462b9f50..165b187710 100644
--- a/doc/classes/TabBar.xml
+++ b/doc/classes/TabBar.xml
@@ -317,7 +317,7 @@
<signal name="tab_selected">
<param index="0" name="tab" type="int" />
<description>
- Emitted when a tab is selected via click or script, even if it is the current tab.
+ Emitted when a tab is selected via click, directional input, or script, even if it is the current tab.
</description>
</signal>
</signals>
diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml
index 1b0054ef25..b3a9264569 100644
--- a/doc/classes/TabContainer.xml
+++ b/doc/classes/TabContainer.xml
@@ -243,7 +243,7 @@
<signal name="tab_selected">
<param index="0" name="tab" type="int" />
<description>
- Emitted when a tab is selected via click or script, even if it is the current tab.
+ Emitted when a tab is selected via click, directional input, or script, even if it is the current tab.
</description>
</signal>
</signals>
diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp
index d8abb36c8d..777ca96cc4 100644
--- a/scene/gui/tab_bar.cpp
+++ b/scene/gui/tab_bar.cpp
@@ -675,7 +675,7 @@ bool TabBar::select_previous_available() {
if (target_tab < 0) {
target_tab += get_tab_count();
}
- if (!is_tab_disabled(target_tab)) {
+ if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
set_current_tab(target_tab);
return true;
}
@@ -687,7 +687,7 @@ bool TabBar::select_next_available() {
const int offset_end = (get_tab_count() - get_current_tab());
for (int i = 1; i < offset_end; i++) {
int target_tab = (get_current_tab() + i) % get_tab_count();
- if (!is_tab_disabled(target_tab)) {
+ if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
set_current_tab(target_tab);
return true;
}
@@ -1100,6 +1100,23 @@ void TabBar::remove_tab(int p_idx) {
max_drawn_tab = 0;
previous = 0;
} else {
+ // Try to change to a valid tab if possible (without firing the `tab_selected` signal).
+ for (int i = current; i < tabs.size(); i++) {
+ if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
+ current = i;
+ break;
+ }
+ }
+ // If nothing, try backwards.
+ if (is_tab_disabled(current) || is_tab_hidden(current)) {
+ for (int i = current - 1; i >= 0; i--) {
+ if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
+ current = i;
+ break;
+ }
+ }
+ }
+
offset = MIN(offset, tabs.size() - 1);
max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);