Merge pull request #42766 from volzhs/tab-container

Put unselected tabs back in TabContainer control
This commit is contained in:
Rémi Verschelde 2020-10-28 09:51:41 +01:00 committed by GitHub
commit 60ad64a101
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 43 deletions

View file

@ -282,7 +282,6 @@ void TabContainer::_notification(int p_what) {
Color font_color_bg = get_theme_color("font_color_bg"); Color font_color_bg = get_theme_color("font_color_bg");
Color font_color_disabled = get_theme_color("font_color_disabled"); Color font_color_disabled = get_theme_color("font_color_disabled");
int side_margin = get_theme_constant("side_margin"); int side_margin = get_theme_constant("side_margin");
int icon_text_distance = get_theme_constant("icon_separation");
// Find out start and width of the header area. // Find out start and width of the header area.
int header_x = side_margin; int header_x = side_margin;
@ -348,61 +347,33 @@ void TabContainer::_notification(int p_what) {
break; break;
} }
// Draw the tab area. // Draw unselected tabs in back
panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
// Draw all visible tabs.
int x = 0; int x = 0;
int x_current = 0;
for (int i = 0; i < tab_widths.size(); i++) { for (int i = 0; i < tab_widths.size(); i++) {
if (get_tab_hidden(i)) { if (get_tab_hidden(i)) {
continue; continue;
} }
Ref<StyleBox> tab_style;
Color font_color;
if (get_tab_disabled(i + first_tab_cache)) {
tab_style = tab_disabled;
font_color = font_color_disabled;
} else if (i + first_tab_cache == current) {
tab_style = tab_fg;
font_color = font_color_fg;
} else {
tab_style = tab_bg;
font_color = font_color_bg;
}
// Draw the tab background.
int tab_width = tab_widths[i]; int tab_width = tab_widths[i];
Rect2 tab_rect(tabs_ofs_cache + x, 0, tab_width, header_height); if (get_tab_disabled(i + first_tab_cache)) {
tab_style->draw(canvas, tab_rect); _draw_tab(tab_disabled, font_color_disabled, i, tabs_ofs_cache + x);
} else if (i + first_tab_cache == current) {
// Draw the tab contents. x_current = x;
Control *control = Object::cast_to<Control>(tabs[i + first_tab_cache]); } else {
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name())); _draw_tab(tab_bg, font_color_bg, i, tabs_ofs_cache + x);
int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT);
int top_margin = tab_style->get_margin(MARGIN_TOP);
int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;
// Draw the tab icon.
if (control->has_meta("_tab_icon")) {
Ref<Texture2D> icon = control->get_meta("_tab_icon");
if (icon.is_valid()) {
int y = y_center - (icon->get_height() / 2);
icon->draw(canvas, Point2i(x_content, y));
if (text != "") {
x_content += icon->get_width() + icon_text_distance;
}
}
} }
// Draw the tab text.
Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
font->draw(canvas, text_pos, text, font_color);
x += tab_width; x += tab_width;
last_tab_cache = i + first_tab_cache; last_tab_cache = i + first_tab_cache;
} }
// Draw the tab area.
panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
// Draw selected tab in front
_draw_tab(tab_fg, font_color_fg, current, tabs_ofs_cache + x_current);
// Draw the popup menu. // Draw the popup menu.
x = get_size().width; x = get_size().width;
if (popup) { if (popup) {
@ -438,6 +409,43 @@ void TabContainer::_notification(int p_what) {
} }
} }
void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
Vector<Control *> tabs = _get_tabs();
RID canvas = get_canvas_item();
Ref<Font> font = get_theme_font("font");
int icon_text_distance = get_theme_constant("icon_separation");
int tab_width = _get_tab_width(p_index);
int header_height = _get_top_margin();
// Draw the tab background.
Rect2 tab_rect(p_x, 0, tab_width, header_height);
p_tab_style->draw(canvas, tab_rect);
// Draw the tab contents.
Control *control = Object::cast_to<Control>(tabs[p_index + first_tab_cache]);
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);
int top_margin = p_tab_style->get_margin(MARGIN_TOP);
int y_center = top_margin + (tab_rect.size.y - p_tab_style->get_minimum_size().y) / 2;
// Draw the tab icon.
if (control->has_meta("_tab_icon")) {
Ref<Texture2D> icon = control->get_meta("_tab_icon");
if (icon.is_valid()) {
int y = y_center - (icon->get_height() / 2);
icon->draw(canvas, Point2i(x_content, y));
if (text != "") {
x_content += icon->get_width() + icon_text_distance;
}
}
}
// Draw the tab text.
Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
font->draw(canvas, text_pos, text, p_font_color);
}
void TabContainer::_on_theme_changed() { void TabContainer::_on_theme_changed() {
if (get_tab_count() > 0) { if (get_tab_count() > 0) {
_repaint(); _repaint();

View file

@ -68,6 +68,7 @@ private:
void _repaint(); void _repaint();
void _on_mouse_exited(); void _on_mouse_exited();
void _update_current_tab(); void _update_current_tab();
void _draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x);
protected: protected:
void _child_renamed_callback(); void _child_renamed_callback();