Skip to content

Commit 8bcbc2f

Browse files
committed
Give TabBar overridable colors per tab
1 parent 7fbc3a5 commit 8bcbc2f

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

@@ -526,13 +530,13 @@ void TabBar::_notification(int p_what) {
526530

527531
if (tabs[i].disabled) {
528532
sb = theme_cache.tab_disabled_style;
529-
col = theme_cache.font_disabled_color;
533+
col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_DISABLED], theme_cache.font_disabled_color);
530534
} else if (i == hover) {
531535
sb = theme_cache.tab_hovered_style;
532-
col = theme_cache.font_hovered_color;
536+
col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_HOVER], theme_cache.font_hovered_color);
533537
} else {
534538
sb = theme_cache.tab_unselected_style;
535-
col = theme_cache.font_unselected_color;
539+
col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_NORMAL], theme_cache.font_unselected_color);
536540
}
537541

538542
_draw_tab(sb, col, i, rtl ? (size.width - tabs[i].ofs_cache - tabs[i].size_cache) : tabs[i].ofs_cache, false);
@@ -542,8 +546,9 @@ void TabBar::_notification(int p_what) {
542546
// Draw selected tab in the front, but only if it's visible.
543547
if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
544548
Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
549+
Color col = _select_color(tabs[current].font_color_overrides[DrawMode::DRAW_PRESSED], theme_cache.font_selected_color);
545550

546-
_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(true));
551+
_draw_tab(sb, col, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus(true));
547552
}
548553

549554
if (buttons_visible) {
@@ -1005,6 +1010,37 @@ int TabBar::get_tab_icon_max_width(int p_tab) const {
10051010
return tabs[p_tab].icon_max_width;
10061011
}
10071012

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

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

@@ -223,6 +234,10 @@ class TabBar : public Control {
223234
void set_tab_icon_max_width(int p_tab, int p_width);
224235
int get_tab_icon_max_width(int p_tab) const;
225236

237+
void set_font_color_override_all(int p_tab, const Color &p_color);
238+
void set_font_color_override(int p_tab, DrawMode p_draw_mode, const Color &p_color);
239+
Color get_font_color_override(int p_tab, DrawMode p_draw_mode) const;
240+
226241
void set_tab_disabled(int p_tab, bool p_disabled);
227242
bool is_tab_disabled(int p_tab) const;
228243

0 commit comments

Comments
 (0)