Skip to content

Commit 8ce4f80

Browse files
committed
Merge pull request #106263 from lodetrick/tabbar-individual-colors
Add support for custom font colors in the TabBar
2 parents d3285f5 + 914a72f commit 8ce4f80

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

scene/gui/tab_bar.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include "scene/main/viewport.h"
3737
#include "scene/theme/theme_db.h"
3838

39+
static inline Color _select_color(const Color &p_override_color, const Color &p_default_color) {
40+
return p_override_color.a > 0 ? p_override_color : p_default_color;
41+
}
42+
3943
Size2 TabBar::get_minimum_size() const {
4044
Size2 ms;
4145

@@ -527,15 +531,15 @@ void TabBar::_notification(int p_what) {
527531

528532
if (tabs[i].disabled) {
529533
sb = theme_cache.tab_disabled_style;
530-
fnt_col = theme_cache.font_disabled_color;
534+
fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_DISABLED], theme_cache.font_disabled_color);
531535
icn_col = theme_cache.icon_disabled_color;
532536
} else if (i == hover) {
533537
sb = theme_cache.tab_hovered_style;
534-
fnt_col = theme_cache.font_hovered_color;
538+
fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_HOVER], theme_cache.font_hovered_color);
535539
icn_col = theme_cache.icon_hovered_color;
536540
} else {
537541
sb = theme_cache.tab_unselected_style;
538-
fnt_col = theme_cache.font_unselected_color;
542+
fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_NORMAL], theme_cache.font_unselected_color);
539543
icn_col = theme_cache.icon_unselected_color;
540544
}
541545

@@ -546,8 +550,9 @@ void TabBar::_notification(int p_what) {
546550
// Draw selected tab in the front, but only if it's visible.
547551
if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
548552
Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
553+
Color col = _select_color(tabs[current].font_color_overrides[DrawMode::DRAW_PRESSED], theme_cache.font_selected_color);
549554

550-
_draw_tab(sb, theme_cache.font_selected_color, theme_cache.icon_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus(true));
555+
_draw_tab(sb, col, theme_cache.icon_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus(true));
551556
}
552557

553558
if (buttons_visible) {
@@ -1009,6 +1014,37 @@ int TabBar::get_tab_icon_max_width(int p_tab) const {
10091014
return tabs[p_tab].icon_max_width;
10101015
}
10111016

1017+
void TabBar::set_font_color_override_all(int p_tab, const Color &p_color) {
1018+
ERR_FAIL_INDEX(p_tab, tabs.size());
1019+
1020+
Tab &tab = tabs.write[p_tab];
1021+
for (int i = 0; i < DrawMode::DRAW_MAX; i++) {
1022+
tab.font_color_overrides[i] = p_color;
1023+
}
1024+
1025+
queue_redraw();
1026+
}
1027+
1028+
void TabBar::set_font_color_override(int p_tab, DrawMode p_draw_mode, const Color &p_color) {
1029+
ERR_FAIL_INDEX(p_tab, tabs.size());
1030+
ERR_FAIL_INDEX(p_draw_mode, DrawMode::DRAW_MAX);
1031+
1032+
if (tabs[p_tab].font_color_overrides[p_draw_mode] == p_color) {
1033+
return;
1034+
}
1035+
1036+
tabs.write[p_tab].font_color_overrides[p_draw_mode] = p_color;
1037+
1038+
queue_redraw();
1039+
}
1040+
1041+
Color TabBar::get_font_color_override(int p_tab, DrawMode p_draw_mode) const {
1042+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1043+
ERR_FAIL_INDEX_V(p_draw_mode, DrawMode::DRAW_MAX, Color());
1044+
1045+
return tabs[p_tab].font_color_overrides[p_draw_mode];
1046+
}
1047+
10121048
void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
10131049
ERR_FAIL_INDEX(p_tab, tabs.size());
10141050

scene/gui/tab_bar.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ class TabBar : public Control {
5252
CLOSE_BUTTON_MAX
5353
};
5454

55+
enum DrawMode {
56+
DRAW_NORMAL,
57+
DRAW_PRESSED,
58+
DRAW_HOVER,
59+
DRAW_DISABLED,
60+
DRAW_MAX,
61+
};
62+
5563
private:
5664
struct Tab {
5765
mutable RID accessibility_item_element;
5866
mutable bool accessibility_item_dirty = true;
5967

68+
// Corresponds to color overrides for the DrawMode enum
69+
Color font_color_overrides[DrawMode::DRAW_MAX] = { Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0) };
70+
6071
String text;
6172
String tooltip;
6273

@@ -228,6 +239,10 @@ class TabBar : public Control {
228239
void set_tab_icon_max_width(int p_tab, int p_width);
229240
int get_tab_icon_max_width(int p_tab) const;
230241

242+
void set_font_color_override_all(int p_tab, const Color &p_color);
243+
void set_font_color_override(int p_tab, DrawMode p_draw_mode, const Color &p_color);
244+
Color get_font_color_override(int p_tab, DrawMode p_draw_mode) const;
245+
231246
void set_tab_disabled(int p_tab, bool p_disabled);
232247
bool is_tab_disabled(int p_tab) const;
233248

0 commit comments

Comments
 (0)