Skip to content

Commit

Permalink
Merge pull request #44328 from gongpha/tabs-label-incorrect
Browse files Browse the repository at this point in the history
Refresh TextLine buffer when moving a tab
  • Loading branch information
akien-mga authored Dec 20, 2020
2 parents 153c132 + 0394ff3 commit f7ddcbf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
68 changes: 20 additions & 48 deletions scene/gui/tab_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,12 @@ void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, in
text_buf[p_index]->draw(canvas, text_pos, p_font_color);
}

void TabContainer::_on_theme_changed() {
if (!_theme_changing) {
return;
}

void TabContainer::_refresh_texts() {
text_buf.clear();
Vector<Control *> tabs = _get_tabs();
bool rtl = is_layout_rtl();
Ref<Font> font = get_theme_font("font");
int font_size = get_theme_font_size("font_size");
Vector<Control *> tabs = _get_tabs();
for (int i = 0; i < tabs.size(); i++) {
Control *control = Object::cast_to<Control>(tabs[i]);
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
Expand All @@ -587,6 +583,14 @@ void TabContainer::_on_theme_changed() {
name->add_string(text, font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
text_buf.push_back(name);
}
}

void TabContainer::_on_theme_changed() {
if (!_theme_changing) {
return;
}

_refresh_texts();

minimum_size_changed();
if (get_tab_count() > 0) {
Expand Down Expand Up @@ -679,21 +683,7 @@ Vector<Control *> TabContainer::_get_tabs() const {
}

void TabContainer::_child_renamed_callback() {
text_buf.clear();
Vector<Control *> tabs = _get_tabs();
bool rtl = is_layout_rtl();
Ref<Font> font = get_theme_font("font");
int font_size = get_theme_font_size("font_size");
for (int i = 0; i < tabs.size(); i++) {
Control *control = Object::cast_to<Control>(tabs[i]);
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
Ref<TextLine> name;
name.instance();
name->set_direction(rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
name->add_string(text, font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
text_buf.push_back(name);
}

_refresh_texts();
update();
}

Expand All @@ -708,20 +698,8 @@ void TabContainer::add_child_notify(Node *p_child) {
return;
}

text_buf.clear();
Vector<Control *> tabs = _get_tabs();
bool rtl = is_layout_rtl();
Ref<Font> font = get_theme_font("font");
int font_size = get_theme_font_size("font_size");
for (int i = 0; i < tabs.size(); i++) {
Control *control = Object::cast_to<Control>(tabs[i]);
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
Ref<TextLine> name;
name.instance();
name->set_direction(rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
name->add_string(text, font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
text_buf.push_back(name);
}
_refresh_texts();

bool first = false;

Expand All @@ -743,14 +721,20 @@ void TabContainer::add_child_notify(Node *p_child) {
c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));

update();
p_child->connect("renamed", callable_mp(this, &TabContainer::_child_renamed_callback));
if (first && is_inside_tree()) {
emit_signal("tab_changed", current);
}
}

void TabContainer::move_child_notify(Node *p_child) {
Container::move_child_notify(p_child);
call_deferred("_update_current_tab");
_refresh_texts();
update();
}

int TabContainer::get_tab_count() const {
return _get_tabs().size();
}
Expand Down Expand Up @@ -813,20 +797,8 @@ void TabContainer::remove_child_notify(Node *p_child) {
}

void TabContainer::_update_current_tab() {
text_buf.clear();
Vector<Control *> tabs = _get_tabs();
bool rtl = is_layout_rtl();
Ref<Font> font = get_theme_font("font");
int font_size = get_theme_font_size("font_size");
for (int i = 0; i < tabs.size(); i++) {
Control *control = Object::cast_to<Control>(tabs[i]);
String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
Ref<TextLine> name;
name.instance();
name->set_direction(rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
name->add_string(text, font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
text_buf.push_back(name);
}
_refresh_texts();

int tc = tabs.size();
if (current >= tc) {
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/tab_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ class TabContainer : public Container {
void _on_mouse_exited();
void _update_current_tab();
void _draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x);
void _refresh_texts();

protected:
void _child_renamed_callback();
void _gui_input(const Ref<InputEvent> &p_event);
void _notification(int p_what);
virtual void add_child_notify(Node *p_child) override;
virtual void move_child_notify(Node *p_child) override;
virtual void remove_child_notify(Node *p_child) override;

Variant get_drag_data(const Point2 &p_point) override;
Expand Down

0 comments on commit f7ddcbf

Please sign in to comment.