Skip to content

Commit

Permalink
Fix TabBar initialization issue and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbdev committed Sep 20, 2024
1 parent 2be730a commit 99cca82
Show file tree
Hide file tree
Showing 7 changed files with 1,548 additions and 6 deletions.
15 changes: 12 additions & 3 deletions scene/gui/tab_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion scene/gui/tab_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ class TabBar : public Control {
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;
Expand Down Expand Up @@ -249,6 +250,7 @@ class TabBar : public Control {
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;

Expand Down
4 changes: 2 additions & 2 deletions scene/gui/tab_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<Control *> 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);
}
Expand Down
Loading

0 comments on commit 99cca82

Please sign in to comment.