Skip to content

Commit 87acf3d

Browse files
committed
Fix Segfault and Incorrect theme drawing in Tabs
1 parent 6a6a116 commit 87acf3d

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

scene/gui/tab_bar.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,8 @@ void TabBar::_notification(int p_what) {
507507

508508
int limit_minus_buttons = size.width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
509509

510-
int ofs = tabs[offset].ofs_cache;
511-
int tab_separation_offset = 0;
512-
513510
// Draw unselected tabs in the back.
514511
for (int i = offset; i <= max_drawn_tab; i++) {
515-
tab_separation_offset = (i - offset) * theme_cache.tab_separation;
516-
517512
if (tabs[i].hidden) {
518513
continue;
519514
}
@@ -533,23 +528,15 @@ void TabBar::_notification(int p_what) {
533528
col = theme_cache.font_unselected_color;
534529
}
535530

536-
_draw_tab(sb, col, i, rtl ? (size.width - ofs - tab_separation_offset - tabs[i].size_cache) : (ofs + tab_separation_offset), false);
531+
_draw_tab(sb, col, i, rtl ? (size.width - tabs[i].ofs_cache - tabs[i].size_cache) : tabs[i].ofs_cache, false);
537532
}
538-
539-
ofs += tabs[i].size_cache;
540533
}
541534

542535
// Draw selected tab in the front, but only if it's visible.
543536
if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
544-
tab_separation_offset = (current - offset) * theme_cache.tab_separation;
545-
if (tab_alignment == ALIGNMENT_LEFT && (current - offset) > 1) {
546-
tab_separation_offset = theme_cache.tab_separation;
547-
}
548-
549537
Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
550-
float x = rtl ? (size.width - tabs[current].ofs_cache - tab_separation_offset - tabs[current].size_cache) : (tabs[current].ofs_cache + tab_separation_offset);
551538

552-
_draw_tab(sb, theme_cache.font_selected_color, current, x, has_focus());
539+
_draw_tab(sb, theme_cache.font_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus());
553540
}
554541

555542
if (buttons_visible) {
@@ -1187,7 +1174,7 @@ void TabBar::_update_cache(bool p_update_hover) {
11871174
}
11881175

11891176
w += tabs[i].size_cache;
1190-
if ((i - offset) > 0) {
1177+
if (i < max_drawn_tab) {
11911178
w += theme_cache.tab_separation;
11921179
}
11931180

@@ -1196,7 +1183,6 @@ void TabBar::_update_cache(bool p_update_hover) {
11961183
tabs.write[i].ofs_cache = 0;
11971184

11981185
w -= tabs[i].size_cache;
1199-
w -= theme_cache.tab_separation;
12001186

12011187
max_drawn_tab = i - 1;
12021188

@@ -1236,6 +1222,7 @@ void TabBar::_update_cache(bool p_update_hover) {
12361222

12371223
if (!tabs[i].hidden) {
12381224
w += tabs[i].size_cache;
1225+
w += theme_cache.tab_separation;
12391226
}
12401227
}
12411228

@@ -1827,14 +1814,11 @@ void TabBar::ensure_tab_visible(int p_idx) {
18271814

18281815
Rect2 TabBar::get_tab_rect(int p_tab) const {
18291816
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
1830-
int tab_separation_offset = (p_tab - offset) * theme_cache.tab_separation;
1831-
if (tab_alignment == ALIGNMENT_LEFT && (p_tab - offset) > 1) {
1832-
tab_separation_offset = theme_cache.tab_separation;
1833-
}
1817+
18341818
if (is_layout_rtl()) {
1835-
return Rect2(get_size().width - tabs[p_tab].ofs_cache - tab_separation_offset - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
1819+
return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
18361820
} else {
1837-
return Rect2(tabs[p_tab].ofs_cache + tab_separation_offset, 0, tabs[p_tab].size_cache, get_size().height);
1821+
return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
18381822
}
18391823
}
18401824

scene/gui/tab_container.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,27 +347,29 @@ void TabContainer::_repaint() {
347347
}
348348

349349
void TabContainer::_update_margins() {
350-
int menu_width = theme_cache.menu_icon->get_width();
351-
352350
// Directly check for validity, to avoid errors when quitting.
353351
bool has_popup = popup_obj_id.is_valid();
352+
int menu_width = 0;
353+
if (has_popup) {
354+
menu_width = theme_cache.menu_icon->get_width();
355+
}
354356

355357
if (get_tab_count() == 0) {
356358
tab_bar->set_offset(SIDE_LEFT, 0);
357-
tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
359+
tab_bar->set_offset(SIDE_RIGHT, -menu_width);
358360

359361
return;
360362
}
361363

362364
switch (get_tab_alignment()) {
363365
case TabBar::ALIGNMENT_LEFT: {
364366
tab_bar->set_offset(SIDE_LEFT, theme_cache.side_margin);
365-
tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
367+
tab_bar->set_offset(SIDE_RIGHT, -menu_width);
366368
} break;
367369

368370
case TabBar::ALIGNMENT_CENTER: {
369371
tab_bar->set_offset(SIDE_LEFT, 0);
370-
tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
372+
tab_bar->set_offset(SIDE_RIGHT, -menu_width);
371373
} break;
372374

373375
case TabBar::ALIGNMENT_RIGHT: {
@@ -384,7 +386,7 @@ void TabContainer::_update_margins() {
384386

385387
// Calculate if all the tabs would still fit if the margin was present.
386388
if (get_clip_tabs() && (tab_bar->get_offset_buttons_visible() || (get_tab_count() > 1 && (total_tabs_width + theme_cache.side_margin) > get_size().width))) {
387-
tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
389+
tab_bar->set_offset(SIDE_RIGHT, -menu_width);
388390
} else {
389391
tab_bar->set_offset(SIDE_RIGHT, -theme_cache.side_margin);
390392
}

0 commit comments

Comments
 (0)