From f2f34e9c50906596cc9eb340cf708ac0103ca8c8 Mon Sep 17 00:00:00 2001 From: kit Date: Fri, 20 Sep 2024 15:40:55 -0400 Subject: Fix TabBar initialization issue and add tests --- scene/gui/tab_bar.cpp | 15 ++++++++++++--- scene/gui/tab_bar.h | 4 +++- scene/gui/tab_container.cpp | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) (limited to 'scene') diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp index 90bb0799b1..6cc7ec3bd5 100644 --- a/scene/gui/tab_bar.cpp +++ b/scene/gui/tab_bar.cpp @@ -354,6 +354,8 @@ void TabBar::_notification(int p_what) { if (scroll_to_selected) { ensure_tab_visible(current); } + // Set initialized even if no tabs were set. + initialized = true; } break; case NOTIFICATION_INTERNAL_PROCESS: { @@ -655,10 +657,10 @@ void TabBar::set_tab_count(int p_count) { } if (!initialized) { - if (queued_current != current) { - current = queued_current; - } initialized = true; + if (queued_current != CURRENT_TAB_UNINITIALIZED && queued_current != current) { + set_current_tab(queued_current); + } } queue_redraw(); @@ -740,6 +742,13 @@ bool TabBar::select_next_available() { return false; } +void TabBar::set_tab_offset(int p_offset) { + ERR_FAIL_INDEX(p_offset, tabs.size()); + offset = p_offset; + _update_cache(); + queue_redraw(); +} + int TabBar::get_tab_offset() const { return offset; } diff --git a/scene/gui/tab_bar.h b/scene/gui/tab_bar.h index d62b39ae16..f5ae75de51 100644 --- a/scene/gui/tab_bar.h +++ b/scene/gui/tab_bar.h @@ -117,8 +117,9 @@ private: bool scroll_to_selected = true; int tabs_rearrange_group = -1; + static const int CURRENT_TAB_UNINITIALIZED = -2; bool initialized = false; - int queued_current = -1; + int queued_current = CURRENT_TAB_UNINITIALIZED; const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5; const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20; @@ -249,6 +250,7 @@ public: bool select_previous_available(); bool select_next_available(); + void set_tab_offset(int p_offset); int get_tab_offset() const; bool get_offset_buttons_visible() const; diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index eb9616c939..33e72428cf 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -190,7 +190,7 @@ void TabContainer::_notification(int p_what) { } break; case NOTIFICATION_VISIBILITY_CHANGED: { - if (!is_visible() || setup_current_tab > -2) { + if (!is_visible()) { return; } @@ -200,7 +200,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 controls = _get_tab_controls(); - int current = get_current_tab(); + int current = setup_current_tab > -2 ? setup_current_tab : get_current_tab(); for (int i = 0; i < controls.size(); i++) { controls[i]->set_visible(i == current); } -- cgit v1.2.3